diff --git a/.prettierrc b/.prettierrc
new file mode 100644
index 0000000..aa85a8f
--- /dev/null
+++ b/.prettierrc
@@ -0,0 +1,2 @@
+printWidth: 140
+tabWidth: 4
diff --git a/pom.xml b/pom.xml
index b48d8db..9126ecc 100644
--- a/pom.xml
+++ b/pom.xml
@@ -177,6 +177,23 @@
+
+ com.hubspot.maven.plugins
+ prettier-maven-plugin
+ 0.17
+
+ 12.13.0
+ 1.6.1
+
+
+
+ validate
+
+ check
+
+
+
+
diff --git a/src/main/java/io/gravitee/policy/xmlvalidation/XmlValidationPolicy.java b/src/main/java/io/gravitee/policy/xmlvalidation/XmlValidationPolicy.java
index 089deb1..8d2cf66 100644
--- a/src/main/java/io/gravitee/policy/xmlvalidation/XmlValidationPolicy.java
+++ b/src/main/java/io/gravitee/policy/xmlvalidation/XmlValidationPolicy.java
@@ -28,26 +28,24 @@
import io.gravitee.policy.api.PolicyResult;
import io.gravitee.policy.api.annotations.OnRequestContent;
import io.gravitee.policy.xmlvalidation.configuration.XmlValidationPolicyConfiguration;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.xml.sax.SAXException;
-
+import java.io.IOException;
+import java.io.StringReader;
import javax.xml.XMLConstants;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
-import java.io.IOException;
-import java.io.StringReader;
-
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.xml.sax.SAXException;
public class XmlValidationPolicy {
- private final static Logger logger = LoggerFactory.getLogger(XmlValidationPolicy.class);
+ private static final Logger logger = LoggerFactory.getLogger(XmlValidationPolicy.class);
- private final static String BAD_REQUEST = "Bad Request";
- private final static String INTERNAL_ERROR = "Internal Error";
+ private static final String BAD_REQUEST = "Bad Request";
+ private static final String INTERNAL_ERROR = "Internal Error";
private XmlValidationPolicyConfiguration configuration;
@@ -56,10 +54,14 @@ public XmlValidationPolicy(XmlValidationPolicyConfiguration jsonSchemaValidatorP
}
@OnRequestContent
- public ReadWriteStream onRequestContent(Request request, Response response, ExecutionContext executionContext, PolicyChain policyChain) {
+ public ReadWriteStream onRequestContent(
+ Request request,
+ Response response,
+ ExecutionContext executionContext,
+ PolicyChain policyChain
+ ) {
logger.debug("Execute XML validation policy on request {}", request.id());
return new BufferedReadWriteStream() {
-
Buffer buffer = Buffer.buffer();
@Override
diff --git a/src/main/java/io/gravitee/policy/xmlvalidation/swagger/XmlValidationOAIOperationVisitor.java b/src/main/java/io/gravitee/policy/xmlvalidation/swagger/XmlValidationOAIOperationVisitor.java
index eaf9ad8..1d68617 100644
--- a/src/main/java/io/gravitee/policy/xmlvalidation/swagger/XmlValidationOAIOperationVisitor.java
+++ b/src/main/java/io/gravitee/policy/xmlvalidation/swagger/XmlValidationOAIOperationVisitor.java
@@ -29,6 +29,10 @@
import io.swagger.v3.core.util.Json;
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.parameters.RequestBody;
+import java.io.IOException;
+import java.io.Reader;
+import java.io.StringReader;
+import java.util.Optional;
import org.apache.commons.lang3.StringUtils;
import org.w3c.dom.DOMConfiguration;
import org.w3c.dom.Document;
@@ -36,11 +40,6 @@
import org.w3c.dom.ls.DOMImplementationLS;
import org.w3c.dom.ls.LSSerializer;
-import java.io.IOException;
-import java.io.Reader;
-import java.io.StringReader;
-import java.util.Optional;
-
/**
* @author Florent CHAMFROY (florent.chamfroy at graviteesource.com)
* @author GraviteeSource Team
@@ -50,6 +49,7 @@ public class XmlValidationOAIOperationVisitor implements OAIOperationVisitor {
public static final String XSD_TARGET_NAMESPACE = "urn:io:gravitee:policy:xml-validation";
private final ObjectMapper mapper = new ObjectMapper();
+
{
mapper.configure(JsonGenerator.Feature.WRITE_NUMBERS_AS_STRINGS, true);
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
@@ -85,16 +85,16 @@ public Optional visit(io.swagger.v3.oas.models.OpenAPI openAPI, io.swagg
}
private String convert(String jsonSchema) throws IOException, InstantiationException, IllegalAccessException, ClassNotFoundException {
- try(Reader jsonReader = new StringReader(jsonSchema)) {
+ try (Reader jsonReader = new StringReader(jsonSchema)) {
final Config cfg = new Config.Builder()
- .name("rootType")
- .targetNamespace(XSD_TARGET_NAMESPACE)
- .createRootElement(true)
- .nsAlias("ns")
- .rootElement("root")
- .validateXsdSchema(false)
- .customTypeMapping(JsonSimpleType.INTEGER, "int64", XsdSimpleType.LONG)
- .build();
+ .name("rootType")
+ .targetNamespace(XSD_TARGET_NAMESPACE)
+ .createRootElement(true)
+ .nsAlias("ns")
+ .rootElement("root")
+ .validateXsdSchema(false)
+ .customTypeMapping(JsonSimpleType.INTEGER, "int64", XsdSimpleType.LONG)
+ .build();
final Document xmlSchemaDoc = Jsons2Xsd.convert(jsonReader, cfg);
DOMImplementationLS impl = (DOMImplementationLS) DOMImplementationRegistry.newInstance().getDOMImplementation("LS");
diff --git a/src/main/java/io/gravitee/policy/xmlvalidation/swagger/XmlValidationSwaggerOperationVisitor.java b/src/main/java/io/gravitee/policy/xmlvalidation/swagger/XmlValidationSwaggerOperationVisitor.java
index 231c74c..b4485fa 100644
--- a/src/main/java/io/gravitee/policy/xmlvalidation/swagger/XmlValidationSwaggerOperationVisitor.java
+++ b/src/main/java/io/gravitee/policy/xmlvalidation/swagger/XmlValidationSwaggerOperationVisitor.java
@@ -19,7 +19,6 @@
import io.gravitee.policy.api.swagger.v2.SwaggerOperationVisitor;
import io.swagger.models.Operation;
import io.swagger.models.Swagger;
-
import java.util.Optional;
/**
@@ -27,6 +26,7 @@
* @author GraviteeSource Team
*/
public class XmlValidationSwaggerOperationVisitor implements SwaggerOperationVisitor {
+
@Override
public Optional visit(Swagger swagger, Operation o) {
return Optional.empty();
diff --git a/src/test/java/io/gravitee/policy/xmlvalidation/XmlValidationTest.java b/src/test/java/io/gravitee/policy/xmlvalidation/XmlValidationTest.java
index 0f156f1..4f395e8 100644
--- a/src/test/java/io/gravitee/policy/xmlvalidation/XmlValidationTest.java
+++ b/src/test/java/io/gravitee/policy/xmlvalidation/XmlValidationTest.java
@@ -15,6 +15,9 @@
*/
package io.gravitee.policy.xmlvalidation;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.*;
+
import io.gravitee.common.http.HttpStatusCode;
import io.gravitee.common.util.ServiceLoaderHelper;
import io.gravitee.el.TemplateEngine;
@@ -36,9 +39,6 @@
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
-import static org.assertj.core.api.Assertions.assertThat;
-import static org.mockito.Mockito.*;
-
@RunWith(MockitoJUnitRunner.class)
public class XmlValidationTest {
@@ -59,55 +59,60 @@ public class XmlValidationTest {
private BufferFactory factory = ServiceLoaderHelper.loadFactory(BufferFactory.class);
- private String xsdSchema = "\n" +
- "\n" +
- " \n" +
- " \n" +
- "\n" +
- " \n" +
- " \n" +
- " \n" +
- " \n" +
- " \n" +
- "\n" +
- " \n" +
- " \n" +
- " \n" +
- " \n" +
- " \n" +
- "\n" +
- " \n" +
- " \n" +
- " \n" +
- " \n" +
- " \n" +
- " \n" +
- " \n" +
- " \n" +
- "";
-
- private Buffer validXmlContent = factory.buffer("\n" +
- "\n" +
- " \n" +
- " \n" +
- " Foo Inc\n" +
- " 752\n" +
- " 10451541505\n" +
- " John Doo\n" +
- " \n" +
- " \n" +
- "");
-
- private Buffer invalidXmContent = factory.buffer("\n" +
- "\n" +
- " \n" +
- " Foo Inc\n" +
- " 752\n" +
- " 10451541505\n" +
- " John Doo\n" +
- " \n" +
- "");
+ private String xsdSchema =
+ "\n" +
+ "\n" +
+ " \n" +
+ " \n" +
+ "\n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ "\n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ "\n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ "";
+
+ private Buffer validXmlContent = factory.buffer(
+ "\n" +
+ "\n" +
+ " \n" +
+ " \n" +
+ " Foo Inc\n" +
+ " 752\n" +
+ " 10451541505\n" +
+ " John Doo\n" +
+ " \n" +
+ " \n" +
+ ""
+ );
+
+ private Buffer invalidXmContent = factory.buffer(
+ "\n" +
+ "\n" +
+ " \n" +
+ " Foo Inc\n" +
+ " 752\n" +
+ " 10451541505\n" +
+ " John Doo\n" +
+ " \n" +
+ ""
+ );
private Metrics metrics;
@@ -117,11 +122,14 @@ public class XmlValidationTest {
public void beforeAll() {
metrics = Metrics.on(System.currentTimeMillis()).build();
- when(configuration.getErrorMessage()).thenReturn("\n" +
+ when(configuration.getErrorMessage())
+ .thenReturn(
+ "\n" +
" \n" +
" validation/internal\n" +
" Internal error occurred. Please retry...\n" +
- " ");
+ " "
+ );
when(configuration.getXsdSchema()).thenReturn(xsdSchema);
when(mockRequest.metrics()).thenReturn(metrics);
when(mockExecutionContext.getTemplateEngine()).thenReturn(TemplateEngine.templateEngine());
diff --git a/src/test/java/io/gravitee/policy/xmlvalidation/swagger/XmlValidationOAIOperationVisitorTest.java b/src/test/java/io/gravitee/policy/xmlvalidation/swagger/XmlValidationOAIOperationVisitorTest.java
index b2d3760..49812a6 100644
--- a/src/test/java/io/gravitee/policy/xmlvalidation/swagger/XmlValidationOAIOperationVisitorTest.java
+++ b/src/test/java/io/gravitee/policy/xmlvalidation/swagger/XmlValidationOAIOperationVisitorTest.java
@@ -15,6 +15,11 @@
*/
package io.gravitee.policy.xmlvalidation.swagger;
+import static org.junit.Assert.*;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
import com.fasterxml.jackson.databind.ObjectMapper;
import io.gravitee.policy.api.swagger.Policy;
import io.swagger.v3.core.util.Json;
@@ -24,20 +29,14 @@
import io.swagger.v3.oas.models.media.MediaType;
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.parameters.RequestBody;
+import java.util.HashMap;
+import java.util.Optional;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
-import java.util.HashMap;
-import java.util.Optional;
-
-import static org.junit.Assert.*;
-import static org.mockito.ArgumentMatchers.any;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
-
/**
* @author Florent CHAMFROY (florent.chamfroy at graviteesource.com)
* @author GraviteeSource Team
@@ -81,7 +80,7 @@ public void operationWithEmptyRequestBody() {
when(requestBody.getContent()).thenReturn(content);
when(content.get("application/xml")).thenReturn(applicationXml);
- try(MockedStatic theMock = Mockito.mockStatic(Json.class)) {
+ try (MockedStatic theMock = Mockito.mockStatic(Json.class)) {
theMock.when(() -> Json.pretty(any(Schema.class))).thenReturn("");
Optional policy = visitor.visit(mock(OpenAPI.class), operationMock);
assertFalse(policy.isPresent());
@@ -90,49 +89,55 @@ public void operationWithEmptyRequestBody() {
@Test
public void operationWithJsonRequestBody() throws Exception {
- final String jsonSchema = "{\n" +
- " \"title\": \"Person\",\n" +
- " \"type\": \"object\",\n" +
- " \"properties\": {\n" +
- " \"firstName\": {\n" +
- " \"type\": \"string\",\n" +
- " \"description\": \"The person's first name.\"\n" +
- " },\n" +
- " \"lastName\": {\n" +
- " \"type\": \"string\",\n" +
- " \"description\": \"The person's last name.\"\n" +
- " },\n" +
- " \"age\": {\n" +
- " \"description\": \"Age in years which must be equal to or greater than zero.\",\n" +
- " \"type\": \"integer\",\n" +
- " \"format\": \"int64\",\n" +
- " \"minimum\": 0\n" +
- " }\n" +
- " }\n" +
- "}";
-
- final String expectedXsdSchema = "\n" +
- " \n" +
- " \n" +
- " \n" +
- " \n" +
- " \n" +
- " The person's first name.\n" +
- " \n" +
- " \n" +
- " \n" +
- " \n" +
- " The person's last name.\n" +
- " \n" +
- " \n" +
- " \n" +
- " \n" +
- " Age in years which must be equal to or greater than zero.\n" +
- " \n" +
- " \n" +
- " \n" +
- " \n" +
- "\n";
+ final String jsonSchema =
+ "{\n" +
+ " \"title\": \"Person\",\n" +
+ " \"type\": \"object\",\n" +
+ " \"properties\": {\n" +
+ " \"firstName\": {\n" +
+ " \"type\": \"string\",\n" +
+ " \"description\": \"The person's first name.\"\n" +
+ " },\n" +
+ " \"lastName\": {\n" +
+ " \"type\": \"string\",\n" +
+ " \"description\": \"The person's last name.\"\n" +
+ " },\n" +
+ " \"age\": {\n" +
+ " \"description\": \"Age in years which must be equal to or greater than zero.\",\n" +
+ " \"type\": \"integer\",\n" +
+ " \"format\": \"int64\",\n" +
+ " \"minimum\": 0\n" +
+ " }\n" +
+ " }\n" +
+ "}";
+
+ final String expectedXsdSchema =
+ "\n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " The person's first name.\n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " The person's last name.\n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " Age in years which must be equal to or greater than zero.\n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ "\n";
Operation operationMock = mock(Operation.class);
@@ -145,7 +150,7 @@ public void operationWithJsonRequestBody() throws Exception {
when(content.get("application/xml")).thenReturn(applicationXml);
when(applicationXml.getSchema()).thenReturn(schema);
- try(MockedStatic theMock = Mockito.mockStatic(Json.class)) {
+ try (MockedStatic theMock = Mockito.mockStatic(Json.class)) {
theMock.when(() -> Json.pretty(any(Schema.class))).thenReturn(jsonSchema);
Optional policy = visitor.visit(mock(OpenAPI.class), operationMock);