Jelajahi Sumber

UpdateRecordedYearOfBirthImpl updated

Andrew Grant 5 bulan lalu
induk
melakukan
b46c47e3a9

+ 14 - 19
src/main/java/scot/carricksoftware/grants/services/census/censusentry/UpdateRecordedYearOfBirthImpl.java

@@ -34,26 +34,21 @@ public class UpdateRecordedYearOfBirthImpl implements UpdateRecordedYearOfBirth
         logger.debug("UpdateRecordedYearOfBirthImpl::updateRecordedYearOfBirth");
         Person person = censusEntryCommand.getPerson();
         if (!isNull(person)) {
-            if (isNull(person.getRecordedYearOfBirth())) {
-                if (!isNull(censusEntryCommand.getBirthYear())) {
-                    PersonCommand personCommand = personConverter.convert(person);
-                    if (!isNull(personCommand)) {
-                        personCommand.setRecordedYearOfBirth(censusEntryCommand.getBirthYear());
-                        personService.savePersonCommand(personCommand);
-                    } else {
-                        logNoCommandError();
-                    }
-                } else {
-                    String dateString = censusEntryCommand.getCensus().getCensusDate().label;
-                    String[] dateStrings = dateString.split("/");
-                    Integer year = Integer.valueOf(dateStrings[2]);
-                    try {
-                        Integer age = Integer.valueOf(censusEntryCommand.getAge());
-                        updateDate(person, String.valueOf(year - age));
-                    } catch (NumberFormatException e) {
-                        logger.info(" -- Age cannot be parsed");
-                    }
+            if (isNull(censusEntryCommand.getBirthYear()) || censusEntryCommand.getBirthYear().isEmpty()) {
+                String dateString = censusEntryCommand.getCensus().getCensusDate().label;
+                String[] dateStrings = dateString.split("/");
+                Integer year = Integer.valueOf(dateStrings[2]);
+                try {
+                    Integer age = Integer.valueOf(censusEntryCommand.getAge());
+                    updateDate(person, String.valueOf(year - age));
+                } catch (NumberFormatException e) {
+                    logger.info(" -- Age cannot be parsed");
                 }
+            } else {
+                PersonCommand personCommand = personConverter.convert(person);
+                assert personCommand != null;
+                personCommand.setRecordedYearOfBirth(censusEntryCommand.getBirthYear());
+                personService.savePersonCommand(personCommand);
             }
         }
     }

+ 0 - 3
src/test/java/scot/carricksoftware/grants/GenerateRandomNumberValues.java

@@ -19,8 +19,5 @@ public class GenerateRandomNumberValues {
         return rand.nextLong();
     }
 
-    public static Integer GetRandomInteger() {
-        return rand.nextInt();
-    }
 
 }

+ 0 - 96
src/test/java/scot/carricksoftware/grants/services/census/censusentry/UpdateRecordedYearOfBirthFailingTest.java

@@ -1,96 +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 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.junit.jupiter.api.Assertions.assertThrows;
-import static org.mockito.Mockito.*;
-import static scot.carricksoftware.grants.enums.census.CensusDate.CENSUS_1881;
-
-@ExtendWith(MockitoExtension.class)
-class UpdateRecordedYearOfBirthFailingTest {
-
-    private UpdateRecordedYearOfBirth updateRecordedYearOfBirth;
-
-    @Mock
-    private Census censusMock;
-
-    @Mock
-    private Person personMock;
-
-    @Mock
-    private PersonCommand personCommandMock;
-
-    @Mock
-    private PersonService personServiceMock;
-
-
-    @Mock
-    private CensusEntryCommand censusEntryCommandMock;
-
-    @Mock
-    private PersonConverter personConverterMock;
-
-    @BeforeEach
-    void setUp() {
-        updateRecordedYearOfBirth = new UpdateRecordedYearOfBirthImpl(personConverterMock, personServiceMock);
-    }
-
-
-    @Test
-    public void theYearOfBirthIsNotUpdatedIfAgeIsInvalidTest() {
-        updateRecordedYearOfBirth = new UpdateRecordedYearOfBirthImpl(personConverterMock, personServiceMock);
-        when(censusEntryCommandMock.getPerson()).thenReturn(personMock);
-        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(personCommandMock, times(0)).setRecordedYearOfBirth(anyString());
-    }
-
-
-    @Test
-    public void theYearOfBirthIsNotResetTest() {
-        when(censusEntryCommandMock.getPerson()).thenReturn(personMock);
-        when(personMock.getRecordedYearOfBirth()).thenReturn("1874");
-        updateRecordedYearOfBirth.updateRecordedYearOfBirth(censusEntryCommandMock);
-        verify(personCommandMock, times(0)).setRecordedYearOfBirth(anyString());
-    }
-
-    @Test
-    public void anExceptionsIsThrownOnNullPersonCommandWithABirthYearTest() {
-        when(censusEntryCommandMock.getPerson()).thenReturn(personMock);
-        when(personMock.getRecordedYearOfBirth()).thenReturn(null);
-        when(censusEntryCommandMock.getBirthYear()).thenReturn("1880");
-        when(personConverterMock.convert(personMock)).thenReturn(null);
-        assertThrows(NullPointerException.class, () -> updateRecordedYearOfBirth.updateRecordedYearOfBirth(censusEntryCommandMock));
-    }
-
-    @Test
-    public void anExceptionsIsThrownOnNullPersonCommandWithNoBirthYearTest() {
-        when(personMock.getRecordedYearOfBirth()).thenReturn(null);
-        when(censusEntryCommandMock.getPerson()).thenReturn(personMock);
-        when(censusEntryCommandMock.getBirthYear()).thenReturn(null);
-        when(censusEntryCommandMock.getCensus()).thenReturn(censusMock);
-        when(censusEntryCommandMock.getAge()).thenReturn("7");
-        when(censusMock.getCensusDate()).thenReturn(CENSUS_1881);
-        assertThrows(NullPointerException.class, () -> updateRecordedYearOfBirth.updateRecordedYearOfBirth(censusEntryCommandMock));
-    }
-
-
-}

+ 0 - 1
src/test/java/scot/carricksoftware/grants/services/census/censusentry/UpdateRecordedYearOfBirthPassingTest.java

@@ -54,7 +54,6 @@ class UpdateRecordedYearOfBirthPassingTest {
     @Test
     public void theYearOfBirthIsUpdatedTest() {
         when(censusEntryCommandMock.getAge()).thenReturn("7");
-        when(personMock.getRecordedYearOfBirth()).thenReturn(null);
         when(censusMock.getCensusDate()).thenReturn(CENSUS_1881);
 
         updateRecordedYearOfBirth.updateRecordedYearOfBirth(censusEntryCommandMock);

+ 0 - 63
src/test/java/scot/carricksoftware/grants/services/census/censusentry/UpdateRecordedYearOfBirthViaBirthYearTest.java

@@ -1,63 +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 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.people.Person;
-import scot.carricksoftware.grants.services.people.PersonService;
-
-import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.when;
-import static scot.carricksoftware.grants.GenerateRandomNumberValues.GetRandomInteger;
-
-@ExtendWith(MockitoExtension.class)
-class UpdateRecordedYearOfBirthViaBirthYearTest {
-
-    private UpdateRecordedYearOfBirth updateRecordedYearOfBirth;
-
-
-    @Mock
-    private Person personMock;
-
-    @Mock
-    private PersonCommand personCommandMock;
-
-    @Mock
-    private PersonService personServiceMock;
-
-    @Mock
-    private CensusEntryCommand censusEntryCommandMock;
-
-    @Mock
-    private PersonConverter personConverterMock;
-
-    @BeforeEach
-    void setUp() {
-        updateRecordedYearOfBirth = new UpdateRecordedYearOfBirthImpl(personConverterMock, personServiceMock);
-        when(censusEntryCommandMock.getPerson()).thenReturn(personMock);
-        when(personConverterMock.convert(personMock)).thenReturn(personCommandMock);
-    }
-
-    @Test
-    public void theYearOfBirthIsUpdatedTest() {
-        Integer yearOfBirth = GetRandomInteger();
-        when(censusEntryCommandMock.getBirthYear()).thenReturn(yearOfBirth.toString());
-        when(personMock.getRecordedYearOfBirth()).thenReturn(null);
-
-        updateRecordedYearOfBirth.updateRecordedYearOfBirth(censusEntryCommandMock);
-
-        verify(personCommandMock).setRecordedYearOfBirth(yearOfBirth.toString());
-    }
-
-
-}