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

import android.support.test.runner.AndroidJUnit4;

import org.hisp.dhis.android.core.D2;
import org.hisp.dhis.android.core.common.D2Factory;
import org.hisp.dhis.android.core.common.GenericCallData;
import org.hisp.dhis.android.core.data.database.AbsStoreTestCase;
import org.hisp.dhis.android.core.organisationunit.OrganisationUnit;
import org.hisp.dhis.android.core.resource.ResourceHandler;
import org.hisp.dhis.android.core.resource.ResourceStoreImpl;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.io.IOException;

import static com.google.common.truth.Truth.assertThat;

@RunWith(AndroidJUnit4.class)
public class TrackedEntityAttributeReservedValueEndpointCallRealIntegrationShould extends AbsStoreTestCase {
    /**
     * A quick integration test that is probably flaky, but will help with finding bugs related to the
     * metadataSyncCall. It works against the demo server.
     */
    private D2 d2;
    private TrackedEntityAttributeReservedValueEndpointCall reservedValueEndpointCall;

    @Before
    @Override
    public void setUp() throws IOException {
        super.setUp();
        d2 = D2Factory.create("https://play.dhis2.org/android-current/api/", databaseAdapter());
        reservedValueEndpointCall = createCall();
    }

    private TrackedEntityAttributeReservedValueEndpointCall createCall() {
        ResourceHandler resourceHandler =
                new ResourceHandler(new ResourceStoreImpl(databaseAdapter()));
        GenericCallData data = GenericCallData.create(databaseAdapter(), resourceHandler, d2.retrofit());

        OrganisationUnit organisationUnit =  OrganisationUnit.create("orgUnitUid", "ORG_UNIT",
                null, null, null, null, null, null,
                null, null, null, null, null, null,
                null, null, null, null, null);

        return TrackedEntityAttributeReservedValueEndpointCall.FACTORY.create(data, "xs8A6tQJY0s",
                5, organisationUnit);
    }

    @Test
    public void download_data_values() throws Exception {
        if (!d2.isUserLoggedIn().call()) {
            retrofit2.Response loginResponse = d2.logIn("android", "Android123").call();
            assertThat(loginResponse.isSuccessful()).isTrue();
        }

        /*  This test won't pass independently of the sync of metadata, as the foreign keys
            constraints won't be satisfied.
            To run the test, you will need to disable foreign key support in database in
            DbOpenHelper.java replacing 'foreign_keys = ON' with 'foreign_keys = OFF' and
            uncomment the @Test tag */

        retrofit2.Response reservedValueResponse = reservedValueEndpointCall.call();
        assertThat(reservedValueResponse.isSuccessful()).isTrue();
    }

    @Test
    public void stub() {
    }
}
@@ -309,16 +309,80 @@
 stCase;%0A
+import org.hisp.dhis.android.core.data.server.RealServerMother;%0A
 import o
@@ -673,16 +673,38 @@
 ception;
+%0Aimport java.util.Set;
 %0A%0Aimport
@@ -1176,16 +1176,57 @@
 intCall;
+%0A    private Integer numberToReserve = 5;
 %0A%0A    @B
@@ -1347,53 +1347,28 @@
 ate(
-%22https://play.dhis2.org/android-current/api/%22
+RealServerMother.url
 , da
@@ -2107,17 +2107,31 @@
         
-5
+numberToReserve
 , organi
@@ -2145,32 +2145,35 @@
 nit);%0A    %7D%0A%0A   
+ //
  @Test%0A    publi
@@ -2188,20 +2188,24 @@
 ownload_
-data
+reserved
 _values(
@@ -2237,123 +2237,93 @@
     
-if (!d2.isUserLoggedIn().call()) %7B%0A            retrofit2.Response loginResponse = d2.logIn(%22android%22, %22Android123%22)
+login();%0A        retrofit2.Response reservedValueResponse = reservedValueEndpointCall
 .cal
@@ -2323,28 +2323,24 @@
 all.call();%0A
-    
         asse
@@ -2346,21 +2346,29 @@
 ertThat(
-login
+reservedValue
 Response
@@ -2390,28 +2390,24 @@
 ).isTrue();%0A
-    
     %7D%0A%0A     
@@ -2409,435 +2409,747 @@
     
-    /*  This test won't pass independently of the sync of metadata, as the foreign keys%0A            constraints won't be satisfied.%0A            To run the test, you will need to disable foreign key support in database in%0A            DbOpenHelper.java replacing 'foreign_keys = ON' with 'foreign_keys = OFF' and
+// @Test%0A    public void download_and_persist_reserved_values() throws Exception %7B%0A        login();%0A        retrofit2.Response reservedValueResponse = reservedValueEndpointCall.call();%0A        assertThat(reservedValueResponse.isSuccessful()).isTrue();%0A%0A        Set%3CTrackedEntityAttributeReservedValueModel%3E reservedValues = TrackedEntityAttributeReservedValueStore.create(%0A                databaseAdapter()).selectAll(TrackedEntityAttributeReservedValueModel.factory);%0A%0A        assertThat(reservedValues.size()).isEqualTo(numberToReserve);
 %0A    
+%7D%0A%0A
     
-    uncomment the @Test tag */%0A%0A        retrofit2.Response reservedValueResponse = reservedValueEndpointCall
+private void login() throws Exception %7B%0A        if (!d2.isUserLoggedIn().call()) %7B%0A            retrofit2.Response loginResponse = d2.logIn(RealServerMother.user, RealServerMother.password)
 .cal
@@ -3153,32 +3153,36 @@
 call();%0A        
+    
 assertThat(reser
@@ -3172,37 +3172,29 @@
  assertThat(
-reservedValue
+login
 Response.isS
@@ -3208,32 +3208,42 @@
 ul()).isTrue();%0A
+        %7D%0A
     %7D%0A%0A    @Test

add download_and_persist_reserved_values test