Kaynağa Gözat

Refactor Bootstrap

Andrew Grant 6 ay önce
ebeveyn
işleme
fbed52d830

+ 4 - 65
src/main/java/scot/carricksoftware/grants/bootstrap/DataLoadCertificates.java

@@ -9,17 +9,7 @@ import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
 import org.springframework.context.annotation.Profile;
 import org.springframework.stereotype.Component;
-import scot.carricksoftware.grants.commands.certificates.birthcertificates.BirthCertificateCommand;
-import scot.carricksoftware.grants.commands.certificates.birthcertificates.BirthCertificateCommandImpl;
-import scot.carricksoftware.grants.domains.certificates.DeathCertificate;
-import scot.carricksoftware.grants.domains.people.Person;
-import scot.carricksoftware.grants.domains.places.Organisation;
-import scot.carricksoftware.grants.enums.general.Sex;
-import scot.carricksoftware.grants.enums.certificates.CertificateType;
-import scot.carricksoftware.grants.services.certificates.birthcertificates.BirthCertificateService;
-import scot.carricksoftware.grants.services.certificates.deathcertificates.DeathCertificateService;
-import scot.carricksoftware.grants.services.people.PersonService;
-import scot.carricksoftware.grants.services.places.organisations.OrganisationService;
+
 
 @Component
 @Profile("dev")
@@ -27,69 +17,18 @@ public class DataLoadCertificates {
 
     private static final Logger logger = LogManager.getLogger(DataLoadCertificates.class);
 
-    private final BirthCertificateService birthCertificateService;
-    private final DeathCertificateService deathCertificateService;
-    private final PersonService personService;
-
-    public DataLoadCertificates(BirthCertificateService birthCertificateService,
-                                DeathCertificateService deathCertificateService,
-                                PersonService personService) {
-        this.birthCertificateService = birthCertificateService;
-        this.deathCertificateService = deathCertificateService;
-        this.personService = personService;
-    }
-
-
     public void load() {
-        logger.debug("DataLoadPlaces::load");
+        logger.debug("DataLoadCertificates::load");
         loadBirthCertificates();
         loadDeathCertificates();
     }
 
-
-
     private void loadBirthCertificates() {
-        Person newBorn = new Person();
-        newBorn.setFirstName("new born");
-
-        BirthCertificateCommand birthCertificateCommand = new BirthCertificateCommandImpl();
-        birthCertificateCommand.setNewBorn(newBorn);
-        birthCertificateCommand.setCertificateDate("25/01/1953");
-        birthCertificateCommand.setCertificateNumber("999");
-        birthCertificateCommand.setCertificateType(CertificateType.EXTRACT);
-
-        Organisation registrationAuthority = new Organisation();
-        registrationAuthority.setName("registration authority");
-        birthCertificateCommand.setRegistrationAuthority(registrationAuthority);
-
-        Organisation certificateSource = new Organisation();
-        certificateSource.setName("certificate source");
-        birthCertificateCommand.setCertificateSource(certificateSource);
-
-        birthCertificateCommand.setNumber("01");
-        birthCertificateCommand.setVolume("1953");
-        birthCertificateCommand.setSex(Sex.MALE);
-        birthCertificateCommand.setWhenBorn("25/01/1953 01:01");
-        birthCertificateCommand.setWhereBorn("where born");
-        birthCertificateCommand.setDateAndPlaceOfMarriage("date and place of marriage");
-        birthCertificateCommand.setFatherRank("fatherRank");
-
-        Person father = new Person();
-        father.setFirstName("father");
-        birthCertificateCommand.setFather(father);
-        birthCertificateCommand.setUntrackedFather("untrackedFather");
-
-        Person mother = new Person();
-        mother.setFirstName("mother");
-        birthCertificateCommand.setMother(mother);
-
-        birthCertificateService.saveBirthCertificateCommand(birthCertificateCommand);
+        logger.debug("DataLoadCertificates::LoadBirthCertificates");
     }
 
     private void loadDeathCertificates() {
-        DeathCertificate deathCertificate = new DeathCertificate();
-        deathCertificate.setDeceased(personService.findById(1L));
-        deathCertificateService.save(deathCertificate);
+        logger.debug("DataLoadCertificates::LoadDeathCertificates");
     }
 
 

+ 49 - 0
src/main/java/scot/carricksoftware/grants/bootstrap/DataLoadOrganisations.java

@@ -0,0 +1,49 @@
+/*
+ * Copyright (c)  18 Feb 2025, Andrew Grant of Carrick Software .
+ * All rights reserved.
+ */
+
+package scot.carricksoftware.grants.bootstrap;
+
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+import org.springframework.context.annotation.Profile;
+import org.springframework.stereotype.Component;
+import scot.carricksoftware.grants.commands.places.organisations.OrganisationCommand;
+import scot.carricksoftware.grants.commands.places.organisations.OrganisationCommandImpl;
+import scot.carricksoftware.grants.services.places.organisations.OrganisationService;
+
+
+@Component
+@Profile("dev")
+public class DataLoadOrganisations {
+
+    private static final Logger logger = LogManager.getLogger(DataLoadOrganisations.class);
+
+    private final OrganisationService organisationService;
+
+    public DataLoadOrganisations(OrganisationService organisationService) {
+        this.organisationService = organisationService;
+    }
+
+    public void load() {
+        logger.debug("DataLoadOrganisation::load");
+        loadRegistrationAuthority();
+        loadCertificateSource();
+    }
+
+    private void loadCertificateSource() {
+        logger.debug("DataLoadOrganisation::loadCertificateSource");
+        OrganisationCommand certificateSourceCommand = new OrganisationCommandImpl();
+        certificateSourceCommand.setName("Certificate Source");
+        organisationService.saveOrganisationCommand(certificateSourceCommand);
+    }
+
+    private void loadRegistrationAuthority() {
+        logger.debug("DataLoadOrganisation::loadRegistrationAuthority");
+        OrganisationCommand registrationAuthorityCommand = new OrganisationCommandImpl();
+        registrationAuthorityCommand.setName("Registration Authority");
+        organisationService.saveOrganisationCommand(registrationAuthorityCommand);
+    }
+
+}

+ 5 - 2
src/main/java/scot/carricksoftware/grants/bootstrap/DataLoaderPrimary.java

@@ -20,17 +20,20 @@ public class DataLoaderPrimary {
     private final DataLoadPlaces dataLoadPlaces;
     private final DataLoadPeople dataLoadPeople;
     private final DataLoadCensus dataLoadCensus;
+    private final DataLoadOrganisations dataLoadOrganisations;
 
-    public DataLoaderPrimary(DataLoadPlaces dataLoadPlaces, DataLoadPeople dataLoadPeople, DataLoadCensus dataLoadCensus) {
+    public DataLoaderPrimary(DataLoadPlaces dataLoadPlaces, DataLoadPeople dataLoadPeople, DataLoadCensus dataLoadCensus, DataLoadOrganisations dataLoadOrganisations) {
         this.dataLoadPlaces = dataLoadPlaces;
         this.dataLoadPeople = dataLoadPeople;
         this.dataLoadCensus = dataLoadCensus;
+        this.dataLoadOrganisations = dataLoadOrganisations;
     }
 
     public void load () {
         logger.debug("DataLoaderPrimary::load");
-        dataLoadPlaces.load();
         dataLoadPeople.load();
+        dataLoadOrganisations.load();
+        dataLoadPlaces.load();
         dataLoadCensus.load();
     }
 

+ 2 - 0
src/main/java/scot/carricksoftware/grants/repositories/places/OrganisationRepository.java

@@ -23,4 +23,6 @@ public interface OrganisationRepository extends PagingAndSortingRepository<Organ
     Optional<Organisation> findById(Long id);
 
     Iterable<Organisation> findAll();
+
+    Optional<Organisation> findByName(String name);
 }

+ 3 - 0
src/main/java/scot/carricksoftware/grants/services/places/organisations/OrganisationService.java

@@ -27,6 +27,9 @@ public interface OrganisationService {
 
     Organisation findById(Long id);
 
+    @SuppressWarnings("unused")
+    Organisation findByName(String name);
+
     @SuppressWarnings("UnusedReturnValue")
     Organisation save(Organisation organisation);
 }

+ 7 - 0
src/main/java/scot/carricksoftware/grants/services/places/organisations/OrganisationServiceImpl.java

@@ -92,6 +92,13 @@ public class OrganisationServiceImpl implements OrganisationService {
         return organisation.orElse(null);
     }
 
+    @Override
+    public Organisation findByName(String name) {
+        logger.debug("OrganisationServiceImpl::findByName");
+        Optional<Organisation> organisation = organisationRepository.findByName(name);
+        return organisation.orElse(null);
+    }
+
     @Override
     public Organisation save(Organisation organisation) {
         logger.debug("OrganisationServiceImpl::save");

+ 0 - 86
src/test/java/scot/carricksoftware/grants/bootstrap/DataLoadCertificatesTest.java

@@ -1,86 +0,0 @@
-package scot.carricksoftware.grants.bootstrap;
-
-import org.junit.jupiter.api.BeforeEach;
-import org.junit.jupiter.api.Test;
-import org.junit.jupiter.api.extension.ExtendWith;
-import org.mockito.ArgumentCaptor;
-import org.mockito.Mock;
-import org.mockito.junit.jupiter.MockitoExtension;
-import scot.carricksoftware.grants.commands.certificates.birthcertificates.BirthCertificateCommand;
-import scot.carricksoftware.grants.domains.certificates.DeathCertificate;
-import scot.carricksoftware.grants.domains.people.Person;
-import scot.carricksoftware.grants.enums.general.Sex;
-import scot.carricksoftware.grants.enums.certificates.CertificateType;
-import scot.carricksoftware.grants.services.certificates.birthcertificates.BirthCertificateService;
-import scot.carricksoftware.grants.services.certificates.deathcertificates.DeathCertificateService;
-import scot.carricksoftware.grants.services.people.PersonService;
-
-import static org.junit.jupiter.api.Assertions.assertEquals;
-import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.when;
-import static scot.carricksoftware.grants.GenerateRandomPeopleValues.GetRandomPerson;
-
-@ExtendWith(MockitoExtension.class)
-public class DataLoadCertificatesTest {
-
-    private DataLoadCertificates dataLoadCertificates;
-
-    @Mock
-    private BirthCertificateService birthCertificateServiceMock;
-
-    @Mock
-    private DeathCertificateService deathCertificateServiceMock;
-
-    @Mock
-    private PersonService personServiceMock;
-
-
-    @BeforeEach
-    public void setUp() {
-        dataLoadCertificates = new DataLoadCertificates(birthCertificateServiceMock,
-                deathCertificateServiceMock,
-                personServiceMock);
-    }
-
-    @Test
-    public void birthCertificatesAreLoadedTest() {
-
-        ArgumentCaptor<BirthCertificateCommand> captor = ArgumentCaptor.forClass(BirthCertificateCommand.class);
-
-        dataLoadCertificates.load();
-
-        verify(birthCertificateServiceMock).saveBirthCertificateCommand(captor.capture());
-        assertEquals("new born", captor.getValue().getNewBorn().getFirstName());
-        assertEquals("certificate source", captor.getValue().getCertificateSource().getName());
-        assertEquals("999", captor.getValue().getCertificateNumber());
-        assertEquals("25/01/1953", captor.getValue().getCertificateDate());
-        assertEquals(CertificateType.EXTRACT, captor.getValue().getCertificateType());
-        assertEquals("registration authority", captor.getValue().getRegistrationAuthority().getName());
-        assertEquals("01", captor.getValue().getNumber());
-        assertEquals("1953", captor.getValue().getVolume());
-        assertEquals(Sex.MALE, captor.getValue().getSex());
-        assertEquals("25/01/1953 01:01", captor.getValue().getWhenBorn());
-        assertEquals("where born", captor.getValue().getWhereBorn());
-        assertEquals("fatherRank", captor.getValue().getFatherRank());
-        assertEquals("untrackedFather", captor.getValue().getUntrackedFather());
-        assertEquals("father", captor.getValue().getFather().getFirstName());
-        assertEquals("mother", captor.getValue().getMother().getFirstName());
-        assertEquals("date and place of marriage", captor.getValue().getDateAndPlaceOfMarriage());
-
-    }
-
-    @Test
-    public void deathCertificatesAreLoadedTest() {
-        Person person = GetRandomPerson();
-        when(personServiceMock.findById(1L)).thenReturn(person);
-        ArgumentCaptor<DeathCertificate> captor = ArgumentCaptor.forClass(DeathCertificate.class);
-
-        dataLoadCertificates.load();
-
-        verify(deathCertificateServiceMock).save(captor.capture());
-        assertEquals(person, captor.getValue().getDeceased());
-
-    }
-
-
-}

+ 10 - 1
src/test/java/scot/carricksoftware/grants/bootstrap/DataLoaderPrimaryTest.java

@@ -27,11 +27,14 @@ class DataLoaderPrimaryTest {
     @Mock
     private DataLoadCensus dataLoadCensusMock;
 
+    @Mock
+    private DataLoadOrganisations dataLoadOrganisationsMock;
+
     @BeforeEach
     void setUp() {
         dataLoaderPrimary = new DataLoaderPrimary(dataLoadPlacesMock,
                 dataLoadPeopleMock,
-                dataLoadCensusMock);
+                dataLoadCensusMock, dataLoadOrganisationsMock);
     }
 
     @Test
@@ -52,5 +55,11 @@ class DataLoaderPrimaryTest {
         verify(dataLoadCensusMock).load();
     }
 
+    @Test
+    void loadCertificatesOrganisationsIsCalledTest() {
+        dataLoaderPrimary.load();
+        verify(dataLoadOrganisationsMock).load();
+    }
+
 
 }