ソースを参照

BMDCachePlacesTest

Andrew Grant 3 ヶ月 前
コミット
49b2b375b1

+ 4 - 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.Place;
 
 import java.util.List;
 
@@ -15,4 +16,7 @@ public interface BMDCache {
     List<Person> getPeople();
     void invalidatePeople();
 
+    List<Place> getPlaces();
+
+    void invalidatePlaces();
 }

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

@@ -7,7 +7,9 @@ package scot.carricksoftware.grants.cache;
 
 import org.springframework.stereotype.Component;
 import scot.carricksoftware.grants.domains.people.Person;
+import scot.carricksoftware.grants.domains.places.Place;
 import scot.carricksoftware.grants.services.people.PersonService;
+import scot.carricksoftware.grants.services.places.places.PlaceService;
 
 import java.util.List;
 
@@ -15,11 +17,14 @@ import java.util.List;
 public class BMDCacheImpl implements BMDCache {
 
     private final PersonService personService;
+    private final PlaceService placeService;
 
     private List<Person> people = null;
+    private List<Place> places = null;
 
-    public BMDCacheImpl(PersonService personService) {
+    public BMDCacheImpl(PersonService personService, PlaceService placeService) {
         this.personService = personService;
+        this.placeService = placeService;
     }
 
     @Override
@@ -34,4 +39,17 @@ public class BMDCacheImpl implements BMDCache {
     public void invalidatePeople() {
          people = null;
     }
+
+    @Override
+    public List<Place> getPlaces() {
+        if (places == null) {
+            places = placeService.findAll();
+        }
+        return places;
+    }
+
+    @Override
+    public void invalidatePlaces() {
+        places = null;
+    }
 }

+ 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.places.PlaceService;
 
 import java.util.ArrayList;
 import java.util.List;
@@ -28,6 +29,9 @@ public class BMDPeopleCacheTest {
     @Mock
     PersonService personServiceMock;
 
+    @Mock
+    PlaceService placeServiceMock;
+
     private BMDCache cache;
 
     private List<Person> peopleList = null;
@@ -37,7 +41,7 @@ public class BMDPeopleCacheTest {
         peopleList = new ArrayList<>();
         peopleList.add(GetRandomPerson());
         when(personServiceMock.findAll()).thenReturn(peopleList);
-        cache = new BMDCacheImpl(personServiceMock);
+        cache = new BMDCacheImpl(personServiceMock, placeServiceMock);
     }
 
     @Test

+ 68 - 0
src/test/java/scot/carricksoftware/grants/cache/BMDPlacesCacheTest.java

@@ -0,0 +1,68 @@
+/*
+ * 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.services.people.PersonService;
+import scot.carricksoftware.grants.services.places.places.PlaceService;
+import scot.carricksoftware.grants.domains.places.Place;
+
+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.GetRandomPlace;
+
+@ExtendWith(MockitoExtension.class)
+public class BMDPlacesCacheTest {
+
+
+    @Mock
+    PersonService personServiceMock;
+
+    @Mock
+    PlaceService placeServiceMock;
+
+    private BMDCache cache;
+
+    private List<Place> placesList = null;
+
+    @BeforeEach
+    void setUp() {
+        placesList = new ArrayList<>();
+        placesList.add(GetRandomPlace());
+        when(placeServiceMock.findAll()).thenReturn(placesList);
+        cache = new BMDCacheImpl(personServiceMock, placeServiceMock);
+    }
+
+    @Test
+    void nullPlacesTest() {
+        assertEquals(placesList,cache.getPlaces());
+        verify(placeServiceMock).findAll();
+    }
+
+    @Test
+    void alreadyExistsTest() {
+        assertEquals(placesList,cache.getPlaces());
+        assertEquals(placesList,cache.getPlaces());
+        verify(placeServiceMock, times(1)).findAll();
+    }
+
+    @Test
+    void invalidateTest() {
+        assertEquals(placesList,cache.getPlaces());
+        cache.invalidatePlaces();
+        assertEquals(placesList,cache.getPlaces());
+        verify(placeServiceMock, times(2)).findAll();
+    }
+}