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.data.server.RealServerMother;
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 java.util.Set;

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;
    private Integer numberToReserve = 5;

    @Before
    @Override
    public void setUp() throws IOException {
        super.setUp();
        d2 = D2Factory.create(RealServerMother.url, 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",
                numberToReserve, organisationUnit);
    }

    // @Test
    public void download_reserved_values() throws Exception {
        login();
        retrofit2.Response reservedValueResponse = reservedValueEndpointCall.call();
        assertThat(reservedValueResponse.isSuccessful()).isTrue();
    }

    // @Test
    public void download_and_persist_reserved_values() throws Exception {
        login();
        retrofit2.Response reservedValueResponse = reservedValueEndpointCall.call();
        assertThat(reservedValueResponse.isSuccessful()).isTrue();

        Set<TrackedEntityAttributeReservedValueModel> reservedValues = TrackedEntityAttributeReservedValueStore.create(
                databaseAdapter()).selectAll(TrackedEntityAttributeReservedValueModel.factory);

        assertThat(reservedValues.size()).isEqualTo(numberToReserve);
    }

    private void login() throws Exception {
        if (!d2.isUserLoggedIn().call()) {
            retrofit2.Response loginResponse = d2.logIn(RealServerMother.user, RealServerMother.password).call();
            assertThat(loginResponse.isSuccessful()).isTrue();
        }
    }

    @Test
    public void stub() {
    }
}
@@ -446,130 +446,8 @@
 it;%0A
-import org.hisp.dhis.android.core.resource.ResourceHandler;%0Aimport org.hisp.dhis.android.core.resource.ResourceStoreImpl;%0A
 impo
@@ -467,16 +467,16 @@
 Before;%0A
+
 import o
@@ -524,16 +524,16 @@
 nWith;%0A%0A
-
 import j
@@ -552,16 +552,39 @@
 eption;%0A
+import java.util.Date;%0A
 import j
@@ -1422,129 +1422,8 @@
 ) %7B%0A
-        ResourceHandler resourceHandler =%0A                new ResourceHandler(new ResourceStoreImpl(databaseAdapter()));%0A
     
@@ -1495,36 +1495,31 @@
 (), 
-resourceHandler, d2.retrofit
+d2.retrofit(), new Date
 ());

fix TrackedEntityAttributeReservedValueEndpointCallRealIntegrationShould