package com.khoubyari.example.test.api.rest;

import com.khoubyari.example.Application;
import com.khoubyari.example.api.rest.GithubHookController;
import com.khoubyari.example.api.rest.HotelController;
import com.khoubyari.example.domain.Hotel;
import org.flywaydb.test.annotation.FlywayTest;
import org.flywaydb.test.junit.FlywayTestExecutionListener;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Profile;
import org.springframework.http.MediaType;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.ResultMatcher;
import org.springframework.web.context.WebApplicationContext;

import java.io.File;
import java.util.regex.Pattern;

import static org.hamcrest.Matchers.*;
import static org.junit.Assert.assertTrue;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

/**
 * Created by ann on 5/27/15.
 */
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(classes = Application.class)
@Profile("test")
@TestExecutionListeners({DependencyInjectionTestExecutionListener.class, FlywayTestExecutionListener.class })
@FlywayTest
public class GithubHookControllerTest {

    @InjectMocks
    private GithubHookController controller;

    @Autowired
    private WebApplicationContext context;

    private MockMvc mvc;

    private static final String RESOURCE_LOCATION_PATTERN = "http://localhost/example/v1/hotels/[0-9]+";

    @Test
    public void shouldCreate() throws Exception {

        ClassLoader classLoader = getClass().getClassLoader();
        File file = new File(classLoader.getResource("json/githubPayload.json").getFile());

        //CREATE
        MvcResult result = mvc.perform(post("/example/v1/hotels")
                .content(r1Json)
                .contentType(MediaType.APPLICATION_JSON)
                .accept(MediaType.APPLICATION_JSON))
                .andExpect(status().isCreated())
                .andExpect(redirectedUrlPattern(RESOURCE_LOCATION_PATTERN))
                .andReturn();
        long id = getResourceIdFromUrl(result.getResponse().getRedirectedUrl());


    }

    private long getResourceIdFromUrl(String locationUrl) {
        String[] parts = locationUrl.split("/");
        return Long.valueOf(parts[parts.length - 1]);
    }

    // match redirect header URL (aka Location header)
    private static ResultMatcher redirectedUrlPattern(final String expectedUrlPattern) {
        return new ResultMatcher() {
            public void match(MvcResult result) {
                Pattern pattern = Pattern.compile("\\A" + expectedUrlPattern + "\\z");
                assertTrue(pattern.matcher(result.getResponse().getRedirectedUrl()).find());
            }
        };
    }


}

@@ -35,24 +35,76 @@
 .api.rest;%0A%0A
+import com.fasterxml.jackson.databind.ObjectMapper;%0A
 import com.k
@@ -291,16 +291,61 @@
 .Hotel;%0A
+import com.khoubyari.example.domain.Payload;%0A
 import o
@@ -436,32 +436,57 @@
 cutionListener;%0A
+import org.junit.Before;%0A
 import org.junit
@@ -557,16 +557,55 @@
 tMocks;%0A
+import org.mockito.MockitoAnnotations;%0A
 import o
@@ -1273,24 +1273,138 @@
 ultMatcher;%0A
+import org.springframework.test.web.servlet.setup.MockMvcBuilders;%0Aimport org.springframework.util.ResourceUtils;%0A
 import org.s
@@ -1471,24 +1471,84 @@
 va.io.File;%0A
+import java.io.FileInputStream;%0Aimport java.io.InputStream;%0A
 import java.
@@ -1568,16 +1568,17 @@
 ttern;%0A%0A
+%0A
 import s
@@ -2499,16 +2499,177 @@
 -9%5D+%22;%0A%0A
+    @Before%0A    public void initTests() %7B%0A        MockitoAnnotations.initMocks(this);%0A        mvc = MockMvcBuilders.webAppContextSetup(context).build();%0A    %7D%0A%0A%0A
     @Tes
@@ -2876,16 +2876,105 @@
 ile());%0A
+        InputStream is = new FileInputStream(file);%0A        byte%5B%5D json = toJson(file);%0A%0A
 %0A       
@@ -2990,16 +2990,18 @@
 %0A       
+/*
  MvcResu
@@ -3033,28 +3033,44 @@
 ost(
-%22/example/v1/hotels%22
+GithubHookController.REQUEST_MAPPING
 )%0A  
@@ -3096,11 +3096,9 @@
 ent(
-r1J
+j
 son)
@@ -3246,23 +3246,18 @@
 tus().is
-Created
+Ok
 ())%0A    
@@ -3362,266 +3362,18 @@
 ();%0A
-        long id = getResourceIdFromUrl(result.getResponse().getRedirectedUrl());%0A%0A%0A    %7D%0A%0A    private long getResourceIdFromUrl(String locationUrl) %7B%0A        String%5B%5D parts = locationUrl.split(%22/%22);%0A        return Long.valueOf(parts%5Bparts.length - 1%5D);%0A    %7D
+%0A*/%0A    %7D%0A
 %0A%0A  
@@ -3813,11 +3813,173 @@
 %0A    %7D%0A%0A
+    private byte%5B%5D toJson(Object r) throws Exception %7B%0A        ObjectMapper map = new ObjectMapper();%0A        return map.writeValueAsString(r).getBytes();%0A    %7D%0A%0A
 %0A%7D%0A

test the github controller