package uk.ac.ebi.quickgo.client.service.loader.presets;

import uk.ac.ebi.quickgo.rest.search.RetrievalException;
import uk.ac.ebi.quickgo.rest.search.request.FilterRequest;
import uk.ac.ebi.quickgo.rest.search.request.converter.ConvertedFilter;
import uk.ac.ebi.quickgo.rest.search.request.converter.RESTFilterConverterFactory;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.collection.IsCollectionWithSize.hasSize;
import static org.hamcrest.core.Is.is;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class RestValuesRetrieverTest {

    private static final String RETRIEVE_KEY = "BogusKey";
    private RestValuesRetriever retriever;

    @Mock private RESTFilterConverterFactory converterFactory;

    private ConvertedFilter<List<String>> convertedValuesFound;
    private ConvertedFilter<List<String>> convertedValuesNotFound;

    @Before
    public void setup() {
        List<String> returnValues = Arrays.asList("Brandish", "Bungalow");
        convertedValuesFound = new ConvertedFilter<>(returnValues);
        convertedValuesNotFound = new ConvertedFilter<>(Collections.emptyList());
        retriever = new RestValuesRetriever(converterFactory);
    }

    @Test
    public void valuesAreRetrieved() {
        when(converterFactory.<List<String>>convert(any(FilterRequest.class))).thenReturn(convertedValuesFound);

        Optional<List<String>> retrievedValues = retriever.retrieveValues(RETRIEVE_KEY);

        assertThat(retrievedValues.isPresent(), is(true));
        assertThat(retrievedValues.get(), hasSize(2));

    }

    @Test
    public void noValueRetrievedButNoExceptionThrown() {
        when(converterFactory.<List<String>>convert(any(FilterRequest.class))).thenReturn(convertedValuesNotFound);

        Optional<List<String>> retrievedValues = retriever.retrieveValues(RETRIEVE_KEY);

        assertThat(retrievedValues.isPresent(), is(true));
        assertThat(retrievedValues.get(), hasSize(0));
    }

    @Test
    public void illegalStateExceptionThrownDuringRetrieval() {
        when(converterFactory.<List<String>>convert(any(FilterRequest.class))).thenThrow(IllegalStateException.class);

        Optional<List<String>> retrievedValues = retriever.retrieveValues(RETRIEVE_KEY);

        assertThat(retrievedValues.isPresent(), is(false));
    }

    @Test
    public void retrievalExceptionThrownDuringRetrieval() {
        when(converterFactory.<List<String>>convert(any(FilterRequest.class))).thenThrow(RetrievalException.class);

        Optional<List<String>> retrievedValues = retriever.retrieveValues(RETRIEVE_KEY);

        assertThat(retrievedValues.isPresent(), is(false));
    }

}
@@ -2293,32 +2293,322 @@
 ize(0));%0A    %7D%0A%0A
+    @Test(expected = IllegalArgumentException.class)%0A    public void nullLookupKeyResultsInIllegalArgumentExceptionThrown() %7B%0A        when(converterFactory.%3CList%3CString%3E%3Econvert(any(FilterRequest.class))).thenReturn(convertedValuesNotFound);%0A%0A        retriever.retrieveValues(null);%0A    %7D%0A%0A
     @Test%0A    pu
@@ -3285,9 +3285,280 @@
 %0A    %7D%0A%0A
+    @Test(expected = NullPointerException.class)%0A    public void anyOtherExceptionsArePastOn() %7B%0A        when(converterFactory.%3CList%3CString%3E%3Econvert(any(FilterRequest.class))).thenThrow(NullPointerException.class);%0A%0A        retriever.retrieveValues(RETRIEVE_KEY);%0A    %7D%0A%0A
 %7D

Add tests for null argument to retrieveValues() and anyOtherExceptionsArePastOn()