package org.atlasapi.content;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.mockito.Matchers.anyList;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.io.IOException;
import java.util.Optional;

import org.atlasapi.EsSchema;
import org.atlasapi.channel.ChannelGroupResolver;
import org.atlasapi.criteria.AttributeQuerySet;
import org.atlasapi.entity.Id;
import org.atlasapi.media.entity.Publisher;
import org.atlasapi.util.CassandraSecondaryIndex;
import org.atlasapi.util.ElasticSearchHelper;
import org.atlasapi.util.SecondaryIndex;
import org.elasticsearch.client.Client;
import org.junit.Before;
import org.junit.Test;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.metabroadcast.common.query.Selection;

public class PseudoEquivalentContentIndexTest {

    private SecondaryIndex equivIndex = mock(CassandraSecondaryIndex.class);
    private ContentIndex contentIndex;
    private Client esNode = ElasticSearchHelper.testNode().client();

    @Before
    public void setUp() {
        EsUnequivalentContentIndex delegate = new EsUnequivalentContentIndex(
                esNode,
                EsSchema.CONTENT_INDEX,
                new NoOpContentResolver(),
                mock(ChannelGroupResolver.class),
                equivIndex,
                60000
        );
        delegate.startAsync().awaitRunning();
        contentIndex = new PseudoEquivalentContentIndex(delegate);
    }

    @Test
    public void testQuery() throws Exception {
        Item item1 = new Item(Id.valueOf(1l), Publisher.METABROADCAST);
        Item item2 = new Item(Id.valueOf(2l), Publisher.METABROADCAST);
        Item item3 = new Item(Id.valueOf(3l), Publisher.METABROADCAST);

        when(equivIndex.lookup(anyList()))
                .thenReturn(Futures.immediateFuture(ImmutableMap.of(1l, 2l)))
                .thenReturn(Futures.immediateFuture(ImmutableMap.of(2l, 2l)))
                .thenReturn(Futures.immediateFuture(ImmutableMap.of(3l, 3l)))
                .thenReturn(
                        Futures.immediateFuture(
                            ImmutableMap.of(
                                    1l, 2l,
                                    2l, 2l,
                                    3l, 3l
                            )
                )
        );


        indexAndRefresh(item1, item2, item3);

        ListenableFuture<IndexQueryResult> result = contentIndex.query(
                new AttributeQuerySet(ImmutableList.of()),
                ImmutableList.of(Publisher.METABROADCAST),
                Selection.all(),
                Optional.empty()
        );

        ImmutableList<Id> ids = ImmutableList.copyOf(Futures.get(result, IOException.class).getIds());

        assertThat(ids, is(ImmutableList.of(Id.valueOf(2l), Id.valueOf(3l))));
    }

    private void indexAndRefresh(Content... contents) throws IndexException {
        for (Content content : contents) {
            contentIndex.index(content);
        }
        ElasticSearchHelper.refresh(esNode);
    }
}
@@ -296,16 +296,64 @@
 ptional;
+%0Aimport java.util.concurrent.ExecutionException;
 %0A%0Aimport
@@ -678,32 +678,136 @@
 SecondaryIndex;%0A
+import org.elasticsearch.action.get.GetRequestBuilder;%0Aimport org.elasticsearch.action.get.GetResponse;%0A
 import org.elast
@@ -3189,24 +3189,1545 @@
 )));%0A    %7D%0A%0A
+    @Test%0A    public void testRemovesNonActivelyPublishedContent() throws IndexException, ExecutionException, InterruptedException %7B%0A        when(equivIndex.lookup(anyList()))%0A                .thenReturn(Futures.immediateFuture(ImmutableMap.of(10l, 10l)))%0A                .thenReturn(Futures.immediateFuture(ImmutableMap.of(20l, 20l)))%0A                .thenReturn(Futures.immediateFuture(ImmutableMap.of(3l, 3l)))%0A                .thenReturn(%0A                        Futures.immediateFuture(%0A                                ImmutableMap.of(%0A                                        1l, 2l,%0A                                        2l, 2l,%0A                                        3l, 3l%0A                                )%0A                        )%0A                );%0A%0A        Item item = new Item(Id.valueOf(10l), Publisher.METABROADCAST);%0A        indexAndRefresh(item);%0A        item.setActivelyPublished(false);%0A        indexAndRefresh(item);%0A        GetResponse resp = esNode.prepareGet().setIndex(%22content%22).setId(%2210%22).execute().get();%0A        assertThat(resp.isExists(), is(false));%0A%0A        Episode episode = new Episode(Id.valueOf(20l), Publisher.METABROADCAST);%0A        episode.setContainerRef(new BrandRef(Id.valueOf(30l), Publisher.METABROADCAST));%0A        indexAndRefresh(episode);%0A        episode.setActivelyPublished(false);%0A        indexAndRefresh(episode);%0A        GetResponse resp2 = esNode.prepareGet().setIndex(%22content%22).setId(%2220%22).execute().get();%0A        assertThat(resp2.isExists(), is(false));%0A    %7D%0A%0A
     private 

Test to ensure content that is not actively published is removed