Skip to content

Commit

Permalink
style: setup & apply prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
gcusnieux authored and ThibaudAV committed Jan 21, 2022
1 parent 96585cd commit 7c3ea81
Show file tree
Hide file tree
Showing 7 changed files with 168 additions and 134 deletions.
2 changes: 2 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
printWidth: 140
tabWidth: 4
17 changes: 17 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,23 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.hubspot.maven.plugins</groupId>
<artifactId>prettier-maven-plugin</artifactId>
<version>0.17</version>
<configuration>
<nodeVersion>12.13.0</nodeVersion>
<prettierJavaVersion>1.6.1</prettierJavaVersion>
</configuration>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,17 @@
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;
import org.w3c.dom.bootstrap.DOMImplementationRegistry;
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
Expand All @@ -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);
Expand Down Expand Up @@ -85,16 +85,16 @@ public Optional<Policy> 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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@
import io.gravitee.policy.api.swagger.v2.SwaggerOperationVisitor;
import io.swagger.models.Operation;
import io.swagger.models.Swagger;

import java.util.Optional;

/**
* @author Florent CHAMFROY (florent.chamfroy at graviteesource.com)
* @author GraviteeSource Team
*/
public class XmlValidationSwaggerOperationVisitor implements SwaggerOperationVisitor {

@Override
public Optional<Policy> visit(Swagger swagger, Operation o) {
return Optional.empty();
Expand Down
116 changes: 62 additions & 54 deletions src/test/java/io/gravitee/policy/xmlvalidation/XmlValidationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {

Expand All @@ -59,55 +59,60 @@ public class XmlValidationTest {

private BufferFactory factory = ServiceLoaderHelper.loadFactory(BufferFactory.class);

private String xsdSchema = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" attributeFormDefault=\"unqualified\"\n" +
" elementFormDefault=\"qualified\">\n" +
" <xs:element name=\"root\" type=\"rootType\">\n" +
" </xs:element>\n" +
"\n" +
" <xs:complexType name=\"rootType\">\n" +
" <xs:sequence>\n" +
" <xs:element name=\"companies\" type=\"companiesType\"/>\n" +
" </xs:sequence>\n" +
" </xs:complexType>\n" +
"\n" +
" <xs:complexType name=\"companiesType\">\n" +
" <xs:sequence>\n" +
" <xs:element name=\"company\" type=\"companyType\" maxOccurs=\"unbounded\" minOccurs=\"0\"/>\n" +
" </xs:sequence>\n" +
" </xs:complexType>\n" +
"\n" +
" <xs:complexType name=\"companyType\">\n" +
" <xs:sequence>\n" +
" <xs:element type=\"xs:string\" name=\"name\"/>\n" +
" <xs:element type=\"xs:integer\" name=\"employeeNumber\"/>\n" +
" <xs:element type=\"xs:long\" name=\"sales\"/>\n" +
" <xs:element type=\"xs:string\" name=\"CEO\"/>\n" +
" </xs:sequence>\n" +
" </xs:complexType>\n" +
"</xs:schema>";

private Buffer validXmlContent = factory.buffer("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<root>\n" +
" <companies>\n" +
" <company>\n" +
" <name>Foo Inc</name>\n" +
" <employeeNumber>752</employeeNumber>\n" +
" <sales>10451541505</sales>\n" +
" <CEO>John Doo</CEO>\n" +
" </company>\n" +
" </companies>\n" +
"</root>");

private Buffer invalidXmContent = factory.buffer("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<root>\n" +
" <company>\n" +
" <name>Foo Inc</name>\n" +
" <employeeNumber>752</employeeNumber>\n" +
" <sales>10451541505</sales>\n" +
" <CEO>John Doo</CEO>\n" +
" </company>\n" +
"</root>");
private String xsdSchema =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" attributeFormDefault=\"unqualified\"\n" +
" elementFormDefault=\"qualified\">\n" +
" <xs:element name=\"root\" type=\"rootType\">\n" +
" </xs:element>\n" +
"\n" +
" <xs:complexType name=\"rootType\">\n" +
" <xs:sequence>\n" +
" <xs:element name=\"companies\" type=\"companiesType\"/>\n" +
" </xs:sequence>\n" +
" </xs:complexType>\n" +
"\n" +
" <xs:complexType name=\"companiesType\">\n" +
" <xs:sequence>\n" +
" <xs:element name=\"company\" type=\"companyType\" maxOccurs=\"unbounded\" minOccurs=\"0\"/>\n" +
" </xs:sequence>\n" +
" </xs:complexType>\n" +
"\n" +
" <xs:complexType name=\"companyType\">\n" +
" <xs:sequence>\n" +
" <xs:element type=\"xs:string\" name=\"name\"/>\n" +
" <xs:element type=\"xs:integer\" name=\"employeeNumber\"/>\n" +
" <xs:element type=\"xs:long\" name=\"sales\"/>\n" +
" <xs:element type=\"xs:string\" name=\"CEO\"/>\n" +
" </xs:sequence>\n" +
" </xs:complexType>\n" +
"</xs:schema>";

private Buffer validXmlContent = factory.buffer(
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<root>\n" +
" <companies>\n" +
" <company>\n" +
" <name>Foo Inc</name>\n" +
" <employeeNumber>752</employeeNumber>\n" +
" <sales>10451541505</sales>\n" +
" <CEO>John Doo</CEO>\n" +
" </company>\n" +
" </companies>\n" +
"</root>"
);

private Buffer invalidXmContent = factory.buffer(
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<root>\n" +
" <company>\n" +
" <name>Foo Inc</name>\n" +
" <employeeNumber>752</employeeNumber>\n" +
" <sales>10451541505</sales>\n" +
" <CEO>John Doo</CEO>\n" +
" </company>\n" +
"</root>"
);

private Metrics metrics;

Expand All @@ -117,11 +122,14 @@ public class XmlValidationTest {
public void beforeAll() {
metrics = Metrics.on(System.currentTimeMillis()).build();

when(configuration.getErrorMessage()).thenReturn("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
when(configuration.getErrorMessage())
.thenReturn(
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
" <error>\n" +
" <reason>validation/internal</reason>\n" +
" <internalReason>Internal error occurred. Please retry...</internalReason>\n" +
" </error>");
" </error>"
);
when(configuration.getXsdSchema()).thenReturn(xsdSchema);
when(mockRequest.metrics()).thenReturn(metrics);
when(mockExecutionContext.getTemplateEngine()).thenReturn(TemplateEngine.templateEngine());
Expand Down
Loading

0 comments on commit 7c3ea81

Please sign in to comment.