Commit 3bfc2a99 authored by Abhishek Kumar (SLB)'s avatar Abhishek Kumar (SLB)
Browse files

Merge branch 'slb-nested-ref-validation' into 'master'

Slb nested ref validation

See merge request !321
parents 26406754 97b2294b
Pipeline #117908 failed with stages
in 195 minutes and 54 seconds
......@@ -3,15 +3,15 @@ package org.opengroup.osdu.schema.validation.version.handler;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import org.opengroup.osdu.core.common.logging.JaxRsDpsLog;
import org.opengroup.osdu.schema.exceptions.ApplicationException;
import org.opengroup.osdu.schema.util.JSONUtil;
import org.opengroup.osdu.schema.validation.version.handler.SchemaValidationChainOfHandlers;
import org.opengroup.osdu.schema.validation.version.handler.SchemaValidationHandler;
import org.opengroup.osdu.schema.validation.version.model.SchemaBreakingChanges;
import org.opengroup.osdu.schema.validation.version.model.SchemaHandlerVO;
import org.opengroup.osdu.schema.validation.version.model.SchemaPatch;
import org.opengroup.osdu.schema.validation.version.model.SchemaPatchRefComp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
......@@ -38,7 +38,10 @@ public class SchemaValidationManager {
log.info("Finding difference between two schemas");
schemaPatchList = jsonUtil.findJSONDiff(schemaDiff.getSourceSchema(), schemaDiff.getTargetSchema());
//Sort the patch such that $ref are processed first
if(schemaPatchList != null && schemaPatchList.size()>0) {
schemaPatchList = schemaPatchList.stream().sorted(new SchemaPatchRefComp()).collect(Collectors.toList());
}
log.info("Total differences found :"+schemaPatchList.size());
Set<String> processedArrayPath = new HashSet<>();
......
package org.opengroup.osdu.schema.validation.version.handler.common;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.regex.Pattern;
import org.apache.commons.lang3.StringUtils;
import org.opengroup.osdu.schema.constants.SchemaConstants;
import org.opengroup.osdu.schema.exceptions.ApplicationException;
import org.opengroup.osdu.schema.util.SchemaUtil;
import org.opengroup.osdu.schema.validation.version.SchemaValidationType;
import org.opengroup.osdu.schema.validation.version.handler.SchemaValidationHandler;
import org.opengroup.osdu.schema.validation.version.model.SchemaBreakingChanges;
import org.opengroup.osdu.schema.validation.version.model.SchemaHandlerVO;
import org.opengroup.osdu.schema.validation.version.model.SchemaPatch;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
......@@ -18,6 +23,8 @@ import org.springframework.stereotype.Component;
public class RemoveOperationHandler implements SchemaValidationHandler{
private SchemaValidationHandler nextHandler;
@Autowired
private SchemaUtil schemaUtil;
@Override
public void setNextHandler(SchemaValidationHandler nextHandler) {
......@@ -26,24 +33,50 @@ public class RemoveOperationHandler implements SchemaValidationHandler{
@Override
public void compare(SchemaHandlerVO schemaDiff, SchemaPatch patch, List<SchemaBreakingChanges> schemaBreakingChanges, Set<String> processedArrayPath) throws ApplicationException {
String attributeName = StringUtils.substringAfterLast(patch.getPath(), "/");
if(SchemaConstants.OP_REMOVE.equals(patch.getOp())) {
if(schemaDiff.getChangedRefIds().containsKey(attributeName))
if(schemaDiff.getChangedRefIds().containsKey(attributeName)) {
return;
}else if(isPresentInSource(schemaDiff, patch)) {
return;
}
schemaBreakingChanges.add(new SchemaBreakingChanges(patch, "Removing elements from a schema at Minor level not permitted."));
}else if(null != nextHandler){
this.nextHandler.compare(schemaDiff, patch, schemaBreakingChanges, processedArrayPath);
}
}
@Override
public SchemaValidationType getValidationType() {
return SchemaValidationType.COMMON;
}
private boolean isPresentInSource(SchemaHandlerVO schemaDiff, SchemaPatch patch) {
Pattern pattern = Pattern.compile(SchemaConstants.SCHEMA_KIND_REGEX);
String path = patch.getPath();
String sourceField = StringUtils.substringAfterLast(path, "/");
if(!pattern.matcher(path).matches() || !isAtRoot(path))
return false;
Iterator<String> fieldNameItr = schemaDiff.getTargetSchema().fieldNames();
while(fieldNameItr.hasNext()) {
String fieldName = fieldNameItr.next();
if(pattern.matcher(fieldName).matches()) {
if(schemaUtil.isValidSchemaVersionChange(sourceField, fieldName,schemaDiff.getValidationType())){
return true;
}
}
}
return false;
}
private boolean isAtRoot(String path) {
String subString [] = path.split("\\/");
return subString.length == 2;
}
}
\ No newline at end of file
package org.opengroup.osdu.schema.validation.version.model;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.JsonNode;
import lombok.Data;
......
package org.opengroup.osdu.schema.validation.version.model;
import java.util.Comparator;
import org.apache.commons.lang3.StringUtils;
public class SchemaPatchRefComp implements Comparator<SchemaPatch> {
@Override
public int compare(SchemaPatch patch1, SchemaPatch patch2) {
String attributeName1 = StringUtils.substringAfterLast(patch1.getPath(), "/");
String attributeName2 = StringUtils.substringAfterLast(patch2.getPath(), "/");
if("$ref".equals(attributeName1)) {
return 1;
}else if("$ref".equals(attributeName2)) {
return -1;
}
return 0;
}
}
\ No newline at end of file
package org.opengroup.osdu.schema.validation.version.handler.common;
import static org.assertj.core.api.Assertions.assertThat;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.opengroup.osdu.schema.exceptions.ApplicationException;
import org.opengroup.osdu.schema.util.SchemaUtil;
import org.opengroup.osdu.schema.util.TestUtility;
import org.opengroup.osdu.schema.validation.version.SchemaValidationType;
import org.opengroup.osdu.schema.validation.version.handler.SchemaValidationHandler;
......@@ -28,6 +32,9 @@ public class RemoveOperationHandlerTest {
@InjectMocks
RemoveOperationHandler removeOperationHandler;
@Mock
SchemaUtil schemaUtil;
@Test
public void testCompare_ValidChange_RemovePropWithOldTrue() throws IOException, ApplicationException {
......@@ -45,6 +52,102 @@ public class RemoveOperationHandlerTest {
}
}
@Test
public void testCompare_ValidChange_RemovePropInDefinition() throws IOException, ApplicationException {
List<SchemaBreakingChanges> schemaBreakingChanges = new ArrayList<>();
Set<String> processedArrayPath = new HashSet<>();
SchemaHandlerVO schemaHandlerVO = getMockSchemaHandlerVO("/schema_compare/remove_operation/allowed/base-schema.json"
,"/schema_compare/remove_operation/allowed/new-schema.json");
schemaHandlerVO.setValidationType(SchemaValidationType.MINOR);
Mockito.when(schemaUtil.isValidSchemaVersionChange("osdu:wks:AbstractCommonResources:1.0.0", "osdu:wks:AbstractCommonResources:1.1.0", SchemaValidationType.MINOR)).thenReturn(true);
List<SchemaPatch> schemaPatchList = TestUtility.findSchemaPatch(schemaHandlerVO.getSourceSchema(), schemaHandlerVO.getTargetSchema());
for(SchemaPatch patch : schemaPatchList) {
removeOperationHandler.compare(schemaHandlerVO, patch, schemaBreakingChanges, processedArrayPath);
if(schemaBreakingChanges.size() > 0)
Assert.fail();
}
}
@Test
public void testCompare_ValidChange_RemovePropInDefinition_WithPatchUpgrage() throws IOException, ApplicationException {
List<SchemaBreakingChanges> schemaBreakingChanges = new ArrayList<>();
Set<String> processedArrayPath = new HashSet<>();
SchemaHandlerVO schemaHandlerVO = getMockSchemaHandlerVO("/schema_compare/remove_operation/allowed-patch/base-schema.json"
,"/schema_compare/remove_operation/allowed-patch/new-schema.json");
schemaHandlerVO.setValidationType(SchemaValidationType.MINOR);
Mockito.when(schemaUtil.isValidSchemaVersionChange("osdu:wks:AbstractCommonResources:1.1.0", "osdu:wks:AbstractCommonResources:1.1.1", SchemaValidationType.MINOR)).thenReturn(true);
List<SchemaPatch> schemaPatchList = TestUtility.findSchemaPatch(schemaHandlerVO.getSourceSchema(), schemaHandlerVO.getTargetSchema());
for(SchemaPatch patch : schemaPatchList) {
removeOperationHandler.compare(schemaHandlerVO, patch, schemaBreakingChanges, processedArrayPath);
if(schemaBreakingChanges.size() > 0)
Assert.fail();
}
}
@Test
public void testCompare_ValidChange_RemovePropInDefinition_Patch() throws IOException, ApplicationException {
List<SchemaBreakingChanges> schemaBreakingChanges = new ArrayList<>();
Set<String> processedArrayPath = new HashSet<>();
SchemaHandlerVO schemaHandlerVO = getMockSchemaHandlerVO("/schema_compare/remove_operation/allowed-patch/base-schema.json"
,"/schema_compare/remove_operation/allowed-patch/new-schema.json");
schemaHandlerVO.setValidationType(SchemaValidationType.PATCH);
Mockito.when(schemaUtil.isValidSchemaVersionChange("osdu:wks:AbstractCommonResources:1.1.0", "osdu:wks:AbstractCommonResources:1.1.1", SchemaValidationType.PATCH)).thenReturn(true);
List<SchemaPatch> schemaPatchList = TestUtility.findSchemaPatch(schemaHandlerVO.getSourceSchema(), schemaHandlerVO.getTargetSchema());
for(SchemaPatch patch : schemaPatchList) {
removeOperationHandler.compare(schemaHandlerVO, patch, schemaBreakingChanges, processedArrayPath);
if(schemaBreakingChanges.size() > 0)
Assert.fail();
}
}
@Test
public void testCompare_BreakingChange_RemovePropInDefinition() throws IOException, ApplicationException {
List<SchemaBreakingChanges> schemaBreakingChanges = new ArrayList<>();
Set<String> processedArrayPath = new HashSet<>();
SchemaHandlerVO schemaHandlerVO = getMockSchemaHandlerVO("/schema_compare/remove_operation/allowed/new-schema.json",
"/schema_compare/remove_operation/allowed/base-schema.json"
);
List<SchemaPatch> schemaPatchList = TestUtility.findSchemaPatch(schemaHandlerVO.getSourceSchema(), schemaHandlerVO.getTargetSchema());
schemaHandlerVO.setValidationType(SchemaValidationType.MINOR);
Mockito.when(schemaUtil.isValidSchemaVersionChange("osdu:wks:AbstractCommonResources:1.1.0", "osdu:wks:AbstractCommonResources:1.0.0", SchemaValidationType.MINOR)).thenReturn(false);
for(SchemaPatch patch : schemaPatchList) {
removeOperationHandler.compare(schemaHandlerVO, patch, schemaBreakingChanges, processedArrayPath);
if(schemaBreakingChanges.size() > 0)
Assert.assertTrue(true);
else
Assert.fail();
}
}
@Test
public void testCompare_BreakingChange_RemovePropInDefinition_Patch() throws IOException, ApplicationException {
List<SchemaBreakingChanges> schemaBreakingChanges = new ArrayList<>();
Set<String> processedArrayPath = new HashSet<>();
SchemaHandlerVO schemaHandlerVO = getMockSchemaHandlerVO("/schema_compare/remove_operation/allowed-patch/new-schema.json",
"/schema_compare/remove_operation/allowed-patch/base-schema.json"
);
List<SchemaPatch> schemaPatchList = TestUtility.findSchemaPatch(schemaHandlerVO.getSourceSchema(), schemaHandlerVO.getTargetSchema());
schemaHandlerVO.setValidationType(SchemaValidationType.PATCH);
Mockito.when(schemaUtil.isValidSchemaVersionChange("osdu:wks:AbstractCommonResources:1.1.1", "osdu:wks:AbstractCommonResources:1.1.0", SchemaValidationType.PATCH)).thenReturn(false);
for(SchemaPatch patch : schemaPatchList) {
removeOperationHandler.compare(schemaHandlerVO, patch, schemaBreakingChanges, processedArrayPath);
if(schemaBreakingChanges.size() > 0)
Assert.assertTrue(true);
else
Assert.fail();
}
}
@Test
public void testCompare_BreakingChange_ChangeFromTrueToFalse() throws IOException, ApplicationException {
......
{
"osdu:wks:AbstractCommonResources:1.1.0": {
"x-osdu-inheriting-from-kind": [],
"x-osdu-license": "Copyright 2021, The Open Group \\nLicensed under the Apache License, Version 2.0 (the \"License\"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 . Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.",
"$schema": "http://json-schema.org/draft-07/schema#",
"x-osdu-schema-source": "osdu:wks:AbstractCommonResources:1.1.0",
"description": "Common resources to be injected at root 'data' level for every entity, which is persistable in Storage. The insertion is performed by the OsduSchemaComposer script.",
"title": "OSDU Common Resources",
"type": "object",
"properties": {
"ResourceHomeRegionID": {
"x-osdu-relationship": [
{
"EntityType": "OSDURegion",
"GroupType": "reference-data"
}
],
"pattern": "^[\\w\\-\\.]+:reference-data\\-\\-OSDURegion:[\\w\\-\\.\\:\\%]+:[0-9]*$",
"description": "The name of the home [cloud environment] region for this OSDU resource object.",
"title": "Resource Home Region ID",
"type": "string"
},
"ResourceHostRegionIDs": {
"description": "The name of the host [cloud environment] region(s) for this OSDU resource object.",
"title": "Resource Host Region ID",
"type": "array",
"items": {
"x-osdu-relationship": [
{
"EntityType": "OSDURegion",
"GroupType": "reference-data"
}
],
"pattern": "^[\\w\\-\\.]+:reference-data\\-\\-OSDURegion:[\\w\\-\\.\\:\\%]+:[0-9]*$",
"type": "string"
}
},
"ResourceLifecycleStatus": {
"x-osdu-relationship": [
{
"EntityType": "ResourceLifecycleStatus",
"GroupType": "reference-data"
}
],
"pattern": "^[\\w\\-\\.]+:reference-data\\-\\-ResourceLifecycleStatus:[\\w\\-\\.\\:\\%]+:[0-9]*$",
"description": "Describes the current Resource Lifecycle status.",
"title": "Resource Lifecycle Status",
"type": "string"
},
"ResourceSecurityClassification": {
"x-osdu-relationship": [
{
"EntityType": "ResourceSecurityClassification",
"GroupType": "reference-data"
}
],
"pattern": "^[\\w\\-\\.]+:reference-data\\-\\-ResourceSecurityClassification:[\\w\\-\\.\\:\\%]+:[0-9]*$",
"description": "Classifies the security level of the resource.",
"title": "Resource Security Classification",
"type": "string"
},
"ResourceCurationStatus": {
"x-osdu-relationship": [
{
"EntityType": "ResourceCurationStatus",
"GroupType": "reference-data"
}
],
"pattern": "^[\\w\\-\\.]+:reference-data\\-\\-ResourceCurationStatus:[\\w\\-\\.\\:\\%]+:[0-9]*$",
"description": "Describes the current Curation status.",
"title": "Resource Curation Status",
"type": "string"
},
"ExistenceKind": {
"x-osdu-relationship": [
{
"EntityType": "ExistenceKind",
"GroupType": "reference-data"
}
],
"pattern": "^[\\w\\-\\.]+:reference-data\\-\\-ExistenceKind:[\\w\\-\\.\\:\\%]+:[0-9]*$",
"description": "Where does this data resource sit in the cradle-to-grave span of its existence?",
"title": "Existence Kind",
"type": "string"
},
"Source": {
"description": "The entity that produced the record, or from which it is received; could be an organization, agency, system, internal team, or individual. For informational purposes only, the list of sources is not governed.",
"title": "Data Source",
"type": "string"
}
},
"$id": "https://schema.osdu.opengroup.org/json/abstract/AbstractCommonResources.1.0.0.json"
}
}
\ No newline at end of file
{
"osdu:wks:AbstractCommonResources:1.1.1": {
"x-osdu-inheriting-from-kind": [],
"x-osdu-license": "Copyright 2021, The Open Group \\nLicensed under the Apache License, Version 2.0 (the \"License\"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 . Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.",
"$schema": "http://json-schema.org/draft-07/schema#",
"x-osdu-schema-source": "osdu:wks:AbstractCommonResources:1.1.1",
"description": "Common resources to be injected at root 'data' level for every entity, which is persistable in Storage. The insertion is performed by the OsduSchemaComposer script.",
"title": "OSDU Common Resources",
"type": "object",
"properties": {
"ResourceHomeRegionID": {
"x-osdu-relationship": [
{
"EntityType": "OSDURegion",
"GroupType": "reference-data"
}
],
"pattern": "^[\\w\\-\\.]+:reference-data\\-\\-OSDURegion:[\\w\\-\\.\\:\\%]+:[0-9]*$",
"description": "The name of the home [cloud environment] region for this OSDU resource object.",
"title": "Resource Home Region ID",
"type": "string"
},
"ResourceHostRegionIDs": {
"description": "The name of the host [cloud environment] region(s) for this OSDU resource object.",
"title": "Resource Host Region ID",
"type": "array",
"items": {
"x-osdu-relationship": [
{
"EntityType": "OSDURegion",
"GroupType": "reference-data"
}
],
"pattern": "^[\\w\\-\\.]+:reference-data\\-\\-OSDURegion:[\\w\\-\\.\\:\\%]+:[0-9]*$",
"type": "string"
}
},
"ResourceLifecycleStatus": {
"x-osdu-relationship": [
{
"EntityType": "ResourceLifecycleStatus",
"GroupType": "reference-data"
}
],
"pattern": "^[\\w\\-\\.]+:reference-data\\-\\-ResourceLifecycleStatus:[\\w\\-\\.\\:\\%]+:[0-9]*$",
"description": "Describes the current Resource Lifecycle status.",
"title": "Resource Lifecycle Status",
"type": "string"
},
"ResourceSecurityClassification": {
"x-osdu-relationship": [
{
"EntityType": "ResourceSecurityClassification",
"GroupType": "reference-data"
}
],
"pattern": "^[\\w\\-\\.]+:reference-data\\-\\-ResourceSecurityClassification:[\\w\\-\\.\\:\\%]+:[0-9]*$",
"description": "Classifies the security level of the resource.",
"title": "Resource Security Classification",
"type": "string"
},
"ResourceCurationStatus": {
"x-osdu-relationship": [
{
"EntityType": "ResourceCurationStatus",
"GroupType": "reference-data"
}
],
"pattern": "^[\\w\\-\\.]+:reference-data\\-\\-ResourceCurationStatus:[\\w\\-\\.\\:\\%]+:[0-9]*$",
"description": "Describes the current Curation status.",
"title": "Resource Curation Status",
"type": "string"
},
"ExistenceKind": {
"x-osdu-relationship": [
{
"EntityType": "ExistenceKind",
"GroupType": "reference-data"
}
],
"pattern": "^[\\w\\-\\.]+:reference-data\\-\\-ExistenceKind:[\\w\\-\\.\\:\\%]+:[0-9]*$",
"description": "Where does this data resource sit in the cradle-to-grave span of its existence?",
"title": "Existence Kind",
"type": "string"
},
"Source": {
"description": "The entity that produced the record, or from which it is received; could be an organization, agency, system, internal team, or individual. For informational purposes only, the list of sources is not governed.",
"title": "Data Source",
"type": "string"
}
},
"$id": "https://schema.osdu.opengroup.org/json/abstract/AbstractCommonResources.1.0.0.json"
}
}
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment