package uk.ac.ebi.quickgo.rest.search.filter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.test.ConfigFileApplicationContextInitializer;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.hasSize;
import static uk.ac.ebi.quickgo.rest.search.filter.FieldExecutionConfig.*;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = ExternalFilterExecutionConfigIT.TestApplication.class,
        initializers = ConfigFileApplicationContextInitializer.class)
public class ExternalFilterExecutionConfigIT {
    @Configuration
    @ComponentScan
    @EnableConfigurationProperties
    public static class TestApplication {
        public static void main(String[] args) {
            SpringApplication.run(TestApplication.class);
        }
    }

    @Autowired
    private ExternalFilterExecutionConfig externalFilterExecutionConfig;

    @Test
    public void yamlPropertiesLoadedCorrectlyIntoBean() {
        List<FieldExecutionConfig> fieldConfigs = externalFilterExecutionConfig.getFields();

        FieldExecutionConfig aspectField = createStubAspectField();
        FieldExecutionConfig usageField = createStubUsageField();

        assertThat(fieldConfigs, hasSize(2));
        assertThat(fieldConfigs, containsInAnyOrder(aspectField, usageField));
    }

    private FieldExecutionConfig createStubAspectField() {
        Map<String, String> map = new HashMap<>();
        map.put("fromTable", "ontology");
        map.put("fromAttribute", "id");
        map.put("toTable", "annotation");
        map.put("toAttribute", "id");

        return FilterUtil.createExecutionConfigWithProps("aspect", ExecutionType.JOIN, map);
    }

    private FieldExecutionConfig createStubUsageField() {
        Map<String, String> map = new HashMap<>();
        map.put("ip", "123.456.789");
        map.put("endpoint", "endpoint");

        return FilterUtil.createExecutionConfigWithProps("usage", ExecutionType.REST_COMM, map);
    }
}
@@ -36,24 +36,83 @@
 ch.filter;%0A%0A
+import uk.ac.ebi.quickgo.common.SearchableDocumentFields;%0A%0A
 import java.
@@ -121,24 +121,24 @@
 il.HashMap;%0A
-
 import java.
@@ -166,24 +166,56 @@
 a.util.Map;%0A
+import java.util.stream.Stream;%0A
 import org.j
@@ -528,24 +528,76 @@
 nitializer;%0A
+import org.springframework.context.annotation.Bean;%0A
 import org.s
@@ -1388,16 +1388,16 @@
 perties%0A
-
     publ
@@ -1430,16 +1430,452 @@
 ation %7B%0A
+%0A        @Bean%0A        public SearchableDocumentFields dummySearchableDocumentFields() %7B%0A            return new SearchableDocumentFields() %7B%0A                @Override public boolean isDocumentSearchable(String field) %7B%0A                    return false;%0A                %7D%0A%0A                @Override public Stream%3CString%3E searchableDocumentFields() %7B%0A                    return Stream.empty();%0A                %7D%0A            %7D;%0A        %7D%0A%0A
         

Modify test setup so that it does not fail when looking for a Bean of type SearchableDocumentFields.