Skip to content

Commit

Permalink
chore: update to latest snapshot of camel-dhis2 (#135)
Browse files Browse the repository at this point in the history
  • Loading branch information
cjmamo authored Jun 12, 2023
1 parent bfe5e87 commit 22edb3d
Show file tree
Hide file tree
Showing 18 changed files with 99 additions and 102 deletions.
8 changes: 7 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,13 @@
<dependency>
<groupId>org.hisp.dhis.integration.camel</groupId>
<artifactId>camel-dhis2</artifactId>
<version>2.0.0-SNAPSHOT</version>
<version>3.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.hisp.dhis.integration.sdk</groupId>
<artifactId>jackson-resource-model</artifactId>
<classifier>v40.0</classifier>
<version>2.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,10 @@
package org.hisp.dhis.integration.rapidpro;

import org.apache.camel.Exchange;
import org.hisp.dhis.api.model.v2_38_1.CompleteDataSetRegistration;
import org.hisp.dhis.api.model.v2_38_1.CompleteDataSetRegistrations;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.function.Function;

@Component
Expand All @@ -42,11 +41,9 @@ public class CompleteDataSetRegistrationFunction implements Function<Exchange, O
@Override
public Object apply( Exchange exchange )
{
CompleteDataSetRegistrations completeDataSetRegistrations = new CompleteDataSetRegistrations().withCompleteDataSetRegistrations(
List.of( new CompleteDataSetRegistration().withCompleted( true )
.withAdditionalProperty( "dataSet", exchange.getMessage().getHeader( "dataSetCode" ) )
.withAdditionalProperty( "organisationUnit", exchange.getMessage().getHeader( "orgUnitId" ) )
.withAdditionalProperty( "period", exchange.getMessage().getHeader( "period" ) ) ) );
return completeDataSetRegistrations;
return Map.of( "CompleteDataSetRegistrations", List.of(
Map.of( "completed", true, "dataSet", exchange.getMessage().getHeader( "dataSetCode" ), "organisationUnit",
exchange.getMessage().getHeader( "orgUnitId" ), "period",
exchange.getMessage().getHeader( "period" ) ) ) );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
import java.util.ResourceBundle;
import java.util.Set;

import org.hisp.dhis.api.model.v2_38_1.CategoryOptionCombo;
import org.hisp.dhis.api.model.v40_0.CategoryOptionCombo;
import org.hisp.dhis.integration.sdk.api.Dhis2Client;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.hisp.dhis.api.model.v2_38_1.DataSet;
import org.hisp.dhis.api.model.v40_0.DataSet;
import org.hisp.dhis.integration.sdk.support.period.PeriodBuilder;
import org.springframework.stereotype.Component;

Expand All @@ -43,7 +43,7 @@ public void process( Exchange exchange )
throws Exception
{
Iterable<DataSet> dataSets = exchange.getMessage().getBody( Iterable.class );
String periodType = (String) dataSets.iterator().next().getPeriodType().get();
String periodType = dataSets.iterator().next().getPeriodType().get().value();
int reportPeriodOffset = exchange.getMessage().getHeader( "reportPeriodOffset", Integer.class );
String period;
if ( periodType.equalsIgnoreCase( "Daily" ) )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.hisp.dhis.api.model.v2_38_1.User;
import org.hisp.dhis.api.model.v40_0.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.hisp.dhis.api.model.v2_38_1.User;
import org.hisp.dhis.api.model.v40_0.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.hisp.dhis.api.model.v2_38_1.DataSet;
import org.hisp.dhis.api.model.v2_38_1.OrganisationUnit;
import org.hisp.dhis.api.model.v40_0.DataSet;
import org.hisp.dhis.api.model.v40_0.OrganisationUnit;
import org.hisp.dhis.api.model.v40_0.RefOrganisationUnit;
import org.hisp.dhis.integration.sdk.support.period.PeriodBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
Expand All @@ -58,18 +59,18 @@ public void process( Exchange exchange )
Map<String, Object> contacts = (Map<String, Object>) exchange.getProperty( "contacts" );
Set<String> contactOrgUnitIds = reduceOrgUnitIds( (List<Map<String, Object>>) contacts.get( "results" ) );

String periodType = (String) dataSet.getPeriodType().get();
String periodType = dataSet.getPeriodType().get().value();
String lastElapsedPeriod = createLastElapsedPeriod( periodType );

if ( dataSet.getOrganisationUnits().isPresent() && !dataSet.getOrganisationUnits().get().isEmpty() )
{
String orgUnitIdScheme = exchange.getProperty( "orgUnitIdScheme", String.class ).toLowerCase();
StringBuilder ouDimensionStringBuilder = new StringBuilder();
for ( OrganisationUnit organisationUnit : dataSet.getOrganisationUnits().get() )
for ( RefOrganisationUnit organisationUnit : dataSet.getOrganisationUnits().get() )
{
if ( contactOrgUnitIds.contains( ((Optional<String>) organisationUnit.get( orgUnitIdScheme )).get() ) )
{
ouDimensionStringBuilder.append( organisationUnit.getId().get() ).append( ";" );
ouDimensionStringBuilder.append( organisationUnit.getId() ).append( ";" );
}
}
Map<String, Object> queryParams = Map.of( "dimension",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ protected void doConfigure()

from( "direct:computePeriod" )
.routeId( "Compute Period" )
.toD( "dhis2://get/collection?path=dataSets&filter=code:eq:${headers['dataSetCode']}&fields=periodType&itemType=org.hisp.dhis.api.model.v2_38_1.DataSet&paging=false&client=#dhis2Client" )
.toD( "dhis2://get/collection?path=dataSets&arrayName=dataSet&filter=code:eq:${headers['dataSetCode']}&fields=periodType&itemType=org.hisp.dhis.api.model.v40_0.DataSet&paging=false&client=#dhis2Client" )
.process( currentPeriodCalculator );

from( "direct:completeDataSetRegistration" )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@

import org.apache.camel.Exchange;
import org.apache.camel.LoggingLevel;
import org.hisp.dhis.api.model.v2_38_1.ListGrid;
import org.hisp.dhis.integration.rapidpro.expression.IterableReader;
import org.hisp.dhis.integration.rapidpro.processor.SetReportRateQueryParamProcessor;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -104,7 +103,7 @@ protected void doConfigure()
from( "direct:fetchReportRate" )
.process( setReportRateQueryParamProcessor )
.to( "dhis2://get/resource?path=analytics&client=#dhis2Client" )
.unmarshal().json( ListGrid.class );
.unmarshal().json( Map.class );

from( "direct:sendBroadcast" )
.transform( datasonnet( "resource:classpath:broadcast.ds", String.class, "application/x-java-object", "application/json" ) )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public void doConfigure()
.log( LoggingLevel.INFO, LOGGER, "Synchronising RapidPro contacts..." )
.to( "direct:prepareRapidPro" )
.setProperty( "orgUnitIdScheme", simple( "{{org.unit.id.scheme}}" ) )
.toD( "dhis2://get/collection?path=users&fields=id,firstName,surname,phoneNumber,telegram,whatsApp,twitter,facebookMessenger,organisationUnits[${exchangeProperty.orgUnitIdScheme.toLowerCase()}~rename(id)]&filter=organisationUnits.id:!null&itemType=org.hisp.dhis.api.model.v2_38_1.User&paging=false&client=#dhis2Client" )
.toD( "dhis2://get/collection?path=users&arrayName=users&fields=id,firstName,surname,phoneNumber,telegram,whatsApp,twitter,facebookMessenger,organisationUnits[${exchangeProperty.orgUnitIdScheme.toLowerCase()}~rename(id)]&filter=organisationUnits.id:!null&itemType=org.hisp.dhis.api.model.v40_0.User&paging=false&client=#dhis2Client" )
.setProperty( "dhis2Users", iterableReader )
.setHeader( "Authorization", constant( "Token {{rapidpro.api.token}}" ) )
.setProperty( "nextContactsPageUrl", simple( "{{rapidpro.api.url}}/contacts.json?group=DHIS2" ) )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
import org.apache.camel.test.spring.junit5.CamelSpringBootTest;
import org.apache.camel.test.spring.junit5.UseAdviceWith;
import org.apache.commons.io.FileUtils;
import org.hisp.dhis.api.model.v2_38_1.DataValueSet;
import org.hisp.dhis.api.model.v2_38_1.DataValue__1;
import org.hisp.dhis.api.model.v40_0.DataValue;
import org.hisp.dhis.api.model.v40_0.DataValueSet;
import org.hisp.dhis.integration.sdk.support.period.PeriodBuilder;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
Expand Down Expand Up @@ -131,11 +131,11 @@ public void beforeEach()
.withDataSet( "qNtxTrp56wV" ).withPeriod( PeriodBuilder.yearOf( new Date(), -1 ) )
.withDataValues(
List.of(
new DataValue__1().withDataElement( "MAL_POP_TOTAL" ).withCategoryOptionCombo( "MAL-0514Y" )
new DataValue().withDataElement( "MAL_POP_TOTAL" ).withCategoryOptionCombo( "MAL-0514Y" )
.withValue( "0" ),
new DataValue__1().withDataElement( "MAL_LLIN_DISTR_PW" ).withValue( "0" ),
new DataValue__1().withDataElement( "GEN_DOMESTIC_FUND" ).withValue( "0" ),
new DataValue__1().withDataElement( "GEN_EXT_FUND" ).withValue( "0" ) ) ) )
new DataValue().withDataElement( "MAL_LLIN_DISTR_PW" ).withValue( "0" ),
new DataValue().withDataElement( "GEN_DOMESTIC_FUND" ).withValue( "0" ),
new DataValue().withDataElement( "GEN_EXT_FUND" ).withValue( "0" ) ) ) )
.withParameter( "dataElementIdScheme", "CODE" )
.withParameter( "categoryOptionComboIdScheme", "CODE" )
.transfer().close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
package org.hisp.dhis.integration.rapidpro;

import org.hisp.dhis.integration.sdk.Dhis2ClientBuilder;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
Expand All @@ -43,6 +44,7 @@
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

@Disabled
public class ApplicationTestCase
{
public static class TerminateException extends RuntimeException
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.model.language.DatasonnetExpression;
import org.apache.camel.support.DefaultExchange;
import org.hisp.dhis.api.model.v2_38_1.OrganisationUnit;
import org.hisp.dhis.api.model.v2_38_1.User;
import org.hisp.dhis.api.model.v40_0.RefOrganisationUnit;
import org.hisp.dhis.api.model.v40_0.User;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -68,7 +68,7 @@ public void testMapping()
Map<String, Object> user = new ObjectMapper().registerModule( new Jdk8Module() ).convertValue(
new User().withId( "AIK2aQOJIbj" ).withFirstName( "Alice" ).withSurname( "Wonderland" )
.withPhoneNumber( "+233223232" ).withTelegram( "6249937697" )
.withOrganisationUnits( List.of( new OrganisationUnit().withId( "lc3eMKXaEfw" ) ) ), Map.class );
.withOrganisationUnits( List.of( new RefOrganisationUnit().withId( "lc3eMKXaEfw" ) ) ), Map.class );
exchange.getMessage().setBody( user );
Map<String, Object> contact = new ValueBuilder( dsExpression ).evaluate( exchange, Map.class );

Expand Down
67 changes: 32 additions & 35 deletions src/test/java/org/hisp/dhis/integration/rapidpro/Environment.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,30 +27,23 @@
*/
package org.hisp.dhis.integration.rapidpro;

import static io.restassured.RestAssured.given;
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Path;
import java.time.Duration;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Stream;

import com.github.javafaker.Faker;
import com.github.javafaker.Name;
import io.restassured.RestAssured;
import io.restassured.builder.MultiPartSpecBuilder;
import org.hisp.dhis.api.model.v2_38_1.DescriptiveWebMessage;
import org.hisp.dhis.api.model.v2_38_1.ImportReport;
import org.hisp.dhis.api.model.v2_38_1.Notification;
import org.hisp.dhis.api.model.v2_38_1.OrganisationUnit;
import org.hisp.dhis.api.model.v2_38_1.OrganisationUnitLevel;
import org.hisp.dhis.api.model.v2_38_1.User;
import org.hisp.dhis.api.model.v2_38_1.UserCredentialsDto;
import org.hisp.dhis.api.model.v2_38_1.UserRole;
import org.hisp.dhis.api.model.v2_38_1.WebMessage;
import io.restassured.builder.RequestSpecBuilder;
import io.restassured.http.ContentType;
import io.restassured.response.ExtractableResponse;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
import org.hisp.dhis.api.model.v40_0.Notification;
import org.hisp.dhis.api.model.v40_0.OrganisationUnit;
import org.hisp.dhis.api.model.v40_0.OrganisationUnitLevel;
import org.hisp.dhis.api.model.v40_0.RefOrganisationUnit;
import org.hisp.dhis.api.model.v40_0.RefUserRole;
import org.hisp.dhis.api.model.v40_0.User;
import org.hisp.dhis.api.model.v40_0.UserCredentialsDto;
import org.hisp.dhis.api.model.v40_0.WebMessage;
import org.hisp.dhis.integration.sdk.Dhis2ClientBuilder;
import org.hisp.dhis.integration.sdk.api.Dhis2Client;
import org.hisp.dhis.integration.sdk.api.Dhis2Response;
Expand All @@ -66,15 +59,19 @@
import org.testcontainers.images.builder.ImageFromDockerfile;
import org.testcontainers.utility.DockerImageName;

import com.github.javafaker.Faker;
import com.github.javafaker.Name;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Path;
import java.time.Duration;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Stream;

import io.restassured.RestAssured;
import io.restassured.builder.RequestSpecBuilder;
import io.restassured.http.ContentType;
import io.restassured.response.ExtractableResponse;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
import static io.restassured.RestAssured.given;
import static org.junit.jupiter.api.Assertions.assertEquals;

public final class Environment
{
Expand Down Expand Up @@ -333,7 +330,7 @@ public static void runAnalytics()
String taskId = (String) ((Map<String, Object>) webMessage.getResponse().get()).get( "id" );

Notification notification = null;
while ( notification == null || !(Boolean) notification.getCompleted().get() )
while ( notification == null || !(Boolean) notification.getCompleted() )
{
Thread.sleep( 5000 );
Iterable<Notification> notifications = DHIS2_CLIENT.get( "system/tasks/ANALYTICS_TABLE/{taskId}",
Expand Down Expand Up @@ -399,12 +396,12 @@ public static String createDhis2User( String orgUnitId, String phoneNumber )
.withResource( new User().withFirstName( name.firstName() ).withSurname( name.lastName() )
.withPhoneNumber( phoneNumber )
.withAttributeValues( Collections.emptyList() )
.withOrganisationUnits( List.of( new OrganisationUnit().withId( orgUnitId ) ) )
.withOrganisationUnits( List.of( new RefOrganisationUnit().withId( orgUnitId ) ) )
.withUserCredentials(
new UserCredentialsDto().withCatDimensionConstraints( Collections.emptyList() )
.withCogsDimensionConstraints( Collections.emptyList() ).withUsername( name.username() )
.withPassword( "aKa9CD8HyAi8Y7!" ).withUserRoles(
List.of( new UserRole().withId( "yrB6vc5Ip3r" ) ) ) ) )
List.of( new RefUserRole().withId( "yrB6vc5Ip3r" ) ) ) ) )
.transfer();

return (String) ((Map<String, Object>) dhis2Response.returnAs(
Expand Down Expand Up @@ -451,7 +448,7 @@ private static void importMetaData( String orgUnitLevelId )
.withResource( metaData )
.withParameter( "atomicMode", "NONE" ).transfer().returnAs( WebMessage.class );

assertEquals( DescriptiveWebMessage.Status.OK, webMessage.getStatus().get() );
assertEquals( WebMessage.Status.OK, webMessage.getStatus().value() );
}

private static void createOrgUnitLevel()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,9 @@
import org.apache.camel.ExchangePattern;
import org.apache.camel.builder.AdviceWith;
import org.apache.camel.component.mock.MockEndpoint;
import org.hisp.dhis.api.model.v2_38_1.DataValueSet;
import org.hisp.dhis.api.model.v2_38_1.DataValue__1;
import org.hisp.dhis.api.model.v2_38_1.DescriptiveWebMessage;
import org.hisp.dhis.api.model.v2_38_1.WebMessage;
import org.hisp.dhis.api.model.v40_0.DataValue;
import org.hisp.dhis.api.model.v40_0.DataValueSet;
import org.hisp.dhis.api.model.v40_0.WebMessage;
import org.hisp.dhis.integration.rapidpro.AbstractFunctionalTestCase;
import org.hisp.dhis.integration.rapidpro.Environment;
import org.hisp.dhis.integration.sdk.support.period.PeriodBuilder;
Expand Down Expand Up @@ -95,7 +94,7 @@ public void testDataValueSetIsCreated()
.returnAs(
DataValueSet.class );

Optional<DataValue__1> dataValue = dataValueSet.getDataValues().get().stream()
Optional<DataValue> dataValue = dataValueSet.getDataValues().get().stream()
.filter( v -> v.getDataElement().get().equals( "tpz77FcntKx" ) ).findFirst();

assertTrue( dataValue.isPresent() );
Expand Down Expand Up @@ -133,7 +132,7 @@ public void testRecordInDeadLetterChannelIsCreatedGivenWebMessageErrorWhileCreat
MockEndpoint fakeDhis2Endpoint = camelContext.getEndpoint( "mock:dhis2", MockEndpoint.class );
fakeDhis2Endpoint.whenAnyExchangeReceived(
exchange -> exchange.getMessage().setBody( objectMapper.writeValueAsString(
new WebMessage().withStatus( DescriptiveWebMessage.Status.ERROR ) ) ) );
new WebMessage().withStatus( WebMessage.Status.ERROR ) ) ) );

camelContext.start();
String contactUuid = syncContactsAndFetchFirstContactUuid();
Expand All @@ -150,7 +149,7 @@ public void testRecordInDeadLetterChannelIsCreatedGivenWebMessageErrorWhileCreat
assertEquals( "ERROR",
objectMapper.readValue( (String) deadLetterChannel.get( 0 ).get( "error_message" ),
WebMessage.class )
.getStatus().get().value() );
.getStatus().value() );
}

@Test
Expand All @@ -165,7 +164,7 @@ public void testRecordInDeadLetterChannelIsCreatedGivenMissingDataSetCode()
MockEndpoint fakeDhis2Endpoint = camelContext.getEndpoint( "mock:dhis2", MockEndpoint.class );
fakeDhis2Endpoint.whenAnyExchangeReceived(
exchange -> exchange.getMessage().setBody( objectMapper.writeValueAsString(
new WebMessage().withStatus( DescriptiveWebMessage.Status.ERROR ) ) ) );
new WebMessage().withStatus( WebMessage.Status.ERROR ) ) ) );

camelContext.start();
String contactUuid = syncContactsAndFetchFirstContactUuid();
Expand Down Expand Up @@ -323,7 +322,7 @@ public void testDataValueSetIsCreatedGivenOrgUnitIdSchemeIsCode()
.returnAs(
DataValueSet.class );

Optional<DataValue__1> dataValue = dataValueSet.getDataValues().get().stream()
Optional<DataValue> dataValue = dataValueSet.getDataValues().get().stream()
.filter( v -> v.getDataElement().get().equals( "tpz77FcntKx" ) ).findFirst();

assertTrue( dataValue.isPresent() );
Expand Down
Loading

0 comments on commit 22edb3d

Please sign in to comment.