package com.github.solairerove.woodstock;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.solairerove.woodstock.domain.Unit;
import com.github.solairerove.woodstock.dto.UnitDTO;
import com.github.solairerove.woodstock.repository.UnitRepository;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.web.context.WebApplicationContext;

import static com.github.solairerove.woodstock.controller.ControllerApi.UNIT_API;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.springframework.http.HttpMethod.GET;
import static org.springframework.http.HttpMethod.POST;
import static org.springframework.http.HttpMethod.PUT;
import static org.springframework.http.MediaType.APPLICATION_JSON_UTF8_VALUE;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.request;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup;

@SpringBootTest
@WebAppConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
public class UnitControllerTest {

    private MockMvc mvc;

    @Autowired
    private UnitRepository repository;

    @Autowired
    private WebApplicationContext context;

    @Before
    public void setup() throws Exception {
        this.mvc = webAppContextSetup(context).build();
    }

    @Test
    public void createUnitTest() throws Exception {
        repository.deleteAll();
        UnitDTO dto = new UnitDTO("Label", "URL to avatar", "Short MD description");
        ObjectMapper objectMapper = new ObjectMapper();

        mvc.perform(request(POST, UNIT_API)
                .accept(APPLICATION_JSON_UTF8_VALUE)
                .contentType(APPLICATION_JSON_UTF8_VALUE)
                .content(objectMapper.writeValueAsString(dto)))
                .andExpect(status().isCreated())
                .andExpect(content().contentType(APPLICATION_JSON_UTF8_VALUE))
                .andExpect(jsonPath("$.label", is("Label")));
    }

    @Test
    public void getUnitTest() throws Exception {
        repository.deleteAll();
        Unit unit = new Unit("Label", "URL to avatar", "Short MD description");
        String id = repository.save(unit).getId();

        mvc.perform(request(GET, UNIT_API + "/" + id)
                .accept(APPLICATION_JSON_UTF8_VALUE)
                .contentType(APPLICATION_JSON_UTF8_VALUE))
                .andExpect(status().isOk())
                .andExpect(content().contentType(APPLICATION_JSON_UTF8_VALUE))
                .andExpect(jsonPath("$.label", is("Label")))
                .andExpect(jsonPath("$.avatar", is("URL to avatar")));
    }

    @Test
    public void getAllUnitsTest() throws Exception {
        repository.deleteAll();
        Unit unit = new Unit("Label", "URL to avatar", "Short MD description");
        repository.save(unit);

        mvc.perform(request(GET, UNIT_API)
                .accept(APPLICATION_JSON_UTF8_VALUE)
                .contentType(APPLICATION_JSON_UTF8_VALUE))
                .andExpect(status().isOk())
                .andExpect(content().contentType(APPLICATION_JSON_UTF8_VALUE))
                .andExpect(jsonPath("$", hasSize(1)))
                .andExpect(jsonPath("$.[0].label", is("Label")))
                .andExpect(jsonPath("$.[0].avatar", is("URL to avatar")));
    }

    @Test
    public void updateUnitTest() throws Exception {
        repository.deleteAll();
        Unit unit = new Unit("Label", "URL to avatar", "Short MD description");
        String id = repository.save(unit).getId();
        UnitDTO dto = new UnitDTO("Updated Label", "URL to avatar", "Short MD description");
        ObjectMapper objectMapper = new ObjectMapper();

        mvc.perform(request(PUT, UNIT_API + "/" + id)
                .accept(APPLICATION_JSON_UTF8_VALUE)
                .contentType(APPLICATION_JSON_UTF8_VALUE)
                .content(objectMapper.writeValueAsString(dto)))
                .andExpect(status().isAccepted())
                .andExpect(content().contentType(APPLICATION_JSON_UTF8_VALUE))
                .andExpect(jsonPath("$.label", is("Updated Label")));
    }
}

@@ -261,16 +261,41 @@
 sitory;%0A
+import org.junit.Assert;%0A
 import o
@@ -933,99 +933,33 @@
 org.
-springframework.http.HttpMethod.GET;%0Aimport static org.springframework.http.HttpMethod.POST
+junit.Assert.assertEquals
 ;%0Aim
@@ -1006,19 +1006,17 @@
 pMethod.
-PUT
+*
 ;%0Aimport
@@ -4522,11 +4522,720 @@
 );%0A    %7D
+%0A%0A    @Test%0A    public void deleteUnitTest() throws Exception %7B%0A        repository.deleteAll();%0A        Unit unit = new Unit(%22Deleted Label%22, %22URL to avatar%22, %22Short MD description%22);%0A        String id = repository.save(unit).getId();%0A%0A        mvc.perform(request(DELETE, UNIT_API + %22/%22 + id)%0A                .accept(APPLICATION_JSON_UTF8_VALUE)%0A                .contentType(APPLICATION_JSON_UTF8_VALUE))%0A                .andExpect(status().isAccepted())%0A                .andExpect(content().contentType(APPLICATION_JSON_UTF8_VALUE))%0A                .andExpect(jsonPath(%22$.id%22, is(id)))%0A                .andExpect(jsonPath(%22$.label%22, is(%22Deleted Label%22)));%0A%0A        assertEquals(0L, repository.count());%0A    %7D
 %0A%7D%0A

Add delete unit controller test