Browse Source

ValidateNonNegativeIntegerTest

Andrew Grant 2 months ago
parent
commit
da8345d8e5

+ 23 - 1
src/main/java/scot/carricksoftware/grants/validators/text/AppendixTextCommandValidator.java

@@ -10,6 +10,9 @@ import org.apache.logging.log4j.Logger;
 import org.springframework.stereotype.Component;
 import org.springframework.validation.BindingResult;
 import scot.carricksoftware.grants.commands.text.AppendixTextCommand;
+import scot.carricksoftware.grants.constants.ApplicationConstants;
+import scot.carricksoftware.grants.constants.ValidationConstants;
+import scot.carricksoftware.grants.validators.helpers.ValidateTypes;
 
 @SuppressWarnings("unused")
 @Component
@@ -17,9 +20,28 @@ public class AppendixTextCommandValidator {
 
     private static final Logger logger = LogManager.getLogger(AppendixTextCommandValidator.class);
 
+    private final ValidateTypes validateTypes;
+
+    public AppendixTextCommandValidator(ValidateTypes validateTypes) {
+        this.validateTypes = validateTypes;
+    }
+
     public void validate(AppendixTextCommand appendixTextCommand, BindingResult bindingResult) {
-      logger.debug("DocumentTextCommandValidator::validate");
+        logger.debug("PersonTextCommandValidator::validate");
+        validateOrder(appendixTextCommand.getOrder(), bindingResult);
+        validateLevel(appendixTextCommand.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 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);
     }
 
 

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

@@ -0,0 +1,55 @@
+/*
+ * 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.junit.jupiter.api.Assertions.assertNotNull;
+import static org.mockito.Mockito.verify;
+import static scot.carricksoftware.grants.GenerateCertificateRandomValues.GetRandomString;
+
+@ExtendWith(MockitoExtension.class)
+class ValidateNonNegativeIntegerTest {
+
+    private ValidateTypes validateTypes;
+
+    @Mock
+    private BindingResult bindingResultMock;
+
+    @BeforeEach
+    public void setUp() {
+        validateTypes = new ValidateTypesImpl();
+    }
+
+    @Test
+    public void constructorTest() {
+        assertNotNull(validateTypes);
+    }
+
+    @Test
+    public void validateNullInteger() {
+        String fieldName = GetRandomString();
+        String nullMessage = GetRandomString();
+
+        validateTypes.validateNonNegativeInteger(null, fieldName, nullMessage, "", "", bindingResultMock);
+        verify(bindingResultMock).rejectValue(fieldName, "", null, nullMessage);
+    }
+
+    @Test
+    public void validateEmptyInteger() {
+        String fieldName = GetRandomString();
+        String nullMessage = GetRandomString();
+
+        validateTypes.validateNonNegativeInteger("", fieldName, nullMessage, "", "", bindingResultMock);
+        verify(bindingResultMock).rejectValue(fieldName, "", null, nullMessage);
+    }
+}