Ver código fonte

BirthCertificateCommandValidator

Andrew Grant 6 meses atrás
pai
commit
fc8746ac61

+ 1 - 1
src/main/java/scot/carricksoftware/grants/constants/ApplicationConstants.java

@@ -18,7 +18,7 @@ public class ApplicationConstants {
     }
 
     public static final int DEFAULT_PAGE_SIZE = 15;
-    public static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("dd-MM-yyyy");
+    public static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("dd/MM/yyyy");
     public static final String DATE_TIME_FORMAT = "dd-MM-yyyy";
     public final static String EMPTY_STRING = "";
 

+ 5 - 0
src/main/java/scot/carricksoftware/grants/constants/ValidationConstants.java

@@ -25,6 +25,11 @@ public class ValidationConstants {
     public static final String CENSUS_NAME_IS_NOT_NULL = "Both Person and Untracked Person cannot be given";
 
     public static final String PERSON_IS_NULL = "The person cannot be null.";
+    public static final String NEWBORN_IS_NULL = "The New Born cannot be null.";
+    public static final String CERTIFICATE_NUMBER_IS_NULL = "The certificate number cannot be null.";
+    public static final String ISSUED_AT_IS_NULL = "The certificate source cannot be null.";
+    public static final String CERTIFICATE_DATE_IS_NULL = "The certificate date cannot be null.";
+    public static final String DATE_IS_INVALID = "The certificate date is invalid or of the wrong format.";
 
     public static final String PLACE_IS_NULL = "The place cannot be null.";
     public static final String COUNTRY_IS_NULL = "The country cannot be null.";

+ 59 - 3
src/main/java/scot/carricksoftware/grants/validators/certificates/BirthCertificateCommandValidator.java

@@ -13,6 +13,7 @@ import scot.carricksoftware.grants.commands.certificates.birthcertificates.Birth
 import scot.carricksoftware.grants.constants.ApplicationConstants;
 import scot.carricksoftware.grants.constants.ValidationConstants;
 
+import java.time.LocalDate;
 
 @Component
 public class BirthCertificateCommandValidator {
@@ -21,10 +22,65 @@ public class BirthCertificateCommandValidator {
     @SuppressWarnings("unused")
     public void validate(BirthCertificateCommand birthCertificateCommand, BindingResult bindingResult) {
         logger.debug("Validating birth certificate command");
-        if (birthCertificateCommand.getNewBorn() == null) {
-            bindingResult.rejectValue("person", ApplicationConstants.EMPTY_STRING,
+
+        validateNewBorn(birthCertificateCommand, bindingResult);
+        validateCertificateNumber(birthCertificateCommand, bindingResult);
+        validateCertificateIssuedAt(birthCertificateCommand, bindingResult);
+        validateCertificateDate(birthCertificateCommand, bindingResult);
+        if (!bindingResult.hasErrors()) {
+            validateCertificateLegitimateDate(birthCertificateCommand, bindingResult);
+        }
+    }
+
+    private void validateNewBorn(BirthCertificateCommand birthCertificateCommand, BindingResult bindingResult) {
+        logger.debug("Validating birth certificate newBorn");
+        if (birthCertificateCommand.getNewBorn() == null ) {
+            bindingResult.rejectValue("newBorn", ApplicationConstants.EMPTY_STRING,
+                    null,
+                    ValidationConstants.NEWBORN_IS_NULL);
+        }
+    }
+
+    private void validateCertificateNumber(BirthCertificateCommand birthCertificateCommand, BindingResult bindingResult) {
+        logger.debug("Validating birth certificate Certificate Number");
+        if (birthCertificateCommand.getCertificateNumber() == null || birthCertificateCommand.getCertificateNumber().isEmpty()) {
+            bindingResult.rejectValue("certificateNumber", ApplicationConstants.EMPTY_STRING,
+                    null,
+                    ValidationConstants.CERTIFICATE_NUMBER_IS_NULL);
+        }
+    }
+
+    private void validateCertificateIssuedAt(BirthCertificateCommand birthCertificateCommand, BindingResult bindingResult) {
+        logger.debug("Validating birth certificate Certificate Issued At");
+        if (birthCertificateCommand.getCertificateIssuedAt() == null ) {
+            bindingResult.rejectValue("certificateIssuedAt", ApplicationConstants.EMPTY_STRING,
+                    null,
+                    ValidationConstants.ISSUED_AT_IS_NULL);
+        }
+    }
+
+    private void validateCertificateDate(BirthCertificateCommand birthCertificateCommand, BindingResult bindingResult) {
+        logger.debug("Validating birth certificate Certificate Date");
+        if (birthCertificateCommand.getCertificateDate() == null || birthCertificateCommand.getCertificateDate().isEmpty()) {
+            bindingResult.rejectValue("certificateDate", ApplicationConstants.EMPTY_STRING,
+                    null,
+                    ValidationConstants.CERTIFICATE_DATE_IS_NULL);
+        }
+        try {
+            LocalDate.parse(birthCertificateCommand.getCertificateDate(), ApplicationConstants.FORMATTER);
+        } catch (Exception e) {
+            bindingResult.rejectValue("certificateDate", ApplicationConstants.EMPTY_STRING,
+                    null,
+                    ValidationConstants.DATE_IS_INVALID);
+        }
+    }
+
+    private void validateCertificateLegitimateDate(BirthCertificateCommand birthCertificateCommand, BindingResult bindingResult) {
+        LocalDate testDate = LocalDate.parse(birthCertificateCommand.getCertificateDate(), ApplicationConstants.FORMATTER);
+        if (testDate.isAfter(LocalDate.now())) {
+            bindingResult.rejectValue("certificateDate", ApplicationConstants.EMPTY_STRING,
                     null,
-                    ValidationConstants.PERSON_IS_NULL);
+                    ValidationConstants.DATE_IN_FUTURE);
         }
     }
 

+ 23 - 11
src/main/resources/templates/certificates/birthCertificate/form.html

@@ -22,7 +22,9 @@
 <!--/*@thymesVar id="place" type="scot.carricksoftware.grants.domains.places.Place"*/-->
 <div th:insert="~{fragments/layout::banner}"></div>
 
+
 <div class="container border border-info rounded-3 text-center p-4">
+    <h3>Birth Certificate Details</h3>
     <form th:object="${birthCertificateCommand}" th:action="@{/birthCertificate}" method="post">
         <div th:if="${#fields.hasErrors('*')}" class="alert alert-danger">
             <p>Please Correct The Errors Below</p>
@@ -30,28 +32,33 @@
 
         <table>
             <tr>
-                <td style = "text-align: right;">
+                <td style="text-align: right;">
                     <label for="id">Database Id &nbsp;<span style="color: rgb(255,0,0);">*</span></label>
                 </td>
-                <td style = "text-align: left;">
+                <td style="text-align: left;">
                     <input class="form-control" id="id"
                            th:field="*{id}" type="text" readonly>
                 </td>
             </tr>
             <tr>
-                <td style = "text-align: right;">
+                <td style="text-align: right;">
                     <label for="certificateNumber">Certificate Number&nbsp;</label>
                 </td>
-                <td style = "text-align: left;">
+                <td style="text-align: left;">
                     <input class="form-control" id="certificateNumber"
-                           th:field="*{certificateNumber}" type="text" readonly>
+                           th:field="*{certificateNumber}" type="text">
+                    <div th:if="${#fields.hasErrors('certificateNumber')}">
+                        <ul class="text-danger">
+                            <li th:each="err : ${#fields.errors('certificateNumber')}" th:text="${err}"/>
+                        </ul>
+                    </div>
                 </td>
             </tr>
             <tr>
-                <td style = "text-align: right;">
+                <td style="text-align: right;">
                     <label for="certificateIssuedAt">Issued From&nbsp;</label>
                 </td>
-                <td style = "text-align: left;">
+                <td style="text-align: left;">
                     <div>
                         <select id="certificateIssuedAt" name="place" th:field="*{certificateIssuedAt}">
                             <option th:value="${''}" th:text="${''}"></option>
@@ -67,19 +74,24 @@
                 </td>
             </tr>
             <tr>
-                <td style = "text-align: right;">
+                <td style="text-align: right;">
                     <label for="certificateDate">Issued At (dd/mm/yyyy)&nbsp;</label>
                 </td>
-                <td style = "text-align: left;">
+                <td style="text-align: left;">
                     <input class="form-control" id="certificateDate"
                            th:field="*{certificateDate}" type="text">
+                    <div th:if="${#fields.hasErrors('certificateDate')}">
+                        <ul class="text-danger">
+                            <li th:each="err : ${#fields.errors('certificateDate')}" th:text="${err}"/>
+                        </ul>
+                    </div>
                 </td>
             </tr>
             <tr>
-                <td style = "text-align: right;">
+                <td style="text-align: right;">
                     <label for="newBorn">NewBorn</label>
                 </td>
-                <td style = "text-align: left;">
+                <td style="text-align: left;">
                     <select id="newBorn" name="place" th:field="*{newBorn}">
                         <option th:value="${''}" th:text="${''}"></option>
                         <option th:each="person : ${people}"

+ 12 - 4
src/test/java/scot/carricksoftware/grants/validators/certificates/BirthCertificateCommandValidatorTest.java

@@ -17,9 +17,12 @@ import scot.carricksoftware.grants.commands.certificates.birthcertificates.Birth
 import scot.carricksoftware.grants.commands.certificates.birthcertificates.BirthCertificateCommandImpl;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.times;
 import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.verifyNoInteractions;
+import static org.mockito.Mockito.when;
 import static scot.carricksoftware.grants.GenerateRandomPeopleValues.GetRandomPerson;
+import static scot.carricksoftware.grants.GenerateRandomPlaceValues.GetRandomPlace;
 
 @ExtendWith(MockitoExtension.class)
 class BirthCertificateCommandValidatorTest {
@@ -45,6 +48,10 @@ class BirthCertificateCommandValidatorTest {
         objectArgumentCaptor = ArgumentCaptor.forClass(Object[].class);
 
         birthCertificateCommand = new BirthCertificateCommandImpl();
+        birthCertificateCommand.setCertificateDate("25/01/1953");
+        birthCertificateCommand.setCertificateNumber("1953");
+        birthCertificateCommand.setCertificateIssuedAt(GetRandomPlace());
+
     }
 
     @Test
@@ -56,17 +63,18 @@ class BirthCertificateCommandValidatorTest {
                 objectArgumentCaptor.capture(),
                 stringArgumentCaptor3.capture());
 
-        assertEquals("person", stringArgumentCaptor.getValue());
-        assertEquals("The person cannot be null.", stringArgumentCaptor3.getValue());
+        assertEquals("newBorn", stringArgumentCaptor.getValue());
+        assertEquals("The New Born cannot be null.", stringArgumentCaptor3.getValue());
 
     }
 
     @Test
     public void notNullPersonTest() {
         birthCertificateCommand.setNewBorn(GetRandomPerson());
+        when(bindingResultMock.hasErrors()).thenReturn(false);
         commandValidator.validate(birthCertificateCommand, bindingResultMock);
 
-        verifyNoInteractions(bindingResultMock);
+        verify(bindingResultMock, times(0)).rejectValue(any(), any(), any(), any());
     }
 
 }