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

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.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.HashSet;
import java.util.Set;

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

@RunWith(AndroidJUnit4.class)
public class DataValueEndpointCallRealIntegrationShould 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 DataValueEndpointCall dataValueCall;

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

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

        Set<String> dataSetUids = new HashSet<>();
        dataSetUids.add("BfMAe6Itzgt");
        dataSetUids.add("TuL8IOPzpHh");

        Set<String> periodIds = new HashSet<>();
        periodIds.add("201712");
        periodIds.add("2017");

        Set<String> orgUnitUids = new HashSet<>();
        orgUnitUids.add("DiszpKrYNg8");

        return DataValueEndpointCall.FACTORY.create(data, dataSetUids, periodIds, orgUnitUids);
    }

    // @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 DataValueEndpointCall and
            OrganisationUnitCall, 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 dataValueResponse = dataValueCall.call();
        assertThat(dataValueResponse.isSuccessful()).isTrue();
    }

    @Test
    public void stub() {
    }
}

@@ -540,56 +540,8 @@
 ion;
-%0Aimport java.util.HashSet;%0Aimport java.util.Set;
 %0A%0Aim
@@ -593,16 +593,266 @@
 ertThat;
+%0Aimport static org.hisp.dhis.android.core.data.datavalue.DataValueUtils.dataSetUids;%0Aimport static org.hisp.dhis.android.core.data.datavalue.DataValueUtils.orgUnitUids;%0Aimport static org.hisp.dhis.android.core.data.datavalue.DataValueUtils.periodIds;
 %0A%0A@RunWi
@@ -1722,346 +1722,8 @@
 );%0A%0A
-        Set%3CString%3E dataSetUids = new HashSet%3C%3E();%0A        dataSetUids.add(%22BfMAe6Itzgt%22);%0A        dataSetUids.add(%22TuL8IOPzpHh%22);%0A%0A        Set%3CString%3E periodIds = new HashSet%3C%3E();%0A        periodIds.add(%22201712%22);%0A        periodIds.add(%222017%22);%0A%0A        Set%3CString%3E orgUnitUids = new HashSet%3C%3E();%0A        orgUnitUids.add(%22DiszpKrYNg8%22);%0A%0A
     

refactor DataValueEndpointCallRealIntegrationShould with new DataValue utils for tests