package uk.ac.ebi.quickgo.annotation.service.statistics;

import uk.ac.ebi.quickgo.annotation.model.Annotation;
import uk.ac.ebi.quickgo.annotation.model.AnnotationRequest;
import uk.ac.ebi.quickgo.rest.search.SearchService;
import uk.ac.ebi.quickgo.rest.search.request.converter.FilterConverterFactory;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;

import static org.mockito.Mockito.when;

/**
 * Tests the behaviour of the {@link AnnotationStatisticsService} class.
 */
@RunWith(MockitoJUnitRunner.class)
public class AnnotationStatisticsServiceTest {
    @Rule
    public ExpectedException thrown = ExpectedException.none();

    private AnnotationStatisticsService statsService;

    @Mock
    private FilterConverterFactory filterFactoryMock;

    @Mock
    private SearchService<Annotation> searchServiceMock;

    @Mock
    private StatsConverter statsConverterMock;

    @Mock
    private RequiredStatistics requiredStatistics;

    @Before
    public void setUp() throws Exception {
        statsService = new AnnotationStatisticsService(filterFactoryMock, searchServiceMock,
                statsConverterMock, requiredStatistics);
    }

    @Test
    public void nullFilterFactoryThrowsExceptionInConstructor() throws Exception {
        thrown.expect(IllegalArgumentException.class);
        thrown.expectMessage("Filter factory cannot be null");

        statsService = new AnnotationStatisticsService(null, searchServiceMock,
                statsConverterMock, requiredStatistics);
    }

    @Test
    public void nullSearchServiceThrowsExceptionInConstructor() throws Exception {
        thrown.expect(IllegalArgumentException.class);
        thrown.expectMessage("Search service cannot be null");

        statsService = new AnnotationStatisticsService(filterFactoryMock, null,
                statsConverterMock, requiredStatistics);
    }

    @Test
    public void nullStatsConverterThrowsExceptionInConstructor() throws Exception {
        thrown.expect(IllegalArgumentException.class);
        thrown.expectMessage("Stats request converter cannot be null");

        statsService = new AnnotationStatisticsService(filterFactoryMock, searchServiceMock,
                null, requiredStatistics);
    }

    @Test
    public void nullRequiredStatsThrowsExceptionInConstructor() throws Exception {
        thrown.expect(IllegalArgumentException.class);
        thrown.expectMessage("Required stats cannot be null");

        statsService = new AnnotationStatisticsService(filterFactoryMock, searchServiceMock,
                statsConverterMock, null);
    }

    @Test
    public void calculatingNullAnnotationRequestThrowsException() throws Exception {
        thrown.expect(IllegalArgumentException.class);
        thrown.expectMessage("Annotation request cannot be null");

        statsService.calculate(null);
    }

    @Test
    public void calculatingStatsWithNullRequiredStatsListThrowsException() throws Exception {
        thrown.expect(IllegalStateException.class);
        thrown.expectMessage("Required stats list cannot be null");

        when(requiredStatistics.getStats()).thenReturn(null);
        statsService = new AnnotationStatisticsService(filterFactoryMock, searchServiceMock,
                statsConverterMock, requiredStatistics);
        AnnotationRequest request = Mockito.mock(AnnotationRequest.class);

        statsService.calculate(request);
    }
}
@@ -162,24 +162,96 @@
 ionRequest;%0A
+import uk.ac.ebi.quickgo.annotation.service.search.SearchServiceConfig;%0A
 import uk.ac
@@ -290,16 +290,16 @@
 ervice;%0A
-
 import u
@@ -1204,16 +1204,106 @@
 stics;%0A%0A
+    @Mock%0A    private SearchServiceConfig.StatisticsSearchConfig statisticsSearchConfig;%0A%0A
     @Bef
@@ -1488,32 +1488,56 @@
 quiredStatistics
+, statisticsSearchConfig
 );%0A    %7D%0A%0A    @T
@@ -1868,32 +1868,56 @@
 quiredStatistics
+, statisticsSearchConfig
 );%0A    %7D%0A%0A    @T
@@ -2248,32 +2248,56 @@
 quiredStatistics
+, statisticsSearchConfig
 );%0A    %7D%0A%0A    @T
@@ -2641,24 +2641,48 @@
 edStatistics
+, statisticsSearchConfig
 );%0A    %7D%0A%0A  
@@ -3015,24 +3015,476 @@
 nverterMock,
+ null, statisticsSearchConfig);%0A    %7D%0A%0A    @Test%0A    public void nullStatisticsConfigThrowsExceptionInConstructor() throws Exception %7B%0A        thrown.expect(IllegalArgumentException.class);%0A        thrown.expectMessage(%22Statistics configuration cannot be null.%22);%0A%0A        statsService = new AnnotationStatisticsService(filterFactoryMock, searchServiceMock,%0A                                                       statsConverterMock, requiredStatistics,
  null);%0A    
@@ -4183,16 +4183,40 @@
 atistics
+, statisticsSearchConfig
 );%0A     
@@ -4333,9 +4333,11 @@
 ;%0A    %7D%0A
-%7D
+%0A%7D%0A

Add SearchServiceConfig.StatisticsSearchConfig