package com.github.solairerove.woodstock;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.solairerove.woodstock.domain.Module;
import com.github.solairerove.woodstock.domain.Reference;
import com.github.solairerove.woodstock.domain.Unit;
import com.github.solairerove.woodstock.dto.ReferenceDTO;
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 org.hamcrest.Matchers.is;
import static org.springframework.http.HttpMethod.GET;
import static org.springframework.http.HttpMethod.POST;
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.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup;

/**
 * Created by solairerove on 3/29/17.
 */
@SpringBootTest
@WebAppConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
public class ReferenceControllerTest {

    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 createReferenceTest() throws Exception {
        repository.deleteAll();
        Unit unit = new Unit("Label", "URL to avatar", "Short MD description");
        Module module = new Module("Name", "Link to avatar", "Short Description");
        unit.add(module);

        String unitId = repository.save(unit).getId();
        String moduleId = module.getId();

        ReferenceDTO dto = new ReferenceDTO("Title", "Reference version");
        ObjectMapper objectMapper = new ObjectMapper();

        mvc.perform(request(POST, "/api/units/" + unitId + "/modules/" + moduleId + "/references/")
                .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("$.title", is("Title")))
                .andExpect(jsonPath("$.version", is("Reference version")));
    }

    @Test
    public void getReferenceTest() throws Exception {
        repository.deleteAll();
        Unit unit = new Unit("Label", "URL to avatar", "Short MD description");
        Module module = new Module("Name", "Link to avatar", "Short Description");
        Reference reference = new Reference("Reference Title", "Version");

        module.addReference(reference);
        unit.add(module);

        String unitId = repository.save(unit).getId();
        String moduleId = module.getId();
        String refId = reference.getId();

        mvc.perform(request(GET, "/api/units/" + unitId + "/modules/" + moduleId + "/references/" + refId)
                .accept(APPLICATION_JSON_UTF8_VALUE)
                .contentType(APPLICATION_JSON_UTF8_VALUE))
                .andExpect(status().isOk())
                .andExpect(content().contentType(APPLICATION_JSON_UTF8_VALUE))
                .andExpect(jsonPath("$.title", is("Reference Title")));
    }
}

@@ -837,16 +837,61 @@
 ntext;%0A%0A
+import static org.hamcrest.Matchers.hasSize;%0A
 import s
@@ -4011,11 +4011,984 @@
 );%0A    %7D
+%0A%0A    @Test%0A    public void getAllReferencesTest() throws Exception %7B%0A        repository.deleteAll();%0A        Unit unit = new Unit(%22Label%22, %22URL to avatar%22, %22Short MD description%22);%0A        Module module = new Module(%22Name%22, %22Link to avatar%22, %22Short Description%22);%0A        Reference reference = new Reference(%22Reference Title%22, %22Version%22);%0A%0A        module.addReference(reference);%0A        unit.add(module);%0A%0A        String unitId = repository.save(unit).getId();%0A        String moduleId = module.getId();%0A%0A        mvc.perform(request(GET, %22/api/units/%22 + unitId + %22/modules/%22 + moduleId + %22/references%22)%0A                .accept(APPLICATION_JSON_UTF8_VALUE)%0A                .contentType(APPLICATION_JSON_UTF8_VALUE))%0A                .andExpect(status().isOk())%0A                .andExpect(content().contentType(APPLICATION_JSON_UTF8_VALUE))%0A                .andExpect(jsonPath(%22$%22, hasSize(1)))%0A                .andExpect(jsonPath(%22$%5B0%5D.title%22, is(%22Reference Title%22)));%0A    %7D
 %0A%7D%0A

Create get all references controller test