Skip to content

Commit

Permalink
feat: uuid v4 validation matcher
Browse files Browse the repository at this point in the history
  • Loading branch information
phos-web committed Nov 6, 2024
1 parent d512416 commit a9f25fc
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String> 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.");
}
}
Original file line number Diff line number Diff line change
@@ -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.");
}
}

0 comments on commit a9f25fc

Please sign in to comment.