package uk.ac.ebi.atlas.widget;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.test.context.ContextConfiguration;
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.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.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.forwardedUrl;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(locations = {"/applicationContext.xml", "/solrContext.xml", "/dbContext.xml"})
public class JsonBaselineRefExperimentControllerWIT {

    @Autowired
    WebApplicationContext wac;

    MockMvc mockMvc;

    @Before
    public void setUp() {
        this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
    }

    @Test
    public void oldReferenceExperiment() throws Exception {
        this.mockMvc.perform(
                get("/widgets/heatmap/referenceExperiment")
                        .param("geneQuery", "zinc finger")
                        .param("species", "caenorhabditis elegans"))
                .andExpect(status().isOk())
                .andExpect(forwardedUrl("/json/baseline_refexperiment"));
    }

    @Test
    public void jsonBaselineRefExperiment() throws Exception {
        this.mockMvc.perform(
                get("/json/baseline_refexperiment")
                        .param("geneQuery", "zinc finger")
                        .param("species", "caenorhabditis elegans"))
                .andExpect(status().isOk())
                .andExpect(forwardedUrl("/json/experiments/E-MTAB-2812"));
    }

    @Test
    public void jsonBaselineRefExperimentWithUnknownSpecies() throws Exception {
        this.mockMvc.perform(get("/json/baseline_refexperiment")
                    .param("geneQuery", "zinc finger")
                    .param("species", "foobar"))
                .andExpect(status().isBadRequest())
                .andExpect(content().contentType(MediaType.parseMediaType("application/json;charset=UTF-8")));
                // Unfortunately, Spring 3.2.2 requires json-path 0.8.1 which is super ancient and breaks all sorts of
                // things... :(
                //.andExpect(jsonPath("$.error").value(is("blah")));
    }

}
@@ -1366,421 +1366,8 @@
  %7D%0A%0A
-    @Test%0A    public void oldReferenceExperiment() throws Exception %7B%0A        this.mockMvc.perform(%0A                get(%22/widgets/heatmap/referenceExperiment%22)%0A                        .param(%22geneQuery%22, %22zinc finger%22)%0A                        .param(%22species%22, %22caenorhabditis elegans%22))%0A                .andExpect(status().isOk())%0A                .andExpect(forwardedUrl(%22/json/baseline_refexperiment%22));%0A    %7D%0A%0A
     
@@ -1692,82 +1692,8 @@
 k())
-%0A                .andExpect(forwardedUrl(%22/json/experiments/E-MTAB-2812%22))
 ;%0A  

Butcher up a test