Search in sources :

Example 1 with Dhis2Payload

use of net.geoprism.registry.etl.DHIS2TestService.Dhis2Payload in project geoprism-registry by terraframe.

the class DHIS2ServiceTest method exportCustomAttribute.

private void exportCustomAttribute(TestGeoObjectTypeInfo got, TestGeoObjectInfo go, TestAttributeTypeInfo attr) throws InterruptedException {
    DHIS2SyncLevel level2 = new DHIS2SyncLevel();
    level2.setGeoObjectType(got.getServerObject().getCode());
    level2.setSyncType(DHIS2SyncLevel.Type.ALL);
    level2.setLevel(1);
    Collection<DHIS2AttributeMapping> mappings = getDefaultMappings();
    DHIS2AttributeMapping mapping;
    if (attr.getType().equals(AttributeTermType.TYPE)) {
        mapping = new DHIS2TermAttributeMapping();
        mapping.setAttributeMappingStrategy(DHIS2TermAttributeMapping.class.getName());
        Map<String, String> terms = new HashMap<String, String>();
        terms.put(AllAttributesDataset.AT_GO_TERM.fetchRootAsClassifier().getClassifierId(), "TEST_EXTERNAL_ID");
        terms.put(AllAttributesDataset.TERM_TERM_VAL1.fetchClassifier().getClassifierId(), "TEST_EXTERNAL_ID");
        terms.put(AllAttributesDataset.TERM_TERM_VAL2.fetchClassifier().getClassifierId(), "TEST_EXTERNAL_ID");
        ((DHIS2TermAttributeMapping) mapping).setTerms(terms);
    } else {
        mapping = new DHIS2AttributeMapping();
        mapping.setAttributeMappingStrategy(DHIS2AttributeMapping.class.getName());
    }
    mapping.setCgrAttrName(attr.getAttributeName());
    mapping.setDhis2AttrName(attr.getAttributeName());
    mapping.setExternalId("TEST_EXTERNAL_ID");
    mappings.add(mapping);
    level2.setMappings(mappings);
    SynchronizationConfig config = createSyncConfig(this.system, level2);
    JsonObject jo = syncService.run(testData.clientSession.getSessionId(), config.getOid());
    ExportHistory hist = ExportHistory.get(jo.get("historyId").getAsString());
    SchedulerTestUtils.waitUntilStatus(hist.getOid(), AllJobStatus.SUCCESS);
    LinkedList<Dhis2Payload> payloads = this.dhis2.getPayloads();
    Assert.assertEquals(2, payloads.size());
    for (int level = 0; level < payloads.size(); ++level) {
        Dhis2Payload payload = payloads.get(level);
        JsonObject joPayload = JsonParser.parseString(payload.getData()).getAsJsonObject();
        JsonArray orgUnits = joPayload.get("organisationUnits").getAsJsonArray();
        Assert.assertEquals(1, orgUnits.size());
        JsonObject orgUnit = orgUnits.get(0).getAsJsonObject();
        Assert.assertEquals(level, orgUnit.get("level").getAsInt());
        Assert.assertEquals("MULTI_POLYGON", orgUnit.get("featureType").getAsString());
        if (level == 0) {
            Assert.assertEquals(AllAttributesDataset.GO_ALL.getCode(), orgUnit.get("code").getAsString());
        } else {
            Assert.assertEquals(go.getCode(), orgUnit.get("code").getAsString());
            Assert.assertTrue(orgUnit.has("attributeValues"));
            JsonArray attributeValues = orgUnit.get("attributeValues").getAsJsonArray();
            Assert.assertEquals(1, attributeValues.size());
            JsonObject attributeValue = attributeValues.get(0).getAsJsonObject();
            Assert.assertNotNull(attributeValue.get("lastUpdated").getAsString());
            Assert.assertNotNull(attributeValue.get("created").getAsString());
            AttributeType attrDto = attr.fetchDTO();
            if (attrDto instanceof AttributeIntegerType) {
                Assert.assertEquals(go.getServerObject().getValue(attr.getAttributeName()), attributeValue.get("value").getAsLong());
            } else if (attrDto instanceof AttributeFloatType) {
                Assert.assertEquals(go.getServerObject().getValue(attr.getAttributeName()), attributeValue.get("value").getAsDouble());
            } else if (attrDto instanceof AttributeDateType) {
                // TODO : If we fetch the object from the database in this manner the
                // miliseconds aren't included on the date. But if we fetch the object
                // via a query (as in DataExportJob) then the miliseconds ARE
                // included...
                // String expected =
                // DHIS2GeoObjectJsonAdapters.DHIS2Serializer.formatDate((Date)
                // go.getServerObject().getValue(attr.getAttributeName()));
                String expected = DHIS2GeoObjectJsonAdapters.DHIS2Serializer.formatDate(AllAttributesDataset.GO_DATE_VALUE);
                String actual = attributeValue.get("value").getAsString();
                Assert.assertEquals(expected, actual);
            } else if (attrDto instanceof AttributeBooleanType) {
                Assert.assertEquals(go.getServerObject().getValue(attr.getAttributeName()), attributeValue.get("value").getAsBoolean());
            } else if (attrDto instanceof AttributeTermType) {
                String dhis2Id = attributeValue.get("value").getAsString();
                // Term term = (Term)
                // go.getServerObject().getValue(attr.getAttributeName());
                Assert.assertEquals("TEST_EXTERNAL_ID", dhis2Id);
            } else {
                Assert.assertEquals(go.getServerObject().getValue(attr.getAttributeName()), attributeValue.get("value").getAsString());
            }
            Assert.assertEquals("TEST_EXTERNAL_ID", attributeValue.get("attribute").getAsJsonObject().get("id").getAsString());
        }
    }
}
Also used : AttributeIntegerType(org.commongeoregistry.adapter.metadata.AttributeIntegerType) HashMap(java.util.HashMap) JsonObject(com.google.gson.JsonObject) Dhis2Payload(net.geoprism.registry.etl.DHIS2TestService.Dhis2Payload) AttributeFloatType(org.commongeoregistry.adapter.metadata.AttributeFloatType) AttributeDateType(org.commongeoregistry.adapter.metadata.AttributeDateType) JsonArray(com.google.gson.JsonArray) ExportHistory(net.geoprism.registry.etl.export.ExportHistory) AttributeType(org.commongeoregistry.adapter.metadata.AttributeType) SynchronizationConfig(net.geoprism.registry.SynchronizationConfig) AttributeBooleanType(org.commongeoregistry.adapter.metadata.AttributeBooleanType) AttributeTermType(org.commongeoregistry.adapter.metadata.AttributeTermType)

Example 2 with Dhis2Payload

use of net.geoprism.registry.etl.DHIS2TestService.Dhis2Payload in project geoprism-registry by terraframe.

the class DHIS2ServiceTest method testExportGeoObjects.

@Request
@Test
public void testExportGeoObjects() throws InterruptedException {
    SynchronizationConfig config = createSyncConfig(this.system, null);
    SynchronizationConfigService service = new SynchronizationConfigService();
    JsonObject joHist = service.run(testData.clientSession.getSessionId(), config.getOid());
    ExportHistory hist = ExportHistory.get(joHist.get("historyId").getAsString());
    SchedulerTestUtils.waitUntilStatus(hist.getOid(), AllJobStatus.SUCCESS);
    hist = ExportHistory.get(hist.getOid());
    Assert.assertEquals(new Long(1), hist.getWorkTotal());
    Assert.assertEquals(new Long(1), hist.getWorkProgress());
    Assert.assertEquals(ImportStage.COMPLETE.name(), hist.getStage().get(0).name());
    LinkedList<Dhis2Payload> payloads = this.dhis2.getPayloads();
    Assert.assertEquals(1, payloads.size());
    Dhis2Payload payload = payloads.get(0);
    JsonObject data = JsonParser.parseString(payload.getData()).getAsJsonObject();
    JsonArray orgUnits = data.get("organisationUnits").getAsJsonArray();
    Assert.assertEquals(1, orgUnits.size());
    JsonObject orgUnit = orgUnits.get(0).getAsJsonObject();
    Assert.assertEquals("AllAttrGO_ALL", orgUnit.get("code").getAsString());
}
Also used : JsonArray(com.google.gson.JsonArray) ExportHistory(net.geoprism.registry.etl.export.ExportHistory) SynchronizationConfigService(net.geoprism.registry.service.SynchronizationConfigService) JsonObject(com.google.gson.JsonObject) SynchronizationConfig(net.geoprism.registry.SynchronizationConfig) Dhis2Payload(net.geoprism.registry.etl.DHIS2TestService.Dhis2Payload) Test(org.junit.Test) Request(com.runwaysdk.session.Request)

Aggregations

JsonArray (com.google.gson.JsonArray)2 JsonObject (com.google.gson.JsonObject)2 SynchronizationConfig (net.geoprism.registry.SynchronizationConfig)2 Dhis2Payload (net.geoprism.registry.etl.DHIS2TestService.Dhis2Payload)2 ExportHistory (net.geoprism.registry.etl.export.ExportHistory)2 Request (com.runwaysdk.session.Request)1 HashMap (java.util.HashMap)1 SynchronizationConfigService (net.geoprism.registry.service.SynchronizationConfigService)1 AttributeBooleanType (org.commongeoregistry.adapter.metadata.AttributeBooleanType)1 AttributeDateType (org.commongeoregistry.adapter.metadata.AttributeDateType)1 AttributeFloatType (org.commongeoregistry.adapter.metadata.AttributeFloatType)1 AttributeIntegerType (org.commongeoregistry.adapter.metadata.AttributeIntegerType)1 AttributeTermType (org.commongeoregistry.adapter.metadata.AttributeTermType)1 AttributeType (org.commongeoregistry.adapter.metadata.AttributeType)1 Test (org.junit.Test)1