package io.github.jordao76.quotes;

import static io.github.jordao76.quotes.support.QuoteMatchers.*;
import static org.springframework.http.MediaType.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.*;

import org.junit.*;
import org.junit.runner.*;
import org.springframework.beans.factory.annotation.*;
import org.springframework.boot.test.*;
import org.springframework.test.context.junit4.*;
import org.springframework.test.context.web.*;
import org.springframework.test.web.servlet.*;
import org.springframework.web.context.*;

import io.github.jordao76.quotes.domain.*;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = QuoteApplication.class)
@WebAppConfiguration
public class QuoteControllerTest {

  @Autowired
  private WebApplicationContext wac;

  private MockMvc client;

  @Before
  public void setup() {
    client = webAppContextSetup(wac).build();
  }

  // first quote added to the repository (@see QuoteApplication.populateRepo),
  // should have ID = 1
  String firstQuoteText = "Any sufficiently advanced technology is indistinguishable from magic.";
  String firstQuoteAuthor = "Arthur C. Clarke";

  @Test
  public void getQuotes_checkFirstQuote() throws Exception {
    client
      .perform(get("/quotes"))
      .andExpect(status().isOk())
      .andExpect(content().contentType(APPLICATION_JSON_UTF8))
      .andExpect(contentAsQuotes(hasQuote(matching(firstQuoteText, firstQuoteAuthor))));
  }

  @Test
  public void getFirstQuote() throws Exception {
    client
      .perform(get("/quotes/1"))
      .andExpect(status().isOk())
      .andExpect(content().contentType(APPLICATION_JSON_UTF8))
      .andExpect(contentAsQuote(matching(firstQuoteText, firstQuoteAuthor)));
  }

  @Test
  public void getQuote_notFound() throws Exception {
    client
      .perform(get("/quotes/42"))
      .andExpect(status().isNotFound());
  }

  @Test
  public void getQuote_invalidId() throws Exception {
    client
      .perform(get("/quotes/blah"))
      .andExpect(status().isBadRequest());
  }

  @Test
  public void putQuote() throws Exception {
    String quoteText = "Quick decisions are unsafe decisions.",
      quoteAuthor = "Sophocles";
    Quote quote = new Quote(quoteText, quoteAuthor);
    client
      .perform(put("/quotes")
        .contentType(APPLICATION_JSON)
        .content(serializeJson(quote)))
      .andExpect(status().isOk())
      .andExpect(content().contentType(APPLICATION_JSON_UTF8))
      .andExpect(contentAsQuote(matching(quoteText, quoteAuthor)));
  }

  @Test
  public void putQuote_invalidText() throws Exception {
    String quoteText = null,
      quoteAuthor = "Sophocles";
    Quote quote = new Quote(quoteText, quoteAuthor);
    client
      .perform(put("/quotes")
        .contentType(APPLICATION_JSON)
        .content(serializeJson(quote)))
      .andExpect(status().isBadRequest());
  }

  @Test
  public void putQuote_wrongJson() throws Exception {
    client
      .perform(put("/quotes")
        .contentType(APPLICATION_JSON)
        .content("{\"citation\":\"to be or not to be\"}"))
      .andExpect(status().isBadRequest());
  }

}

@@ -3321,10 +3321,250 @@
 );%0A  %7D%0A%0A
+  @Test%0A  public void putQuote_notJson() throws Exception %7B%0A    client%0A      .perform(put(%22/quotes%22)%0A        .contentType(APPLICATION_XML)%0A        .content(%22%3Croot%3Enot JSON%3C/root%3E%22))%0A      .andExpect(status().isUnsupportedMediaType());%0A  %7D%0A%0A
 %7D%0A

Add test to PUT non-JSON content