Skip to content

Commit

Permalink
feat: adds tracker response (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
vilkg authored Feb 5, 2021
1 parent cdb1f42 commit 2fd7390
Show file tree
Hide file tree
Showing 5 changed files with 237 additions and 1 deletion.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@
<artifactId>javafaker</artifactId>
<version>1.0.2</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>5.3.1</version>
<scope>test</scope>
</dependency>
</dependencies>

<distributionManagement>
Expand Down
116 changes: 116 additions & 0 deletions src/main/java/org/hisp/dhis/actions/TrackerActions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package org.hisp.dhis.actions;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import org.hisp.dhis.request.QueryParamsBuilder;
import org.hisp.dhis.response.dto.ApiResponse;
import org.hisp.dhis.response.dto.TrackerApiResponse;
import org.hisp.dhis.utils.JsonObjectBuilder;

import java.io.File;
import java.time.Instant;
import java.util.logging.Logger;

import static org.hamcrest.Matchers.notNullValue;

/**
* @author Gintare Vilkelyte <[email protected]>
*/
public class TrackerActions
extends RestApiActions
{
private Logger logger = Logger.getLogger( TrackerActions.class.getName() );

public TrackerActions()
{
super( "/tracker" );
}

public ApiResponse getJob( String jobId )
{
return this.get( "/jobs/" + jobId );
}

public ApiResponse waitUntilJobIsCompleted( String jobId )
{
logger.info( String.format( "Waiting until tracker job with id %s is completed", jobId ) );
ApiResponse response = null;
boolean completed = false;
int maxAttempts = 100;

while ( !completed && maxAttempts > 0 )
{
response = getJob( jobId );
response.validate().statusCode( 200 );
completed = response.extractList( "completed" ).contains( true );
maxAttempts--;
}

if ( maxAttempts == 0 )
{
logger.warning(
String.format( "Tracker job didn't complete in %d. Message: %s", maxAttempts, response.extract( "message" ) ) );
}

logger.info( "Tracker job is completed. Message: " + response.extract( "message" ) );
return response;
}

public TrackerApiResponse postAndGetJobReport( File file )
{
ApiResponse response = this.postFile( file );

return new TrackerApiResponse( response );
//return getJobReportByImportResponse( response );
}

public TrackerApiResponse postAndGetJobReport( File file, QueryParamsBuilder queryParamsBuilder )
{
queryParamsBuilder.add( "async=false" );
ApiResponse response = this.postFile( file, queryParamsBuilder );

return new TrackerApiResponse( response );
//return getJobReportByImportResponse( response );
}

public TrackerApiResponse postAndGetJobReport( JsonObject jsonObject )
{
ApiResponse response = this.post( jsonObject );

return new TrackerApiResponse( response );
//return getJobReportByImportResponse( response );
}

public TrackerApiResponse postAndGetJobReport( JsonObject jsonObject, QueryParamsBuilder queryParamsBuilder )
{
queryParamsBuilder.add( "async=false" );

ApiResponse response = this.post( jsonObject, queryParamsBuilder );

return new TrackerApiResponse( response );
//return getJobReportByImportResponse( response );
}

public TrackerApiResponse getJobReport( String jobId, String reportMode )
{
ApiResponse response = this.get( String.format( "/jobs/%s/report?reportMode=%s", jobId, reportMode ) );

// add created entities

return new TrackerApiResponse( response );
}

private TrackerApiResponse getJobReportByImportResponse( ApiResponse response )
{
response.validate()
.statusCode( 200 )
.body( "response.id", notNullValue() );

String jobId = response.extractString( "response.id" );

this.waitUntilJobIsCompleted( jobId );

return this.getJobReport( jobId, "FULL" );

}
}
2 changes: 1 addition & 1 deletion src/main/java/org/hisp/dhis/response/dto/ApiResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public Response getRaw()
return raw;
}

private Response raw;
protected Response raw;

public ApiResponse( Response response )
{
Expand Down
90 changes: 90 additions & 0 deletions src/main/java/org/hisp/dhis/response/dto/TrackerApiResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package org.hisp.dhis.response.dto;

import io.restassured.response.ValidatableResponse;
import org.hamcrest.Matchers;

import java.util.List;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.number.OrderingComparison.greaterThanOrEqualTo;

/**
* @author Gintare Vilkelyte <[email protected]>
*/
public class TrackerApiResponse
extends ApiResponse
{

public TrackerApiResponse( ApiResponse response )
{
super( response.raw );
}

public List<String> extractImportedTeis()
{
return this.extractList( "bundleReport.typeReportMap.TRACKED_ENTITY.objectReports.uid" );
}

public List<String> extractImportedEnrollments()
{
return this.extractList( "bundleReport.typeReportMap.ENROLLMENT.objectReports.uid" );
}

public List<String> extractImportedEvents()
{
return this.extractList( "bundleReport.typeReportMap.EVENT.objectReports.uid" );
}

public List<String> extractImportedRelationships()
{
return this.extractList( "bundleReport.typeReportMap.RELATIONSHIP.objectReports.uid" );
}

public TrackerApiResponse validateSuccessfulImport()
{
validate()
.statusCode( 200 )
.body( "status", equalTo( "OK" ) )
.body( "stats.created", greaterThanOrEqualTo( 1 ) )
.body( "stats.ignored", equalTo( 0 ) )
.body( "stats.total", greaterThanOrEqualTo( 1 ) )
.body( "bundleReport.typeReportMap", notNullValue() );

return this;
}

public ValidatableResponse validateErrorReport()
{
return validate().statusCode( 200 )
.body( "stats.ignored", greaterThanOrEqualTo( 1 ) )
.body( "validationReport.errorReports", Matchers.notNullValue() )
.rootPath( "validationReport.errorReports" );
}

public ValidatableResponse validateWarningReport() {
return validate().statusCode( 200 )
.body( "validationReport.warningReports", Matchers.notNullValue())
.rootPath( "validationReport.warningReports");
}

public ValidatableResponse validateTeis() {
return validate()
.body( "bundleReport.typeReportMap.TRACKED_ENTITY", notNullValue() )
.rootPath( "bundleReport.typeReportMap.TRACKED_ENTITY" );
}

public ValidatableResponse validateEvents() {
return validate()
.body( "bundleReport.typeReportMap.EVENT", notNullValue() )
.rootPath( "bundleReport.typeReportMap.EVENT" );
}

public ValidatableResponse validateEnrollments()
{
return validate()
.body( "bundleReport.typeReportMap.ENROLLMENT", notNullValue() )
.rootPath( "bundleReport.typeReportMap.ENROLLMENT" );
}

}
24 changes: 24 additions & 0 deletions src/test/java/org/hisp/dhis/request/DataRandomizerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.hisp.dhis.request;

import org.hisp.dhis.utils.DataRandomizer;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

import static org.junit.jupiter.api.Assertions.assertEquals;

/**
* @author Gintare Vilkelyte <[email protected]>
*/
public class DataRandomizerTest
{
@ParameterizedTest
@CsvSource( {
"1,1,1"
} )
public void shouldGenerateRandomNumberBetween(int min, int max, int expected) {
int random = DataRandomizer.randomIntInRange( min, max );

assertEquals(random, expected);
}
}

0 comments on commit 2fd7390

Please sign in to comment.