Skip to content

Commit

Permalink
fix: endpoint for single TEI and collection should return same result…
Browse files Browse the repository at this point in the history
…s [TECH-785] (dhis2#9139)

* fix: fixed timestamps and user info in TEI collection endpoint [TECH-785]

* fix: fixed timestamps and user info in TEI collection endpoint [TECH-785]
  • Loading branch information
gnespolino authored Oct 28, 2021
1 parent 82880ff commit 87a8135
Show file tree
Hide file tree
Showing 15 changed files with 133 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,17 @@ public TrackerApiResponse getJobReport( String jobId, String reportMode )

public TrackerApiResponse getTrackedEntity( String entityId )
{
return new TrackerApiResponse( this.get( "/trackedEntities/" + entityId ) );
return getTrackedEntity( entityId, new QueryParamsBuilder());
}

public TrackerApiResponse getTrackedEntity( String entityId, QueryParamsBuilder queryParamsBuilder )
{
return new TrackerApiResponse( this.get( "/trackedEntities/" + entityId , queryParamsBuilder ));
}

public TrackerApiResponse getTrackedEntities( QueryParamsBuilder queryParamsBuilder )
{
return new TrackerApiResponse( this.get( "/trackedEntities/", queryParamsBuilder ) );
}

public TrackerApiResponse getEnrollment( String enrollmentId )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,8 @@
*/
package org.hisp.dhis.tracker.importer;

import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.emptyIterable;
import static org.hamcrest.Matchers.everyItem;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.Matchers.*;
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -42,6 +37,7 @@

import org.hisp.dhis.dto.ApiResponse;
import org.hisp.dhis.dto.TrackerApiResponse;
import org.hisp.dhis.helpers.QueryParamsBuilder;
import org.hisp.dhis.tracker.TrackerNtiApiTest;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -112,6 +108,24 @@ public void shouldReturnRequestedFields( String endpoint, String fields, String
} );
}

@Test
public void singleTeiAndCollectionTeiShouldReturnSameResult()
{

TrackerApiResponse trackedEntity = trackerActions.getTrackedEntity( "Kj6vYde4LHh",
new QueryParamsBuilder()
.add( "fields", "*" )
.add( "includeAllAttributes", "true" ) );

TrackerApiResponse trackedEntities = trackerActions.getTrackedEntities( new QueryParamsBuilder()
.add( "fields", "*" )
.add( "includeAllAttributes", "true" )
.add( "trackedEntity", "Kj6vYde4LHh" )
.add( "orgUnit", "O6uvpzGd5pu" ) );

assertEquals( trackedEntities.extractJsonObject( "instances[0]" ), trackedEntity.getBody() );
}

private List<String> splitFields( String fields )
{
List<String> split = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package org.hisp.dhis.tracker.importer.databuilder;

import com.google.gson.JsonObject;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package org.hisp.dhis.tracker.importer.databuilder;

import com.google.gson.JsonObject;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package org.hisp.dhis.tracker.importer.databuilder;

import com.google.gson.JsonObject;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package org.hisp.dhis.tracker.importer.databuilder;

import com.google.gson.JsonObject;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,19 @@
*/
package org.hisp.dhis.dxf2.events.trackedentity.store.mapper;

import static org.hisp.dhis.dxf2.events.trackedentity.store.mapper.JsonbToObjectHelper.setUserInfoSnapshot;
import static org.hisp.dhis.dxf2.events.trackedentity.store.query.EnrollmentQuery.COLUMNS.COMPLETED;
import static org.hisp.dhis.dxf2.events.trackedentity.store.query.EnrollmentQuery.COLUMNS.COMPLETEDBY;
import static org.hisp.dhis.dxf2.events.trackedentity.store.query.EnrollmentQuery.COLUMNS.CREATED;
import static org.hisp.dhis.dxf2.events.trackedentity.store.query.EnrollmentQuery.COLUMNS.CREATEDCLIENT;
import static org.hisp.dhis.dxf2.events.trackedentity.store.query.EnrollmentQuery.COLUMNS.CREATED_BY;
import static org.hisp.dhis.dxf2.events.trackedentity.store.query.EnrollmentQuery.COLUMNS.DELETED;
import static org.hisp.dhis.dxf2.events.trackedentity.store.query.EnrollmentQuery.COLUMNS.ENROLLMENTDATE;
import static org.hisp.dhis.dxf2.events.trackedentity.store.query.EnrollmentQuery.COLUMNS.FOLLOWUP;
import static org.hisp.dhis.dxf2.events.trackedentity.store.query.EnrollmentQuery.COLUMNS.GEOMETRY;
import static org.hisp.dhis.dxf2.events.trackedentity.store.query.EnrollmentQuery.COLUMNS.ID;
import static org.hisp.dhis.dxf2.events.trackedentity.store.query.EnrollmentQuery.COLUMNS.INCIDENTDATE;
import static org.hisp.dhis.dxf2.events.trackedentity.store.query.EnrollmentQuery.COLUMNS.LAST_UPDATED_BY;
import static org.hisp.dhis.dxf2.events.trackedentity.store.query.EnrollmentQuery.COLUMNS.ORGUNIT_NAME;
import static org.hisp.dhis.dxf2.events.trackedentity.store.query.EnrollmentQuery.COLUMNS.ORGUNIT_UID;
import static org.hisp.dhis.dxf2.events.trackedentity.store.query.EnrollmentQuery.COLUMNS.PROGRAM_UID;
Expand Down Expand Up @@ -89,9 +92,11 @@ private Enrollment getEnrollment( ResultSet rs )
enrollment.setOrgUnitName( rs.getString( getColumnName( ORGUNIT_NAME ) ) );
enrollment.setCreated( DateUtils.getIso8601NoTz( rs.getTimestamp( getColumnName( CREATED ) ) ) );
enrollment.setCreatedAtClient( DateUtils.getIso8601NoTz( rs.getTimestamp( getColumnName( CREATEDCLIENT ) ) ) );
setUserInfoSnapshot( rs, getColumnName( CREATED_BY ), enrollment::setCreatedByUserInfo );
enrollment.setLastUpdated( DateUtils.getIso8601NoTz( rs.getTimestamp( getColumnName( UPDATED ) ) ) );
enrollment
.setLastUpdatedAtClient( DateUtils.getIso8601NoTz( rs.getTimestamp( getColumnName( UPDATEDCLIENT ) ) ) );
setUserInfoSnapshot( rs, getColumnName( LAST_UPDATED_BY ), enrollment::setLastUpdatedByUserInfo );
enrollment.setProgram( rs.getString( getColumnName( PROGRAM_UID ) ) );
enrollment.setStatus( EnrollmentStatus.fromStatusString( rs.getString( getColumnName( STATUS ) ) ) );
enrollment.setEnrollmentDate( rs.getTimestamp( getColumnName( ENROLLMENTDATE ) ) );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
*/
package org.hisp.dhis.dxf2.events.trackedentity.store.mapper;

import static org.hisp.dhis.dxf2.events.trackedentity.store.mapper.JsonbToObjectHelper.setUserInfoSnapshot;
import static org.hisp.dhis.dxf2.events.trackedentity.store.mapper.MapperGeoUtils.resolveGeometry;
import static org.hisp.dhis.dxf2.events.trackedentity.store.query.EventQuery.COLUMNS;
import static org.hisp.dhis.dxf2.events.trackedentity.store.query.EventQuery.getColumnName;
Expand Down Expand Up @@ -80,9 +81,11 @@ private Event getEvent( ResultSet rs )
event.setCreated( DateUtils.getIso8601NoTz( rs.getTimestamp( getColumnName( COLUMNS.CREATED ) ) ) );
event.setCreatedAtClient(
DateUtils.getIso8601NoTz( rs.getTimestamp( getColumnName( COLUMNS.CREATEDCLIENT ) ) ) );
setUserInfoSnapshot( rs, getColumnName( COLUMNS.CREATED_BY ), event::setCreatedByUserInfo );
event.setLastUpdated( DateUtils.getIso8601NoTz( rs.getTimestamp( getColumnName( COLUMNS.UPDATED ) ) ) );
event.setLastUpdatedAtClient(
DateUtils.getIso8601NoTz( rs.getTimestamp( getColumnName( COLUMNS.UPDATEDCLIENT ) ) ) );
setUserInfoSnapshot( rs, getColumnName( COLUMNS.LAST_UPDATED_BY ), event::setLastUpdatedByUserInfo );

resolveGeometry( rs.getBytes( getColumnName( COLUMNS.GEOMETRY ) ) ).ifPresent( event::setGeometry );

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright (c) 2004-2021, University of Oslo
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* Neither the name of the HISP project nor the names of its contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.hisp.dhis.dxf2.events.trackedentity.store.mapper;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Optional;
import java.util.function.Consumer;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.SneakyThrows;

import org.hisp.dhis.program.UserInfoSnapshot;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

@NoArgsConstructor( access = AccessLevel.PRIVATE )
public class JsonbToObjectHelper
{

private final static ObjectMapper MAPPER;

static
{
MAPPER = new ObjectMapper();
MAPPER.configure( SerializationFeature.FAIL_ON_EMPTY_BEANS, false );
MAPPER.configure( DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false );
MAPPER.configure( DeserializationFeature.FAIL_ON_INVALID_SUBTYPE, false );
}

static void setUserInfoSnapshot( ResultSet rs, String columnName,
Consumer<UserInfoSnapshot> applier )
throws SQLException
{
Optional.ofNullable( rs.getObject( columnName ) )
.map( Object::toString )
.map( JsonbToObjectHelper::safelyConvert )
.ifPresent( applier );
}

@SneakyThrows
static UserInfoSnapshot safelyConvert( String userInfoSnapshotAsString )
{
return MAPPER.readValue( userInfoSnapshotAsString, UserInfoSnapshot.class );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ private Relationship getRelationship( ResultSet rs )
relationship.setFrom( createItem( rs.getString( "from_uid" ) ) );
relationship.setTo( createItem( rs.getString( "to_uid" ) ) );
relationship.setBidirectional( rs.getBoolean( "reltype_bi" ) );
relationship.setCreated( DateUtils.getIso8601NoTz( rs.getDate( "created" ) ) );
relationship.setLastUpdated( DateUtils.getIso8601NoTz( rs.getDate( "lastupdated" ) ) );
relationship.setCreated( DateUtils.getIso8601NoTz( rs.getTimestamp( "created" ) ) );
relationship.setLastUpdated( DateUtils.getIso8601NoTz( rs.getTimestamp( "lastupdated" ) ) );

return relationship;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ private Attribute getAttribute( ResultSet rs )
{
Attribute attribute = new Attribute();

attribute.setCreated( DateUtils.getIso8601NoTz( rs.getDate( getColumnName( CREATED ) ) ) );
attribute.setLastUpdated( DateUtils.getIso8601NoTz( rs.getDate( getColumnName( UPDATED ) ) ) );
attribute.setCreated( DateUtils.getIso8601NoTz( rs.getTimestamp( getColumnName( CREATED ) ) ) );
attribute.setLastUpdated( DateUtils.getIso8601NoTz( rs.getTimestamp( getColumnName( UPDATED ) ) ) );
attribute.setDisplayName( rs.getString( getColumnName( ATTR_NAME ) ) );
attribute.setAttribute( rs.getString( getColumnName( ATTR_UID ) ) );
attribute.setValueType( ValueType.fromString( rs.getString( getColumnName( ATTR_VALUE_TYPE ) ) ) );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,14 @@
*/
package org.hisp.dhis.dxf2.events.trackedentity.store.mapper;

import static org.hisp.dhis.dxf2.events.trackedentity.store.mapper.JsonbToObjectHelper.setUserInfoSnapshot;
import static org.hisp.dhis.dxf2.events.trackedentity.store.query.TrackedEntityInstanceQuery.COLUMNS.CREATED;
import static org.hisp.dhis.dxf2.events.trackedentity.store.query.TrackedEntityInstanceQuery.COLUMNS.CREATEDCLIENT;
import static org.hisp.dhis.dxf2.events.trackedentity.store.query.TrackedEntityInstanceQuery.COLUMNS.CREATED_BY;
import static org.hisp.dhis.dxf2.events.trackedentity.store.query.TrackedEntityInstanceQuery.COLUMNS.DELETED;
import static org.hisp.dhis.dxf2.events.trackedentity.store.query.TrackedEntityInstanceQuery.COLUMNS.GEOMETRY;
import static org.hisp.dhis.dxf2.events.trackedentity.store.query.TrackedEntityInstanceQuery.COLUMNS.INACTIVE;
import static org.hisp.dhis.dxf2.events.trackedentity.store.query.TrackedEntityInstanceQuery.COLUMNS.LAST_UPDATED_BY;
import static org.hisp.dhis.dxf2.events.trackedentity.store.query.TrackedEntityInstanceQuery.COLUMNS.ORGUNIT_UID;
import static org.hisp.dhis.dxf2.events.trackedentity.store.query.TrackedEntityInstanceQuery.COLUMNS.TYPE_UID;
import static org.hisp.dhis.dxf2.events.trackedentity.store.query.TrackedEntityInstanceQuery.COLUMNS.UID;
Expand Down Expand Up @@ -77,8 +80,10 @@ private TrackedEntityInstance getTei( ResultSet rs )
tei.setTrackedEntityType( rs.getString( getColumnName( TYPE_UID ) ) );
tei.setCreated( DateUtils.getIso8601NoTz( rs.getTimestamp( getColumnName( CREATED ) ) ) );
tei.setCreatedAtClient( DateUtils.getIso8601NoTz( rs.getTimestamp( getColumnName( CREATEDCLIENT ) ) ) );
setUserInfoSnapshot( rs, getColumnName( CREATED_BY ), tei::setCreatedByUserInfo );
tei.setLastUpdated( DateUtils.getIso8601NoTz( rs.getTimestamp( getColumnName( UPDATED ) ) ) );
tei.setLastUpdatedAtClient( DateUtils.getIso8601NoTz( rs.getTimestamp( getColumnName( UPDATEDCLIENT ) ) ) );
setUserInfoSnapshot( rs, getColumnName( LAST_UPDATED_BY ), tei::setLastUpdatedByUserInfo );
tei.setInactive( rs.getBoolean( getColumnName( INACTIVE ) ) );
tei.setDeleted( rs.getBoolean( getColumnName( DELETED ) ) );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,10 @@ public enum COLUMNS
UID,
CREATED,
CREATEDCLIENT,
CREATED_BY,
UPDATED,
UPDATEDCLIENT,
LAST_UPDATED_BY,
STATUS,
GEOMETRY,
ENROLLMENTDATE,
Expand All @@ -72,8 +74,10 @@ public enum COLUMNS
.put( COLUMNS.UID, new TableColumn( "pi", "uid" ) )
.put( COLUMNS.CREATED, new TableColumn( "pi", "created" ) )
.put( COLUMNS.CREATEDCLIENT, new TableColumn( "pi", "createdatclient" ) )
.put( COLUMNS.CREATED_BY, new TableColumn( "pi", "createdbyuserinfo" ) )
.put( COLUMNS.UPDATED, new TableColumn( "pi", "lastupdated" ) )
.put( COLUMNS.UPDATEDCLIENT, new TableColumn( "pi", "lastupdatedatclient" ) )
.put( COLUMNS.LAST_UPDATED_BY, new TableColumn( "pi", "lastupdatedbyuserinfo" ) )
.put( COLUMNS.STATUS, new TableColumn( "pi", "status" ) )
.put( COLUMNS.ENROLLMENTDATE, new TableColumn( "pi", "enrollmentdate" ) )
.put( COLUMNS.INCIDENTDATE, new TableColumn( "pi", "incidentdate" ) )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,12 @@ public enum COLUMNS
STOREDBY( new TableColumn( "psi", "storedby" ) ),
COMPLETEDBY( new TableColumn( "psi", "completedby" ) ),
COMPLETEDDATE( new TableColumn( "psi", "completeddate" ) ),
CREATED_BY( new TableColumn( "psi", "createdbyuserinfo" ) ),
CREATED( new TableColumn( "psi", "created" ) ),
CREATEDCLIENT( new TableColumn( "psi", "createdatclient" ) ),
UPDATED( new TableColumn( "psi", "lastupdated" ) ),
UPDATEDCLIENT( new TableColumn( "psi", "lastupdatedatclient" ) ),
LAST_UPDATED_BY( new TableColumn( "psi", "lastupdatedbyuserinfo" ) ),
DELETED( new TableColumn( "psi", "deleted" ) ),
GEOMETRY( new Function( "ST_AsBinary", "psi", "geometry", "geometry" ) ),
TEI_UID( new TableColumn( "tei", "uid", "tei_uid" ) ),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@ public enum COLUMNS
UID,
CREATED,
CREATEDCLIENT,
CREATED_BY,
UPDATED,
UPDATEDCLIENT,
LAST_UPDATED_BY,
INACTIVE,
DELETED,
GEOMETRY,
Expand All @@ -59,8 +61,10 @@ public enum COLUMNS
.put( COLUMNS.UID, new TableColumn( "tei", "uid", "tei_uid" ) )
.put( COLUMNS.CREATED, new TableColumn( "tei", "created" ) )
.put( COLUMNS.CREATEDCLIENT, new TableColumn( "tei", "createdatclient" ) )
.put( COLUMNS.CREATED_BY, new TableColumn( "tei", "createdbyuserinfo" ) )
.put( COLUMNS.UPDATED, new TableColumn( "tei", "lastupdated" ) )
.put( COLUMNS.UPDATEDCLIENT, new TableColumn( "tei", "lastupdatedatclient" ) )
.put( COLUMNS.LAST_UPDATED_BY, new TableColumn( "tei", "lastupdatedbyuserinfo" ) )
.put( COLUMNS.INACTIVE, new TableColumn( "tei", "inactive" ) )
.put( COLUMNS.DELETED, new TableColumn( "tei", "deleted" ) )
.put( COLUMNS.GEOMETRY, new Function( "ST_AsBinary", "tei", "geometry", "geometry" ) )
Expand Down

0 comments on commit 87a8135

Please sign in to comment.