Andrew Grant 3 сар өмнө
parent
commit
dc71aa0abb

+ 5 - 0
src/main/java/scot/carricksoftware/grants/cache/BMDCache.java

@@ -6,6 +6,7 @@
 package scot.carricksoftware.grants.cache;
 
 import scot.carricksoftware.grants.domains.people.Person;
+import scot.carricksoftware.grants.domains.places.Organisation;
 import scot.carricksoftware.grants.domains.places.Place;
 
 import java.util.List;
@@ -19,4 +20,8 @@ public interface BMDCache {
     List<Place> getPlaces();
 
     void invalidatePlaces();
+
+    List<Organisation> getOrganisations();
+
+    void invalidateOrganisations();
 }

+ 19 - 1
src/main/java/scot/carricksoftware/grants/cache/BMDCacheImpl.java

@@ -7,8 +7,10 @@ package scot.carricksoftware.grants.cache;
 
 import org.springframework.stereotype.Component;
 import scot.carricksoftware.grants.domains.people.Person;
+import scot.carricksoftware.grants.domains.places.Organisation;
 import scot.carricksoftware.grants.domains.places.Place;
 import scot.carricksoftware.grants.services.people.PersonService;
+import scot.carricksoftware.grants.services.places.organisations.OrganisationService;
 import scot.carricksoftware.grants.services.places.places.PlaceService;
 
 import java.util.List;
@@ -18,13 +20,16 @@ public class BMDCacheImpl implements BMDCache {
 
     private final PersonService personService;
     private final PlaceService placeService;
+    private final OrganisationService organisationService;
 
     private List<Person> people = null;
     private List<Place> places = null;
+    private List<Organisation> organisations = null;
 
-    public BMDCacheImpl(PersonService personService, PlaceService placeService) {
+    public BMDCacheImpl(PersonService personService, PlaceService placeService, OrganisationService organisationService) {
         this.personService = personService;
         this.placeService = placeService;
+        this.organisationService = organisationService;
     }
 
     @Override
@@ -52,4 +57,17 @@ public class BMDCacheImpl implements BMDCache {
     public void invalidatePlaces() {
         places = null;
     }
+
+    @Override
+    public List<Organisation> getOrganisations() {
+        if (organisations == null) {
+            organisations = organisationService.findAll();
+        }
+        return organisations;
+    }
+
+    @Override
+    public void invalidateOrganisations() {
+        organisations = null;
+    }
 }

+ 71 - 0
src/test/java/scot/carricksoftware/grants/cache/BMDOrganisationsCacheTest.java

@@ -0,0 +1,71 @@
+/*
+ * Copyright (c) 2025.  Andrew Grant Carrick Software. All rights reserved
+ *
+ */
+
+package scot.carricksoftware.grants.cache;
+
+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.domains.places.Organisation;
+import scot.carricksoftware.grants.services.people.PersonService;
+import scot.carricksoftware.grants.services.places.organisations.OrganisationService;
+import scot.carricksoftware.grants.services.places.places.PlaceService;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+import static scot.carricksoftware.grants.GenerateRandomPlaceValues.GetRandomOrganisation;
+
+@ExtendWith(MockitoExtension.class)
+public class BMDOrganisationsCacheTest {
+    
+    @Mock
+    PersonService personServiceMock;
+
+    @Mock
+    PlaceService placeServiceMock;
+
+    @Mock
+    OrganisationService organisationServiceMock;
+
+    private BMDCache cache;
+
+    private List<Organisation> organisationsList = null;
+
+    @BeforeEach
+    void setUp() {
+        organisationsList = new ArrayList<>();
+        organisationsList.add(GetRandomOrganisation());
+        when(organisationServiceMock.findAll()).thenReturn(organisationsList);
+        cache = new BMDCacheImpl(personServiceMock, placeServiceMock, organisationServiceMock);
+    }
+
+    @Test
+    void nullOrganisationsTest() {
+        assertEquals(organisationsList,cache.getOrganisations());
+        verify(organisationServiceMock).findAll();
+    }
+
+    @Test
+    void alreadyExistsTest() {
+        assertEquals(organisationsList,cache.getOrganisations());
+        assertEquals(organisationsList,cache.getOrganisations());
+        verify(organisationServiceMock, times(1)).findAll();
+    }
+
+    @Test
+    void invalidateTest() {
+        assertEquals(organisationsList,cache.getOrganisations());
+        cache.invalidateOrganisations();
+        assertEquals(organisationsList,cache.getOrganisations());
+        verify(organisationServiceMock, times(2)).findAll();
+    }
+}

+ 5 - 1
src/test/java/scot/carricksoftware/grants/cache/BMDPeopleCacheTest.java

@@ -12,6 +12,7 @@ import org.mockito.Mock;
 import org.mockito.junit.jupiter.MockitoExtension;
 import scot.carricksoftware.grants.domains.people.Person;
 import scot.carricksoftware.grants.services.people.PersonService;
+import scot.carricksoftware.grants.services.places.organisations.OrganisationService;
 import scot.carricksoftware.grants.services.places.places.PlaceService;
 
 import java.util.ArrayList;
@@ -32,6 +33,9 @@ public class BMDPeopleCacheTest {
     @Mock
     PlaceService placeServiceMock;
 
+    @Mock
+    OrganisationService organisationServiceMock;
+
     private BMDCache cache;
 
     private List<Person> peopleList = null;
@@ -41,7 +45,7 @@ public class BMDPeopleCacheTest {
         peopleList = new ArrayList<>();
         peopleList.add(GetRandomPerson());
         when(personServiceMock.findAll()).thenReturn(peopleList);
-        cache = new BMDCacheImpl(personServiceMock, placeServiceMock);
+        cache = new BMDCacheImpl(personServiceMock, placeServiceMock, organisationServiceMock);
     }
 
     @Test

+ 5 - 1
src/test/java/scot/carricksoftware/grants/cache/BMDPlacesCacheTest.java

@@ -11,6 +11,7 @@ import org.junit.jupiter.api.extension.ExtendWith;
 import org.mockito.Mock;
 import org.mockito.junit.jupiter.MockitoExtension;
 import scot.carricksoftware.grants.services.people.PersonService;
+import scot.carricksoftware.grants.services.places.organisations.OrganisationService;
 import scot.carricksoftware.grants.services.places.places.PlaceService;
 import scot.carricksoftware.grants.domains.places.Place;
 
@@ -33,6 +34,9 @@ public class BMDPlacesCacheTest {
     @Mock
     PlaceService placeServiceMock;
 
+    @Mock
+    OrganisationService organisationServiceMock;
+
     private BMDCache cache;
 
     private List<Place> placesList = null;
@@ -42,7 +46,7 @@ public class BMDPlacesCacheTest {
         placesList = new ArrayList<>();
         placesList.add(GetRandomPlace());
         when(placeServiceMock.findAll()).thenReturn(placesList);
-        cache = new BMDCacheImpl(personServiceMock, placeServiceMock);
+        cache = new BMDCacheImpl(personServiceMock, placeServiceMock, organisationServiceMock);
     }
 
     @Test