From a9f25fc29e79d22f41f7b9609e4d20f2759c19c2 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 | 24 +------- .../matcher/core/UuidV4ValidationMatcher.java | 48 ++++++++++++++++ .../core/UuidV4ValidationMatcherTest.java | 55 +++++++++++++++++++ 3 files changed, 105 insertions(+), 22 deletions(-) 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..509445f0c2 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 @@ -16,28 +16,7 @@ package org.citrusframework.validation.matcher; -import org.citrusframework.validation.matcher.core.ContainsIgnoreCaseValidationMatcher; -import org.citrusframework.validation.matcher.core.ContainsValidationMatcher; -import org.citrusframework.validation.matcher.core.CreateVariableValidationMatcher; -import org.citrusframework.validation.matcher.core.DatePatternValidationMatcher; -import org.citrusframework.validation.matcher.core.DateRangeValidationMatcher; -import org.citrusframework.validation.matcher.core.EmptyValidationMatcher; -import org.citrusframework.validation.matcher.core.EndsWithValidationMatcher; -import org.citrusframework.validation.matcher.core.EqualsIgnoreCaseValidationMatcher; -import org.citrusframework.validation.matcher.core.GreaterThanValidationMatcher; -import org.citrusframework.validation.matcher.core.IgnoreNewLineValidationMatcher; -import org.citrusframework.validation.matcher.core.IgnoreValidationMatcher; -import org.citrusframework.validation.matcher.core.IsNumberValidationMatcher; -import org.citrusframework.validation.matcher.core.LowerThanValidationMatcher; -import org.citrusframework.validation.matcher.core.MatchesValidationMatcher; -import org.citrusframework.validation.matcher.core.NotEmptyValidationMatcher; -import org.citrusframework.validation.matcher.core.NotNullValidationMatcher; -import org.citrusframework.validation.matcher.core.NullValidationMatcher; -import org.citrusframework.validation.matcher.core.StartsWithValidationMatcher; -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.WeekdayValidationMatcher; +import org.citrusframework.validation.matcher.core.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -74,6 +53,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..0090d833ab --- /dev/null +++ b/core/citrus-base/src/test/java/org/citrusframework/validation/matcher/core/UuidV4ValidationMatcherTest.java @@ -0,0 +1,55 @@ +/* + * 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."); + } +}