Validation tools of the Oaf objects #186

Open
claudio.atzori wants to merge 5 commits from validation into beta
6 changed files with 187 additions and 1 deletions

View File

@ -166,6 +166,21 @@
<groupId>com.opencsv</groupId>
<artifactId>opencsv</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
</dependency>
<dependency>
<groupId>javax.el</groupId>
<artifactId>javax.el-api</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.el</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,35 @@
package eu.dnetlib.dhp.schema.oaf.utils;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
public class ConstraintViolation implements Serializable {
private Map<String, String> properties = new HashMap<>();
public ConstraintViolation() {}
/*
@JsonCreator
public ConstraintViolation(
@JsonProperty("path") final String path,
@JsonProperty("message") final String message) {
this.properties.put(path, message);
}
*/
@JsonAnyGetter
public Map<String, String> getProperties() {
return properties;
}
@JsonAnySetter
public void add(String key, String value) {
properties.put(key, value);
}
}

View File

@ -0,0 +1,32 @@
package eu.dnetlib.dhp.schema.oaf.utils;
import org.hibernate.validator.messageinterpolation.ParameterMessageInterpolator;
import javax.validation.Validation;
import javax.validation.Validator;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;
public class OafValidator {
private static final Validator VALIDATOR = Validation
.byDefaultProvider()
.configure()
.messageInterpolator(new ParameterMessageInterpolator())
.buildValidatorFactory()
.getValidator();
public static <T> Set<ConstraintViolation> validate(final T oaf) {
return VALIDATOR.validate(oaf)
.stream()
.map(v -> {
final String path = v.getRootBeanClass().getSimpleName() + "." + v.getPropertyPath().toString();
ConstraintViolation cv = new ConstraintViolation();
cv.add(path, v.getMessage());
return cv;
})
.collect(Collectors.toCollection(HashSet::new));
}
}

View File

@ -0,0 +1,46 @@
package eu.dnetlib.dhp.schema.oaf.utils;
import java.io.IOException;
import java.util.Set;
import com.fasterxml.jackson.core.type.TypeReference;
import org.apache.commons.io.IOUtils;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import com.fasterxml.jackson.databind.ObjectMapper;
import eu.dnetlib.dhp.schema.oaf.Publication;
import static org.junit.jupiter.api.Assertions.*;
public class OafValidatorTest {
@Test
void test_validate() throws IOException {
ObjectMapper mapper = new ObjectMapper();
String json = IOUtils.toString(getClass().getResourceAsStream("publication_3.json"));
Publication p = mapper.readValue(json, Publication.class);
Set<ConstraintViolation> res = OafValidator.validate(p);
assertNotNull(res);
assertFalse(res.isEmpty());
res.forEach(v -> {
System.out.println(v.getProperties());
});
String reportJson = mapper.writeValueAsString(res);
assertNotNull(reportJson);
Set<ConstraintViolation> report = mapper.readValue(reportJson, new TypeReference<Set<ConstraintViolation>>() { });
assertNotNull(report);
System.out.println(mapper.writeValueAsString(report));
}
}

View File

@ -0,0 +1,41 @@
{
"id": "50|DansKnawCris::0829b5191605bdbea36d6502b8c1ce1f",
"resulttype": {
"classid": "publication"
},
"pid": [
{
"qualifier": {
"classid": "doi"
},
"value": "10.1016/j.cmet.2011.03.013"
},
{
"qualifier": {
"classid": "urn"
},
"value": "urn:nbn:nl:ui:29-f3ed5f9e-edf6-457e-8848-61b58a4075e2"
},
{
"qualifier": {
"classid": "scp-number"
},
"value": "79953761260"
},
{
"qualifier": {
"classid": "pmc"
},
"value": "21459329"
}
],
"collectedfrom": [
{
"key": "",
"value": ""
}
],
"instance": [
{}
]
}

19
pom.xml
View File

@ -531,6 +531,23 @@
<version>4.8.71</version>
</dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.0.13.Final</version>
</dependency>
<dependency>
<groupId>javax.el</groupId>
<artifactId>javax.el-api</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.el</artifactId>
<version>3.0.0</version>
</dependency>
</dependencies>
</dependencyManagement>
@ -801,7 +818,7 @@
<mockito-core.version>3.3.3</mockito-core.version>
<mongodb.driver.version>3.4.2</mongodb.driver.version>
<vtd.version>[2.12,3.0)</vtd.version>
<dhp-schemas.version>[2.10.32]</dhp-schemas.version>
<dhp-schemas.version>[2.10.26-SNAPSHOT]</dhp-schemas.version>
<dnet-actionmanager-api.version>[4.0.3]</dnet-actionmanager-api.version>
<dnet-actionmanager-common.version>[6.0.5]</dnet-actionmanager-common.version>
<dnet-openaire-broker-common.version>[3.1.6]</dnet-openaire-broker-common.version>