package com.poprosturonin.sites.ninegag;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;

import java.util.regex.Pattern;

import static org.hamcrest.text.MatchesPattern.matchesPattern;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

/**
 * 9gag tests
 */
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class NinegagControllerTest {

    @Autowired
    private MockMvc mockMvc;

    private Pattern good9GAGURLPattern = Pattern.compile("^/9gag/page/([a-zA-Z0-9%]+)$");

    @Test
    public void shouldReturnJson() throws Exception {
        mockMvc.perform(get("/9gag/"))
                .andDo(print())
                .andExpect(status().isOk())
                .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
                .andExpect(jsonPath("nextPage", matchesPattern(good9GAGURLPattern)));
    }

    @Test
    public void shouldReturnEmptyPage() throws Exception {
        mockMvc.perform(get("/9gag/page/-1"))
                .andExpect(status().isNotFound());
    }
}
@@ -35,16 +35,44 @@
 negag;%0A%0A
+import org.json.JSONObject;%0A
 import o
@@ -82,24 +82,24 @@
 junit.Test;%0A
-
 import org.j
@@ -484,16 +484,71 @@
 MockMvc;
+%0Aimport org.springframework.test.web.servlet.MvcResult;
 %0A%0Aimport
@@ -1221,32 +1221,50 @@
 eption %7B%0A       
+ MvcResult first =
  mockMvc.perform
@@ -1438,16 +1438,449 @@
 _UTF8))%0A
+                .andExpect(jsonPath(%22nextPage%22, matchesPattern(good9GAGURLPattern)))%0A                .andReturn();%0A%0A        JSONObject firstContent = new JSONObject(first.getResponse().getContentAsString());%0A%0A        mockMvc.perform(get(firstContent.getString(%22nextPage%22)))%0A                .andDo(print())%0A                .andExpect(status().isOk())%0A                .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))%0A
         

Add 9gag test for further pages