@@ -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