Skip to content

Commit f84a068

Browse files
committed
WS-2609: Enable a few more tests.
1 parent f2640f9 commit f84a068

File tree

1 file changed

+98
-66
lines changed

1 file changed

+98
-66
lines changed

api/src/test/java/com/basistech/rosette/api/RosetteAPITest.java

Lines changed: 98 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import com.basistech.rosette.apimodel.EntitiesOptions;
2626
import com.basistech.rosette.apimodel.EntitiesResponse;
2727
import com.basistech.rosette.apimodel.ErrorResponse;
28+
import com.basistech.rosette.apimodel.LanguageDetectionResult;
2829
import com.basistech.rosette.apimodel.LanguageResponse;
2930
import com.basistech.rosette.apimodel.MorphologyResponse;
3031
import com.basistech.rosette.apimodel.NameDeduplicationRequest;
@@ -45,9 +46,9 @@
4546
import org.apache.commons.io.FileUtils;
4647
import org.apache.commons.io.IOUtils;
4748
import org.apache.http.HttpHeaders;
49+
import org.junit.jupiter.api.AfterEach;
4850
import org.junit.jupiter.api.BeforeAll;
4951
import org.junit.jupiter.api.BeforeEach;
50-
import org.junit.jupiter.api.Test;
5152
import org.junit.jupiter.api.extension.ExtendWith;
5253
import org.junit.jupiter.params.ParameterizedTest;
5354
import org.junit.jupiter.params.provider.Arguments;
@@ -81,23 +82,25 @@ class RosetteAPITest {
8182
private MockServerClient mockServer;
8283
private ObjectMapper mapper;
8384

85+
86+
8487
@BeforeEach
8588
public void setUp(MockServerClient mockServer) {
86-
this.mockServer = mockServer;
87-
8889
mapper = ApiModelMixinModule.setupObjectMapper(new ObjectMapper());
8990
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
9091

91-
mockServer.when(HttpRequest.request()
92+
this.mockServer = mockServer;
93+
this.mockServer.when(HttpRequest.request()
9294
.withMethod("GET")
9395
.withPath("/rest/v1/ping")
9496
.withHeader(HttpHeaders.USER_AGENT, HttpRosetteAPI.USER_AGENT_STR))
9597
.respond(HttpResponse.response()
96-
.withBody("{\"message\":\"Rosette API at your service\",\"time\":1461788498633}", StandardCharsets.UTF_8)
98+
.withBody("{\"message\":\"Rosette API at your service\",\"time\":1461788498633}",
99+
StandardCharsets.UTF_8)
97100
.withStatusCode(HTTP_OK)
98101
.withHeader("X-RosetteAPI-Concurrency", "5"));
99102

100-
mockServer.when(HttpRequest.request()
103+
this.mockServer.when(HttpRequest.request()
101104
.withPath("/info"))
102105
.respond(HttpResponse.response()
103106
.withStatusCode(HTTP_OK)
@@ -111,6 +114,11 @@ public void setUp(MockServerClient mockServer) {
111114
.build();
112115
}
113116

117+
@AfterEach
118+
public void reset() {
119+
mockServer.reset();
120+
}
121+
114122
private static byte[] gzip(String text) throws IOException {
115123
ByteArrayOutputStream baos = new ByteArrayOutputStream();
116124
try (GZIPOutputStream out = new GZIPOutputStream(baos)) {
@@ -187,7 +195,8 @@ void testMatchName(String testFilename, String responseStr, int statusCode) thro
187195
setStatusCodeResponse(responseStr, statusCode);
188196
NameSimilarityRequest request = readValueNameMatcher(testFilename);
189197
try {
190-
NameSimilarityResponse response = api.perform(AbstractRosetteAPI.NAME_SIMILARITY_SERVICE_PATH, request, NameSimilarityResponse.class);
198+
NameSimilarityResponse response = api.perform(AbstractRosetteAPI.NAME_SIMILARITY_SERVICE_PATH, request,
199+
NameSimilarityResponse.class);
191200
verifyNameMatcher(response, responseStr);
192201
} catch (HttpRosetteAPIException e) {
193202
verifyException(e, responseStr);
@@ -204,122 +213,145 @@ private NameSimilarityRequest readValueNameMatcher(String testFilename) throws I
204213
return mapper.readValue(input, NameSimilarityRequest.class);
205214
}
206215

207-
/*
208-
@Test
209-
public void testMatchAddress() throws IOException {
210-
if (!(testFilename.endsWith("-address-similarity.json"))) {
211-
return;
212-
}
213-
AddressSimilarityRequest request = readValueAddressMatcher();
216+
private static Stream<Arguments> testMatchAddressParameters() throws IOException {
217+
return getTestFiles("-address-similarity.json");
218+
}
219+
220+
@ParameterizedTest(name = "testFilename: {0}; statusCode: {2}")
221+
@MethodSource("testMatchAddressParameters")
222+
void testMatchAddress(String testFilename, String responseStr, int statusCode) throws IOException {
223+
setStatusCodeResponse(responseStr, statusCode);
224+
AddressSimilarityRequest request = readValueAddressMatcher(testFilename);
214225
try {
215-
AddressSimilarityResponse response = api.perform(AbstractRosetteAPI.ADDRESS_SIMILARITY_SERVICE_PATH, request, AddressSimilarityResponse.class);
216-
verifyAddressMatcher(response);
226+
AddressSimilarityResponse response = api.perform(AbstractRosetteAPI.ADDRESS_SIMILARITY_SERVICE_PATH,
227+
request, AddressSimilarityResponse.class);
228+
verifyAddressMatcher(response, responseStr);
217229
} catch (HttpRosetteAPIException e) {
218-
verifyException(e);
230+
verifyException(e, responseStr);
219231
}
220232
}
221233

222-
private void verifyAddressMatcher(AddressSimilarityResponse response) throws IOException {
234+
private void verifyAddressMatcher(AddressSimilarityResponse response, String responseStr) throws IOException {
223235
AddressSimilarityResponse goldResponse = mapper.readValue(responseStr, AddressSimilarityResponse.class);
224236
assertEquals(goldResponse.getScore(), response.getScore(), 0.0);
225237
}
226238

227-
private AddressSimilarityRequest readValueAddressMatcher() throws IOException {
239+
private AddressSimilarityRequest readValueAddressMatcher(String testFilename) throws IOException {
228240
File input = new File("src/test/mock-data/request", testFilename);
229241
return mapper.readValue(input, AddressSimilarityRequest.class);
230242
}
231243

232-
@Test
233-
public void testTranslateName() throws IOException {
234-
if (!(testFilename.endsWith("-translated-name.json"))) {
235-
return;
236-
}
237-
NameTranslationRequest request = readValueNameTranslation();
244+
private static Stream<Arguments> testTranslateNameParameters() throws IOException {
245+
return getTestFiles("-name-translation.json");
246+
}
247+
248+
@ParameterizedTest(name = "testFilename: {0}; statusCode: {2}")
249+
@MethodSource("testTranslateNameParameters")
250+
void testTranslateName(String testFilename, String responseStr, int statusCode) throws IOException {
251+
setStatusCodeResponse(responseStr, statusCode);
252+
NameTranslationRequest request = readValueNameTranslation(testFilename);
238253
try {
239-
NameTranslationResponse response = api.perform(AbstractRosetteAPI.NAME_TRANSLATION_SERVICE_PATH, request, NameTranslationResponse.class);
240-
verifyNameTranslation(response);
254+
NameTranslationResponse response = api.perform(AbstractRosetteAPI.NAME_TRANSLATION_SERVICE_PATH, request,
255+
NameTranslationResponse.class);
256+
verifyNameTranslation(response, responseStr);
241257
} catch (HttpRosetteAPIException e) {
242-
verifyException(e);
258+
verifyException(e, responseStr);
243259
}
244260
}
245261

246-
private void verifyNameTranslation(NameTranslationResponse response) throws IOException {
262+
private void verifyNameTranslation(NameTranslationResponse response, String responseStr) throws IOException {
247263
NameTranslationResponse goldResponse = mapper.readValue(responseStr, NameTranslationResponse.class);
248264
assertEquals(goldResponse.getTranslation(), response.getTranslation());
249265
}
250266

251-
private NameTranslationRequest readValueNameTranslation() throws IOException {
267+
private NameTranslationRequest readValueNameTranslation(String testFilename) throws IOException {
252268
File input = new File("src/test/mock-data/request", testFilename);
253269
return mapper.readValue(input, NameTranslationRequest.class);
254270
}
255271

256-
private void verifyLanguage(LanguageResponse response) throws IOException {
272+
private void verifyLanguage(LanguageResponse response, String responseStr) throws IOException {
257273
LanguageResponse goldResponse = mapper.readValue(responseStr, LanguageResponse.class);
258274
assertEquals(goldResponse.getLanguageDetections().size(), response.getLanguageDetections().size());
259275
}
260276

261-
@Test
262-
public void testGetLanguageDoc() throws IOException {
263-
if (!(testFilename.endsWith("-language.json") && testFilename.contains("-doc-"))) {
264-
return;
265-
}
277+
private static Stream<Arguments> testGetLanguageDocParameters() throws IOException {
278+
return getTestFiles("-doc-language.json");
279+
}
280+
281+
@ParameterizedTest(name = "testFilename: {0}; statusCode: {2}")
282+
@MethodSource("testGetLanguageDocParameters")
283+
void testGetLanguageDoc(String testFilename, String responseStr, int statusCode) throws IOException {
284+
setStatusCodeResponse(responseStr, statusCode);
266285
DocumentRequest<?> request = readValue(DocumentRequest.class, testFilename);
267286
try {
268-
LanguageResponse response = api.perform(AbstractRosetteAPI.LANGUAGE_SERVICE_PATH, request, LanguageResponse.class);
269-
verifyLanguage(response);
287+
LanguageResponse response = api.perform(AbstractRosetteAPI.LANGUAGE_SERVICE_PATH, request,
288+
LanguageResponse.class);
289+
verifyLanguage(response, responseStr);
270290
} catch (HttpRosetteAPIException e) {
271-
verifyException(e);
291+
verifyException(e, responseStr);
272292
}
273293
}
274294

275-
@Test
276-
public void testGetLanguageURL() throws IOException {
277-
if (!(testFilename.endsWith("-language.json") && testFilename.contains("-url-"))) {
278-
return;
279-
}
295+
private static Stream<Arguments> testGetLanguageURLParameters() throws IOException {
296+
return getTestFiles("-url-language.json");
297+
}
298+
299+
@ParameterizedTest(name = "testFilename: {0}; statusCode: {2}")
300+
@MethodSource("testGetLanguageURLParameters")
301+
void testGetLanguageURL(String testFilename, String responseStr, int statusCode) throws IOException {
302+
setStatusCodeResponse(responseStr, statusCode);
280303
DocumentRequest<?> request = readValue(DocumentRequest.class, testFilename);
281304
try {
282-
LanguageResponse response = api.perform(AbstractRosetteAPI.LANGUAGE_SERVICE_PATH, request, LanguageResponse.class);
283-
verifyLanguage(response);
305+
LanguageResponse response = api.perform(AbstractRosetteAPI.LANGUAGE_SERVICE_PATH, request,
306+
LanguageResponse.class);
307+
verifyLanguage(response, responseStr);
284308
} catch (HttpRosetteAPIException e) {
285-
verifyException(e);
309+
verifyException(e, responseStr);
286310
}
287311
}
288312

289-
@Test
290-
public void testGetMorphologyDoc() throws IOException {
291-
if (!(testFilename.endsWith("-morphology_complete.json") && testFilename.contains("-doc-"))) {
292-
return;
293-
}
294-
DocumentRequest<?> request = readValue(DocumentRequest.class);
313+
private static Stream<Arguments> testGetMorphologyDocParameters() throws IOException {
314+
return getTestFiles("-doc-morphology_complete.json");
315+
}
316+
317+
@ParameterizedTest(name = "testFilename: {0}; statusCode: {2}")
318+
@MethodSource("testGetMorphologyDocParameters")
319+
void testGetMorphologyDoc(String testFilename, String responseStr, int statusCode) throws IOException {
320+
setStatusCodeResponse(responseStr, statusCode);
321+
DocumentRequest<?> request = readValue(DocumentRequest.class, testFilename);
295322
try {
296-
MorphologyResponse response = api.perform(AbstractRosetteAPI.MORPHOLOGY_SERVICE_PATH + "/" + MorphologicalFeature.COMPLETE, request, MorphologyResponse.class);
297-
verifyMorphology(response);
323+
MorphologyResponse response = api.perform(AbstractRosetteAPI.MORPHOLOGY_SERVICE_PATH + "/"
324+
+ MorphologicalFeature.COMPLETE, request, MorphologyResponse.class);
325+
verifyMorphology(response, responseStr);
298326
} catch (HttpRosetteAPIException e) {
299-
verifyException(e);
327+
verifyException(e, responseStr);
300328
}
301329
}
302330

303-
private void verifyMorphology(MorphologyResponse response) throws IOException {
331+
private void verifyMorphology(MorphologyResponse response, String responseStr) throws IOException {
304332
MorphologyResponse goldResponse = mapper.readValue(responseStr, MorphologyResponse.class);
305333
assertEquals(response.getPosTags().size(), goldResponse.getPosTags().size());
306334
}
307335

308-
@Test
309-
public void testGetMorphologyURL() throws IOException {
310-
if (!(testFilename.endsWith("-morphology_complete.json") && testFilename.contains("-url-"))) {
311-
return;
312-
}
336+
private static Stream<Arguments> testGetMorphologyURLParameters() throws IOException {
337+
return getTestFiles("-url-morphology_complete.json");
338+
}
339+
340+
@ParameterizedTest(name = "testFilename: {0}; statusCode: {2}")
341+
@MethodSource("testGetMorphologyURLParameters")
342+
void testGetMorphologyURL(String testFilename, String responseStr, int statusCode) throws IOException {
343+
setStatusCodeResponse(responseStr, statusCode);
313344
DocumentRequest<?> request = readValue(DocumentRequest.class, testFilename);
314345
try {
315-
MorphologyResponse response = api.perform(AbstractRosetteAPI.MORPHOLOGY_SERVICE_PATH + "/" + MorphologicalFeature.COMPLETE, request, MorphologyResponse.class);
316-
verifyMorphology(response);
346+
MorphologyResponse response = api.perform(AbstractRosetteAPI.MORPHOLOGY_SERVICE_PATH + "/"
347+
+ MorphologicalFeature.COMPLETE, request, MorphologyResponse.class);
348+
verifyMorphology(response, responseStr);
317349
} catch (HttpRosetteAPIException e) {
318-
verifyException(e);
350+
verifyException(e, responseStr);
319351
}
320352
}
321353

322-
@Test
354+
/* @Test
323355
public void testGetEntityDoc() throws IOException {
324356
if (!(testFilename.endsWith("-entities.json") && testFilename.contains("-doc-"))) {
325357
return;

0 commit comments

Comments
 (0)