From 9cfbb7645bc7a4f7d1dbd8a2febdbb35b1e75aa9 Mon Sep 17 00:00:00 2001 From: Simon Johnson <2924684+simon622@users.noreply.github.com> Date: Tue, 12 Sep 2023 10:33:35 +0100 Subject: [PATCH] Applied anchors to regex, fixed character groupings and supplied tests --- .../java/com/hivemq/uns/UnsModelTest.java | 125 ++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 hivemq-edge/src/test/java/com/hivemq/uns/UnsModelTest.java diff --git a/hivemq-edge/src/test/java/com/hivemq/uns/UnsModelTest.java b/hivemq-edge/src/test/java/com/hivemq/uns/UnsModelTest.java new file mode 100644 index 0000000000..c49d75d381 --- /dev/null +++ b/hivemq-edge/src/test/java/com/hivemq/uns/UnsModelTest.java @@ -0,0 +1,125 @@ +package com.hivemq.uns; + +import com.hivemq.api.utils.ApiValidation; +import com.hivemq.edge.HiveMQEdgeConstants; +import org.junit.Assert; +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Iterator; +import java.util.List; +import java.util.regex.Pattern; + +/** + * @author Simon L Johnson + */ +public class UnsModelTest { + + @Test + void testApiAlphaNumValidation_empty_null() { + + Assert.assertTrue("Empty string should be allowed when specified", + ApiValidation.validAlphaNumericSpaces("", true)); + Assert.assertFalse("Empty string should NOT be allowed when specified", + ApiValidation.validAlphaNumericSpaces("", false)); + + Assert.assertTrue(" string should be allowed when specified", + ApiValidation.validAlphaNumericSpaces(null, true)); + Assert.assertFalse(" string should NOT be allowed when specified", + ApiValidation.validAlphaNumericSpaces(null, false)); + } + + @Test + void testApiAlphaNumSpacesValidation() { + + Assert.assertTrue("Valid value (with spaces) should be valid", + ApiValidation.validAlphaNumericSpaces("valid value", false)); + + Assert.assertTrue("Valid value (without spaces) should be valid", + ApiValidation.validAlphaNumericSpaces("validvalue", false)); + + Assert.assertTrue("Multi character should be valid", + ApiValidation.validAlphaNumericSpaces("vv", false)); + + Assert.assertTrue("Multi character with space should be valid", + ApiValidation.validAlphaNumericSpaces("v ", false)); + + Assert.assertTrue("Single character should be valid", + ApiValidation.validAlphaNumericSpaces("v", false)); + + Assert.assertFalse("Special char should not be invalid", + ApiValidation.validAlphaNumericSpaces("\'", false)); + + Assert.assertTrue("Mixed case should be valid", + ApiValidation.validAlphaNumericSpaces("aAb", false)); + } + + @Test + void testApiAlphaNumValidation() { + + Assert.assertFalse("Invalid value (with spaces) should be invalid", + ApiValidation.validAlphaNumeric("invalid value", false)); + + Assert.assertTrue("Valid value (without spaces) should be valid", + ApiValidation.validAlphaNumeric("validvalue", false)); + + Assert.assertTrue("Multi character should be valid", + ApiValidation.validAlphaNumeric("vv", false)); + + Assert.assertFalse("Multi character with space should NOT be valid", + ApiValidation.validAlphaNumeric("v ", false)); + + Assert.assertTrue("Single character should be valid", + ApiValidation.validAlphaNumeric("v", false)); + + Assert.assertFalse("Special char should not be invalid", + ApiValidation.validAlphaNumeric("\'", false)); + + Assert.assertTrue("Mixed case should be valid", + ApiValidation.validAlphaNumericSpaces("aAb", false)); + } + + @Test + void testApiAlphaNumSpacesAndDashesValidation() { + + Assert.assertTrue("Valid value (with spaces) should be valid", + ApiValidation.validAlphaNumericSpacesAndDashes("valid value", false)); + + Assert.assertTrue("Valid value (without spaces) should be valid", + ApiValidation.validAlphaNumericSpacesAndDashes("validvalue", false)); + + Assert.assertTrue("Multi character should be valid", + ApiValidation.validAlphaNumericSpacesAndDashes("vv", false)); + + Assert.assertTrue("Multi character with space should be valid", + ApiValidation.validAlphaNumericSpacesAndDashes("v ", false)); + + Assert.assertTrue("Single character should be valid", + ApiValidation.validAlphaNumericSpacesAndDashes("v", false)); + + Assert.assertFalse("Special char should not be invalid", + ApiValidation.validAlphaNumericSpacesAndDashes("\'", false)); + + Assert.assertTrue("Mixed case should be valid", + ApiValidation.validAlphaNumericSpacesAndDashes("aAb", false)); + + Assert.assertTrue("Dashes should be valid", + ApiValidation.validAlphaNumericSpacesAndDashes("a-b", false)); + + Assert.assertTrue("Leading dashes should be valid", + ApiValidation.validAlphaNumericSpacesAndDashes("-ab", false)); + + Assert.assertTrue("Trailing dashes should be valid", + ApiValidation.validAlphaNumericSpacesAndDashes("ab-", false)); + + Assert.assertTrue("Underscores should be valid", + ApiValidation.validAlphaNumericSpacesAndDashes("a_b", false)); + + Assert.assertTrue("Trailing Underscores should be valid", + ApiValidation.validAlphaNumericSpacesAndDashes("a_", false)); + + Assert.assertTrue("Leading Underscores should be valid", + ApiValidation.validAlphaNumericSpacesAndDashes("_a", false)); + } +}