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 java.util.Optional;
import java.util.stream.Stream;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import static java.util.stream.Collectors.joining;
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 AnnotationFilter annotationFilter;

    private String multiAssignedBy;

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

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

    @Test
    public void successfullyAddOnlyOneSingleFilter() {
        final List<PrototypeFilter> pfList = annotationFilter.stream().collect(toList());
        assertThat(pfList.get(0).getFilterField(), is(equalTo(AnnotationFields.ASSIGNED_BY)));
        assertThat(pfList, hasSize(1));

        Optional<String> result = pfList.get(0)
                .provideArgStream()
                .findFirst();

        assertThat(result.isPresent(), is(true));
        assertThat(result.get(), is(equalTo(UNI_PROT)));
    }

    @Test
    public void successfullyAddMultiFilter() {
        final List<PrototypeFilter> pfList = annotationFilter.stream().collect(toList());
        assertThat(pfList, hasSize(1));
        assertThat(pfList.get(0).getFilterField(), is(equalTo(AnnotationFields.ASSIGNED_BY)));

        Optional<String> result1 = pfList.get(0)
                .provideArgStream()
                .findFirst();
        assertThat(result1.get(), is(equalTo(UNI_PROT)));

        Optional<String> result2 = pfList.get(0)
                .provideArgStream()
                .filter(a -> a.equals(ASPGD))
                .findFirst();
        assertThat(result2.get(), is(equalTo(ASPGD)));

        long countASPGD = pfList.get(0)
                .provideArgStream()
                .filter(a -> a.equals(ASPGD))
                .count();
        assertThat(countASPGD, is(1l));
    }

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

    @Test
    public void successfullySetPageAndLimitValues() {
        annotationFilter.setPage(4);
        annotationFilter.setLimit(15);
        assertThat(annotationFilter.getPage(), equalTo(4));
        assertThat(annotationFilter.getLimit(), equalTo(15));
    }
}
@@ -951,57 +951,8 @@
 D%22;%0A
-%0A    private AnnotationFilter annotationFilter;%0A%0A
     
@@ -1023,62 +1023,10 @@
 Up()
- 
 %7B%0A
-        annotationFilter = new AnnotationFilter();%0A
     
@@ -1059,17 +1059,13 @@
 PROT
- + %22,%22 + 
++%22,%22+
 ASPG
@@ -1119,17 +1119,16 @@
 n thrown
- 
 = Expect
@@ -1202,34 +1202,152 @@
 neSingleFilter()
- 
 %7B
+%0A%0A        AnnotationFilter annotationFilter = new AnnotationFilter();%0A        annotationFilter.setAssignedBy(UNI_PROT);
 %0A        final L
@@ -1778,32 +1778,33 @@
 PROT)));%0A    %7D%0A%0A
+%0A
     @Test%0A    pu
@@ -1841,18 +1841,143 @@
 Filter()
- 
 %7B
+%0A%0A        AnnotationFilter annotationFilter = new AnnotationFilter();%0A        annotationFilter.setAssignedBy(multiAssignedBy);
 %0A       
@@ -2593,17 +2593,16 @@
 %0A       
- 
 long cou
@@ -2772,32 +2772,34 @@
 is(1l));%0A    %7D%0A%0A
+%0A%0A
     @Test%0A    pu
@@ -2845,18 +2845,85 @@
 orrect()
- 
 %7B
+%0A        AnnotationFilter annotationFilter = new AnnotationFilter();
 %0A       
@@ -3105,18 +3105,85 @@
 Values()
- 
 %7B
+%0A        AnnotationFilter annotationFilter = new AnnotationFilter();
 %0A       
@@ -3379,9 +3379,12 @@
 ;%0A    %7D%0A
-%7D
+%0A%0A%7D%0A

Revert back to previous code version.