package com.addicks.sendash.admin.api.rest.test;

import static org.junit.Assert.assertTrue;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.regex.Pattern;

import org.flywaydb.test.annotation.FlywayTest;
import org.flywaydb.test.junit.FlywayTestExecutionListener;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.MockitoAnnotations;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener;
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
import org.springframework.test.context.support.DirtiesContextTestExecutionListener;
import org.springframework.test.context.transaction.TransactionalTestExecutionListener;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.ResultMatcher;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import com.addicks.sendash.admin.api.rest.GithubHookController;
import com.addicks.sendash.admin.domain.Payload;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.springtestdbunit.DbUnitTestExecutionListener;

/**
 * Created by ann on 5/27/15.
 */
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class,
    TransactionalTestExecutionListener.class, DirtiesContextTestExecutionListener.class,
    SqlScriptsTestExecutionListener.class, DbUnitTestExecutionListener.class,
    FlywayTestExecutionListener.class })
@FlywayTest
public class GithubHookControllerTest extends ControllerTest {

  @InjectMocks
  private GithubHookController controller;

  @Autowired
  private WebApplicationContext context;

  private MockMvc mvc;

  @Before
  public void initTests() {
    MockitoAnnotations.initMocks(this);
    mvc = MockMvcBuilders.webAppContextSetup(context).build();
  }

  @Test
  public void shouldCreate() throws Exception {

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

    ObjectMapper mapper = new ObjectMapper();
    Payload payload = mapper.readValue(new File(path), Payload.class);
    byte[] json = toJson(payload);

    /*
     * MvcResult result = mvc.perform(post(GithubHookController.REQUEST_MAPPING)
     * .content(json) .contentType(MediaType.APPLICATION_JSON)
     * .accept(MediaType.APPLICATION_JSON)) .andExpect(status().isOk())
     * .andReturn();
     * 
     */

  }

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

  private byte[] toJson(Object r) throws Exception {
    ObjectMapper map = new ObjectMapper();
    return map.writeValueAsString(r).getBytes();
  }

  private String loadFile(String pathToFile) throws IOException {
    return new String(Files.readAllBytes(Paths.get(pathToFile)));

  }

}

@@ -65,139 +65,167 @@
 org.
-junit.Assert.assertTrue;%0A%0Aimport java.io.File;%0Aimport java.io.IOException;%0Aimport java.nio.file.Files;%0Aimport java.nio.file.Path
+springframework.test.web.servlet.request.MockMvcRequestBuilders.post;%0Aimport static org.springframework.test.web.servlet.result.MockMvcResultMatchers.statu
 s;%0A
+%0A
 impo
@@ -236,26 +236,15 @@
 ava.
-util.regex.Pattern
+io.File
 ;%0A%0Ai
@@ -532,16 +532,59 @@
 owired;%0A
+import org.springframework.http.MediaType;%0A
 import o
@@ -1037,122 +1037,8 @@
 vc;%0A
-import org.springframework.test.web.servlet.MvcResult;%0Aimport org.springframework.test.web.servlet.ResultMatcher;%0A
 impo
@@ -2472,37 +2472,8 @@
 %0A   
- /*%0A     * MvcResult result =
  mvc
@@ -2486,16 +2486,25 @@
 rm(post(
+SERVER + 
 GithubHo
@@ -2532,24 +2532,16 @@
 MAPPING)
-%0A     * 
 .content
@@ -2546,16 +2546,24 @@
 nt(json)
+%0A       
  .conten
@@ -2595,24 +2595,16 @@
 ON_JSON)
-%0A     * 
 .accept(
@@ -2631,16 +2631,24 @@
 N_JSON))
+%0A       
  .andExp
@@ -2667,24 +2667,16 @@
 .isOk())
-%0A     * 
 .andRetu
@@ -2685,441 +2685,8 @@
 ();%0A
-     * %0A     */%0A%0A  %7D%0A%0A  // match redirect header URL (aka Location header)%0A  private static ResultMatcher redirectedUrlPattern(final String expectedUrlPattern) %7B%0A    return new ResultMatcher() %7B%0A      @Override%0A      public void match(MvcResult result) %7B%0A        Pattern pattern = Pattern.compile(%22%5C%5CA%22 + expectedUrlPattern + %22%5C%5Cz%22);%0A        assertTrue(pattern.matcher(result.getResponse().getRedirectedUrl()).find());%0A      %7D%0A    %7D;
 %0A  %7D
@@ -2841,144 +2841,6 @@
  %7D%0A%0A
-  private String loadFile(String pathToFile) throws IOException %7B%0A    return new String(Files.readAllBytes(Paths.get(pathToFile)));%0A%0A  %7D%0A%0A
 %7D%0A

test working.