Skip to content

Commit 1cb991a

Browse files
authored
Merge pull request #1247 from WildMeOrg/1227_country_api_bugfix
fix for bulk import country-checking logic
2 parents 5be26d8 + 403fb47 commit 1cb991a

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

src/main/java/org/ecocean/api/bulk/BulkValidator.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -385,8 +385,7 @@ public static Object validateValue(String fieldName, Object value, Shepherd mySh
385385
return value;
386386

387387
case "Encounter.country":
388-
if ((value != null) && !CommonConfiguration.getIndexedPropertyValues("country",
389-
myShepherd.getContext()).contains(value))
388+
if ((value != null) && !Util.getCountries().contains(value))
390389
throw new BulkValidatorException("invalid country value: " + value,
391390
ApiException.ERROR_RETURN_CODE_INVALID);
392391
return value;

src/test/java/org/ecocean/api/bulk/BulkApiPostTest.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,9 @@ private boolean hasError(JSONObject rtnJson, int rowNumber, String fieldName) {
500500
Occurrence occ = mock(Occurrence.class);
501501
String requestBody = getValidPayloadArrays();
502502

503+
// test a valid country while we are at it
504+
requestBody = addToRows(requestBody, "Encounter.country", "Germany");
505+
503506
when(mockRequest.getRequestURI()).thenReturn("/api/v3/bulk-import");
504507
when(mockRequest.getReader()).thenReturn(new BufferedReader(new StringReader(requestBody)));
505508

@@ -605,4 +608,36 @@ private boolean hasError(JSONObject rtnJson, int rowNumber, String fieldName) {
605608
}
606609
}
607610
}
611+
612+
@Test void apiPostInvalidCountry()
613+
throws ServletException, IOException {
614+
User user = mock(User.class);
615+
String requestBody = getValidPayloadArrays();
616+
617+
requestBody = addToRows(requestBody, "Encounter.country", "fail-country");
618+
619+
when(mockRequest.getRequestURI()).thenReturn("/api/v3/bulk-import");
620+
when(mockRequest.getReader()).thenReturn(new BufferedReader(new StringReader(requestBody)));
621+
622+
try (MockedConstruction<Shepherd> mockShepherd = mockConstruction(Shepherd.class,
623+
(mock, context) -> {
624+
when(mock.getUser(any(HttpServletRequest.class))).thenReturn(user);
625+
})) {
626+
try (MockedStatic<UploadedFiles> mockUF = mockStatic(UploadedFiles.class)) {
627+
mockUF.when(() -> UploadedFiles.findFiles(any(HttpServletRequest.class),
628+
any(String.class))).thenReturn(emptyFiles);
629+
try (MockedStatic<ShepherdPMF> mockService = mockStatic(ShepherdPMF.class)) {
630+
mockService.when(() -> ShepherdPMF.getPMF(any(String.class))).thenReturn(
631+
mockPMF);
632+
apiServlet.doPost(mockRequest, mockResponse);
633+
responseOut.flush();
634+
JSONObject jout = new JSONObject(responseOut.toString());
635+
verify(mockResponse).setStatus(400);
636+
assertFalse(jout.getBoolean("success"));
637+
assertTrue(hasError(jout, 0, "Encounter.country"));
638+
}
639+
}
640+
}
641+
}
642+
608643
}

0 commit comments

Comments
 (0)