Commit 6b5c668a authored by descamps's avatar descamps
Browse files

Merge branch 'fix/sprint4_150322' into 'develop'

Fixer les bugs de sprint 4

See merge request !71
parents e967ab48 08fd889a
......@@ -51,7 +51,7 @@ import java.util.stream.Stream;
@NoArgsConstructor
public class ElementProperties {
private String additionalProperties;
private boolean additionalProperties;
private String name;
private String type;
private String cardinality;
......@@ -60,6 +60,7 @@ public class ElementProperties {
private String dataType;
private String value;
private String documentation;
private String editName;
@JsonIgnore
private Object sedaData;
......
......@@ -39,7 +39,6 @@ package fr.gouv.vitamui.pastis.common.dto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import fr.gouv.vitamui.pastis.common.dto.factory.RngTagFactory;
import fr.gouv.vitamui.pastis.common.util.RNGConstants;
import lombok.Data;
import lombok.Getter;
......@@ -91,7 +90,19 @@ public class ElementRNG {
public static void setDocumentationForParentElement(ElementProperties parentNode, ElementRNG node) {
if (null != parentNode.getType() &&
RNGConstants.MetadaDataType.ELEMENT.getLabel().equals(parentNode.getType())) {
parentNode.setDocumentation(node.getValue());
if(parentNode.getName() != null && parentNode.getName().equals("ArchiveUnit")
&& node.getValue().contains("Commentaire : ")){
parentNode.setDocumentation(node.getValue().replace("Commentaire : ", ""));
if(parentNode.getDocumentation() != null ){
parentNode.setEditName(parentNode.getDocumentation());
}
}else{
if(parentNode.getDocumentation() != null ){
parentNode.setEditName(node.getValue());
}else{
parentNode.setDocumentation(node.getValue());
}
}
} else {
setDocumentationForParentElement(parentNode.getParent(), node);
}
......
......@@ -77,6 +77,8 @@ public class BaliseXML {
private static BaliseXML elementOrAttributeRNG;
private static AnnotationXML annotationXML;
private static DocumentationXML documentationXML;
private static AnnotationXML annotationCommentXML;
private static DocumentationXML documentationCommentXML;
public static BaliseXML getBaliseXMLStatic() {
return baliseXMLStatic;
......@@ -96,6 +98,8 @@ public class BaliseXML {
elementOrAttributeRNG = null;
annotationXML = null;
documentationXML = null;
annotationCommentXML = null;
documentationCommentXML = null;
setValueAndDataRNG(node);
// Set annotation and documentation tags (if exists)
......@@ -117,6 +121,8 @@ public class BaliseXML {
if (null != node.getValue() && !node.getValue().equals(UNDEFINED)) {
valueRNG = new ValueXML();
valueRNG.setValue(node.getValue());
} else if (node.getName().equals("Language")) {
dataRNG = new DataXML();
}
// When a value is declared in a profile element, the <rng:data> tag must be suppressed
......@@ -159,13 +165,9 @@ public class BaliseXML {
}
private static void setDocumentationAnnotationElementAttribute(ElementProperties node){
// Set annotation and documentation tags (if exists)
if (null != node.getDocumentation()) {
annotationXML = new AnnotationXML();
documentationXML = new DocumentationXML();
documentationXML.setDocumentation(node.getDocumentation());
annotationXML.setDocumentationXML(documentationXML);
}
setAnnotationDocumentationXML(node);
if (null != node.getType() && !node.getType().equals(UNDEFINED)) {
if (node.getType().equals("element")) {
elementOrAttributeRNG = new ElementXML();
......@@ -177,12 +179,38 @@ public class BaliseXML {
}
}
if (null != documentationXML && elementOrAttributeRNG != null) {
elementOrAttributeRNG.getChildren().add(annotationXML);
annotationXML.setParent(elementOrAttributeRNG);
if (elementOrAttributeRNG != null) {
if(null != documentationXML){
elementOrAttributeRNG.getChildren().add(annotationXML);
annotationXML.setParent(elementOrAttributeRNG);
}
if(annotationCommentXML != null){
elementOrAttributeRNG.getChildren().add(annotationCommentXML);
annotationCommentXML.setParent(elementOrAttributeRNG);
}
}
}
public static void setAnnotationDocumentationXML(ElementProperties node) {
// Set annotation and documentation tags (if exists)
if (null != node.getDocumentation()) {
annotationXML = new AnnotationXML();
documentationXML = new DocumentationXML();
documentationXML.setDocumentation(node.getDocumentation());
annotationXML.setDocumentationXML(documentationXML);
}
if(node.getName().equals("ArchiveUnit") && node.getEditName() != null){
annotationCommentXML = new AnnotationXML();
documentationCommentXML = new DocumentationXML();
documentationCommentXML.setDocumentation(node.getEditName());
annotationCommentXML.setDocumentationXML(documentationCommentXML);
if(node.getDocumentation() != null){
documentationXML.setDocumentation("Commentaire : " + node.getDocumentation());
annotationXML.setDocumentationXML(documentationXML);
}
}
}
/**
* Set Cardinality to element or attribute Rng
* @param node
......
......@@ -39,12 +39,18 @@ knowledge of the CeCILL-C license and that you accept its terms.
package fr.gouv.vitamui.pastis.common.dto.pua;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.module.afterburner.AfterburnerModule;
import lombok.Data;
import org.json.JSONObject;
import java.util.List;
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class PuaMetadataDetails {
String type;
......@@ -55,6 +61,14 @@ public class PuaMetadataDetails {
JSONObject properties;
List<String> required;
PuaMetadata items;
@JsonProperty("enum")
List<String> enums;
String pattern;
public String serialiseString() throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new AfterburnerModule());
return mapper.writeValueAsString(this);
}
}
......@@ -72,6 +72,7 @@ public class JsonFromPUA {
idCounter = 0L;
// Adding root element DescriptiveMetadata
ElementProperties root = new ElementProperties();
root.setAdditionalProperties(controlSchema.getBoolean("additionalProperties"));
root.setName("DescriptiveMetadata");
root.setId(idCounter++);
root.setLevel(0);
......
......@@ -70,7 +70,7 @@ public class PuaFromJSON {
// 2. Add type
controlSchema.put("type", TYPE);
// 3. Add additionProperties
controlSchema.put("additionalProperties", elementProperties.getAdditionalProperties());
controlSchema.put("additionalProperties", elementProperties.isAdditionalProperties());
// 4. Check if tree contains Management metadata
addPatternProperties(elementProperties, controlSchema);
List<ElementProperties> elementsForTree = puaPastisValidator.ignoreMetadata(elementProperties);
......
......@@ -124,16 +124,19 @@ public class PuaPastisValidator {
* @param pua The string containing the JSON file to be validated
* @throws AssertionError
*/
public void validatePUA(JSONObject pua) throws AssertionError {
public void validatePUA(JSONObject pua, boolean standalone) throws AssertionError {
JSONObject profileJson = getProfileJsonExpected();
// Compare list of field at the root level
Set<String> actualFieldList = pua.keySet().stream().collect(toSet());
Set<String> expectedFieldList = profileJson.keySet().stream().collect(Collectors.toSet());
if (!actualFieldList.equals(expectedFieldList)) {
throw new AssertionError("PUA field list does not contains the expected values");
if (!standalone) {
Set<String> actualFieldList = pua.keySet().stream().collect(toSet());
Set<String> expectedFieldList = profileJson.keySet().stream().collect(Collectors.toSet());
if (!actualFieldList.equals(expectedFieldList)) {
throw new AssertionError("PUA field list does not contains the expected values");
}
}
// Next tests are controlling the ControlSchema
String controlSchemaString = pua.getString(CONTROLSCHEMA);
JSONObject controlSchemaActual = new JSONObject(controlSchemaString);
......@@ -147,6 +150,11 @@ public class PuaPastisValidator {
JSONAssert.assertEquals(controlSchemaExpected.getJSONObject(DEFINITIONS),
controlSchemaActual.getJSONObject(DEFINITIONS), JSONCompareMode.STRICT);
// Checking that additionalProperties is present and is boolean
if (controlSchemaActual.has("additionalProperties") &&
!(controlSchemaActual.get("additionalProperties") instanceof Boolean)) {
throw new AssertionError("PUA additionalProperties field does not contains a boolean value");
}
// Checking that #management object is present and at the correct position
if (controlSchemaActual.has("patternProperties")) {
JSONObject patternProperties = controlSchemaActual.getJSONObject("patternProperties");
......@@ -327,7 +335,8 @@ public class PuaPastisValidator {
* @param pua
* @throws IOException
*/
private void retrieveAccumalatedJsonManagaementProperties(ElementProperties element, List<String> rulesMetadata, List<String> childrenToEncapsulate, List<String> rulesFound, JSONObject pua) throws IOException {
private void retrieveAccumalatedJsonManagaementProperties(ElementProperties element, List<String> rulesMetadata,
List<String> childrenToEncapsulate, List<String> rulesFound, JSONObject pua) throws IOException {
for (ElementProperties childElement : element.getChildren()) {
JSONObject childrenOfRule = sortedJSON();
JSONObject grandChildrenOfRule = sortedJSON();
......@@ -338,7 +347,8 @@ public class PuaPastisValidator {
SedaNode sedaElement = getSedaMetadata(childElement.getName());
// 1. Check special cases
if (checkSpecialCases(rulesMetadata, rulesFound, childElement, sedaElement)) continue;
if (checkSpecialCases(rulesMetadata, rulesFound, childElement, sedaElement))
continue;
getMetaDataFromSeda(childElement, ruleTypeMetadataDetails, sedaElement);
Map<String, PuaMetadataDetails> ruleTypeMetadataMap = new HashMap<>();
Map<String, PuaMetadataDetails> nonSpecialChildOfRule = new HashMap<>();
......@@ -350,7 +360,8 @@ public class PuaPastisValidator {
for (ElementProperties grandChild : childElement.getChildren()) {
SedaNode node = getSedaMetadata(grandChild.getName());
if (childrenToEncapsulate.contains(grandChild.getName())) {
childrenContainsGrandChildName(grandChildrenOfRule, ruleTypeMetadataDetails, requiredChildren, grandChild, node);
childrenContainsGrandChildName(grandChildrenOfRule, ruleTypeMetadataDetails, requiredChildren,
grandChild, node);
} else {
getMetaDataFromSeda(grandChild, nonSpecialChildOfRuleDetails, node);
nonSpecialChildOfRule.put(grandChild.getName(), nonSpecialChildOfRuleDetails);
......@@ -388,7 +399,8 @@ public class PuaPastisValidator {
* @param sedaElement
* @return
*/
private boolean checkSpecialCases(List<String> rulesMetadata, List<String> rulesFound, ElementProperties childElement, SedaNode sedaElement) {
private boolean checkSpecialCases(List<String> rulesMetadata, List<String> rulesFound,
ElementProperties childElement, SedaNode sedaElement) {
if (!rulesMetadata.contains(childElement.getName()) || sedaElement == null) {
return true;
}
......@@ -398,25 +410,31 @@ public class PuaPastisValidator {
return false;
}
/** put Required Non Special Children
/**
* put Required Non Special Children
*
* @param childElement
* @param requiredNonSpecialChildren
* @param ruleTypeMetadata
*/
private void putRequiredNonSpecialChildren(ElementProperties childElement, List<String> requiredNonSpecialChildren, JSONObject ruleTypeMetadata) {
private void putRequiredNonSpecialChildren(ElementProperties childElement, List<String> requiredNonSpecialChildren,
JSONObject ruleTypeMetadata) {
if (!requiredNonSpecialChildren.isEmpty()) {
ruleTypeMetadata.getJSONObject(childElement.getName())
.put(REQUIRED, requiredNonSpecialChildren);
}
}
/** Put Children into rules
/**
* Put Children into rules
*
* @param childrenOfRule
* @param grandChildrenOfRule
* @param propertiesRules
* @param requiredChildren
*/
private void putChildrenIntoRules(JSONObject childrenOfRule, JSONObject grandChildrenOfRule, JSONObject propertiesRules, List<String> requiredChildren) {
private void putChildrenIntoRules(JSONObject childrenOfRule, JSONObject grandChildrenOfRule,
JSONObject propertiesRules, List<String> requiredChildren) {
if (!grandChildrenOfRule.isEmpty()) {
JSONObject propretyOfItems = new JSONObject().put(PROPERTIES, grandChildrenOfRule);
propretyOfItems.put(REQUIRED, requiredChildren);
......@@ -435,7 +453,9 @@ public class PuaPastisValidator {
* @param node
* @throws JsonProcessingException
*/
private void childrenContainsGrandChildName(JSONObject grandChildrenOfRule, PuaMetadataDetails ruleTypeMetadataDetails, List<String> requiredChildren, ElementProperties grandChild, SedaNode node) throws JsonProcessingException {
private void childrenContainsGrandChildName(JSONObject grandChildrenOfRule,
PuaMetadataDetails ruleTypeMetadataDetails, List<String> requiredChildren, ElementProperties grandChild,
SedaNode node) throws JsonProcessingException {
PuaMetadataDetails childOfRuleDetails = new PuaMetadataDetails();
getMetaDataFromSeda(grandChild, childOfRuleDetails, node);
if (grandChild.getCardinality().equals("0-1") ||
......@@ -536,7 +556,7 @@ public class PuaPastisValidator {
puaMetadataDetails.setRequired(getRequiredProperties(elementProperties));
}
JSONObject json = new JSONObject();
json.put(elementProperties.getName(), new JSONObject(puaMetadataDetails));
json.put(elementProperties.getName(), new JSONObject(puaMetadataDetails.serialiseString()));
if (!elementProperties.getChildren().isEmpty()) {
json.getJSONObject(elementProperties.getName()).put(PROPERTIES, new JSONObject());
getJSONObjectFromElement(elementProperties,
......@@ -619,12 +639,13 @@ public class PuaPastisValidator {
puaMetadataDetails.setMinItems(0);
puaMetadataDetails.setMaxItems(1);
}
if(!sedaElement.getElement().equals(COMPLEX) && el.getPuaData() != null && el.getPuaData().getPattern() != null){
if (!sedaElement.getElement().equals(COMPLEX) && el.getPuaData() != null &&
el.getPuaData().getPattern() != null) {
puaMetadataDetails.setPattern(el.getPuaData().getPattern());
}
if(el.getPuaData() != null && el.getPuaData().getEnum() != null){
if (el.getPuaData() != null && el.getPuaData().getEnum() != null) {
puaMetadataDetails.setEnums(el.getPuaData().getEnum());
}else{
} else {
if (!sedaElement.getEnumeration().isEmpty() && el.getValue() == null) {
puaMetadataDetails.setEnums(sedaElement.getEnumeration());
}
......
......@@ -8,7 +8,7 @@
"lastUpdate": "2020-11-26T14:08:20.987",
"activationDate": "2016-12-10T00:00:00.000",
"deactivationDate": "2016-12-10T00:00:00.000",
"controlSchema": "{\"$schema\":\"http://json-schema.org/draft-04/schema\",\"type\":\"object\",\"additionalProperties\":false,\"definitions\":{\"pattern-simple-date\":{\"type\":\"string\",\"pattern\":\"[0-9]{4}-[0-9]{2}-[0-9]{2}\"},\"pattern-date\":{\"type\":\"string\",\"pattern\":\"^([0-8][0-9]{3}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01]))$\"}},\"properties\":{}}",
"controlSchema": "{\"$schema\":\"http://json-schema.org/draft-04/schema\",\"type\":\"object\",\"definitions\":{\"pattern-simple-date\":{\"type\":\"string\",\"pattern\":\"[0-9]{4}-[0-9]{2}-[0-9]{2}\"},\"pattern-date\":{\"type\":\"string\",\"pattern\":\"^([0-8][0-9]{3}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01]))$\"}},\"properties\":{}}",
"tenant": 8,
"version": 1,
"fields": [
......
......@@ -52,6 +52,6 @@ public class PuaPastisValidatorTest {
InputStream inputStreamPua = getClass().getClassLoader().getResourceAsStream(pathFile);
JSONTokener tokener = new JSONTokener(new InputStreamReader(inputStreamPua));
JSONObject profileJson = new JSONObject(tokener);
puaPastisValidator.validatePUA(profileJson);
puaPastisValidator.validatePUA(profileJson, false);
}
}
{
"additionalProperties": false,
"name": "DescriptiveMetadata",
"type": "element",
"cardinality": null,
......@@ -7,12 +8,14 @@
"dataType": null,
"value": null,
"documentation": null,
"editName": null,
"level": 0,
"id": 0,
"parentId": null,
"choices": [],
"children": [
{
"additionalProperties": false,
"name": "ArchiveUnit",
"type": "element",
"cardinality": null,
......@@ -21,12 +24,14 @@
"dataType": null,
"value": null,
"documentation": null,
"editName": null,
"level": 1,
"id": 1,
"parentId": 0,
"choices": [],
"children": [
{
"additionalProperties": false,
"name": "id",
"type": "attribute",
"cardinality": null,
......@@ -35,6 +40,7 @@
"dataType": "ID",
"value": null,
"documentation": null,
"editName": null,
"level": 2,
"id": 2,
"parentId": 1,
......@@ -43,6 +49,7 @@
"puaData": null
},
{
"additionalProperties": false,
"name": "ArchiveUnitProfile",
"type": "element",
"cardinality": "0-1",
......@@ -51,6 +58,7 @@
"dataType": "token",
"value": null,
"documentation": "DescriptionLevel : cardinalité 0-1 / valeur au choix",
"editName": null,
"level": 2,
"id": 12,
"parentId": 1,
......@@ -59,6 +67,7 @@
"puaData": null
},
{
"additionalProperties": false,
"name": "Content",
"type": "element",
"cardinality": null,
......@@ -67,12 +76,14 @@
"dataType": null,
"value": null,
"documentation": null,
"editName": null,
"level": 2,
"id": 3,
"parentId": 1,
"choices": [],
"children": [
{
"additionalProperties": false,
"name": "DescriptionLevel",
"type": "element",
"cardinality": "0-1",
......@@ -81,12 +92,21 @@
"dataType": "token",
"value": null,
"documentation": "DescriptionLevel : cardinalité 0-1",
"editName": null,
"level": 3,
"id": 5,
"parentId": 3,
"choices": [],
"children": [],
"puaData": {
"additionalProperties": null,
"pattern": null,
"minLenght": null,
"maxLenght": null,
"minimum": null,
"maximum": null,
"exclusiveMinimum": null,
"exclusiveMaximum": null,
"enum": [
"Fonds",
"Subfonds",
......@@ -99,18 +119,11 @@
"File",
"Item",
"OtherLevel"
],
"pattern": null,
"minimum": null,
"minLenght": null,
"maxLenght": null,
"maximum": null,
"additionalProperties": null,
"exclusiveMaximum": null,
"exclusiveMinimum": null
]
}
},
{
"additionalProperties": false,
"name": "Title",
"type": "element",
"cardinality": "0-N",
......@@ -119,6 +132,7 @@
"dataType": "string",
"value": null,
"documentation": "Title : cardinalité 0-N / valeur au choix",
"editName": null,
"level": 3,
"id": 4,
"parentId": 3,
......@@ -127,6 +141,7 @@
"puaData": null
},
{
"additionalProperties": false,
"name": "FilePlanPosition",
"type": "element",
"cardinality": "0-N",
......@@ -135,6 +150,7 @@
"dataType": "token",
"value": null,
"documentation": "FilePlanPosition : cardinalité 0-N / valeur au choix",
"editName": null,
"level": 3,
"id": 8,
"parentId": 3,
......@@ -143,6 +159,7 @@
"puaData": null
},
{
"additionalProperties": false,
"name": "Tag",
"type": "element",
"cardinality": "0-N",
......@@ -151,6 +168,7 @@
"dataType": "token",
"value": null,
"documentation": "Tag : cardinalité 0-N / valeur au choix",
"editName": null,
"level": 3,
"id": 11,
"parentId": 3,
......@@ -159,6 +177,7 @@
"puaData": null
},
{
"additionalProperties": false,
"name": "Keyword",
"type": "element",
"cardinality": "0-1",
......@@ -167,12 +186,14 @@
"dataType": "null",
"value": null,
"documentation": "Keyword : cardinalité 0-N / valeur au choix",
"editName": null,
"level": 3,
"id": 6,
"parentId": 3,
"choices": [],
"children": [
{
"additionalProperties": false,
"name": "KeywordContent",
"type": "element",
"cardinality": "1",
......@@ -181,6 +202,7 @@
"dataType": "string",
"value": null,
"documentation": "Identifier : cardinalité 1 / valeur au choix",
"editName": null,
"level": 4,
"id": 7,
"parentId": 6,
......@@ -190,18 +212,19 @@
}
],
"puaData": {
"enum": null,
"additionalProperties": false,
"pattern": null,
"minimum": null,
"minLenght": null,
"maxLenght": null,
"minimum": null,
"maximum": null,
"additionalProperties": false,
"exclusiveMinimum": null,
"exclusiveMaximum": null,
"exclusiveMinimum": null
"enum": null
}
},
{
"additionalProperties": false,
"name": "OriginatingAgency",
"type": "element",
"cardinality": "0-1",
......@@ -210,12 +233,14 @@