package uk.ac.ebi.quickgo.annotation.model;

import uk.ac.ebi.quickgo.annotation.common.document.AnnotationFields;
import uk.ac.ebi.quickgo.rest.search.query.PrototypeFilter;

import java.util.List;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import static java.util.stream.Collectors.toList;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsEqual.equalTo;

/**
 *
 * Test methods and structure of AnnotationFilter
 *
 * @author Tony Wardell
 * Date: 29/04/2016
 * Time: 11:25
 * Created with IntelliJ IDEA.
 */
public class AnnotationFilterTest {

    private static final String UNI_PROT = "UniProt";
    private static final String ASPGD = "ASPGD";
    private String multiAssignedBy;

    @Before
    public void setUp(){
        multiAssignedBy = UNI_PROT+","+ASPGD;
    }

    @Rule
    public ExpectedException thrown= ExpectedException.none();

    @Test
    public void successfullyAddOnlyOneSingleFilter(){

        AnnotationFilter annotationFilter = new AnnotationFilter();
        annotationFilter.setAssignedBy(UNI_PROT);
        List<PrototypeFilter> filterList = annotationFilter.stream().collect(toList());
        assertThat(filterList, hasSize(1));
        assertThat(filterList.get(0).getFilterField(), is(equalTo(AnnotationFields.ASSIGNED_BY)));
        assertThat(filterList.get(0).getArgs().get(0), is(equalTo(UNI_PROT)));
    }


    @Test
    public void successfullyAddMultiFilter(){

        AnnotationFilter annotationFilter = new AnnotationFilter();
        annotationFilter.setAssignedBy(multiAssignedBy);
        List<PrototypeFilter> filterList = annotationFilter.stream().collect(toList());
        assertThat(filterList, hasSize(1));
        assertThat(filterList.get(0).getFilterField(), is(equalTo(AnnotationFields.ASSIGNED_BY)));
        assertThat(filterList.get(0).getArgs().get(0), is(equalTo(UNI_PROT)));
        assertThat(filterList.get(0).getArgs().get(1), is(equalTo(ASPGD)));
    }



    @Test
    public void defaultPageAndLimitValuesAreCorrect(){
        AnnotationFilter annotationFilter = new AnnotationFilter();
        assertThat(annotationFilter.getPage(), equalTo(1));
        assertThat(annotationFilter.getLimit(), equalTo(25));
    }

    @Test
    public void successfullySetPageAndLimitValues(){
        AnnotationFilter annotationFilter = new AnnotationFilter();
        annotationFilter.setPage(4);
        annotationFilter.setLimit(15);
        assertThat(annotationFilter.getPage(), equalTo(4));
        assertThat(annotationFilter.getLimit(), equalTo(15));
    }


    @Test
    public void exceptionThrownWhenAsForMoreResultsThanLimit(){
        thrown.expect(IllegalArgumentException.class);
        AnnotationFilter annotationFilter = new AnnotationFilter();
        annotationFilter.setLimit(200);
    }

}

@@ -188,24 +188,83 @@
 .util.List;%0A
+import java.util.Optional;%0Aimport java.util.stream.Stream;%0A
 import org.j
@@ -365,16 +365,67 @@
 ption;%0A%0A
+import static java.util.stream.Collectors.joining;%0A
 import s
@@ -1331,32 +1331,38 @@
 _PROT);%0A        
+final 
 List%3CPrototypeFi
@@ -1359,38 +1359,34 @@
 rototypeFilter%3E 
+p
 f
-ilter
 List = annotatio
@@ -1444,58 +1444,10 @@
 hat(
+p
 f
-ilterList, hasSize(1));%0A        assertThat(filter
 List
@@ -1539,41 +1539,225 @@
 hat(
-filterList.get(0).getArgs()
+pfList, hasSize(1));%0A%0A        Optional%3CString%3E result = pfList.get(0)%0A                .provideArgStream()%0A                .findFirst();%0A%0A        assertThat(result.isPresent(), is(true));%0A        assertThat(result
 .get(
-0
 ), i
@@ -1977,16 +1977,22 @@
         
+final 
 List%3CPro
@@ -2005,22 +2005,18 @@
 Filter%3E 
+p
 f
-ilter
 List = a
@@ -2074,30 +2074,26 @@
  assertThat(
+p
 f
-ilter
 List, hasSiz
@@ -2110,38 +2110,34 @@
      assertThat(
+p
 f
-ilter
 List.get(0).getF
@@ -2190,32 +2190,33 @@
 GNED_BY)));%0A
+%0A
         
 assertThat(f
@@ -2207,52 +2207,146 @@
     
-assertThat(filterList.get(0).getArgs()
+Optional%3CString%3E result1 = pfList.get(0)%0A                .provideArgStream()%0A                .findFirst();%0A        assertThat(result1
 .get(
-0
 ), i
@@ -2364,32 +2364,33 @@
 NI_PROT)));%0A
+%0A
         
 assertThat(f
@@ -2381,52 +2381,192 @@
     
-assertThat(filterList.get(0).getArgs()
+Optional%3CString%3E result2 = pfList.get(0)%0A                .provideArgStream()%0A                .filter(a -%3E a.equals(ASPGD))%0A                .findFirst();%0A        assertThat(result2
 .get(
-1
 ), i
@@ -2580,24 +2580,212 @@
 To(ASPGD)));
+%0A%0A       long countASPGD = pfList.get(0)%0A                .provideArgStream()%0A                .filter(a -%3E a.equals(ASPGD))%0A                .count();%0A        assertThat(countASPGD, is(1l));
 %0A    %7D%0A%0A%0A%0A  

Change test to use stream of data from AnnotationFilter, not have access to lists.