Skip to content

Commit

Permalink
chore(citrus-validation): review and code cleanup
Browse files Browse the repository at this point in the history
pr: #1224

`citrus-validation` module and sub-modules.
  • Loading branch information
bbortt committed Oct 28, 2024
1 parent 6454189 commit 8088de4
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<SimpleJsonSchema> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Object> overwriteElements = new HashMap<>();
overwriteElements.put("$.TestRequest.Message", "Hello World!");
Expand Down Expand Up @@ -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);
Expand All @@ -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),
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<XmlMessageValidationContext> {

public static final String NO_SCHEMA_FOUND_STRATEGY_PROPERTY_NAME = "citrus.xml.no.schema.found.strategy";
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 8088de4

Please sign in to comment.