From 48488de2ca23b72291dcc7d333ef7b3740e8cfa3 Mon Sep 17 00:00:00 2001 From: Andy Huynh Date: Thu, 31 Oct 2024 15:49:02 +0100 Subject: [PATCH] feat: uuid v4 validation matcher --- .../DefaultValidationMatcherLibrary.java | 6 ++- .../matcher/core/UuidV4ValidationMatcher.java | 48 +++++++++++++++++ .../core/UuidV4ValidationMatcherTest.java | 54 +++++++++++++++++++ 3 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 core/citrus-base/src/main/java/org/citrusframework/validation/matcher/core/UuidV4ValidationMatcher.java create mode 100644 core/citrus-base/src/test/java/org/citrusframework/validation/matcher/core/UuidV4ValidationMatcherTest.java diff --git a/core/citrus-base/src/main/java/org/citrusframework/validation/matcher/DefaultValidationMatcherLibrary.java b/core/citrus-base/src/main/java/org/citrusframework/validation/matcher/DefaultValidationMatcherLibrary.java index ad87db3edd..875a6f4733 100644 --- a/core/citrus-base/src/main/java/org/citrusframework/validation/matcher/DefaultValidationMatcherLibrary.java +++ b/core/citrus-base/src/main/java/org/citrusframework/validation/matcher/DefaultValidationMatcherLibrary.java @@ -37,13 +37,16 @@ import org.citrusframework.validation.matcher.core.StringLengthValidationMatcher; import org.citrusframework.validation.matcher.core.TrimAllWhitespacesValidationMatcher; import org.citrusframework.validation.matcher.core.TrimValidationMatcher; +import org.citrusframework.validation.matcher.core.UuidV4ValidationMatcher; import org.citrusframework.validation.matcher.core.WeekdayValidationMatcher; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class DefaultValidationMatcherLibrary extends ValidationMatcherLibrary { - /** Logger */ + /** + * Logger + */ private static final Logger logger = LoggerFactory.getLogger(DefaultValidationMatcherLibrary.class); /** @@ -74,6 +77,7 @@ public DefaultValidationMatcherLibrary() { getMembers().put("notNull", new NotNullValidationMatcher()); getMembers().put("ignore", new IgnoreValidationMatcher()); getMembers().put("hasLength", new StringLengthValidationMatcher()); + getMembers().put("isUUIDv4", new UuidV4ValidationMatcher()); lookupValidationMatchers(); } diff --git a/core/citrus-base/src/main/java/org/citrusframework/validation/matcher/core/UuidV4ValidationMatcher.java b/core/citrus-base/src/main/java/org/citrusframework/validation/matcher/core/UuidV4ValidationMatcher.java new file mode 100644 index 0000000000..cbe0131cec --- /dev/null +++ b/core/citrus-base/src/main/java/org/citrusframework/validation/matcher/core/UuidV4ValidationMatcher.java @@ -0,0 +1,48 @@ +/* + * Copyright the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.citrusframework.validation.matcher.core; + +import org.citrusframework.context.TestContext; +import org.citrusframework.exceptions.ValidationException; +import org.citrusframework.validation.matcher.ValidationMatcher; + +import java.util.List; +import java.util.UUID; + +/** + * UuidValidationMatcher checks if valid UUID version 4 is present + */ +public class UuidV4ValidationMatcher implements ValidationMatcher { + @Override + public void validate(String fieldName, String value, List controlParameters, TestContext context) throws ValidationException { + try { + UUID uuid = UUID.fromString(value); + + if (uuid.version() != 4) { + throwValidationException(fieldName, value); + } + } catch (IllegalArgumentException e) { + throwValidationException(fieldName, value); + } + } + + private void throwValidationException(String fieldName, String value) { + throw new ValidationException(this.getClass().getSimpleName() + + " failed for field '" + fieldName + + "'. Received value '" + value + "' is not a uuid v4."); + } +} diff --git a/core/citrus-base/src/test/java/org/citrusframework/validation/matcher/core/UuidV4ValidationMatcherTest.java b/core/citrus-base/src/test/java/org/citrusframework/validation/matcher/core/UuidV4ValidationMatcherTest.java new file mode 100644 index 0000000000..84d3242234 --- /dev/null +++ b/core/citrus-base/src/test/java/org/citrusframework/validation/matcher/core/UuidV4ValidationMatcherTest.java @@ -0,0 +1,54 @@ +/* + * Copyright the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.citrusframework.validation.matcher.core; + +import org.citrusframework.UnitTestSupport; +import org.citrusframework.exceptions.ValidationException; +import org.testng.annotations.Test; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.assertj.core.api.Fail.fail; + +public class UuidV4ValidationMatcherTest extends UnitTestSupport { + + private UuidV4ValidationMatcher matcher = new UuidV4ValidationMatcher(); + + @Test + public void testValidateSuccess() { + try { + matcher.validate("field", "34d68c48-1455-43ac-a0d6-b7b894c7a7d2", null, context); + } catch (Exception e) { + fail(e); + } + } + + @Test + public void testInvalidUuid() { + assertThatThrownBy(() -> + matcher.validate("field", "34d68c48-1455-43ac-a0d6-b7b894c7a7d2uuidtoolong", null, context)) + .isInstanceOf(ValidationException.class) + .hasMessage("UuidV4ValidationMatcher failed for field 'field'. Received value '34d68c48-1455-43ac-a0d6-b7b894c7a7d2uuidtoolong' is not a uuid v4."); + } + + @Test + public void testWrongUuidVersion() { + assertThatThrownBy(() -> + matcher.validate("field", "34be571d-7180-3bcf-bbda-20cffbfae9ed", null, context)) + .isInstanceOf(ValidationException.class) + .hasMessage("UuidV4ValidationMatcher failed for field 'field'. Received value '34be571d-7180-3bcf-bbda-20cffbfae9ed' is not a uuid v4."); + } +}