Browse Source

In Place Region is checked for null

Andrew Grant 6 months ago
parent
commit
f155fe8930

+ 1 - 0
src/main/java/scot/carricksoftware/grants/constants/ValidationConstants.java

@@ -28,6 +28,7 @@ public class ValidationConstants {
 
     public static final String PLACE_IS_NULL = "The place cannot be null.";
     public static final String COUNTRY_IS_NULL = "The country cannot be null.";
+    public static final String REGION_IS_NULL = "The region cannot be null.";
 
     public static final String BRIDE_IS_NULL = "The bride cannot be null.";
     public static final String GROOM_IS_NULL = "The groom cannot be null.";

+ 15 - 0
src/main/java/scot/carricksoftware/grants/validators/places/PlaceCommandValidator.java

@@ -15,6 +15,19 @@ import scot.carricksoftware.grants.constants.ValidationConstants;
 public class PlaceCommandValidator {
 
     public void validate(PlaceCommand placeCommand, BindingResult bindingResult) {
+        validateName(placeCommand, bindingResult);
+        validateRegion(placeCommand,bindingResult);
+    }
+
+    private void validateRegion(PlaceCommand placeCommand, BindingResult bindingResult) {
+        if (placeCommand.getRegion() == null) {
+            bindingResult.rejectValue("region", ApplicationConstants.EMPTY_STRING,
+                    null,
+                    ValidationConstants.REGION_IS_NULL);
+        }
+    }
+
+    public void validateName(PlaceCommand placeCommand, BindingResult bindingResult) {
         if (placeCommand.getName().length() < ApplicationConstants.MINIMUM_NAME_LENGTH) {
             bindingResult.rejectValue("name", ApplicationConstants.EMPTY_STRING,
                     null,
@@ -27,5 +40,7 @@ public class PlaceCommandValidator {
             }
         }
     }
+
+
 }
 

+ 21 - 0
src/test/java/scot/carricksoftware/grants/validators/places/PlaceCensusCommandValidatorImplTest.java

@@ -21,6 +21,8 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertNull;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.verifyNoInteractions;
+import static scot.carricksoftware.grants.GenerateCertificateRandomValues.GetRandomString;
+import static scot.carricksoftware.grants.GenerateRandomPlaceValues.GetRandomRegion;
 
 @ExtendWith(MockitoExtension.class)
 class PlaceCensusCommandValidatorImplTest {
@@ -42,6 +44,7 @@ class PlaceCensusCommandValidatorImplTest {
     public void minimumSizeIsAllowedTest() {
         String repeated = new String(new char[ApplicationConstants.MINIMUM_NAME_LENGTH]).replace("\0", "x");
         placeCommand.setName(repeated);
+        placeCommand.setRegion(GetRandomRegion());
         validator.validate(placeCommand, bindingResultMock);
         verifyNoInteractions(bindingResultMock);
     }
@@ -50,6 +53,7 @@ class PlaceCensusCommandValidatorImplTest {
     public void maximumSizeIsAllowedTest() {
         String repeated = new String(new char[ApplicationConstants.MAXIMUM_NAME_LENGTH]).replace("\0", "x");
         placeCommand.setName(repeated);
+        placeCommand.setRegion(GetRandomRegion());
         validator.validate(placeCommand, bindingResultMock);
         verifyNoInteractions(bindingResultMock);
     }
@@ -61,6 +65,7 @@ class PlaceCensusCommandValidatorImplTest {
 
         String repeated = new String(new char[ApplicationConstants.MAXIMUM_NAME_LENGTH + 1]).replace("\0", "x");
         placeCommand.setName(repeated);
+        placeCommand.setRegion(GetRandomRegion());
         validator.validate(placeCommand, bindingResultMock);
         verify(bindingResultMock).rejectValue(stringArgumentCaptor.capture(), stringArgumentCaptor.capture(), objectArgumentCaptor.capture(), stringArgumentCaptor.capture());
         assertEquals("name", stringArgumentCaptor.getAllValues().get(0));
@@ -76,6 +81,7 @@ class PlaceCensusCommandValidatorImplTest {
 
         String repeated = new String(new char[ApplicationConstants.MINIMUM_NAME_LENGTH - 1]).replace("\0", "x");
         placeCommand.setName(repeated);
+        placeCommand.setRegion(GetRandomRegion());
         validator.validate(placeCommand, bindingResultMock);
         verify(bindingResultMock).rejectValue(stringArgumentCaptor.capture(), stringArgumentCaptor.capture(), objectArgumentCaptor.capture(), stringArgumentCaptor.capture());
         assertEquals("name", stringArgumentCaptor.getAllValues().get(0));
@@ -83,4 +89,19 @@ class PlaceCensusCommandValidatorImplTest {
         assertEquals(ValidationConstants.NAME_IS_TOO_SHORT, stringArgumentCaptor.getAllValues().get(2));
         assertNull(objectArgumentCaptor.getAllValues().get(0));
     }
+
+    @Test
+    public void nullRegionTest() {
+        ArgumentCaptor<String> stringArgumentCaptor = ArgumentCaptor.forClass(String.class);
+        ArgumentCaptor<Object[]> objectArgumentCaptor = ArgumentCaptor.forClass(Object[].class);
+        placeCommand.setRegion(null);
+        placeCommand.setName(GetRandomString());
+        validator.validate(placeCommand, bindingResultMock);
+
+        verify(bindingResultMock).rejectValue(stringArgumentCaptor.capture(), stringArgumentCaptor.capture(), objectArgumentCaptor.capture(), stringArgumentCaptor.capture());
+        assertEquals("region", stringArgumentCaptor.getAllValues().get(0));
+        assertEquals(ApplicationConstants.EMPTY_STRING, stringArgumentCaptor.getAllValues().get(1));
+        assertEquals(ValidationConstants.REGION_IS_NULL, stringArgumentCaptor.getAllValues().get(2));
+        assertNull(objectArgumentCaptor.getAllValues().get(0));
+    }
 }