-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #47 from com-pas/validation-exception-handling
Add validation constraint and common exception handling.
- Loading branch information
Showing
46 changed files
with
809 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
commons/src/main/java/org/lfenergy/compas/core/commons/constraint/XmlAnyElementValid.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
43 changes: 43 additions & 0 deletions
43
...va/org/lfenergy/compas/core/commons/constraint/impl/XmlAnyElementConstraintValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 0 additions & 14 deletions
14
...s/src/main/java/org/lfenergy/compas/core/commons/exception/CompasValidationException.java
This file was deleted.
Oops, something went wrong.
87 changes: 87 additions & 0 deletions
87
...rg/lfenergy/compas/core/commons/constraint/impl/XmlAnyElementConstraintValidatorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
31 changes: 0 additions & 31 deletions
31
...c/test/java/org/lfenergy/compas/core/commons/exception/CompasValidationExceptionTest.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.