Kaynağa Gözat

Census date changed to an enum

Andrew Grant 7 ay önce
ebeveyn
işleme
d8e726d552

+ 1 - 0
docs/enums.txt

@@ -16,4 +16,5 @@
 15: Generate changelog (ctrl-shift-A) as sql
 16: Add the new file to master.xml
 16: Run in terminal  mvn clean test liquibase:update
+17: Check using dbeaver that the database has changed
 

+ 1 - 1
src/main/java/scot/carricksoftware/grants/converters/census/CensusCommandConverterImpl.java

@@ -16,7 +16,7 @@ public class CensusCommandConverterImpl implements CensusCommandConverter {
     public Census convert(CensusCommand source) {
         Census result = new Census();
         result.setId(source.getId());
-        result.setDate(source.getDate());
+
         result.setCensusEntries(source.getCensusEntries());
         result.setPlace(source.getPlace());
         result.setBoundaryType(source.getBoundaryType());

+ 0 - 1
src/main/java/scot/carricksoftware/grants/converters/census/CensusConverterImpl.java

@@ -17,7 +17,6 @@ public class CensusConverterImpl implements CensusConverter {
     public CensusCommand convert(Census source) {
         CensusCommand result = new CensusCommandImpl();
         result.setId(source.getId());
-        result.setDate(source.getDate());
         result.setCensusEntries(source.getCensusEntries());
         result.setPlace(source.getPlace());
         result.setBoundaryType(source.getBoundaryType());

+ 5 - 5
src/main/java/scot/carricksoftware/grants/domains/census/Census.java

@@ -9,15 +9,16 @@ import jakarta.persistence.*;
 import scot.carricksoftware.grants.BaseEntity;
 import scot.carricksoftware.grants.domains.places.Place;
 import scot.carricksoftware.grants.enums.census.CensusBoundaryType;
+import scot.carricksoftware.grants.enums.census.CensusDate;
 
-import java.time.LocalDate;
 import java.util.ArrayList;
 import java.util.List;
 
 @Entity
 public class Census extends BaseEntity {
 
-    private LocalDate date;
+    @Enumerated(EnumType.STRING)
+    private CensusDate date;
 
     @OneToMany(mappedBy = "census", cascade = CascadeType.ALL, orphanRemoval = true)
     private List<CensusEntry> censusEntries = new ArrayList<>();
@@ -29,12 +30,11 @@ public class Census extends BaseEntity {
     @Enumerated(EnumType.STRING)
     private CensusBoundaryType boundaryType;
 
-
-    public LocalDate getDate() {
+    public CensusDate getDate() {
         return date;
     }
 
-    public void setDate(LocalDate date) {
+    public void setDate(CensusDate date) {
         this.date = date;
     }
 

+ 21 - 0
src/main/java/scot/carricksoftware/grants/enums/census/CensusDate.java

@@ -0,0 +1,21 @@
+/*
+ * Copyright (c) 2025.  Andrew Grant Carrick Software. All rights reserved
+ *
+ */
+
+package scot.carricksoftware.grants.enums.census;
+
+public enum CensusDate {
+
+    CENSUS_1881("03/04/1881"),
+    CENSUS_1901("31/03/1901");
+
+
+    @SuppressWarnings("unused")
+    public final String label;
+
+    @SuppressWarnings("unused")
+    CensusDate(String label) {
+        this.label = label;
+    }
+}

+ 11 - 0
src/main/resources/db/changelog/2025/04/19-01-changelog.sql

@@ -0,0 +1,11 @@
+/*
+ * Copyright (c) 2025.  Andrew Grant Carrick Software. All rights reserved
+ *
+ */
+
+-- liquibase formatted sql
+
+-- changeset apg:1745088309419-2
+ALTER TABLE census
+    ADD boundary_type VARCHAR(255) NULL;
+

+ 1 - 0
src/main/resources/db/changelog/master.xml

@@ -8,5 +8,6 @@
     <include file="/db/changelog/2025/04/14-01-changelog.sql"/>
     <include file="/db/changelog/2025/04/16-01-changelog.sql"/>
     <include file="/db/changelog/2025/04/17-01-changelog.sql"/>
+    <include file="/db/changelog/2025/04/19-01-changelog.sql"/>
 
 </databaseChangeLog>

+ 10 - 0
src/test/java/scot/carricksoftware/grants/GenerateCensusRandomEnums.java

@@ -5,6 +5,7 @@
 
 package scot.carricksoftware.grants;import org.springframework.stereotype.Component;
 import scot.carricksoftware.grants.enums.census.CensusBoundaryType;
+import scot.carricksoftware.grants.enums.census.CensusDate;
 
 import java.util.Random;
 
@@ -21,5 +22,14 @@ public class GenerateCensusRandomEnums {
         return boundaryTypes[randomInt];
     }
 
+    public static CensusDate GetRandomCensusDate() {
+
+        CensusDate[] dates = CensusDate.values();
+
+        Random random = new Random();
+        int randomInt = random.nextInt(0, dates.length );
+        return dates[randomInt];
+    }
+
 
 }

+ 0 - 1
src/test/java/scot/carricksoftware/grants/GenerateRandomCensusValues.java

@@ -26,7 +26,6 @@ public class GenerateRandomCensusValues {
     public static Census GetRandomCensus() {
         Census census = new Census();
         census.setId(GetRandomLong());
-        census.setDate(LocalDate.now());
         return census;
     }
 

+ 0 - 1
src/test/java/scot/carricksoftware/grants/converters/census/CensusCommandConverterTest.java

@@ -52,7 +52,6 @@ class CensusCommandConverterTest {
 
         assert target != null;
         assertEquals(id, target.getId());
-        assertEquals(date, target.getDate());
         assertEquals(censusEntries, target.getCensusEntries());
         assertEquals(place, target.getPlace());
         assertEquals(boundaryType, target.getBoundaryType());

+ 0 - 4
src/test/java/scot/carricksoftware/grants/converters/census/CensusConverterTest.java

@@ -13,7 +13,6 @@ import scot.carricksoftware.grants.domains.census.CensusEntry;
 import scot.carricksoftware.grants.domains.places.Place;
 import scot.carricksoftware.grants.enums.census.CensusBoundaryType;
 
-import java.time.LocalDate;
 import java.util.ArrayList;
 import java.util.List;
 
@@ -35,7 +34,6 @@ class CensusConverterTest {
     @Test
     void convertTest() {
         Long id = GetRandomLong();
-        LocalDate date = LocalDate.now();
         Census source = new Census();
         List<CensusEntry> censusEntries = new ArrayList<>();
         censusEntries.add(GetRandomCensusEntry());
@@ -44,7 +42,6 @@ class CensusConverterTest {
 
 
         source.setId(id);
-        source.setDate(date);
         source.setCensusEntries(censusEntries);
         source.setPlace(place);
         source.setBoundaryType(boundaryType);
@@ -53,7 +50,6 @@ class CensusConverterTest {
 
         assert target != null;
         assertEquals(id, target.getId());
-        assertEquals(date, target.getDate());
         assertEquals(censusEntries, target.getCensusEntries());
         assertEquals(place, target.getPlace());
         assertEquals(boundaryType, target.getBoundaryType());

+ 14 - 0
src/test/java/scot/carricksoftware/grants/domains/census/CensusEnumTest.java

@@ -10,10 +10,12 @@ import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.extension.ExtendWith;
 import org.mockito.junit.jupiter.MockitoExtension;
 import scot.carricksoftware.grants.enums.census.CensusBoundaryType;
+import scot.carricksoftware.grants.enums.census.CensusDate;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertNull;
 import static scot.carricksoftware.grants.GenerateCensusRandomEnums.GetRandomCensusBoundaryType;
+import static scot.carricksoftware.grants.GenerateCensusRandomEnums.GetRandomCensusDate;
 
 @ExtendWith(MockitoExtension.class)
 class CensusEnumTest {
@@ -37,4 +39,16 @@ class CensusEnumTest {
         assertEquals(boundaryType, census.getBoundaryType());
     }
 
+    @Test
+    public void getDateTest() {
+        assertNull(census.getDate());
+    }
+
+    @Test
+    public void setDateTest() {
+        CensusDate date = GetRandomCensusDate();
+        census.setDate(date);
+        assertEquals(date, census.getDate());
+    }
+
 }

+ 0 - 35
src/test/java/scot/carricksoftware/grants/domains/census/CensusTest.java

@@ -8,17 +8,13 @@ package scot.carricksoftware.grants.domains.census;
 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.Place;
 
-import java.time.LocalDate;
 import java.util.ArrayList;
 import java.util.List;
 
 import static org.junit.jupiter.api.Assertions.*;
-import static org.mockito.Mockito.when;
-import static scot.carricksoftware.grants.GenerateCertificateRandomValues.GetRandomString;
 import static scot.carricksoftware.grants.GenerateRandomCensusValues.GetRandomCensusEntry;
 import static scot.carricksoftware.grants.GenerateRandomNumberValues.GetRandomLong;
 import static scot.carricksoftware.grants.GenerateRandomPlaceValues.GetRandomPlace;
@@ -28,29 +24,11 @@ public class CensusTest {
 
     private Census census;
 
-    @Mock
-    Place placeMock;
-
-    @Mock
-    LocalDate dateMock;
-
     @BeforeEach
     public void setUp() {
         census = new Census();
     }
 
-    @Test
-    public void getDateTest() {
-        assertNull(census.getDate());
-    }
-
-    @Test
-    public void setDateTest() {
-        LocalDate localDate = LocalDate.now();
-        census.setDate(localDate);
-        assertEquals(localDate, census.getDate());
-    }
-
     @Test
     public void getIdTest() {
         assertNull(census.getId());
@@ -77,19 +55,6 @@ public class CensusTest {
     }
 
 
-    @Test
-    public void toStringTest() {
-        String string = GetRandomString();
-        String string2 = GetRandomString();
-        census.setPlace(placeMock);
-        census.setDate(dateMock);
-
-        when(placeMock.toString()).thenReturn(string);
-        when(dateMock.toString()).thenReturn(string2);
-
-        assertEquals(string + ", " + string2, census.toString());
-    }
-
     @Test
     public void getPlaceTest() {
         assertNull(census.getPlace());