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


import org.hisp.dhis.android.core.D2;
import org.hisp.dhis.android.core.calls.Call;
import org.hisp.dhis.android.core.common.D2Factory;
import org.hisp.dhis.android.core.common.IdentifiableObjectStore;
import org.hisp.dhis.android.core.common.LinkModelStore;
import org.hisp.dhis.android.core.data.database.AbsStoreTestCase;
import org.hisp.dhis.android.core.data.server.RealServerMother;
import org.junit.Before;

import java.io.IOException;
import java.util.List;

import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertTrue;

public class CategoryComboEndpointCallRealIntegrationShould extends AbsStoreTestCase {

    private D2 d2;

    @Override
    @Before
    public void setUp() throws IOException {
        super.setUp();
        d2 = D2Factory.create(RealServerMother.url, databaseAdapter());
    }

    //@Test
    public void download_categories_combos_and_relatives() throws Exception {

        d2.logIn(RealServerMother.user, RealServerMother.password).call();

        downloadCategories();

        assertNotCombosInDB();
        assertTrue(getCategoryCategoryComboLinkModels().isEmpty());

        Call<List<CategoryCombo>> categoryComboEndpointCall =
                CategoryComboEndpointCall.factory(d2.retrofit()).create(getGenericCallData(d2));
        List<CategoryCombo> categoryCombos = categoryComboEndpointCall.call();

        assertFalse(categoryCombos.isEmpty());

        assertDataIsProperlyParsedAndInsertedInTheDB();
    }

    private void assertDataIsProperlyParsedAndInsertedInTheDB() {
        assertThereAreCombosInDB();
        assertFalse(getCategoryCategoryComboLinkModels().isEmpty());
        assertThereAreCategoryOptionCombosInDB();
        assertThereAreCategoriesInDB();
    }

    private void downloadCategories() throws Exception {
        CategoryEndpointCall.factory(d2.retrofit()).create(getGenericCallData(d2)).call();
    }

    private void assertNotCombosInDB() {
        IdentifiableObjectStore<CategoryCombo> categoryComboStore = CategoryComboStore.create(databaseAdapter());
        List<CategoryCombo> categoryCombos = categoryComboStore.selectAll();
        assertTrue(categoryCombos.isEmpty());
    }

    private void assertThereAreCombosInDB() {
        IdentifiableObjectStore<CategoryCombo> categoryComboStore = CategoryComboStore.create(databaseAdapter());
        List<CategoryCombo> categoryCombos = categoryComboStore.selectAll();
        assertTrue(categoryCombos.size() > 0);
    }

    private List<CategoryCategoryComboLinkModel> getCategoryCategoryComboLinkModels() {
        LinkModelStore<CategoryCategoryComboLinkModel>
                categoryCategoryComboLinkStore = CategoryCategoryComboLinkStore.create(databaseAdapter());
        return categoryCategoryComboLinkStore.selectAll();
    }

    private void assertThereAreCategoryOptionCombosInDB() {
        IdentifiableObjectStore<CategoryOptionCombo> categoryOptionComboStore = CategoryOptionComboStoreImpl.create(databaseAdapter());
        List<CategoryOptionCombo> categoryOptionCombos = categoryOptionComboStore.selectAll();
        assertTrue(categoryOptionCombos.size() > 0);
    }

    private void assertThereAreCategoriesInDB() {
        IdentifiableObjectStore<CategoryOption> categoryOptionStore = CategoryOptionStore.create(databaseAdapter());
        List<String> categoryOptionUids = categoryOptionStore.selectUids();
        assertTrue(categoryOptionUids.size() > 0);
    }
}

@@ -40,16 +40,57 @@
 gory;%0A%0A%0A
+import com.google.common.collect.Lists;%0A%0A
 import o
@@ -119,16 +119,16 @@
 ore.D2;%0A
-
 import o
@@ -524,16 +524,42 @@
 eption;%0A
+import java.util.HashSet;%0A
 import j
@@ -1045,17 +1045,16 @@
 ption %7B%0A
-%0A
         
@@ -1130,34 +1130,84 @@
        d
-ownloadCategories(
+2.databaseAdapter().database().setForeignKeyConstraintsEnabled(false
 );%0A%0A    
@@ -1406,38 +1406,23 @@
 intCall.
-factory(d2.retrofit())
+FACTORY
 .create(
@@ -1443,16 +1443,90 @@
 Data(d2)
+,%0A                        new HashSet%3C%3E(Lists.newArrayList(%22bjDvmb4bfuf%22))
 );%0A     
@@ -1640,32 +1640,63 @@
 os.isEmpty());%0A%0A
+        downloadCategories();%0A%0A
         assertDa
@@ -2100,30 +2100,15 @@
 all.
-factory(d2.retrofit())
+FACTORY
 .cre
@@ -2133,16 +2133,82 @@
 Data(d2)
+,%0A                new HashSet%3C%3E(Lists.newArrayList(%22GLevLNI9wkl%22))
 ).call()

Fix CategoryComboEndpointCallRealIntegrationShould