Andrew Grant 7 сар өмнө
parent
commit
2d2c3a419d

+ 6 - 1
src/test/java/scot/carricksoftware/grants/controllers/census/census/CensusFormControllerSaveOrUpdateTest.java

@@ -16,6 +16,7 @@ import scot.carricksoftware.grants.commands.census.CensusCommand;
 import scot.carricksoftware.grants.commands.census.CensusCommandImpl;
 import scot.carricksoftware.grants.converters.census.CensusConverterImpl;
 import scot.carricksoftware.grants.services.census.CensusService;
+import scot.carricksoftware.grants.services.places.places.PlaceService;
 import scot.carricksoftware.grants.validators.census.CensusCommandValidator;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -39,6 +40,9 @@ public class CensusFormControllerSaveOrUpdateTest {
     @Mock
     private CensusConverterImpl censusConverterMock;
 
+    @Mock
+    private PlaceService placeServiceMock;
+
     @Mock
     Model modelMock;
 
@@ -51,7 +55,8 @@ public class CensusFormControllerSaveOrUpdateTest {
     public void setUp() {
         censusController = new CensusFormControllerImpl(censusServiceMock,
                 censusCommandValidatorMock,
-                censusConverterMock);
+                censusConverterMock,
+                placeServiceMock);
         censusCommand = new CensusCommandImpl();
     }
 

+ 26 - 6
src/test/java/scot/carricksoftware/grants/controllers/census/census/CensusFormControllerTest.java

@@ -18,11 +18,12 @@ import scot.carricksoftware.grants.constants.AttributeConstants;
 import scot.carricksoftware.grants.converters.census.CensusConverterImpl;
 import scot.carricksoftware.grants.domains.census.Census;
 import scot.carricksoftware.grants.services.census.CensusService;
+import scot.carricksoftware.grants.services.places.places.PlaceService;
 import scot.carricksoftware.grants.validators.census.CensusCommandValidator;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
-import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.when;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.*;
 import static scot.carricksoftware.grants.GenerateRandomCensusValues.GetRandomCensus;
 import static scot.carricksoftware.grants.GenerateRandomCensusValues.GetRandomCensusCommand;
 import static scot.carricksoftware.grants.GenerateRandomNumberValues.GetRandomLong;
@@ -37,6 +38,9 @@ public class CensusFormControllerTest {
     @Mock
     private CensusService censusServiceMock;
 
+    @Mock
+    private PlaceService placeServiceMock;
+
 
     @Mock
     private CensusConverterImpl censusConverterMock;
@@ -53,7 +57,8 @@ public class CensusFormControllerTest {
     public void setUp() {
         censusController = new CensusFormControllerImpl(censusServiceMock,
                 censusCommandValidatorMock,
-                censusConverterMock);
+                censusConverterMock,
+                placeServiceMock);
     }
 
     @Test
@@ -61,9 +66,24 @@ public class CensusFormControllerTest {
         ArgumentCaptor<Object> objectCaptor = ArgumentCaptor.forClass(Object.class);
         ArgumentCaptor<String> stringCaptor = ArgumentCaptor.forClass(String.class);
         assertEquals("census/form", censusController.getNewCensus(modelMock));
-        verify(modelMock).addAttribute(stringCaptor.capture(), objectCaptor.capture());
-        assertEquals("censusCommand", stringCaptor.getValue());
-        assertEquals("CensusCommandImpl", objectCaptor.getValue().getClass().getSimpleName());
+        verify(modelMock, atLeast(1)).addAttribute(stringCaptor.capture(), objectCaptor.capture());
+
+        boolean foundCensusCommand = false;
+        boolean foundPlaces = false;
+        for (int i = 0; i < stringCaptor.getAllValues().size(); i++) {
+            if (stringCaptor.getAllValues().get(i).equals("places")) {
+                if (objectCaptor.getAllValues().get(i).getClass().getSimpleName().equals("LinkedList")) {
+                    foundPlaces = true;
+                }
+            }
+            if (stringCaptor.getAllValues().get(i).equals("censusCommand")) {
+                if (objectCaptor.getAllValues().get(i).getClass().getSimpleName().equals("CensusCommandImpl")) {
+                    foundCensusCommand = true;
+                }
+            }
+        }
+
+        assertTrue(foundPlaces && foundCensusCommand);
     }
 
     @Test

+ 31 - 11
src/test/java/scot/carricksoftware/grants/validators/census/CensusCommandValidatorTest.java

@@ -16,9 +16,11 @@ import org.springframework.validation.BindingResult;
 import scot.carricksoftware.grants.commands.census.CensusCommand;
 import scot.carricksoftware.grants.commands.census.CensusCommandImpl;
 
-import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.*;
 import static org.mockito.Mockito.verify;
-import static scot.carricksoftware.grants.GenerateRandomNumberValues.GetRandomLong;
+import static scot.carricksoftware.grants.GenerateCensusRandomEnums.GetRandomCensusBoundaryType;
+import static scot.carricksoftware.grants.GenerateCensusRandomEnums.GetRandomCensusDate;
+import static scot.carricksoftware.grants.GenerateRandomPlaceValues.GetRandomPlace;
 
 @ExtendWith(MockitoExtension.class)
 class CensusCommandValidatorTest {
@@ -48,20 +50,38 @@ class CensusCommandValidatorTest {
 
     @Test
     public void nullDateTest() {
-        censusCommand.setId(GetRandomLong());
-        censusCommand.setDate(null);
-
+        censusCommand.setPlace(GetRandomPlace());
+        censusCommand.setBoundaryType(GetRandomCensusBoundaryType());
         censusCommandValidator.validate(censusCommand, bindingResultMock);
-
-        verify(bindingResultMock).rejectValue(stringArgumentCaptor.capture(),
-                stringArgumentCaptor2.capture(),
-                objectArgumentCaptor.capture(),
-                stringArgumentCaptor3.capture());
-
+        verify(bindingResultMock).rejectValue(stringArgumentCaptor.capture(), stringArgumentCaptor2.capture(), objectArgumentCaptor.capture(),stringArgumentCaptor3.capture());
         assertEquals("date", stringArgumentCaptor.getValue());
+        assertEquals("", stringArgumentCaptor2.getValue());
+        assertNull(objectArgumentCaptor.getValue());
         assertEquals("Date must exist.", stringArgumentCaptor3.getValue());
+    }
 
+    @Test
+    public void nullBoundaryTest() {
+        censusCommand.setPlace(GetRandomPlace());
+        censusCommand.setDate(GetRandomCensusDate());
+        censusCommandValidator.validate(censusCommand, bindingResultMock);
+        verify(bindingResultMock).rejectValue(stringArgumentCaptor.capture(), stringArgumentCaptor2.capture(), objectArgumentCaptor.capture(),stringArgumentCaptor3.capture());
+        assertEquals("boundaryType", stringArgumentCaptor.getValue());
+        assertEquals("", stringArgumentCaptor2.getValue());
+        assertNull(objectArgumentCaptor.getValue());
+        assertEquals("The boundary type cannot be null.", stringArgumentCaptor3.getValue());
     }
 
+    @Test
+    public void nullPlaceTest() {
+        censusCommand.setDate(GetRandomCensusDate());
+        censusCommand.setBoundaryType(GetRandomCensusBoundaryType());
+        censusCommandValidator.validate(censusCommand, bindingResultMock);
+        verify(bindingResultMock).rejectValue(stringArgumentCaptor.capture(), stringArgumentCaptor2.capture(), objectArgumentCaptor.capture(),stringArgumentCaptor3.capture());
+        assertEquals("place", stringArgumentCaptor.getValue());
+        assertEquals("", stringArgumentCaptor2.getValue());
+        assertNull(objectArgumentCaptor.getValue());
+        assertEquals("The place cannot be null.", stringArgumentCaptor3.getValue());
+    }
 
 }