forked from citrusframework/citrus
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(citrusframework#1058): Introduce default fallback text message va…
…lidator - Avoid throwing no message validator found exception in favor of performing default fallback text equals message validation - Provides better context of test failure with message payload mismatch instead of raising generic no proper message validator found error
- Loading branch information
1 parent
ba23d17
commit dc7cd92
Showing
58 changed files
with
305 additions
and
773 deletions.
There are no files selected for viewing
86 changes: 86 additions & 0 deletions
86
...s-api/src/main/java/org/citrusframework/validation/DefaultTextEqualsMessageValidator.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,86 @@ | ||
/* | ||
* Copyright 2006-2023 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; | ||
|
||
import org.citrusframework.context.TestContext; | ||
import org.citrusframework.exceptions.ValidationException; | ||
import org.citrusframework.message.Message; | ||
import org.citrusframework.validation.context.ValidationContext; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* Default message validator implementation performing text equals on given message payloads. | ||
* Validator auto converts message payloads into a String representation in order to perform text equals validation. | ||
* Both received and control message should have textual message payloads. | ||
* By default, the validator ignores leading and trailing whitespaces and normalizes the line endings before the validation. | ||
* Usually this validator implementation is used as a fallback option when no other matching validator implementation could be found. | ||
* | ||
* @author Christoph Deppisch | ||
*/ | ||
public class DefaultTextEqualsMessageValidator extends DefaultMessageValidator { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(DefaultTextEqualsMessageValidator.class); | ||
|
||
private boolean normalizeLineEndings = true; | ||
private boolean trim = true; | ||
|
||
@Override | ||
public void validateMessage(Message receivedMessage, Message controlMessage, | ||
TestContext context, ValidationContext validationContext) { | ||
if (controlMessage == null || controlMessage.getPayload() == null || controlMessage.getPayload(String.class).isEmpty()) { | ||
logger.debug("Skip message payload validation as no control message was defined"); | ||
return; | ||
} | ||
|
||
logger.debug("Start to verify message payload ..."); | ||
|
||
String controlPayload = controlMessage.getPayload(String.class); | ||
String receivedPayload = receivedMessage.getPayload(String.class); | ||
|
||
if (trim) { | ||
controlPayload = controlPayload.trim(); | ||
receivedPayload = receivedPayload.trim(); | ||
} | ||
|
||
if (normalizeLineEndings) { | ||
controlPayload = normalizeLineEndings(controlPayload); | ||
receivedPayload = normalizeLineEndings(receivedPayload); | ||
} | ||
|
||
if (!receivedPayload.equals(controlPayload)) { | ||
throw new ValidationException("Validation failed - message payload not equal!"); | ||
} | ||
} | ||
|
||
public DefaultTextEqualsMessageValidator normalizeLineEndings() { | ||
this.normalizeLineEndings = true; | ||
return this; | ||
} | ||
|
||
public DefaultTextEqualsMessageValidator enableTrim() { | ||
this.trim = true; | ||
return this; | ||
} | ||
|
||
/** | ||
* Normalize the text by replacing line endings by a linux representation. | ||
*/ | ||
private static String normalizeLineEndings(String text) { | ||
return text.replace("\r\n", "\n").replace(" ", ""); | ||
} | ||
} |
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
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
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
86 changes: 86 additions & 0 deletions
86
...e/src/test/java/org/citrusframework/validation/DefaultTextEqualsMessageValidatorTest.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,86 @@ | ||
/* | ||
* Copyright 2006-2023 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; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
|
||
import org.citrusframework.UnitTestSupport; | ||
import org.citrusframework.exceptions.ValidationException; | ||
import org.citrusframework.message.DefaultMessage; | ||
import org.citrusframework.message.Message; | ||
import org.citrusframework.validation.context.DefaultValidationContext; | ||
import org.testng.annotations.DataProvider; | ||
import org.testng.annotations.Test; | ||
|
||
/** | ||
* @author Christoph Deppisch | ||
*/ | ||
public class DefaultTextEqualsMessageValidatorTest extends UnitTestSupport { | ||
|
||
private final DefaultTextEqualsMessageValidator validator = new DefaultTextEqualsMessageValidator(); | ||
private final DefaultValidationContext validationContext = new DefaultValidationContext(); | ||
|
||
@Test(dataProvider = "successTests") | ||
public void testValidate(Object received, Object control) { | ||
Message receivedMessage = new DefaultMessage(received); | ||
Message controlMessage = new DefaultMessage(control); | ||
|
||
validator.validateMessage(receivedMessage, controlMessage, context, validationContext); | ||
} | ||
|
||
@Test(dataProvider = "errorTests", expectedExceptions = ValidationException.class) | ||
public void testValidateError(Object received, Object control) throws Exception { | ||
Message receivedMessage = new DefaultMessage(received); | ||
Message controlMessage = new DefaultMessage(control); | ||
|
||
validator.validateMessage(receivedMessage, controlMessage, context, validationContext); | ||
} | ||
|
||
@DataProvider | ||
private Object[][] successTests() { | ||
return new Object[][] { | ||
new Object[]{ null, null }, | ||
new Object[]{ "", null }, | ||
new Object[]{ null, "" }, | ||
new Object[]{ "Hello World!", "Hello World!" }, | ||
new Object[]{ "Hello World! ", "Hello World!" }, | ||
new Object[]{ "Hello World!", "Hello World! " }, | ||
new Object[]{ "Hello World!\n", "Hello World!" }, | ||
new Object[]{ "Hello World!\n", "Hello World!\n" }, | ||
new Object[]{ "\nHello World!", "\nHello World!" }, | ||
new Object[]{ "Hello\nWorld!\n", "Hello\nWorld!\n" }, | ||
new Object[]{ "Hello\r\nWorld!\r\n", "Hello\nWorld!\n" }, | ||
new Object[]{ "Hello World!", null }, // empty control message | ||
new Object[]{ "Hello World!", "" }, // no control message | ||
new Object[]{ "Hello World!".getBytes(StandardCharsets.UTF_8), "" } // no control message | ||
}; | ||
} | ||
|
||
@DataProvider | ||
private Object[][] errorTests() { | ||
return new Object[][] { | ||
new Object[]{ null, "Hello World!" }, | ||
new Object[]{ "", "Hello World!" }, | ||
new Object[]{ "Hello World!", "Hello World!" }, | ||
new Object[]{ "Hello World!", "Hello World!" }, | ||
new Object[]{ "Hello\nWorld!", "Hello World!" }, | ||
new Object[]{ "Hello World!", "Hello\nWorld!" }, | ||
new Object[]{ "Hello!", "Hi!" }, | ||
}; | ||
} | ||
|
||
} |
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
28 changes: 0 additions & 28 deletions
28
.../citrus-base/src/test/java/org/citrusframework/validation/TextEqualsMessageValidator.java
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.