Kaynağa Gözat

BirthCertificateCommandValidator (2)

Andrew Grant 6 ay önce
ebeveyn
işleme
b5276e42e9

+ 10 - 5
src/main/java/scot/carricksoftware/grants/validators/certificates/BirthCertificateCommandValidator.java

@@ -76,11 +76,16 @@ public class BirthCertificateCommandValidator {
     }
 
     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.DATE_IN_FUTURE);
+        logger.debug("Validating birth Certificate valid date");
+        try {
+            LocalDate testDate = LocalDate.parse(birthCertificateCommand.getCertificateDate(), ApplicationConstants.FORMATTER);
+            if (testDate.isAfter(LocalDate.now())) {
+                bindingResult.rejectValue("certificateDate", ApplicationConstants.EMPTY_STRING,
+                        null,
+                        ValidationConstants.DATE_IN_FUTURE);
+            }
+        } catch (Exception e) {
+            logger.debug("Error trapped in  birth Certificate valid date");
         }
     }
 

+ 110 - 0
src/test/java/scot/carricksoftware/grants/validators/certificates/BirthCertificateCommandValidatorCertificateTest.java

@@ -0,0 +1,110 @@
+/*
+ * Copyright (c) Andrew Grant of Carrick Software 19/03/2025, 11:37. All rights reserved.
+ *
+ */
+
+package scot.carricksoftware.grants.validators.certificates;
+
+
+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 org.springframework.validation.BindingResult;
+import scot.carricksoftware.grants.commands.certificates.birthcertificates.BirthCertificateCommand;
+import scot.carricksoftware.grants.commands.certificates.birthcertificates.BirthCertificateCommandImpl;
+
+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.GenerateCertificateRandomValues.GetRandomString;
+import static scot.carricksoftware.grants.GenerateRandomPeopleValues.GetRandomPerson;
+import static scot.carricksoftware.grants.GenerateRandomPlaceValues.GetRandomPlace;
+
+@ExtendWith(MockitoExtension.class)
+class BirthCertificateCommandValidatorCertificateTest {
+
+    private BirthCertificateCommandValidator commandValidator;
+
+    private ArgumentCaptor<String> stringArgumentCaptor;
+    private ArgumentCaptor<String> stringArgumentCaptor2;
+    private ArgumentCaptor<String> stringArgumentCaptor3;
+    private ArgumentCaptor<Object[]> objectArgumentCaptor;
+
+    private BirthCertificateCommand birthCertificateCommand;
+
+    @Mock
+    BindingResult bindingResultMock;
+
+    @BeforeEach
+    void setUp() {
+        commandValidator = new BirthCertificateCommandValidator();
+        stringArgumentCaptor = ArgumentCaptor.forClass(String.class);
+        stringArgumentCaptor2 = ArgumentCaptor.forClass(String.class);
+        stringArgumentCaptor3 = ArgumentCaptor.forClass(String.class);
+        objectArgumentCaptor = ArgumentCaptor.forClass(Object[].class);
+
+        birthCertificateCommand = new BirthCertificateCommandImpl();
+    }
+
+    @Test
+    public void certificateNumberTest() {
+        birthCertificateCommand.setNewBorn(GetRandomPerson());
+        birthCertificateCommand.setCertificateDate("25/01/1953");
+        birthCertificateCommand.setCertificateIssuedAt(GetRandomPlace());
+
+        commandValidator.validate(birthCertificateCommand, bindingResultMock);
+
+        verify(bindingResultMock).rejectValue(stringArgumentCaptor.capture(),
+                stringArgumentCaptor2.capture(),
+                objectArgumentCaptor.capture(),
+                stringArgumentCaptor3.capture());
+
+        assertEquals("certificateNumber", stringArgumentCaptor.getValue());
+        assertEquals("The certificate number cannot be null.", stringArgumentCaptor3.getValue());
+
+    }
+
+    @Test
+    public void certificateSourceTest() {
+        birthCertificateCommand.setNewBorn(GetRandomPerson());
+        birthCertificateCommand.setCertificateNumber(GetRandomString());
+        birthCertificateCommand.setCertificateDate("25/01/1953");
+
+        commandValidator.validate(birthCertificateCommand, bindingResultMock);
+
+        verify(bindingResultMock).rejectValue(stringArgumentCaptor.capture(),
+                stringArgumentCaptor2.capture(),
+                objectArgumentCaptor.capture(),
+                stringArgumentCaptor3.capture());
+
+        assertEquals("certificateIssuedAt", stringArgumentCaptor.getValue());
+        assertEquals("The certificate source cannot be null.", stringArgumentCaptor3.getValue());
+    }
+
+    @Test
+    public void certificateInvalidDateTest() {
+        birthCertificateCommand.setNewBorn(GetRandomPerson());
+        birthCertificateCommand.setCertificateNumber(GetRandomString());
+        birthCertificateCommand.setCertificateDate(GetRandomString());
+        birthCertificateCommand.setCertificateIssuedAt(GetRandomPlace());
+
+        when(bindingResultMock.hasErrors()).thenReturn(false);
+        commandValidator.validate(birthCertificateCommand, bindingResultMock);
+
+        verify(bindingResultMock).rejectValue(stringArgumentCaptor.capture(),
+                stringArgumentCaptor2.capture(),
+                objectArgumentCaptor.capture(),
+                stringArgumentCaptor3.capture());
+
+        assertEquals("certificateDate", stringArgumentCaptor.getValue());
+        assertEquals("The certificate date is invalid or of the wrong format.", stringArgumentCaptor3.getValue());
+    }
+
+
+
+
+
+}

+ 96 - 0
src/test/java/scot/carricksoftware/grants/validators/certificates/BirthCertificateCommandValidatorDateTest.java

@@ -0,0 +1,96 @@
+/*
+ * Copyright (c) Andrew Grant of Carrick Software 19/03/2025, 11:37. All rights reserved.
+ *
+ */
+
+package scot.carricksoftware.grants.validators.certificates;
+
+
+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 org.springframework.validation.BindingResult;
+import scot.carricksoftware.grants.commands.certificates.birthcertificates.BirthCertificateCommand;
+import scot.carricksoftware.grants.commands.certificates.birthcertificates.BirthCertificateCommandImpl;
+
+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.GenerateCertificateRandomValues.GetRandomString;
+import static scot.carricksoftware.grants.GenerateRandomPeopleValues.GetRandomPerson;
+import static scot.carricksoftware.grants.GenerateRandomPlaceValues.GetRandomPlace;
+
+@ExtendWith(MockitoExtension.class)
+class BirthCertificateCommandValidatorDateTest {
+
+    private BirthCertificateCommandValidator commandValidator;
+
+    private ArgumentCaptor<String> stringArgumentCaptor;
+    private ArgumentCaptor<String> stringArgumentCaptor2;
+    private ArgumentCaptor<String> stringArgumentCaptor3;
+    private ArgumentCaptor<Object[]> objectArgumentCaptor;
+
+    private BirthCertificateCommand birthCertificateCommand;
+
+    @Mock
+    BindingResult bindingResultMock;
+
+    @BeforeEach
+    void setUp() {
+        commandValidator = new BirthCertificateCommandValidator();
+        stringArgumentCaptor = ArgumentCaptor.forClass(String.class);
+        stringArgumentCaptor2 = ArgumentCaptor.forClass(String.class);
+        stringArgumentCaptor3 = ArgumentCaptor.forClass(String.class);
+        objectArgumentCaptor = ArgumentCaptor.forClass(Object[].class);
+
+        birthCertificateCommand = new BirthCertificateCommandImpl();
+    }
+
+
+    @Test
+    public void certificateInvalidDateTest() {
+        birthCertificateCommand.setNewBorn(GetRandomPerson());
+        birthCertificateCommand.setCertificateNumber(GetRandomString());
+        birthCertificateCommand.setCertificateDate(GetRandomString());
+        birthCertificateCommand.setCertificateIssuedAt(GetRandomPlace());
+
+        when(bindingResultMock.hasErrors()).thenReturn(false);
+        commandValidator.validate(birthCertificateCommand, bindingResultMock);
+
+        verify(bindingResultMock).rejectValue(stringArgumentCaptor.capture(),
+                stringArgumentCaptor2.capture(),
+                objectArgumentCaptor.capture(),
+                stringArgumentCaptor3.capture());
+
+        assertEquals("certificateDate", stringArgumentCaptor.getValue());
+        assertEquals("The certificate date is invalid or of the wrong format.", stringArgumentCaptor3.getValue());
+    }
+
+    @Test
+    public void certificateFutureDateTest() {
+        birthCertificateCommand.setNewBorn(GetRandomPerson());
+        birthCertificateCommand.setCertificateNumber(GetRandomString());
+        birthCertificateCommand.setCertificateDate("01/01/2099");
+        birthCertificateCommand.setCertificateIssuedAt(GetRandomPlace());
+
+        when(bindingResultMock.hasErrors()).thenReturn(false);
+        commandValidator.validate(birthCertificateCommand, bindingResultMock);
+
+        verify(bindingResultMock).rejectValue(stringArgumentCaptor.capture(),
+                stringArgumentCaptor2.capture(),
+                objectArgumentCaptor.capture(),
+                stringArgumentCaptor3.capture());
+
+        assertEquals("certificateDate", stringArgumentCaptor.getValue());
+        assertEquals("Date should not be in the future.", stringArgumentCaptor3.getValue());
+    }
+
+
+
+
+
+
+}

+ 1 - 1
src/test/java/scot/carricksoftware/grants/validators/certificates/BirthCertificateCommandValidatorTest.java → src/test/java/scot/carricksoftware/grants/validators/certificates/BirthCertificateCommandValidatorPersonTest.java

@@ -25,7 +25,7 @@ import static scot.carricksoftware.grants.GenerateRandomPeopleValues.GetRandomPe
 import static scot.carricksoftware.grants.GenerateRandomPlaceValues.GetRandomPlace;
 
 @ExtendWith(MockitoExtension.class)
-class BirthCertificateCommandValidatorTest {
+class BirthCertificateCommandValidatorPersonTest {
 
     private BirthCertificateCommandValidator commandValidator;