package sagan.search.support;

import com.google.gson.Gson;
import io.searchbox.action.Action;
import io.searchbox.client.JestClient;
import io.searchbox.client.JestResult;
import io.searchbox.core.Index;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentMatcher;
import org.springframework.data.domain.Pageable;
import sagan.search.SearchException;
import sagan.search.types.SearchEntry;
import sagan.search.types.SearchType;

import java.util.Collections;

import static org.mockito.BDDMockito.given;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.argThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

public class SearchServiceTests {

    private JestClient jestClient = mock(JestClient.class);
    private SearchService searchService = new SearchService(jestClient, mock(SearchResultParser.class), new Gson());
    private SearchEntry entry;

    @Before
    public void setUp() throws Exception {
        entry = SearchEntryBuilder.entry().path("/some/path")
                .title("This week in Spring").rawContent("raw content")
                .summary("Html summary").publishAt("2013-01-01 10:00")
                .blog();
    }

    @SuppressWarnings("unchecked")
    public void throwsException() throws Exception {
        given(jestClient.execute(any(Action.class))).willThrow(Exception.class);
    }

    @Test(expected = SearchException.class)
    public void saveToIndexExceptionHandling() throws Exception {
        throwsException();
        searchService.saveToIndex(entry);
    }

    @Test(expected = SearchException.class)
    public void searchExceptionHandling() throws Exception {
        throwsException();
        searchService.search("foo", mock(Pageable.class), Collections.<String> emptyList());
    }

    @Test(expected = SearchException.class)
    public void removeFromIndexExceptionHandling() throws Exception {
        throwsException();
        searchService.removeFromIndex(entry);
    }

    @Test
    public void usesTheSearchEntriesType() throws Exception {
        given(jestClient.execute(any(Action.class))).willReturn(mock(JestResult.class));
        searchService.saveToIndex(entry);
        verify(jestClient).execute(argThat(new ArgumentMatcher<Action>() {
            @Override
            public boolean matches(Object item) {
                Index action = (Index) item;
                return action.getType().equals(SearchType.BLOG_POST.toString());
            }
        }));
    }

    @Test
    public void handlesNullFilters() throws Exception {
        given(jestClient.execute(any(Action.class))).willReturn(mock(JestResult.class));
        searchService.search("foo", mock(Pageable.class), null);
    }
}

@@ -35,59 +35,32 @@
 ort 
-com.google.gson.Gson;%0Aimport io.searchbox.action.Ac
+sagan.search.SearchExcep
 tion
@@ -72,38 +72,38 @@
 ort 
-io
+sagan
 .search
-box.client.JestClient
+.types.SearchEntry
 ;%0Aim
@@ -111,71 +111,70 @@
 ort 
-io
+sagan
 .search
-box.client.JestResult;%0Aimport io.searchbox.core.Index;
+.types.SearchType;%0A%0Aimport java.util.Collections;%0A
 %0Aimp
@@ -254,16 +254,17 @@
 atcher;%0A
+%0A
 import o
@@ -308,39 +308,38 @@
 le;%0A
+%0A
 import 
-sagan
+io
 .search
-.SearchExcep
+box.action.Ac
 tion
@@ -351,38 +351,38 @@
 ort 
-sagan
+io
 .search
-.types.SearchEntry
+box.client.JestClient
 ;%0Aim
@@ -390,68 +390,100 @@
 ort 
-sagan
+io
 .search
-.types.SearchType;%0A%0Aimport java.util.Collections
+box.client.JestResult;%0Aimport io.searchbox.core.Index;%0A%0Aimport com.google.gson.Gson
 ;%0A%0Ai
@@ -560,19 +560,17 @@
 atchers.
-any
+*
 ;%0Aimport
@@ -594,105 +594,16 @@
 to.M
-atchers.argThat;%0Aimport static org.mockito.Mockito.mock;%0Aimport static org.mockito.Mockito.verify
+ockito.*
 ;%0A%0Ap
@@ -1254,32 +1254,39 @@
 ent.execute(any(
+Generic
 Action.class))).
@@ -2033,32 +2033,39 @@
 ent.execute(any(
+Generic
 Action.class))).
@@ -2205,16 +2205,23 @@
 Matcher%3C
+Generic
 Action%3E(
@@ -2559,16 +2559,23 @@
 ute(any(
+Generic
 Action.c
@@ -2689,10 +2689,82 @@
 ;%0A    %7D%0A
+    %0A    private interface GenericAction extends Action%3CJestResult%3E %7B%7D %0A
 %7D%0A

Make test compile in Eclipse