package uk.ac.ebi.quickgo.annotation.controller;

import uk.ac.ebi.quickgo.annotation.AnnotationREST;
import uk.ac.ebi.quickgo.rest.metadata.MetaDataConfigProperties;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.FileSystemResource;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

/**
 * Test the failure to load the metadata information.
 *
 * @author Tony Wardell
 * Date: 09/03/2017
 * Time: 13:53
 * Created with IntelliJ IDEA.
 */
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = {AnnotationREST.class})
@WebAppConfiguration
public class AnnotationControllerMetaDataSourceFailIT {
    private static final String RESOURCE_URL = "/annotation";
    private MockMvc mockMvc;

    @Autowired
    private WebApplicationContext webApplicationContext;

    @Before
    public void setup() {
        mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();

    }

    // ------------------------------- Check about information -------------------------------

    @Test
    public void about() throws Exception {
        ResultActions response = mockMvc.perform(get(RESOURCE_URL + "/about"));

        response.andDo(print())
                .andExpect(status().isInternalServerError());
    }

    /**
     * Configure properties used by co-term generation, using test values.
     */
    @Configuration
    public static class TestConfig {
        @Primary
        @Bean
        public MetaDataConfigProperties primaryMetaDataConfigProperties() {
            MetaDataConfigProperties properties = new MetaDataConfigProperties();
            FileSystemResource fileSystemResource = new FileSystemResource("/does/not/exist");
            properties.setSource(fileSystemResource);
            return properties;
        }
    }

}

@@ -98,73 +98,8 @@
 EST;
-%0Aimport uk.ac.ebi.quickgo.rest.metadata.MetaDataConfigProperties;
 %0A%0Aim
@@ -340,197 +340,34 @@
 ork.
-context.annotation.Bean;%0Aimport org.springframework.context.annotation.Configuration;%0Aimport org.springframework.context.annotation.Primary;%0Aimport org.springframework.core.io.FileSystemRes
+test.context.TestPropertyS
 ourc
@@ -1293,16 +1293,92 @@
 uration%0A
+@TestPropertySource(locations=%22classpath:metadata-source-fails.properties%22)%0A
 public c
@@ -1425,16 +1425,17 @@
 ailIT %7B%0A
+%0A
     priv
@@ -1714,17 +1714,16 @@
 uild();%0A
-%0A
     %7D%0A%0A 
@@ -1816,17 +1816,16 @@
 -------%0A
-%0A
     @Tes
@@ -1949,17 +1949,16 @@
 out%22));%0A
-%0A
         
@@ -2047,546 +2047,12 @@
 ));%0A
+
     %7D%0A
-%0A    /**%0A     * Configure properties used by co-term generation, using test values.%0A     */%0A    @Configuration%0A    public static class TestConfig %7B%0A        @Primary%0A        @Bean%0A        public MetaDataConfigProperties primaryMetaDataConfigProperties() %7B%0A            MetaDataConfigProperties properties = new MetaDataConfigProperties();%0A            FileSystemResource fileSystemResource = new FileSystemResource(%22/does/not/exist%22);%0A            properties.setSource(fileSystemResource);%0A            return properties;%0A        %7D%0A    %7D%0A%0A
 %7D%0A

Replace local @Configuration with @TestPropertySource