Skip to content

Commit

Permalink
Merge pull request #799 from dhis2/hotbugfixing
Browse files Browse the repository at this point in the history
Hotbugfixing 0.15.4
  • Loading branch information
vgarciabnz authored Apr 26, 2019
2 parents ae85087 + 27496a3 commit 1d75f05
Show file tree
Hide file tree
Showing 10 changed files with 35 additions and 3,667 deletions.
4 changes: 2 additions & 2 deletions core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ ext {
buildToolsVersion: "28.0.3",
minSdkVersion : 19,
targetSdkVersion : 28,
versionCode : 128,
versionName : "0.15.3-SNAPSHOT"
versionCode : 129,
versionName : "0.15.4-SNAPSHOT"
]

libraries = [
Expand Down
4 changes: 2 additions & 2 deletions core/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
# Properties which are consumed by plugins/gradle-mvn-push.gradle plugin.
# They are used for publishing artifact to snapshot repository.

VERSION_NAME=0.15.3-SNAPSHOT
VERSION_CODE=128
VERSION_NAME=0.15.4-SNAPSHOT
VERSION_CODE=129

GROUP=org.hisp.dhis

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,12 @@ public void tearDown() throws IOException {

@Test
public void download_events() throws Exception {
int eventLimitByOrgUnit = 53;
int eventLimitByOrgUnit = 1;

givenAMetadataInDatabase();

dhis2MockServer.enqueueMockResponse("systeminfo/system_info.json");
dhis2MockServer.enqueueMockResponse("event/events_1.json");
dhis2MockServer.enqueueMockResponse("event/events_2.json");

d2.eventModule().downloadSingleEvents(eventLimitByOrgUnit, false).call();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,31 +28,22 @@

package org.hisp.dhis.android.core.event;

import androidx.test.runner.AndroidJUnit4;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

import org.hisp.dhis.android.core.D2;
import org.hisp.dhis.android.core.common.BaseIdentifiableObject;
import org.hisp.dhis.android.core.common.D2Factory;
import org.hisp.dhis.android.core.common.Payload;
import org.hisp.dhis.android.core.data.database.AbsStoreTestCase;
import org.hisp.dhis.android.core.data.file.ResourcesFileReader;
import org.hisp.dhis.android.core.data.server.Dhis2MockServer;
import org.hisp.dhis.android.core.trackedentity.TrackedEntityDataValue;
import org.hisp.dhis.android.core.trackedentity.TrackedEntityDataValueStore;
import org.hisp.dhis.android.core.trackedentity.TrackedEntityDataValueStoreImpl;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;

import androidx.test.runner.AndroidJUnit4;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;

Expand All @@ -61,17 +52,14 @@ public class EventEndpointCallMockIntegrationShould extends AbsStoreTestCase {

private Dhis2MockServer dhis2MockServer;
private D2 d2;
private EventStore eventStore;

@Override
@Before
public void setUp() throws IOException {
super.setUp();

dhis2MockServer = new Dhis2MockServer(new ResourcesFileReader());

d2 = D2Factory.create(dhis2MockServer.getBaseEndpoint(), databaseAdapter());
eventStore = EventStoreImpl.create(databaseAdapter());
}

@Override
Expand All @@ -94,30 +82,27 @@ public void download_events_according_to_default_query() throws Exception {

d2.eventModule().eventPersistenceCallFactory.getCall(events).call();

verifyDownloadedEvents("event/events_1.json");
assertThat(d2.eventModule().events.count(), is(1));
}

@Test
public void download_number_of_events_according_to_page_size() throws Exception {
givenAMetadataInDatabase();

int pageSize = 3;
int pageSize = 1;

Callable<List<Event>> eventEndpointCall = EventCallFactory.create(d2.retrofit(), d2.databaseAdapter(), "DiszpKrYNg8", pageSize);

dhis2MockServer.enqueueMockResponse("event/events_2.json");
dhis2MockServer.enqueueMockResponse("event/events_1.json");

List<Event> events = eventEndpointCall.call();

d2.eventModule().eventPersistenceCallFactory.getCall(events).call();

List<Event> downloadedEvents = eventStore.selectAll();

assertThat(downloadedEvents.size(), is(pageSize));
assertThat(d2.eventModule().events.count(), is(pageSize));
}

//@Test
//TODO Pendding
@Test
public void rollback_transaction_when_insert_a_event_with_wrong_foreign_key()
throws Exception {
givenAMetadataInDatabase();
Expand All @@ -129,77 +114,12 @@ public void rollback_transaction_when_insert_a_event_with_wrong_foreign_key()

eventEndpointCall.call();

verifyNumberOfDownloadedEvents(1);
verifyNumberOfDownloadedTrackedEntityDataValue(6);
verifyDownloadedEvents("event/event_1_with_all_data_values.json");
assertThat(d2.eventModule().events.count(), is(0));
assertThat(d2.trackedEntityModule().trackedEntityDataValues.count(), is(0));
}

private void givenAMetadataInDatabase() throws Exception {
dhis2MockServer.enqueueMetadataResponses();
d2.syncMetaData().call();
}

private void verifyNumberOfDownloadedEvents(int numEvents) {
List<Event> downloadedEvents = eventStore.querySingleEvents();

assertThat(downloadedEvents.size(), is(numEvents));
}

private void verifyNumberOfDownloadedTrackedEntityDataValue(int num) {
TrackedEntityDataValueStore eventStore = TrackedEntityDataValueStoreImpl.create(d2.databaseAdapter());

int numPersisted = eventStore.selectAll().size();

assertThat(numPersisted, is(num));
}

private void verifyDownloadedEvents(String file) throws IOException {
Payload<Event> expectedEventsResponse = parseEventResponse(file);

List<Event> downloadedEvents = getDownloadedEvents();

assertThat(downloadedEvents.size(), is(expectedEventsResponse.items().size()));
assertThat(downloadedEvents, is(expectedEventsResponse.items()));
}

private List<Event> getDownloadedEvents() {
List<Event> downloadedEvents = new ArrayList<>();

List<Event> downloadedEventsWithoutValues = eventStore.querySingleEvents();

TrackedEntityDataValueStore trackedEntityDataValueStore = TrackedEntityDataValueStoreImpl.create(databaseAdapter());

for (int i = 0; i < downloadedEventsWithoutValues.size(); ++i) {

Event event = downloadedEventsWithoutValues.get(i);

List<TrackedEntityDataValue> trackedEntityDataValues =
trackedEntityDataValueStore.queryTrackedEntityDataValuesByEventUid(event.uid());
List<TrackedEntityDataValue> trackedEntityDataValuesWithNullIdsAndEvents = new ArrayList<>();

for (TrackedEntityDataValue trackedEntityDataValue : trackedEntityDataValues) {
trackedEntityDataValuesWithNullIdsAndEvents.add(
trackedEntityDataValue.toBuilder().id(null).event(null).build());
}

downloadedEvents.add(event.toBuilder()
.id(null)
.state(null)
.deleted(false)
.trackedEntityDataValues(trackedEntityDataValuesWithNullIdsAndEvents).build());
}

return downloadedEvents;
}

private Payload<Event> parseEventResponse(String file) throws IOException {
String expectedEventsResponseJson = new ResourcesFileReader().getStringFromFile(file);

ObjectMapper objectMapper = new ObjectMapper().setDateFormat(
BaseIdentifiableObject.DATE_FORMAT.raw());

return objectMapper.readValue(expectedEventsResponseJson,
new TypeReference<Payload<Event>>() {
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -152,53 +152,40 @@ private void givenAMetadataInDatabase() throws Exception {

private void verifyDownloadedTrackedEntityInstancePayload(String file, String teiUid)
throws IOException {
TrackedEntityInstance expectedEnrollmentResponse = parseTrackedEntityInstanceResponsePayload(file);
Payload<TrackedEntityInstance> parsed = parseTrackedEntityInstanceResponse(file,
new TypeReference<Payload<TrackedEntityInstance>>() {});

TrackedEntityInstance downloadedTei = getDownloadedTei(teiUid);

assertThat(downloadedTei, is(expectedEnrollmentResponse));
}

private void verifyDownloadedTrackedEntityInstance(String file, String teiUid)
throws IOException {
TrackedEntityInstance expectedEnrollmentResponse = parseTrackedEntityInstanceResponse(file);
TrackedEntityInstance expectedEnrollmentResponse = removeDeletedData(parsed.items().get(0));

TrackedEntityInstance downloadedTei = getDownloadedTei(teiUid);

assertThat(downloadedTei, is(expectedEnrollmentResponse));
assertThat(downloadedTei.uid(), is(expectedEnrollmentResponse.uid()));
assertThat(downloadedTei.trackedEntityAttributeValues().size(),
is(expectedEnrollmentResponse.trackedEntityAttributeValues().size()));
}

private TrackedEntityInstance parseTrackedEntityInstanceResponsePayload(String file)
private void verifyDownloadedTrackedEntityInstance(String file, String teiUid)
throws IOException {
String expectedEventsResponseJson = new ResourcesFileReader().getStringFromFile(file);

ObjectMapper objectMapper = new ObjectMapper().setDateFormat(
BaseIdentifiableObject.DATE_FORMAT.raw());

Payload<TrackedEntityInstance> trackedEntityInstances = objectMapper.readValue(
expectedEventsResponseJson,
new TypeReference<Payload<TrackedEntityInstance>>() {
});
TrackedEntityInstance parsed = parseTrackedEntityInstanceResponse(file,
new TypeReference<TrackedEntityInstance>() {});

TrackedEntityInstance trackedEntityInstance =
removeDeletedData(trackedEntityInstances.items().get(0));
TrackedEntityInstance expectedEnrollmentResponse = removeDeletedData(parsed);

TrackedEntityInstance downloadedTei = getDownloadedTei(teiUid);

return trackedEntityInstance;
assertThat(downloadedTei.uid(), is(expectedEnrollmentResponse.uid()));
assertThat(downloadedTei.trackedEntityAttributeValues().size(),
is(expectedEnrollmentResponse.trackedEntityAttributeValues().size()));
}

private TrackedEntityInstance parseTrackedEntityInstanceResponse(String file)
private<M> M parseTrackedEntityInstanceResponse(String file, TypeReference<M> reference)
throws IOException {
String expectedEventsResponseJson = new ResourcesFileReader().getStringFromFile(file);

ObjectMapper objectMapper = new ObjectMapper().setDateFormat(
BaseIdentifiableObject.DATE_FORMAT.raw());

TrackedEntityInstance trackedEntityInstance = objectMapper.readValue(
expectedEventsResponseJson,
new TypeReference<TrackedEntityInstance>() {});

return removeDeletedData(trackedEntityInstance);
return objectMapper.readValue(expectedEventsResponseJson, reference);
}

@NonNull
Expand Down
2 changes: 2 additions & 0 deletions core/src/main/assets/migrations/45.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
up:
- CREATE UNIQUE INDEX event_data_element ON TrackedEntityDataValue(event, dataElement);
2 changes: 2 additions & 0 deletions core/src/main/assets/migrations/46.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
up:
- CREATE UNIQUE INDEX tracked_entity_instance_attribute ON TrackedEntityAttributeValue(trackedEntityInstance, trackedEntityAttribute);
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@

public class DbOpenHelper extends SQLBriteOpenHelper {

public static final int VERSION = 44;
public static final int VERSION = 46;

public DbOpenHelper(@NonNull Context context, @Nullable String databaseName) {
super(context, databaseName, null, VERSION);
Expand Down
Loading

0 comments on commit 1d75f05

Please sign in to comment.