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 48488de
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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);

/**
Expand Down Expand Up @@ -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();
}
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,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.");
}
}

0 comments on commit 48488de

Please sign in to comment.