-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
105 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
...se/src/main/java/org/citrusframework/validation/matcher/core/UuidV4ValidationMatcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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."); | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
...rc/test/java/org/citrusframework/validation/matcher/core/UuidV4ValidationMatcherTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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."); | ||
} | ||
} |