Prechádzať zdrojové kódy

Order and level changed to strings (2)

Andrew Grant 2 mesiacov pred
rodič
commit
92dd0b9ea3

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

@@ -26,4 +26,7 @@ public class ApplicationConstants {
     public static final int MINIMUM_NAME_LENGTH = 3;
     public static final int MAXIMUM_NAME_LENGTH = 120;//changed from 40 1/7/2025
 
-}
+    public static final int LATEX_BOOK = -2;
+    public static final int LATEX_SUB_PARAGRAPH = 5;
+
+}

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

@@ -61,10 +61,17 @@ public class ValidationConstants {
     public static final String NAME_IS_TOO_LONG = "Name must be no more than " + ApplicationConstants.MAXIMUM_NAME_LENGTH + " characters long.";
     public static final String NAME_IS_TOO_SHORT = "Name must be at least " + ApplicationConstants.MINIMUM_NAME_LENGTH + " characters long.";
     public static final String NEWBORN_IS_NULL = "The New Born cannot be null.";
-    public static final String NUMBER_IS_INVALID = "The number must be a non negative integer.";
+    public static final String NUMBER_IS_INVALID = "The number must be an integer.";
     public static final String NUMBER_IS_NULL = "The number cannot be null.";
+    public static final String NUMBER_IS_NEGATIVE = "The number cannot be negative.";
+    public static final String ORDER_IS_NULL = "Order must exist.";
+    public static final String ORDER_IS_INVALID = "The order must be an integer." ;
+    public static final String ORDER_IS_NEGATIVE = "The order must be non-negative." ;
     public static final String PERSON_IS_NULL = "The person cannot be null.";
     public static final String PLACE_IS_NULL = "Place must exist.";
+    public static final String LEVEL_IS_NULL = "Level must exist.";
+    public static final String LEVEL_IS_INVALID = "Level must be an integer.";
+    public static final String LEVEL_IS_OUTSIDE_LIMITS = "Level must be between -2 and 5.";
     public static final String REGION_IS_NULL = "Region must exist.";
     public static final String REGISTERED_DATE_IS_NULL = "The registered date cannot be null.";
     public static final String REGISTERED_DATE_INVALID_FORMAT = "The format should be dd/MM/yyyy.";

+ 2 - 1
src/main/java/scot/carricksoftware/grants/validators/certificates/birthcertificate/BirthCertificateCommandPartTwoValidatorImpl.java

@@ -35,7 +35,8 @@ public class BirthCertificateCommandPartTwoValidatorImpl implements BirthCertifi
 
     private void validateNumber(BirthCertificateCommand birthCertificateCommand, BindingResult bindingResult) {
         logger.debug("Validating registration number");
-        validateTypes.validateNonNegativeInteger(birthCertificateCommand.getNumber(), "number", ValidationConstants.NUMBER_IS_NULL, ValidationConstants.NUMBER_IS_INVALID,bindingResult);
+        validateTypes.validateNonNegativeInteger(birthCertificateCommand.getNumber(), "number",
+                ValidationConstants.NUMBER_IS_NULL, ValidationConstants.NUMBER_IS_INVALID, ValidationConstants.NUMBER_IS_NEGATIVE,bindingResult);
     }
 
     private void validateVolume(BirthCertificateCommand birthCertificateCommand, BindingResult bindingResult) {

+ 8 - 3
src/main/java/scot/carricksoftware/grants/validators/helpers/ValidateTypes.java

@@ -23,8 +23,13 @@ public interface ValidateTypes {
 
     void validateNullOrEmptyString(String string, @SuppressWarnings("SameParameterValue") String field, @SuppressWarnings("SameParameterValue") String message, BindingResult bindingResult);
 
-    @SuppressWarnings({"unused", "EmptyMethod"})
-    void validateNonNegativeInteger(String integerString, String fieldName, @SuppressWarnings("SameParameterValue") String nullMessage, String formatMessage, BindingResult bindingResult);
-
+    void validateNonNegativeInteger(String integerString, String fieldName, @SuppressWarnings("SameParameterValue") String nullMessage, String formatMessage, String negativeMessage, BindingResult bindingResult);
+
+    void validateIntegerRange(String integerString,
+                              Integer lowValue,
+                              Integer highValue,
+                              String fieldName, @SuppressWarnings("SameParameterValue")
+                              String nullMessage, String formatMessage, String rangeMessage,
+                              BindingResult bindingResult);
 
 }

+ 21 - 4
src/main/java/scot/carricksoftware/grants/validators/helpers/ValidateTypesImpl.java

@@ -53,22 +53,39 @@ public class ValidateTypesImpl implements ValidateTypes {
     }
 
     @Override
-    public void validateNonNegativeInteger(String integerString, String fieldName, @SuppressWarnings("SameParameterValue") String nullMessage, String formatMessage, BindingResult bindingResult) {
+    public void validateNonNegativeInteger(String integerString, String fieldName, String nullMessage, String formatMessage, String negativeMessage, BindingResult bindingResult) {
         if (integerString == null || integerString.isEmpty()) {
             bindingResult.rejectValue(fieldName, ApplicationConstants.EMPTY_STRING, null, nullMessage);
         } else {
             try {
                 int test = Integer.parseInt(integerString);
                 if (test < 0) {
-                    bindingResult.rejectValue(fieldName, ApplicationConstants.EMPTY_STRING, null, formatMessage);
+                    bindingResult.rejectValue(fieldName, negativeMessage, null, formatMessage);
                 }
             } catch (NumberFormatException e) {
-                bindingResult.rejectValue(fieldName, ApplicationConstants.EMPTY_STRING, null, formatMessage);
+                bindingResult.rejectValue(fieldName, formatMessage, null, formatMessage);
             }
         }
     }
 
-
+    @Override
+    public void validateIntegerRange(String integerString,
+                                     Integer lowValue, Integer highValue, String fieldName,
+                                     String nullMessage, String formatMessage, String rangeMessage,
+                                     BindingResult bindingResult) {
+        if (integerString == null || integerString.isEmpty()) {
+            bindingResult.rejectValue(fieldName, ApplicationConstants.EMPTY_STRING, null, nullMessage);
+        } else {
+            try {
+                int test = Integer.parseInt(integerString);
+                if (test < lowValue || test > highValue) {
+                    bindingResult.rejectValue(fieldName, ApplicationConstants.EMPTY_STRING, null, rangeMessage);
+                }
+            } catch (NumberFormatException e) {
+                bindingResult.rejectValue(fieldName, ApplicationConstants.EMPTY_STRING, null, formatMessage);
+            }
+        }
+    }
 
 
 }

+ 26 - 6
src/main/java/scot/carricksoftware/grants/validators/text/PersonTextCommandValidator.java

@@ -12,6 +12,8 @@ import org.springframework.validation.BindingResult;
 import scot.carricksoftware.grants.commands.text.PersonTextCommand;
 import scot.carricksoftware.grants.constants.ApplicationConstants;
 import scot.carricksoftware.grants.constants.ValidationConstants;
+import scot.carricksoftware.grants.domains.people.Person;
+import scot.carricksoftware.grants.validators.helpers.ValidateTypes;
 
 @SuppressWarnings("unused")
 @Component
@@ -19,17 +21,35 @@ public class PersonTextCommandValidator {
 
     private static final Logger logger = LogManager.getLogger(PersonTextCommandValidator.class);
 
+    private final ValidateTypes validateTypes;
+
+    public PersonTextCommandValidator(ValidateTypes validateTypes) {
+        this.validateTypes = validateTypes;
+    }
+
     public void validate(PersonTextCommand personTextCommand, BindingResult bindingResult) {
-      logger.debug("PersonTextCommandValidator::validate");
-        if (personTextCommand.getPerson() == null) {
-            bindingResult.rejectValue("person", ApplicationConstants.EMPTY_STRING,
-                    null,
-                    ValidationConstants.PERSON_IS_NULL);
-        }
+        logger.debug("PersonTextCommandValidator::validate");
+        validatePerson(personTextCommand.getPerson(), bindingResult);
+        validateOrder(personTextCommand.getOrder(), bindingResult);
+        validateLevel(personTextCommand.getLevel(), bindingResult);
+    }
 
+    private void validateLevel(String level, BindingResult bindingResult) {
+        logger.debug("PersonTextCommandValidator::validateLevel");
+        validateTypes.validateIntegerRange(level, ApplicationConstants.LATEX_BOOK, ApplicationConstants.LATEX_SUB_PARAGRAPH, "level",
+                ValidationConstants.LEVEL_IS_NULL, ValidationConstants.LEVEL_IS_INVALID, ValidationConstants.LEVEL_IS_OUTSIDE_LIMITS, bindingResult);
+    }
 
+    private void validatePerson(Person person, BindingResult bindingResult) {
+        logger.debug("PersonTextCommandValidator::validatePerson");
+        validateTypes.validatePerson(person, "person", ValidationConstants.PERSON_IS_NULL, bindingResult);
     }
 
+    private void validateOrder(String order, BindingResult bindingResult) {
+        logger.debug("PersonTextCommandValidator::validateOrder");
+        validateTypes.validateNonNegativeInteger(order, "order",ValidationConstants.ORDER_IS_NULL,
+                ValidationConstants.ORDER_IS_INVALID, ValidationConstants.ORDER_IS_NEGATIVE,bindingResult);
+    }
 
 }
 

+ 2 - 1
src/test/java/scot/carricksoftware/grants/validators/certificates/birthcertificate/BirthCertificateCommandPartTwoValidatorTest.java

@@ -48,7 +48,8 @@ class BirthCertificateCommandPartTwoValidatorTest {
         verify(validateTypesMock).validateNonNegativeInteger(birthCertificateCommandMock.getNumber(),
                 "number",
                 "The number cannot be null.",
-                "The number must be a non negative integer.",
+                "The number must be an integer.",
+                "The number cannot be negative.",
                 bindingResultMock);
         verify(validateTypesMock).validateNullOrEmptyString(birthCertificateCommandMock.getVolume(),
                 "volume", "The volume cannot be null.", bindingResultMock);

+ 0 - 75
src/test/java/scot/carricksoftware/grants/validators/helpers/ValidateNonNegativeIntegerTest.java

@@ -1,75 +0,0 @@
-/*
- * Copyright (c) 2025.  Andrew Grant Carrick Software. All rights reserved
- *
- */
-
-package scot.carricksoftware.grants.validators.helpers;
-
-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 org.springframework.validation.BindingResult;
-
-import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.verifyNoInteractions;
-
-@ExtendWith(MockitoExtension.class)
-class ValidateNonNegativeIntegerTest {
-
-    private ValidateTypes validateTypes;
-
-    private String field;
-    private String nullMessage;
-    private String formatMessage;
-
-    @Mock
-    private BindingResult bindingResultMock;
-
-    @BeforeEach
-    void setUp() {
-        validateTypes = new ValidateTypesImpl();
-        field = "field";
-        nullMessage = "nullMessage";
-        formatMessage = "formatMessage";
-    }
-
-    @Test
-    void nullTest() {
-        validateTypes.validateNonNegativeInteger(null, field, nullMessage, formatMessage, bindingResultMock);
-        verify(bindingResultMock).rejectValue(field, "", null, nullMessage);
-    }
-
-    @Test
-    void emptyTest() {
-        validateTypes.validateNonNegativeInteger("", field, nullMessage, formatMessage, bindingResultMock);
-        verify(bindingResultMock).rejectValue(field, "", null, nullMessage);
-    }
-
-    @Test
-    void negativeTest() {
-        validateTypes.validateNonNegativeInteger("-1", field, nullMessage, formatMessage, bindingResultMock);
-        verify(bindingResultMock).rejectValue(field, "", null, formatMessage);
-    }
-
-    @Test
-    void zeroTest() {
-        validateTypes.validateNonNegativeInteger("0", field, nullMessage, formatMessage, bindingResultMock);
-        verifyNoInteractions(bindingResultMock);
-    }
-
-    @Test
-    void positiveTest() {
-        validateTypes.validateNonNegativeInteger("1", field, nullMessage, formatMessage, bindingResultMock);
-        verifyNoInteractions(bindingResultMock);
-    }
-
-    @Test
-    void invalidFormatTest() {
-        validateTypes.validateNonNegativeInteger("z", field, nullMessage, formatMessage, bindingResultMock);
-        verify(bindingResultMock).rejectValue(field, "", null, formatMessage);
-    }
-
-
-}

+ 0 - 30
src/test/java/scot/carricksoftware/grants/validators/text/DocumentTextCommandValidatorTest.java

@@ -1,30 +0,0 @@
-/*
- * Copyright (c) Andrew Grant of Carrick Software 30/03/2025, 14:31. All rights reserved.
- *
- */
-
-package scot.carricksoftware.grants.validators.text;
-
-
-import org.junit.jupiter.api.BeforeEach;
-import org.junit.jupiter.api.Test;
-import org.junit.jupiter.api.extension.ExtendWith;
-import org.mockito.junit.jupiter.MockitoExtension;
-
-import static org.junit.jupiter.api.Assertions.assertNotNull;
-
-@ExtendWith(MockitoExtension.class)
-class DocumentTextCommandValidatorTest {
-
-    private DocumentTextCommandValidator validator;
-
-    @BeforeEach
-    void setUp() {
-        validator = new DocumentTextCommandValidator();
-    }
-
-    @Test
-    void existenceTest() {
-        assertNotNull(validator);
-    }
-}

+ 0 - 60
src/test/java/scot/carricksoftware/grants/validators/text/PersonTextCommandValidatorTest.java

@@ -1,60 +0,0 @@
-/*
- * Copyright (c) Andrew Grant of Carrick Software 30/03/2025, 14:31. All rights reserved.
- *
- */
-
-package scot.carricksoftware.grants.validators.text;
-
-
-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.text.PersonTextCommand;
-import scot.carricksoftware.grants.commands.text.PersonTextCommandImpl;
-
-import static org.junit.jupiter.api.Assertions.assertEquals;
-import static org.mockito.Mockito.verify;
-
-@ExtendWith(MockitoExtension.class)
-class PersonTextCommandValidatorTest {
-
-    private PersonTextCommandValidator validator;
-
-    private PersonTextCommand personTextCommand;
-
-    private ArgumentCaptor<String> stringArgumentCaptor;
-    private ArgumentCaptor<String> stringArgumentCaptor2;
-    private ArgumentCaptor<String> stringArgumentCaptor3;
-    private ArgumentCaptor<Object[]> objectArgumentCaptor;
-
-    @Mock
-    private BindingResult bindingResultMock;
-
-    @BeforeEach
-    void setUp() {
-        validator = new PersonTextCommandValidator();
-        personTextCommand = new PersonTextCommandImpl();
-        stringArgumentCaptor = ArgumentCaptor.forClass(String.class);
-        stringArgumentCaptor2 = ArgumentCaptor.forClass(String.class);
-        stringArgumentCaptor3 = ArgumentCaptor.forClass(String.class);
-        objectArgumentCaptor = ArgumentCaptor.forClass(Object[].class);
-    }
-
-    @Test
-    void validateNullPersonTest() {
-
-        validator.validate(personTextCommand, bindingResultMock);
-
-        verify(bindingResultMock).rejectValue(stringArgumentCaptor.capture(),
-                stringArgumentCaptor2.capture(),
-                objectArgumentCaptor.capture(),
-                stringArgumentCaptor3.capture());
-
-        assertEquals("person", stringArgumentCaptor.getValue());
-        assertEquals("The person cannot be null.", stringArgumentCaptor3.getValue());
-    }
-}

+ 0 - 60
src/test/java/scot/carricksoftware/grants/validators/text/PlaceTextCommandValidatorTest.java

@@ -1,60 +0,0 @@
-/*
- * Copyright (c) Andrew Grant of Carrick Software 30/03/2025, 14:31. All rights reserved.
- *
- */
-
-package scot.carricksoftware.grants.validators.text;
-
-
-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.text.PlaceTextCommand;
-import scot.carricksoftware.grants.commands.text.PlaceTextCommandImpl;
-
-import static org.junit.jupiter.api.Assertions.assertEquals;
-import static org.mockito.Mockito.verify;
-
-@ExtendWith(MockitoExtension.class)
-class PlaceTextCommandValidatorTest {
-
-    private PlaceTextCommandValidator validator;
-
-    private PlaceTextCommand placeTextCommand;
-
-    private ArgumentCaptor<String> stringArgumentCaptor;
-    private ArgumentCaptor<String> stringArgumentCaptor2;
-    private ArgumentCaptor<String> stringArgumentCaptor3;
-    private ArgumentCaptor<Object[]> objectArgumentCaptor;
-
-    @Mock
-    private BindingResult bindingResultMock;
-
-    @BeforeEach
-    void setUp() {
-        validator = new PlaceTextCommandValidator();
-        placeTextCommand = new PlaceTextCommandImpl();
-
-        stringArgumentCaptor = ArgumentCaptor.forClass(String.class);
-        stringArgumentCaptor2 = ArgumentCaptor.forClass(String.class);
-        stringArgumentCaptor3 = ArgumentCaptor.forClass(String.class);
-        objectArgumentCaptor = ArgumentCaptor.forClass(Object[].class);
-    }
-
-    @Test
-    void validateNullPlaceTest() {
-        validator.validate(placeTextCommand, bindingResultMock);
-
-        verify(bindingResultMock).rejectValue(stringArgumentCaptor.capture(),
-                stringArgumentCaptor2.capture(),
-                objectArgumentCaptor.capture(),
-                stringArgumentCaptor3.capture());
-
-        assertEquals("place", stringArgumentCaptor.getValue());
-        assertEquals("Place must exist.", stringArgumentCaptor3.getValue());
-    }
-}