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

import uk.ac.ebi.quickgo.client.model.presets.CompositePreset;
import uk.ac.ebi.quickgo.client.model.presets.PresetItem;
import uk.ac.ebi.quickgo.client.service.loader.presets.assignedby.AssignedByPresetsConfig;
import uk.ac.ebi.quickgo.client.service.loader.presets.qualifier.QualifierPresetsConfig;

import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
import org.hamcrest.collection.IsIterableContainingInOrder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.test.JobLauncherTestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationContextLoader;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.hasSize;
import static org.springframework.test.util.MatcherAssertionErrors.assertThat;

/**
 * Tests the population of the preset relevancy information despite a failure in REST communication to an
 * end-point that provides the relevancy ordering.
 *
 * Created 31/08/16
 * @author Edd
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(
        classes = {PresetsConfig.class, MockPresetDataConfig.class, JobTestRunnerConfig.class},
        loader = SpringApplicationContextLoader.class)
@WebAppConfiguration
@ActiveProfiles(profiles = {MockPresetDataConfig.FAILED_FETCHING, MockPresetDataConfig.NO_SEARCH_ATTRIBUTES})
public class PresetsFailedRelevancyFetchingIT {
    @Autowired
    private JobLauncherTestUtils jobLauncherTestUtils;

    @Autowired
    private CompositePreset presets;

    @Test
    public void loadDefaultAssignedByPresetsAfterFailedRESTInfoFetching() throws Exception {
        assertThat(presets.getAssignedBy(), hasSize(0));

        JobExecution jobExecution =
                jobLauncherTestUtils.launchStep(AssignedByPresetsConfig.ASSIGNED_BY_LOADING_STEP_NAME);
        BatchStatus status = jobExecution.getStatus();

        assertThat(status, is(BatchStatus.COMPLETED));
        assertThat(
                extractPresetValues(presets.getAssignedBy(), p -> p.getProperty(PresetItem.Property.NAME.getKey())),
                hasSize(24));
    }

    @Test
    public void loadDefaultQualifierPresetsAfterFailedRESTInfoFetching() throws Exception {
        assertThat(presets.getQualifiers(), hasSize(0));

        JobExecution jobExecution =
                jobLauncherTestUtils.launchStep(QualifierPresetsConfig.QUALIFIER_LOADING_STEP_NAME);
        BatchStatus status = jobExecution.getStatus();

        assertThat(status, is(BatchStatus.COMPLETED));
        assertThat(
                extractPresetValues(presets.getQualifiers(), p -> p.getProperty(PresetItem.Property.NAME.getKey())),
                is(empty()));
    }

    private <T> List<T> extractPresetValues(List<PresetItem> presets, Function<PresetItem, T> extractor) {
        return presets.stream().map(extractor).collect(Collectors.toList());
    }

}

@@ -351,16 +351,103 @@
 sConfig;
+%0Aimport uk.ac.ebi.quickgo.client.service.loader.presets.withFrom.WithFromPresetsConfig;
 %0A%0Aimport
@@ -539,68 +539,8 @@
 rs;%0A
-import org.hamcrest.collection.IsIterableContainingInOrder;%0A
 impo
@@ -2070,24 +2070,589 @@
 t presets;%0A%0A
+    @Test%0A    public void loadDefaultQualifierPresetsAfterFailedRESTInfoFetching() %7B%0A        assertThat(presets.getQualifiers(), hasSize(0));%0A%0A        JobExecution jobExecution =%0A                jobLauncherTestUtils.launchStep(QualifierPresetsConfig.QUALIFIER_LOADING_STEP_NAME);%0A        BatchStatus status = jobExecution.getStatus();%0A%0A        assertThat(status, is(BatchStatus.COMPLETED));%0A        assertThat(%0A                extractPresetValues(presets.getQualifiers(), p -%3E p.getProperty(PresetItem.Property.NAME.getKey())),%0A                is(empty()));%0A    %7D%0A%0A
     @Test%0A  
@@ -2722,33 +2722,16 @@
 tching()
- throws Exception
  %7B%0A     
@@ -3191,18 +3191,18 @@
     
-hasSize(24
+is(empty()
 ));%0A
@@ -3241,33 +3241,32 @@
  loadDefault
-Qualifier
+WithFrom
 PresetsAfter
@@ -3293,25 +3293,8 @@
 ng()
- throws Exception
  %7B%0A 
@@ -3318,34 +3318,32 @@
 (presets.get
-Qualifiers
+WithFrom
 (), hasSize(
@@ -3428,33 +3428,32 @@
 .launchStep(
-Qualifier
+WithFrom
 PresetsConfi
@@ -3450,33 +3450,36 @@
 esetsConfig.
-QUALIFIER
+WITH_FROM_DB
 _LOADING_STE
@@ -3661,34 +3661,32 @@
 (presets.get
-Qualifiers
+WithFrom
 (), p -%3E p.g

Add tests for AssignedBy and WithFrom, these are now being populated only by a REST service call.