← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 21945: added schema-validator to object bundle validation

 

------------------------------------------------------------
revno: 21945
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Thu 2016-02-11 15:11:40 +0700
message:
  added schema-validator to object bundle validation
added:
  dhis-2/dhis-services/dhis-service-dxf2/src/test/resources/dxf2/de_validate2.json
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/validation/ValidationViolation.java
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/DefaultObjectBundleService.java
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/ObjectBundleValidation.java
  dhis-2/dhis-services/dhis-service-dxf2/src/test/java/org/hisp/dhis/dxf2/metadata2/objectbundle/ObjectBundleServiceTest.java


--
lp:dhis2
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk

Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription
=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/validation/ValidationViolation.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/validation/ValidationViolation.java	2016-01-04 02:27:49 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/validation/ValidationViolation.java	2016-02-11 08:11:40 +0000
@@ -32,6 +32,7 @@
 import com.fasterxml.jackson.annotation.JsonPropertyOrder;
 import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
 import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
+import com.google.common.base.MoreObjects;
 import org.hisp.dhis.common.DxfNamespaces;
 
 /**
@@ -98,13 +99,13 @@
         this.value = value;
     }
 
-    @Override public String toString()
+    @Override
+    public String toString()
     {
-        final StringBuilder sb = new StringBuilder( "ValidationViolation{" );
-        sb.append( "property='" ).append( property ).append( '\'' );
-        sb.append( ", message='" ).append( message ).append( '\'' );
-        sb.append( ", value=" ).append( value );
-        sb.append( '}' );
-        return sb.toString();
+        return MoreObjects.toStringHelper( this )
+            .add( "property", property )
+            .add( "message", message )
+            .add( "value", value )
+            .toString();
     }
 }

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/DefaultObjectBundleService.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/DefaultObjectBundleService.java	2016-02-11 07:16:09 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/DefaultObjectBundleService.java	2016-02-11 08:11:40 +0000
@@ -33,12 +33,12 @@
 import org.hisp.dhis.preheat.PreheatMode;
 import org.hisp.dhis.preheat.PreheatParams;
 import org.hisp.dhis.preheat.PreheatService;
-import org.hisp.dhis.schema.Schema;
 import org.hisp.dhis.schema.SchemaService;
 import org.hisp.dhis.validation.ValidationViolation;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 
+import java.util.ArrayList;
 import java.util.List;
 
 /**
@@ -84,10 +84,19 @@
             objectBundleValidation.addInvalidReferences( klass, preheatService.checkReferences(
                 bundle.getObjects().get( klass ), bundle.getPreheat(), bundle.getPreheatIdentifier() ) );
 
+            List<List<ValidationViolation>> validationViolations = new ArrayList<>();
+
             for ( IdentifiableObject object : bundle.getObjects().get( klass ) )
             {
-                List<ValidationViolation> validationViolations = schemaValidator.validate( object );
+                List<ValidationViolation> validate = schemaValidator.validate( object );
+
+                if ( !validate.isEmpty() )
+                {
+                    validationViolations.add( validate );
+                }
             }
+
+            objectBundleValidation.addValidationViolation( klass, validationViolations );
         }
 
         return objectBundleValidation;

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/ObjectBundleValidation.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/ObjectBundleValidation.java	2016-02-11 05:40:52 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/ObjectBundleValidation.java	2016-02-11 08:11:40 +0000
@@ -31,6 +31,7 @@
 import com.google.common.base.MoreObjects;
 import org.hisp.dhis.common.IdentifiableObject;
 import org.hisp.dhis.preheat.PreheatValidation;
+import org.hisp.dhis.validation.ValidationViolation;
 
 import java.util.HashMap;
 import java.util.List;
@@ -43,6 +44,8 @@
 {
     private Map<Class<? extends IdentifiableObject>, List<PreheatValidation>> preheatValidations = new HashMap<>();
 
+    private Map<Class<? extends IdentifiableObject>, List<List<ValidationViolation>>> validationViolations = new HashMap<>();
+
     public ObjectBundleValidation()
     {
     }
@@ -57,6 +60,16 @@
         return preheatValidations;
     }
 
+    public void addValidationViolation( Class<? extends IdentifiableObject> klass, List<List<ValidationViolation>> validationViolations )
+    {
+        this.validationViolations.put( klass, validationViolations );
+    }
+
+    public Map<Class<? extends IdentifiableObject>, List<List<ValidationViolation>>> getValidationViolations()
+    {
+        return validationViolations;
+    }
+
     @Override
     public String toString()
     {

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/test/java/org/hisp/dhis/dxf2/metadata2/objectbundle/ObjectBundleServiceTest.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/test/java/org/hisp/dhis/dxf2/metadata2/objectbundle/ObjectBundleServiceTest.java	2016-02-11 06:53:05 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/test/java/org/hisp/dhis/dxf2/metadata2/objectbundle/ObjectBundleServiceTest.java	2016-02-11 08:11:40 +0000
@@ -208,6 +208,23 @@
         }
     }
 
+    @Test
+    public void testPreheatValidationsInvalidObjects() throws IOException
+    {
+        Map<Class<? extends IdentifiableObject>, List<IdentifiableObject>> metadata = renderService.fromMetadata(
+            new ClassPathResource( "dxf2/de_validate2.json" ).getInputStream(), RenderFormat.JSON );
+
+        ObjectBundleParams params = new ObjectBundleParams();
+        params.setObjectBundleMode( ObjectBundleMode.VALIDATE );
+        params.setObjects( metadata );
+
+        ObjectBundle bundle = objectBundleService.create( params );
+        ObjectBundleValidation validate = objectBundleService.validate( bundle );
+
+        assertFalse( validate.getValidationViolations().isEmpty() );
+        assertEquals( 2, validate.getValidationViolations().get( DataElement.class ).size() );
+    }
+
     private void defaultSetup()
     {
         DataElement de1 = createDataElement( 'A' );

=== added file 'dhis-2/dhis-services/dhis-service-dxf2/src/test/resources/dxf2/de_validate2.json'
--- dhis-2/dhis-services/dhis-service-dxf2/src/test/resources/dxf2/de_validate2.json	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/test/resources/dxf2/de_validate2.json	2016-02-11 08:11:40 +0000
@@ -0,0 +1,79 @@
+{
+  "dataElements": [
+    {
+      "lastUpdated": "2016-01-15T15:44:11.475+0000",
+      "aggregationLevels": [],
+      "name": "Birth certificate",
+      "created": "2016-01-15T11:13:32.416+0000",
+      "shortName": "Birth certificate",
+      "aggregationType": "SUM",
+      "id": "uf3svrmp8Oj",
+      "userGroupAccesses": [],
+      "publicAccess": "rw------",
+      "user": {
+        "id": "GOLswS44mh8"
+      },
+      "zeroIsSignificant": false,
+      "categoryCombo": {
+        "id": "p0KPaWEg3cf"
+      },
+      "attributeValues": []
+    },
+    {
+      "aggregationType": "SUM",
+      "publicAccess": "rw------",
+      "userGroupAccesses": [],
+      "id": "ulD2zW0TIy2",
+      "attributeValues": [],
+      "categoryCombo": {
+        "id": "p0KPaWEg3cf"
+      },
+      "user": {
+        "id": "GOLswS44mh8"
+      },
+      "zeroIsSignificant": false,
+      "name": "Documentation",
+      "lastUpdated": "2016-01-15T15:44:11.475+0000",
+      "aggregationLevels": [],
+      "created": "2016-01-15T11:13:00.541+0000",
+      "shortName": "Documentation",
+      "domainType": "TRACKER"
+    },
+    {
+      "id": "cNkTt6mJQyO",
+      "userGroupAccesses": [],
+      "user": {
+        "id": "GOLswS44mh8"
+      },
+      "description": "Yebo",
+      "zeroIsSignificant": false,
+      "attributeValues": [
+        {
+          "lastUpdated": "2016-01-12T04:53:21.853+0000",
+          "attribute": {
+            "id": "Z4X3J7jMLYV"
+          },
+          "created": "2016-01-12T04:53:21.454+0000",
+          "value": "ACTIVITY"
+        }
+      ],
+      "shortName": "Yebo",
+      "lastUpdated": "2016-01-12T04:53:21.840+0000",
+      "created": "2016-01-12T04:53:21.826+0000",
+      "optionSet": {
+        "id": "pQYCiuosBnZ"
+      },
+      "valueType": "NUMBER",
+      "publicAccess": "rw------",
+      "formName": "Yebo",
+      "categoryCombo": {
+        "id": "p0KPaWEg3cf"
+      },
+      "code": "Yebo",
+      "aggregationType": "SUM",
+      "domainType": "TRACKER",
+      "aggregationLevels": [],
+      "name": "Yebo"
+    }
+  ]
+}