Skip to content

Commit

Permalink
Merge pull request #47 from com-pas/validation-exception-handling
Browse files Browse the repository at this point in the history
Add validation constraint and common exception handling.
  • Loading branch information
Flurb authored Aug 4, 2021
2 parents e1cc0b4 + 80418d5 commit 2becc0b
Show file tree
Hide file tree
Showing 46 changed files with 809 additions and 69 deletions.
15 changes: 15 additions & 0 deletions commons/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ SPDX-License-Identifier: Apache-2.0
<packaging>jar</packaging>

<dependencies>
<dependency>
<groupId>jakarta.validation</groupId>
<artifactId>jakarta.validation-api</artifactId>
</dependency>

<dependency>
<groupId>jakarta.xml.bind</groupId>
<artifactId>jakarta.xml.bind-api</artifactId>
Expand Down Expand Up @@ -57,5 +62,15 @@ SPDX-License-Identifier: Apache-2.0
<artifactId>slf4j-simple</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>jakarta.el</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// SPDX-FileCopyrightText: 2021 Alliander N.V.
//
// SPDX-License-Identifier: Apache-2.0
package org.lfenergy.compas.core.commons.constraint;

import org.lfenergy.compas.core.commons.constraint.impl.XmlAnyElementConstraintValidator;

import javax.validation.Constraint;
import javax.validation.Payload;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

/**
* Annotation to check if a List of XML Elements (mostly annotated with XmlAnyElement) contains a specific number
* of elements and also the expected Element (Name) with the correct namespace.
*/
@Target({FIELD, METHOD, PARAMETER, ANNOTATION_TYPE, TYPE_USE})
@Retention(RUNTIME)
@Constraint(validatedBy = {XmlAnyElementConstraintValidator.class})
@Documented
public @interface XmlAnyElementValid {
String message() default "{org.lfenergy.compas.XmlAnyElementValid.unexpected.message}";

Class<?>[] groups() default {};

Class<? extends Payload>[] payload() default {};

String elementName();

String elementNamespace();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// SPDX-FileCopyrightText: 2021 Alliander N.V.
//
// SPDX-License-Identifier: Apache-2.0
package org.lfenergy.compas.core.commons.constraint.impl;

import org.lfenergy.compas.core.commons.constraint.XmlAnyElementValid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Element;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.List;

/**
* Validator to execute the check on fields annotated with {@link XmlAnyElementValid} to check if the number of
* element are correct and also if the element(s) in the list have the correct Local Name and the correct Namespace.
*/
public class XmlAnyElementConstraintValidator implements ConstraintValidator<XmlAnyElementValid, List<Element>> {
private static final Logger LOGGER = LoggerFactory.getLogger(XmlAnyElementConstraintValidator.class);

private String elementName;
private String elementNamespace;

@Override
public void initialize(XmlAnyElementValid constraintAnnotation) {
LOGGER.debug("Initializing XmlAnyElementValid constraint for List of Elements");
this.elementName = constraintAnnotation.elementName();
this.elementNamespace = constraintAnnotation.elementNamespace();
}

@Override
public boolean isValid(List<Element> elements, ConstraintValidatorContext context) {
// Check if there are elements in the List that don't match the name or namespace.
var numberOfIncorrectElements =
elements.stream()
.filter(element ->
!elementName.equals(element.getLocalName())
|| !elementNamespace.equals(element.getNamespaceURI()))
.count();
return numberOfIncorrectElements == 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,8 @@ public class CompasErrorCode {

public static final String CONVERT_TO_STRING_ERROR = "CORE-1000";
public static final String CONVERT_TO_ELEMENT_ERROR = "CORE-1001";

public static final String VALIDATION_ERROR = "CORE-8000";

public static final String UNKNOWN_EXCEPTION_ERROR = "CORE-9999";
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// SPDX-FileCopyrightText: 2021 Alliander N.V.
//
// SPDX-License-Identifier: Apache-2.0
package org.lfenergy.compas.core.commons.constraint.impl;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.lfenergy.compas.core.commons.constraint.XmlAnyElementValid;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import javax.validation.Validation;
import javax.validation.Validator;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.util.ArrayList;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

class XmlAnyElementConstraintValidatorTest {
private static final String ELEMENT_NAME = "valid";
private static final String ELEMENT_NS = "https://valid.org";

private Document document;
private Validator validator;

@BeforeEach
void setupValidator() throws ParserConfigurationException {
var documentFactory = DocumentBuilderFactory.newInstance();
var documentBuilder = documentFactory.newDocumentBuilder();
document = documentBuilder.newDocument();

var factory = Validation.buildDefaultValidatorFactory();
validator = factory.getValidator();
}

@Test
void isValid_WhenCalledWithCorrectElement_ThenNoViolations() {
var simplePojo = new SimplePojo();
simplePojo.getElements().add(document.createElementNS(ELEMENT_NS, ELEMENT_NAME));

var violations = validator.validate(simplePojo);
assertTrue(violations.isEmpty());
}

@Test
void isValid_WhenCalledWithIncorrectElement_ThenViolationFound() {
var simplePojo = new SimplePojo();
simplePojo.getElements().add(document.createElementNS("https://OtherNS.org", "Other"));

var violations = validator.validate(simplePojo);
assertEquals(1, violations.size());
}

@Test
void isValid_WhenCalledWithMultipleElementAndOneIncorrect_ThenViolationFound() {
var simplePojo = new SimplePojo();
simplePojo.getElements().add(document.createElementNS("https://OtherNS.org", "Other"));
simplePojo.getElements().add(document.createElementNS(ELEMENT_NS, ELEMENT_NAME));
simplePojo.getElements().add(document.createElementNS(ELEMENT_NS, ELEMENT_NAME));

var violations = validator.validate(simplePojo);
assertEquals(1, violations.size());
}

@Test
void isValid_WhenCalledWithMultipleElementAndOneIncorrectNS_ThenViolationFound() {
var simplePojo = new SimplePojo();
simplePojo.getElements().add(document.createElementNS("https://OtherNS.org", ELEMENT_NAME));
simplePojo.getElements().add(document.createElementNS(ELEMENT_NS, ELEMENT_NAME));
simplePojo.getElements().add(document.createElementNS(ELEMENT_NS, ELEMENT_NAME));

var violations = validator.validate(simplePojo);
assertEquals(1, violations.size());
}

private static final class SimplePojo {
@XmlAnyElementValid(elementName = ELEMENT_NAME, elementNamespace = ELEMENT_NS)
private List<Element> elements = new ArrayList<>();

public List<Element> getElements() {
return elements;
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ compas:
scl:
schemas:
- xsdPath: "xsd/xml-element.xsd"
namespace: "https://www.lfenergy.org/compas/v1"
namespace: "https://www.lfenergy.org/compas/extension/v1"
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ compas:
scl:
schemas:
- xsdPath: "xsd/xml-element.xsd"
namespace: "https://www.lfenergy.org/compas/v1"
namespace: "https://www.lfenergy.org/compas/extension/v1"
contextPath: "org.lfenergy.compas.scl.extensions.model.unknown"
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ compas:
scl:
schemas:
- xsdPath: "invalid/xml-element.xsd"
namespace: "https://www.lfenergy.org/compas/v1"
namespace: "https://www.lfenergy.org/compas/extension/v1"
contextPath: "org.lfenergy.compas.scl.extensions.model"
59 changes: 59 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ SPDX-License-Identifier: Apache-2.0
<jackson.version>2.12.4</jackson.version>
<jaxb.bind.version>2.3.3</jaxb.bind.version>
<junit.jupiter.version>5.7.2</junit.jupiter.version>
<mockito-junit-jupiter.version>3.11.2</mockito-junit-jupiter.version>
<openpojo.version>0.9.1</openpojo.version>
</properties>

<distributionManagement>
Expand All @@ -44,6 +46,7 @@ SPDX-License-Identifier: Apache-2.0

<modules>
<module>commons</module>
<module>rest-commons</module>
<module>scl-extension</module>
<module>scl2003</module>
<module>scl2007b</module>
Expand Down Expand Up @@ -79,6 +82,24 @@ SPDX-License-Identifier: Apache-2.0
<version>${jackson.version}</version>
</dependency>

<dependency>
<groupId>jakarta.validation</groupId>
<artifactId>jakarta.validation-api</artifactId>
<version>2.0.2</version>
</dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.2.0.Final</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>jakarta.el</artifactId>
<version>3.0.3</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>jakarta.xml.bind</groupId>
<artifactId>jakarta.xml.bind-api</artifactId>
Expand All @@ -90,6 +111,18 @@ SPDX-License-Identifier: Apache-2.0
<version>${jaxb.bind.version}</version>
</dependency>

<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.1</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-core</artifactId>
<version>4.6.1.Final</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
Expand All @@ -113,6 +146,18 @@ SPDX-License-Identifier: Apache-2.0
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.openpojo</groupId>
<artifactId>openpojo</artifactId>
<version>${openpojo.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>${mockito-junit-jupiter.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>

Expand Down Expand Up @@ -169,6 +214,20 @@ SPDX-License-Identifier: Apache-2.0
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
</plugin>

<plugin>
<groupId>org.jboss.jandex</groupId>
<artifactId>jandex-maven-plugin</artifactId>
<version>1.1.0</version>
<executions>
<execution>
<id>make-index</id>
<goals>
<goal>jandex</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
Expand Down
Loading

0 comments on commit 2becc0b

Please sign in to comment.