package no.uio.ifi.trackfind.backend.rest.controllers;

import no.uio.ifi.trackfind.TestTrackFindApplication;
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.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import static no.uio.ifi.trackfind.TestTrackFindApplication.TEST_DATA_PROVIDER;
import static org.hamcrest.Matchers.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

@SuppressWarnings("PMD.JUnitTestsShouldIncludeAssert")
@RunWith(SpringRunner.class)
@SpringBootTest(classes = TestTrackFindApplication.class)
public class TrackFindControllerTests {

    @Autowired
    private WebApplicationContext webApplicationContext;

    private MockMvc mockMvc;

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

    @Test
    public void getProvidersTest() throws Exception {
        mockMvc.perform(get("/providers"))
                .andExpect(status().isOk())
                .andExpect(content().contentType("application/json;charset=UTF-8"))
                .andExpect(jsonPath("$", hasSize(1)))
                .andExpect(jsonPath("$[0]", is(TEST_DATA_PROVIDER)));
    }

    @Test
    public void getMetamodelTreeTest() throws Exception {
        mockMvc.perform(get("/" + TEST_DATA_PROVIDER + "/metamodel-tree"))
                .andExpect(status().isOk())
                .andExpect(content().contentType("application/json;charset=UTF-8"))
                .andExpect(jsonPath("$.key1", hasSize(2)))
                .andExpect(jsonPath("$.key1", containsInAnyOrder("value1", "value2")))
                .andExpect(jsonPath("$.key2", contains("value3")));
    }

    @Test
    public void getMetamodelFlatTest() throws Exception {
        mockMvc.perform(get("/" + TEST_DATA_PROVIDER + "/metamodel-flat"))
                .andExpect(status().isOk())
                .andExpect(content().contentType("application/json;charset=UTF-8"))
                .andExpect(jsonPath("$.key1", hasSize(2)))
                .andExpect(jsonPath("$.key1", containsInAnyOrder("value1", "value2")))
                .andExpect(jsonPath("$.key2", contains("value3")));
    }

    @Test
    public void getAttributesTest() throws Exception {
        mockMvc.perform(get("/" + TEST_DATA_PROVIDER + "/attributes"))
                .andExpect(status().isOk())
                .andExpect(content().contentType("application/json;charset=UTF-8"))
                .andExpect(jsonPath("$", hasSize(2)))
                .andExpect(jsonPath("$", contains("key1", "key2")));
    }

    @Test
    public void getAttributesWithFilterTest() throws Exception {
        mockMvc.perform(get("/" + TEST_DATA_PROVIDER + "/attributes").param("filter", "ey1"))
                .andExpect(status().isOk())
                .andExpect(content().contentType("application/json;charset=UTF-8"))
                .andExpect(jsonPath("$", hasSize(1)))
                .andExpect(jsonPath("$.[0]", is("key1")));
    }

    @Test
    public void getValuesSingleTest() throws Exception {
        mockMvc.perform(get("/" + TEST_DATA_PROVIDER + "/values").param("attribute", "key2"))
                .andExpect(status().isOk())
                .andExpect(content().contentType("application/json;charset=UTF-8"))
                .andExpect(jsonPath("$", hasSize(1)))
                .andExpect(jsonPath("$.[0]", is("value3")));
    }

    @Test
    public void getValuesMultipleTest() throws Exception {
        mockMvc.perform(get("/" + TEST_DATA_PROVIDER + "/values").param("attribute", "key1"))
                .andExpect(status().isOk())
                .andExpect(content().contentType("application/json;charset=UTF-8"))
                .andExpect(jsonPath("$", hasSize(2)))
                .andExpect(jsonPath("$", containsInAnyOrder("value1", "value2")));
    }

    @Test
    public void getValuesWithFilterTest() throws Exception {
        mockMvc.perform(get("/" + TEST_DATA_PROVIDER + "/values").param("attribute", "key1").param("filter", "lue2"))
                .andExpect(status().isOk())
                .andExpect(content().contentType("application/json;charset=UTF-8"))
                .andExpect(jsonPath("$", hasSize(1)))
                .andExpect(jsonPath("$.[0]", is("value2")));
    }

    @Test
    public void searchTest() throws Exception {
        mockMvc.perform(get("/" + TEST_DATA_PROVIDER + "/search").param("query", "key1: value1 OR key2: value3"))
                .andExpect(status().isOk())
                .andExpect(content().contentType("application/json;charset=UTF-8"))
                .andExpect(jsonPath("$", hasSize(2)))
                .andExpect(jsonPath("$.[0].key1", is("value1")))
                .andExpect(jsonPath("$.[1].key1", is("value2")))
                .andExpect(jsonPath("$.[1].key2", is("value3")));
    }

}

@@ -1025,16 +1025,74 @@
 ests %7B%0A%0A
+    private static final String API_PREFIX = %22/api/v1/%22;%0A%0A
     @Aut
@@ -1098,16 +1098,16 @@
 towired%0A
-
     priv
@@ -1408,18 +1408,30 @@
 orm(get(
+API_PREFIX + 
 %22
-/
 provider
@@ -1782,35 +1782,42 @@
 Mvc.perform(get(
-%22/%22
+API_PREFIX
  + TEST_DATA_PRO
@@ -2281,35 +2281,42 @@
 Mvc.perform(get(
-%22/%22
+API_PREFIX
  + TEST_DATA_PRO
@@ -2777,35 +2777,42 @@
 Mvc.perform(get(
-%22/%22
+API_PREFIX
  + TEST_DATA_PRO
@@ -3188,35 +3188,42 @@
 Mvc.perform(get(
-%22/%22
+API_PREFIX
  + TEST_DATA_PRO
@@ -3604,35 +3604,42 @@
 Mvc.perform(get(
-%22/%22
+API_PREFIX
  + TEST_DATA_PRO
@@ -4024,35 +4024,42 @@
 Mvc.perform(get(
-%22/%22
+API_PREFIX
  + TEST_DATA_PRO
@@ -4468,35 +4468,42 @@
 Mvc.perform(get(
-%22/%22
+API_PREFIX
  + TEST_DATA_PRO
@@ -4913,11 +4913,18 @@
 get(
-%22/%22
+API_PREFIX
  + T

Fix int-tests.