package guru.nidi.ramltester;

import org.junit.Before;
import org.junit.Test;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import static guru.nidi.ramltester.spring.RamlResultMatchers.requestResponse;

/**
 *
 */
@Controller
public class UriTest extends TestBase {
    private RamlDefinition uri = TestRaml.load("uri.raml").fromClasspath(getClass());
    private MockMvc mockMvc;

    @Before
    public void init() {
        mockMvc = MockMvcBuilders.standaloneSetup(this).build();
    }

    @RequestMapping(value = {"/raml/v1/{def}/{type}", "/v1/{def}/{type}", "/{def}/{type}"})
    @ResponseBody
    public HttpEntity<String> test() {
        final HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.TEXT_HTML);
        return new HttpEntity<>(headers);
    }

    @Test(expected = AssertionError.class)
    public void standardServletUri() throws Exception {
        mockMvc.perform(MockMvcRequestBuilders.get("/raml/v1/undefd"))
                .andExpect(requestResponse().matchesRaml(uri));
    }

    @Test(expected = IllegalArgumentException.class)
    public void invalidServletUri() throws Exception {
        mockMvc.perform(MockMvcRequestBuilders.get("/raml/v1/undefd"))
                .andExpect(requestResponse().withServletUri("invalid").matchesRaml(uri));
    }

    @Test
    public void correctServletUri() throws Exception {
        mockMvc.perform(MockMvcRequestBuilders.get("/raml/v1/undefd/type"))
                .andExpect(requestResponse().withServletUri("http://nidi.guru").matchesRaml(uri));

        mockMvc.perform(MockMvcRequestBuilders.get("/v1/undefd/type"))
                .andExpect(requestResponse().withServletUri("http://nidi.guru/raml").matchesRaml(uri));

        mockMvc.perform(MockMvcRequestBuilders.get("/undefd/type"))
                .andExpect(requestResponse().withServletUri("http://nidi.guru/raml/v1").matchesRaml(uri));
    }

    @Test
    public void preferSubResourceWithLessVariables() throws Exception {
        assertNoViolation(
                uri,
                get("/undefined/type/sub"),
                jsonResponse(201, "\"hula\""));
        assertNoViolation(
                uri,
                get("/undefined/type/1"),
                jsonResponse(202, "\"hula\""));
    }

    @Test
    public void checkUriParameters() throws Exception {
        assertOneViolationThat(
                uri,
                get("/undefined/type/other"),
                jsonResponse(202, "\"hula\""),
                allOf(startsWith("URI parameter 'undefined'"), endsWith("Value 'other' is not a valid integer")));
        assertNoViolation(
                uri,
                get("/undefined/type/other/sub"),
                jsonResponse(203, "\"hula\""));
    }
}

@@ -2570,32 +2570,20 @@
 onse(201
-, %22%5C%22hula%5C%22%22
 ));%0A
+
         
@@ -2696,28 +2696,16 @@
 onse(202
-, %22%5C%22hula%5C%22%22
 ));%0A    
@@ -2904,28 +2904,16 @@
 onse(202
-, %22%5C%22hula%5C%22%22
 ),%0A     
@@ -3112,32 +3112,32 @@
 pe/other/sub%22),%0A
+
                 
@@ -3156,20 +3156,8 @@
 (203
-, %22%5C%22hula%5C%22%22
 ));%0A

allow handle responses correctly