package com.utynote.components.search;

import android.support.annotation.NonNull;

import com.utynote.test.annotations.HttpResponse;
import com.utynote.test.core.AnnotationHandler;
import com.utynote.test.dependencies.SearchComponentFactory;
import com.utynote.test.utils.Utils;

import org.hamcrest.Description;
import org.hamcrest.TypeSafeMatcher;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

import javax.inject.Inject;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.emptyIterable;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.hasSize;

public class SearchPresenterTest {
    @Rule public final AnnotationHandler rule = new AnnotationHandler();
    @Inject public SearchPresenter presenter;

    private FakeSearchView view;

    @Before
    public void setup() {
        SearchComponentFactory.create().inject(this);
        view = new FakeSearchView();
    }

    @HttpResponse(path = "search/geojson/berlin")
    @Test
    public void whenAttached_search_hasExpectedResult() {
        presenter.attach(view);

        presenter.search("Berlin");

        assertThat(view.errors, emptyIterable());
        assertThat(view.results, hasSize(1));
        assertThat(view.results.get(0), hasItem(new SearchItemMatcher(SearchItemModel.getBuilder()
                .withPrimaryTitle("Berlin")
                .withSecondaryTitle("")
                .withPrimarySubtitle("Germany")
                .withSecondarySubtitle("52.52045,13.40732")
                .build())));
    }

    @HttpResponse(path = "search/geojson/berlin")
    @Test
    public void whenDetached_search_hasNoResults() {
        presenter.attach(view);
        presenter.detach();

        presenter.search("Berlin");

        assertThat(view.results, emptyIterable());
        assertThat(view.errors, emptyIterable());
    }

    private final class SearchItemMatcher extends TypeSafeMatcher<SearchItemModel> {
        @NonNull private final SearchItemModel mModel;

        SearchItemMatcher(@NonNull SearchItemModel model) {
            mModel = model;
        }

        @Override
        protected boolean matchesSafely(SearchItemModel item) {
            return Utils.equals(mModel.primaryTitle, item.primaryTitle) &&
                   Utils.equals(mModel.secondaryTitle, item.secondaryTitle) &&
                   Utils.equals(mModel.primarySubtitle, item.primarySubtitle) &&
                   Utils.equals(mModel.secondarySubtitle, item.secondarySubtitle);
        }

        @Override
        public void describeTo(Description description) {
            description.appendText("Search item with title: " + mModel.primaryTitle);
        }
    }
}

@@ -1301,29 +1301,13 @@
 tem(
-new SearchItemM
+m
 atch
-er
 (Sea
@@ -1887,53 +1887,28 @@
     
-private final class SearchItemMatcher extends
+@NonNull%0A    private
  Typ
@@ -1937,26 +1937,22 @@
 mModel%3E 
-%7B%0A        
+match(
 @NonNull
@@ -1952,24 +1952,16 @@
 NonNull 
-private 
 final Se
@@ -1979,15 +1979,15 @@
 el m
-M
 odel
-;%0A
+) %7B
 %0A   
@@ -1995,35 +1995,35 @@
     
-SearchItemMatcher(@NonNull 
+return new TypeSafeMatcher%3C
 Sear
@@ -2029,38 +2029,34 @@
 rchItemModel
- model
+%3E(
 ) %7B%0A    
         mMod
@@ -2047,43 +2047,8 @@
     
-        mModel = model;%0A        %7D%0A%0A
     
@@ -2053,32 +2053,36 @@
       @Override%0A
+    
         protecte
@@ -2137,24 +2137,28 @@
             
+  
+  
 return Utils
@@ -2158,33 +2158,32 @@
 n Utils.equals(m
-M
 odel.primaryTitl
@@ -2218,32 +2218,37 @@
                 
+    
+ 
 Utils.equals(mMo
@@ -2237,33 +2237,32 @@
   Utils.equals(m
-M
 odel.secondaryTi
@@ -2282,32 +2282,37 @@
 ondaryTitle) &&%0A
+     
                 
@@ -2324,25 +2324,24 @@
 ils.equals(m
-M
 odel.primary
@@ -2386,32 +2386,37 @@
                 
+     
 Utils.equals(mMo
@@ -2413,17 +2413,16 @@
 equals(m
-M
 odel.sec
@@ -2466,24 +2466,28 @@
 e);%0A        
+    
 %7D%0A%0A        @
@@ -2477,32 +2477,36 @@
      %7D%0A%0A        
+  
+  
 @Override%0A      
@@ -2491,32 +2491,36 @@
       @Override%0A
+    
         public v
@@ -2573,16 +2573,20 @@
         
+ 
+   
 descript
@@ -2630,17 +2630,16 @@
 e: %22 + m
-M
 odel.pri
@@ -2658,17 +2658,32 @@
         
+  
-%7D
+  %7D%0A        %7D;
 %0A    %7D%0A%7D

Use anonymous class instead of inner in test