Ver código fonte

UpdateRecordedYearOfBirthFailing

Andrew Grant 7 meses atrás
pai
commit
a7f47c53d8

+ 1 - 1
src/main/java/scot/carricksoftware/grants/services/census/censusentry/UpdateRecordedYearOfBirth.java

@@ -8,6 +8,6 @@ package scot.carricksoftware.grants.services.census.censusentry;
 import scot.carricksoftware.grants.commands.census.CensusEntryCommand;
 
 public interface UpdateRecordedYearOfBirth {
-     @SuppressWarnings({"EmptyMethod", "unused"})
+
      void updateRecordedYearOfBirth(CensusEntryCommand censusEntryCommand);
 }

+ 21 - 2
src/main/java/scot/carricksoftware/grants/services/census/censusentry/UpdateRecordedYearOfBirthImpl.java

@@ -9,15 +9,22 @@ import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
 import org.springframework.stereotype.Component;
 import scot.carricksoftware.grants.commands.census.CensusEntryCommand;
+import scot.carricksoftware.grants.commands.people.PersonCommand;
 import scot.carricksoftware.grants.converters.people.PersonConverter;
 import scot.carricksoftware.grants.domains.people.Person;
+import scot.carricksoftware.grants.services.people.PersonService;
 
 @Component
 public class UpdateRecordedYearOfBirthImpl implements UpdateRecordedYearOfBirth {
 
     private static final Logger logger = LogManager.getLogger(UpdateRecordedYearOfBirthImpl.class);
+    private final PersonService personService;
+    private final PersonConverter personConverter;
 
-    public UpdateRecordedYearOfBirthImpl(PersonConverter personConverter) {
+    public UpdateRecordedYearOfBirthImpl(PersonConverter personConverter,
+                                         PersonService personService) {
+        this.personService = personService;
+        this.personConverter = personConverter;
     }
 
     @Override
@@ -31,10 +38,22 @@ public class UpdateRecordedYearOfBirthImpl implements UpdateRecordedYearOfBirth
             Integer year = Integer.valueOf(dateStrings[2]);
             try {
                 Integer age = Integer.valueOf(censusEntryCommand.getAge());
-                person.setRecordedYearOfBirth(String.valueOf(year - age));
+                updateDate(person, String.valueOf(year - age));
             } catch (NumberFormatException e) {
                 logger.info(" -- Age cannot be parsed");
             }
         }
     }
+
+    private void updateDate(Person person,
+                            String dateString) {
+        logger.info("UpdateRecordedYearOfBirthImpl::Date");
+        PersonCommand personCommand = personConverter.convert(person);
+        if (personCommand != null) {
+            personCommand.setRecordedYearOfBirth(dateString);
+            personService.savePersonCommand(personCommand);
+        } else {
+            logger.error(" -- Person command is null");
+        }
+    }
 }

+ 13 - 5
src/test/java/scot/carricksoftware/grants/services/census/censusentry/UpdateRecordedYearOfBirthFailingTest.java

@@ -11,9 +11,11 @@ import org.junit.jupiter.api.extension.ExtendWith;
 import org.mockito.Mock;
 import org.mockito.junit.jupiter.MockitoExtension;
 import scot.carricksoftware.grants.commands.census.CensusEntryCommand;
+import scot.carricksoftware.grants.commands.people.PersonCommand;
 import scot.carricksoftware.grants.converters.people.PersonConverter;
 import scot.carricksoftware.grants.domains.census.Census;
 import scot.carricksoftware.grants.domains.people.Person;
+import scot.carricksoftware.grants.services.people.PersonService;
 
 import static org.mockito.Mockito.*;
 import static scot.carricksoftware.grants.enums.census.CensusDate.CENSUS_1881;
@@ -29,29 +31,35 @@ class UpdateRecordedYearOfBirthFailingTest {
     @Mock
     private Person personMock;
 
+    @Mock
+    private PersonCommand personCommandMock;
+
+    @Mock
+    private PersonService personServiceMock;
+
 
     @Mock
-    CensusEntryCommand censusEntryCommandMock;
+    private CensusEntryCommand censusEntryCommandMock;
 
     @Mock
     private PersonConverter personConverterMock;
 
     @BeforeEach
     void setUp() {
-        updateRecordedYearOfBirth = new UpdateRecordedYearOfBirthImpl(personConverterMock);
+        updateRecordedYearOfBirth = new UpdateRecordedYearOfBirthImpl(personConverterMock, personServiceMock);
         when(censusEntryCommandMock.getPerson()).thenReturn(personMock);
     }
 
 
     @Test
     public void theYearOfBirthIsNotUpdatedIfAgeIsInvalidTest() {
-        updateRecordedYearOfBirth = new UpdateRecordedYearOfBirthImpl(personConverterMock);
+        updateRecordedYearOfBirth = new UpdateRecordedYearOfBirthImpl(personConverterMock, personServiceMock);
         when(censusEntryCommandMock.getAge()).thenReturn("3 months");
         when(personMock.getRecordedYearOfBirth()).thenReturn(null);
         when(censusEntryCommandMock.getCensus()).thenReturn(censusMock);
         when(censusMock.getCensusDate()).thenReturn(CENSUS_1881);
         updateRecordedYearOfBirth.updateRecordedYearOfBirth(censusEntryCommandMock);
-        verify(personMock, times(0)).setRecordedYearOfBirth(anyString());
+        verify(personCommandMock, times(0)).setRecordedYearOfBirth(anyString());
     }
 
     @Test
@@ -59,7 +67,7 @@ class UpdateRecordedYearOfBirthFailingTest {
         when(personMock.getRecordedYearOfBirth()).thenReturn("1874");
         when(personMock.getRecordedYearOfBirth()).thenReturn("1800");
         updateRecordedYearOfBirth.updateRecordedYearOfBirth(censusEntryCommandMock);
-        verify(personMock,times(0)).setRecordedYearOfBirth(anyString());
+        verify(personCommandMock,times(0)).setRecordedYearOfBirth(anyString());
     }
 
 

+ 14 - 5
src/test/java/scot/carricksoftware/grants/services/census/censusentry/UpdateRecordedYearOfBirthPassingTest.java

@@ -11,9 +11,11 @@ import org.junit.jupiter.api.extension.ExtendWith;
 import org.mockito.Mock;
 import org.mockito.junit.jupiter.MockitoExtension;
 import scot.carricksoftware.grants.commands.census.CensusEntryCommand;
+import scot.carricksoftware.grants.commands.people.PersonCommand;
 import scot.carricksoftware.grants.converters.people.PersonConverter;
 import scot.carricksoftware.grants.domains.census.Census;
 import scot.carricksoftware.grants.domains.people.Person;
+import scot.carricksoftware.grants.services.people.PersonService;
 
 import static org.mockito.Mockito.*;
 import static scot.carricksoftware.grants.enums.census.CensusDate.CENSUS_1881;
@@ -29,28 +31,35 @@ class UpdateRecordedYearOfBirthPassingTest {
     @Mock
     private Person personMock;
 
+    @Mock
+    private PersonCommand personCommandMock;
+
+    @Mock
+    private PersonService personServiceMock;
 
     @Mock
-    CensusEntryCommand censusEntryCommandMock;
+    private CensusEntryCommand censusEntryCommandMock;
 
     @Mock
     private PersonConverter personConverterMock;
 
     @BeforeEach
     void setUp() {
-        updateRecordedYearOfBirth = new UpdateRecordedYearOfBirthImpl(personConverterMock);
+        updateRecordedYearOfBirth = new UpdateRecordedYearOfBirthImpl(personConverterMock, personServiceMock);
         when(censusEntryCommandMock.getPerson()).thenReturn(personMock);
+        when(personConverterMock.convert(personMock)).thenReturn(personCommandMock);
+        when(censusEntryCommandMock.getCensus()).thenReturn(censusMock);
     }
 
     @Test
     public void theYearOfBirthIsUpdatedTest() {
-        updateRecordedYearOfBirth = new UpdateRecordedYearOfBirthImpl(personConverterMock);
         when(censusEntryCommandMock.getAge()).thenReturn("7");
         when(personMock.getRecordedYearOfBirth()).thenReturn(null);
-        when(censusEntryCommandMock.getCensus()).thenReturn(censusMock);
         when(censusMock.getCensusDate()).thenReturn(CENSUS_1881);
+
        updateRecordedYearOfBirth.updateRecordedYearOfBirth(censusEntryCommandMock);
-       verify(personMock).setRecordedYearOfBirth("1874");
+
+       verify(personCommandMock).setRecordedYearOfBirth("1874");
     }