From 8088de4985cf70887f7612ea8788bd7cfea10fa7 Mon Sep 17 00:00:00 2001 From: Timon Borter Date: Mon, 28 Oct 2024 15:38:22 +0100 Subject: [PATCH] chore(citrus-validation): review and code cleanup pr: https://github.com/citrusframework/citrus/pull/1224 `citrus-validation` module and sub-modules. --- .../json/JsonSchemaRepository.java | 17 ++++++------ .../json/schema/JsonSchemaValidation.java | 1 - .../json/SendMessageActionTest.java | 9 +++---- .../text/PlainTextMessageValidator.java | 13 +++++----- .../xml/schema/XmlSchemaValidation.java | 26 +++++++++---------- .../xml/XsdSchemaRepository.java | 1 + 6 files changed, 33 insertions(+), 34 deletions(-) diff --git a/validation/citrus-validation-json/src/main/java/org/citrusframework/json/JsonSchemaRepository.java b/validation/citrus-validation-json/src/main/java/org/citrusframework/json/JsonSchemaRepository.java index c3dfc33a37..08fa5e8cce 100644 --- a/validation/citrus-validation-json/src/main/java/org/citrusframework/json/JsonSchemaRepository.java +++ b/validation/citrus-validation-json/src/main/java/org/citrusframework/json/JsonSchemaRepository.java @@ -16,39 +16,40 @@ package org.citrusframework.json; -import java.util.ArrayList; -import java.util.List; import org.citrusframework.json.schema.SimpleJsonSchema; import org.citrusframework.repository.BaseRepository; import org.citrusframework.spi.Resource; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.ArrayList; +import java.util.List; + /** * Schema repository holding a set of json schema resources known in the test scope. * @since 2.7.3 */ -public class JsonSchemaRepository extends BaseRepository { +public class JsonSchemaRepository extends BaseRepository { + + /** Logger */ + private static Logger logger = LoggerFactory.getLogger(JsonSchemaRepository.class); private static final String DEFAULT_NAME = "jsonSchemaRepository"; /** List of schema resources */ private List schemas = new ArrayList<>(); - - /** Logger */ - private static Logger logger = LoggerFactory.getLogger(JsonSchemaRepository.class); - public JsonSchemaRepository() { super(DEFAULT_NAME); } - + @Override protected void addRepository(Resource resource) { if (resource.getLocation().endsWith(".json")) { if (logger.isDebugEnabled()) { logger.debug("Loading json schema resource '{}'", resource.getLocation()); } + SimpleJsonSchema simpleJsonSchema = new SimpleJsonSchema(resource); simpleJsonSchema.initialize(); schemas.add(simpleJsonSchema); diff --git a/validation/citrus-validation-json/src/main/java/org/citrusframework/validation/json/schema/JsonSchemaValidation.java b/validation/citrus-validation-json/src/main/java/org/citrusframework/validation/json/schema/JsonSchemaValidation.java index f2e8eaeed5..f80f146670 100644 --- a/validation/citrus-validation-json/src/main/java/org/citrusframework/validation/json/schema/JsonSchemaValidation.java +++ b/validation/citrus-validation-json/src/main/java/org/citrusframework/validation/json/schema/JsonSchemaValidation.java @@ -192,7 +192,6 @@ private static boolean isJsonSchemaValidationEnabled() { @Override public void validate(Message message, TestContext context, String schemaRepository, String schema) { - JsonMessageValidationContext validationContext = Builder.json() .schemaValidation(true) .schema(schema) diff --git a/validation/citrus-validation-json/src/test/java/org/citrusframework/validation/json/SendMessageActionTest.java b/validation/citrus-validation-json/src/test/java/org/citrusframework/validation/json/SendMessageActionTest.java index 4183e3e2d2..e787b41f92 100644 --- a/validation/citrus-validation-json/src/test/java/org/citrusframework/validation/json/SendMessageActionTest.java +++ b/validation/citrus-validation-json/src/test/java/org/citrusframework/validation/json/SendMessageActionTest.java @@ -68,8 +68,7 @@ protected TestContextFactory createTestContextFactory() { @SuppressWarnings("rawtypes") public void testSendMessageOverwriteMessageElementsJsonPath() { DefaultMessageBuilder messageBuilder = new DefaultMessageBuilder(); - messageBuilder.setPayloadBuilder( - new DefaultPayloadBuilder("{ \"TestRequest\": { \"Message\": \"?\" }}")); + messageBuilder.setPayloadBuilder(new DefaultPayloadBuilder("{ \"TestRequest\": { \"Message\": \"?\" }}")); Map overwriteElements = new HashMap<>(); overwriteElements.put("$.TestRequest.Message", "Hello World!"); @@ -99,12 +98,10 @@ public void testSendMessageOverwriteMessageElementsJsonPath() { .process(processor) .build(); sendAction.execute(context); - } @Test public void testSendJsonMessageWithValidation() { - AtomicBoolean validated = new AtomicBoolean(false); SchemaValidator schemaValidator = mock(SchemaValidator.class); @@ -115,6 +112,7 @@ public void testSendJsonMessageWithValidation() { Assert.assertEquals(invocation.getArgument(2, String.class), "fooRepository"); validated.set(true); + return null; }).when(schemaValidator) .validate(isA(Message.class), isA(TestContext.class), isA(String.class), @@ -129,8 +127,7 @@ public void testSendJsonMessageWithValidation() { context.getMessageValidatorRegistry().addSchemaValidator("JSON", schemaValidator); DefaultMessageBuilder messageBuilder = new DefaultMessageBuilder(); - messageBuilder.setPayloadBuilder( - new DefaultPayloadBuilder("{ \"TestRequest\": { \"Message\": \"?\" }}")); + messageBuilder.setPayloadBuilder(new DefaultPayloadBuilder("{ \"TestRequest\": { \"Message\": \"?\" }}")); reset(endpoint, producer, endpointConfiguration); when(endpoint.createProducer()).thenReturn(producer); diff --git a/validation/citrus-validation-text/src/main/java/org/citrusframework/validation/text/PlainTextMessageValidator.java b/validation/citrus-validation-text/src/main/java/org/citrusframework/validation/text/PlainTextMessageValidator.java index 788a845ac8..b6b4dde371 100644 --- a/validation/citrus-validation-text/src/main/java/org/citrusframework/validation/text/PlainTextMessageValidator.java +++ b/validation/citrus-validation-text/src/main/java/org/citrusframework/validation/text/PlainTextMessageValidator.java @@ -16,12 +16,6 @@ package org.citrusframework.validation.text; -import static java.lang.Boolean.parseBoolean; -import static java.lang.Integer.parseInt; -import static org.citrusframework.message.MessagePayloadUtils.normalizeWhitespace; - -import java.util.regex.Matcher; -import java.util.regex.Pattern; import org.citrusframework.CitrusSettings; import org.citrusframework.context.TestContext; import org.citrusframework.exceptions.ValidationException; @@ -32,6 +26,13 @@ import org.citrusframework.validation.context.ValidationContext; import org.citrusframework.validation.matcher.ValidationMatcherUtils; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import static java.lang.Boolean.parseBoolean; +import static java.lang.Integer.parseInt; +import static org.citrusframework.message.MessagePayloadUtils.normalizeWhitespace; + /** * Plain text validator using simple String comparison. * diff --git a/validation/citrus-validation-xml/src/main/java/org/citrusframework/validation/xml/schema/XmlSchemaValidation.java b/validation/citrus-validation-xml/src/main/java/org/citrusframework/validation/xml/schema/XmlSchemaValidation.java index 6423365b1a..26074b3308 100644 --- a/validation/citrus-validation-xml/src/main/java/org/citrusframework/validation/xml/schema/XmlSchemaValidation.java +++ b/validation/citrus-validation-xml/src/main/java/org/citrusframework/validation/xml/schema/XmlSchemaValidation.java @@ -16,18 +16,6 @@ package org.citrusframework.validation.xml.schema; -import static java.lang.String.format; -import static org.citrusframework.validation.xml.schema.ValidationStrategy.FAIL; - -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.util.ArrayList; -import java.util.List; -import java.util.Optional; -import javax.xml.transform.TransformerException; -import javax.xml.transform.TransformerFactory; -import javax.xml.transform.dom.DOMSource; -import javax.xml.transform.stream.StreamResult; import org.citrusframework.CitrusSettings; import org.citrusframework.XmlValidationHelper; import org.citrusframework.context.TestContext; @@ -55,6 +43,19 @@ import org.w3c.dom.Document; import org.xml.sax.SAXParseException; +import javax.xml.transform.TransformerException; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.stream.StreamResult; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; + +import static java.lang.String.format; +import static org.citrusframework.validation.xml.schema.ValidationStrategy.FAIL; + public class XmlSchemaValidation implements SchemaValidator { public static final String NO_SCHEMA_FOUND_STRATEGY_PROPERTY_NAME = "citrus.xml.no.schema.found.strategy"; @@ -243,7 +244,6 @@ private static boolean isXmlSchemaValidationEnabled() { @Override public void validate(Message message, TestContext context, String schemaRepository, String schema) { - XmlMessageValidationContext validationContext = Builder.xml() .schemaValidation(true) .schema(schema) diff --git a/validation/citrus-validation-xml/src/main/java/org/citrusframework/xml/XsdSchemaRepository.java b/validation/citrus-validation-xml/src/main/java/org/citrusframework/xml/XsdSchemaRepository.java index dcc95220b0..097c010d88 100644 --- a/validation/citrus-validation-xml/src/main/java/org/citrusframework/xml/XsdSchemaRepository.java +++ b/validation/citrus-validation-xml/src/main/java/org/citrusframework/xml/XsdSchemaRepository.java @@ -95,6 +95,7 @@ protected void addCitrusSchema(String schemaName) throws SAXException, ParserCon } } + @Override protected void addRepository(Resource resource) { if (resource.getLocation().endsWith(".xsd")) { addXsdSchema(resource);