Ver Fonte

UpdateRecordedYearOfBirth

Andrew Grant há 7 meses atrás
pai
commit
45630ad51a

+ 2 - 3
src/main/java/scot/carricksoftware/grants/services/census/censusentry/UpdateYearsOfBirth.java → src/main/java/scot/carricksoftware/grants/services/census/censusentry/UpdateRecordedYearOfBirth.java

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

+ 40 - 0
src/main/java/scot/carricksoftware/grants/services/census/censusentry/UpdateRecordedYearOfBirthImpl.java

@@ -0,0 +1,40 @@
+/*
+ * Copyright (c) 2025.  Andrew Grant Carrick Software. All rights reserved
+ *
+ */
+
+package scot.carricksoftware.grants.services.census.censusentry;
+
+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.converters.people.PersonConverter;
+import scot.carricksoftware.grants.domains.people.Person;
+
+@Component
+public class UpdateRecordedYearOfBirthImpl implements UpdateRecordedYearOfBirth {
+
+    private static final Logger logger = LogManager.getLogger(UpdateRecordedYearOfBirthImpl.class);
+
+    public UpdateRecordedYearOfBirthImpl(PersonConverter personConverter) {
+    }
+
+    @Override
+    public void updateRecordedYearOfBirth(CensusEntryCommand censusEntryCommand) {
+        logger.info("UpdateRecordedYearOfBirthImpl::updateRecordedYearOfBirth");
+        Person person = censusEntryCommand.getPerson();
+        if (person.getRecordedYearOfBirth() == null) {
+            String dateString = censusEntryCommand.getCensus().getCensusDate().label;
+
+            String[] dateStrings = dateString.split("/");
+            Integer year = Integer.valueOf(dateStrings[2]);
+            try {
+                Integer age = Integer.valueOf(censusEntryCommand.getAge());
+                person.setRecordedYearOfBirth(String.valueOf(year - age));
+            } catch (NumberFormatException e) {
+                logger.info(" -- Age cannot be parsed");
+            }
+        }
+    }
+}

+ 0 - 23
src/main/java/scot/carricksoftware/grants/services/census/censusentry/UpdateYearsOfBirthImpl.java

@@ -1,23 +0,0 @@
-/*
- * Copyright (c) 2025.  Andrew Grant Carrick Software. All rights reserved
- *
- */
-
-package scot.carricksoftware.grants.services.census.censusentry;
-
-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.domains.people.Person;
-
-@Component
-public class UpdateYearsOfBirthImpl implements UpdateYearsOfBirth {
-
-    private static final Logger logger = LogManager.getLogger(UpdateYearsOfBirthImpl.class);
-
-    @Override
-    public void updateRecordedYearOfBirth(CensusEntryCommand censusEntryCommand, Person person) {
-        logger.debug("updateYearOfBirth::updateRecordedYearOfBirth");
-    }
-}

+ 76 - 0
src/test/java/scot/carricksoftware/grants/services/census/censusentry/UpdateRecordedYearOfBirthTest.java

@@ -0,0 +1,76 @@
+/*
+ * Copyright (c) 2025.  Andrew Grant Carrick Software. All rights reserved
+ *
+ */
+
+package scot.carricksoftware.grants.services.census.censusentry;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+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.converters.people.PersonConverter;
+import scot.carricksoftware.grants.domains.census.Census;
+import scot.carricksoftware.grants.domains.people.Person;
+
+import static org.mockito.Mockito.*;
+import static scot.carricksoftware.grants.enums.census.CensusDate.CENSUS_1881;
+
+@ExtendWith(MockitoExtension.class)
+class UpdateRecordedYearOfBirthTest {
+
+    private UpdateRecordedYearOfBirth updateRecordedYearOfBirth;
+
+    @Mock
+    private Census censusMock;
+
+    @Mock
+    private Person personMock;
+
+
+    @Mock
+    CensusEntryCommand censusEntryCommandMock;
+
+    @Mock
+    private PersonConverter personConverterMock;
+
+    @BeforeEach
+    void setUp() {
+        updateRecordedYearOfBirth = new UpdateRecordedYearOfBirthImpl(personConverterMock);
+        when(censusEntryCommandMock.getPerson()).thenReturn(personMock);
+    }
+
+    @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");
+    }
+
+    @Test
+    public void theYearOfBirthIsNotUpdatedIfAgeIsInvalidTest() {
+        updateRecordedYearOfBirth = new UpdateRecordedYearOfBirthImpl(personConverterMock);
+        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());
+    }
+
+    @Test
+    public void theYearOfBirthIsNotResetTest() {
+        when(personMock.getRecordedYearOfBirth()).thenReturn("1874");
+        when(personMock.getRecordedYearOfBirth()).thenReturn("1800");
+        updateRecordedYearOfBirth.updateRecordedYearOfBirth(censusEntryCommandMock);
+        verify(personMock,times(0)).setRecordedYearOfBirth(anyString());
+    }
+
+
+}

+ 0 - 26
src/test/java/scot/carricksoftware/grants/services/census/censusentry/UpdateYearsOfBirthTest.java

@@ -1,26 +0,0 @@
-/*
- * Copyright (c) 2025.  Andrew Grant Carrick Software. All rights reserved
- *
- */
-
-package scot.carricksoftware.grants.services.census.censusentry;
-
-import org.junit.jupiter.api.BeforeEach;
-import org.junit.jupiter.api.Test;
-
-import static org.junit.jupiter.api.Assertions.*;
-
-class UpdateYearsOfBirthTest {
-
-    private UpdateYearsOfBirth updateYearsOfBirth;
-
-    @BeforeEach
-    void setUp() {
-        updateYearsOfBirth = new UpdateYearsOfBirthImpl();
-    }
-
-    @Test
-    public void existsTest() {
-       assertNotNull(updateYearsOfBirth);
-    }
-}