package org.hisp.dhis.android.core.arch.repositories.collection;

import android.support.test.runner.AndroidJUnit4;

import org.hisp.dhis.android.core.arch.repositories.scope.RepositoryScopeItem;
import org.hisp.dhis.android.core.category.CategoryCombo;
import org.hisp.dhis.android.core.data.database.MockIntegrationShould;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.List;

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

@RunWith(AndroidJUnit4.class)
public class IdentifiableCollectionFiltersMockIntegrationShould extends MockIntegrationShould {

    @BeforeClass
    public static void setUpAll() throws Exception {
        downloadMetadata();
    }

    @Test
    public void append_scope_for_multiple_filters() {
        ReadOnlyIdentifiableCollectionRepository<CategoryCombo> repositoryWithUpdatedScope = d2.categoryModule().categoryCombos
                .byName().eq("Combi")
                .byCode().like("cody");
        List<RepositoryScopeItem> scope = repositoryWithUpdatedScope.getScope();

        assertThat(scope.get(0).equals(RepositoryScopeItem.builder().key("name").operator("eq").value("Combi").build()), is(true));
        assertThat(scope.get(1).equals(RepositoryScopeItem.builder().key("code").operator("like").value("cody").build()), is(true));
    }

    @Test
    public void find_objects_with_equal_name() {
        ReadOnlyIdentifiableCollectionRepository<CategoryCombo> repositoryWithUpdatedScope = d2.categoryModule().categoryCombos
                .byName().eq("Births");
        List<CategoryCombo> combos = repositoryWithUpdatedScope.get();
        assertThat(combos.size(), is(1));
        assertThat(combos.get(0).uid(), is("m2jTvAj5kkm"));
    }

    @Test
    public void find_objects_with_equal_code() {
        ReadOnlyIdentifiableCollectionRepository<CategoryCombo> repositoryWithUpdatedScope = d2.categoryModule().categoryCombos
                .byCode().eq("BIRTHS");
        List<CategoryCombo> combos = repositoryWithUpdatedScope.get();
        assertThat(combos.size(), is(1));
        assertThat(combos.get(0).uid(), is("m2jTvAj5kkm"));
    }

    @Test
    public void find_objects_with_equal_uid() {
        ReadOnlyIdentifiableCollectionRepository<CategoryCombo> repositoryWithUpdatedScope = d2.categoryModule().categoryCombos
                .byUid().eq("m2jTvAj5kkm");
        List<CategoryCombo> combos = repositoryWithUpdatedScope.get();
        assertThat(combos.size(), is(1));
        assertThat(combos.get(0).uid(), is("m2jTvAj5kkm"));
    }

    @Test
    public void find_objects_with_equal_display_name() {
        ReadOnlyIdentifiableCollectionRepository<CategoryCombo> repositoryWithUpdatedScope = d2.categoryModule().categoryCombos
                .byDisplayName().eq("Births Display");
        List<CategoryCombo> combos = repositoryWithUpdatedScope.get();
        assertThat(combos.size(), is(1));
        assertThat(combos.get(0).uid(), is("m2jTvAj5kkm"));
    }

    @Test
    public void do_not_find_objects_with_wrong_equal_name() {
        ReadOnlyIdentifiableCollectionRepository<CategoryCombo> repositoryWithUpdatedScope = d2.categoryModule().categoryCombos
                .byName().eq("Deaths");
        List<CategoryCombo> combos = repositoryWithUpdatedScope.get();
        assertThat(combos.isEmpty(), is(true));
    }

    @Test
    public void do_not_find_objects_with_wrong_equal_code() {
        ReadOnlyIdentifiableCollectionRepository<CategoryCombo> repositoryWithUpdatedScope = d2.categoryModule().categoryCombos
                .byCode().eq("DEATHS");
        List<CategoryCombo> combos = repositoryWithUpdatedScope.get();
        assertThat(combos.isEmpty(), is(true));
    }

    @Test
    public void find_objects_with_like_name() {
        ReadOnlyIdentifiableCollectionRepository<CategoryCombo> repositoryWithUpdatedScope = d2.categoryModule().categoryCombos
                .byName().like("%bi%");
        List<CategoryCombo> combos = repositoryWithUpdatedScope.get();
        assertThat(combos.size(), is(1));
        assertThat(combos.get(0).uid(), is("m2jTvAj5kkm"));
    }

    @Test
    public void find_objects_with_like_code() {
        ReadOnlyIdentifiableCollectionRepository<CategoryCombo> repositoryWithUpdatedScope = d2.categoryModule().categoryCombos
                .byCode().like("%bi%");
        List<CategoryCombo> combos = repositoryWithUpdatedScope.get();
        assertThat(combos.size(), is(1));
        assertThat(combos.get(0).uid(), is("m2jTvAj5kkm"));
    }
}
@@ -1777,32 +1777,537 @@
 5kkm%22));%0A    %7D%0A%0A
+    @Test%0A    public void find_objects_with_children_with_equal_name() %7B%0A        ReadOnlyIdentifiableCollectionRepository%3CCategoryCombo%3E repositoryWithUpdatedScope = d2.categoryModule().categoryCombos%0A                .byName().eq(%22Births%22);%0A        List%3CCategoryCombo%3E combos = repositoryWithUpdatedScope.getWithAllChildren();%0A        assertThat(combos.size(), is(1));%0A        assertThat(combos.get(0).uid(), is(%22m2jTvAj5kkm%22));%0A        assertThat(combos.get(0).categories().isEmpty(), is(false));%0A    %7D%0A%0A
     @Test%0A    pu

Add test for getWithChildren