Sfoglia il codice sorgente

BirthCertificateCommandValidator (3)

Andrew Grant 6 mesi fa
parent
commit
913b55515f

+ 8 - 7
src/main/java/scot/carricksoftware/grants/validators/certificates/BirthCertificateCommandValidator.java

@@ -65,13 +65,14 @@ public class BirthCertificateCommandValidator {
             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);
+        } else {
+            try {
+                LocalDate.parse(birthCertificateCommand.getCertificateDate(), ApplicationConstants.FORMATTER);
+            } catch (Exception e) {
+                bindingResult.rejectValue("certificateDate", ApplicationConstants.EMPTY_STRING,
+                        null,
+                        ValidationConstants.DATE_IS_INVALID);
+            }
         }
     }
 

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

@@ -88,6 +88,24 @@ class BirthCertificateCommandValidatorDateTest {
         assertEquals("Date should not be in the future.", stringArgumentCaptor3.getValue());
     }
 
+    @Test
+    public void certificateNullDateTest() {
+        birthCertificateCommand.setNewBorn(GetRandomPerson());
+        birthCertificateCommand.setCertificateNumber(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 cannot be null.", stringArgumentCaptor3.getValue());
+    }
+