package com.github.solairerove.woodstock;

import com.github.solairerove.woodstock.domain.Module;
import com.github.solairerove.woodstock.domain.Unit;
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.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;

/**
 * Created by solairerove on 3/27/17.
 */

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

    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 getModuleTest() 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();

        mvc.perform(request(GET, "/api/units/" + unitId + "/modules/" + moduleId)
                .accept(APPLICATION_JSON_UTF8_VALUE)
                .contentType(APPLICATION_JSON_UTF8_VALUE))
                .andExpect(status().isOk())
                .andExpect(content().contentType(APPLICATION_JSON_UTF8_VALUE))
                .andExpect(jsonPath("$.name", is("Name")))
                .andExpect(jsonPath("$.avatar", is("Link to avatar")));
    }
}

@@ -669,16 +669,61 @@
 ntext;%0A%0A
+import static org.hamcrest.Matchers.hasSize;%0A
 import s
@@ -754,16 +754,16 @@
 ers.is;%0A
-
 import s
@@ -1201,17 +1201,16 @@
 17.%0A */%0A
-%0A
 @SpringB
@@ -2330,16 +2330,16 @@
 ame%22)))%0A
-
         
@@ -2407,11 +2407,1112 @@
 );%0A    %7D
+%0A%0A    @Test%0A    public void getAllModulesTest() 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        Module module2 = new Module(%22Name2%22, %22Link to avatar2%22, %22Short Description%22);%0A        unit.add(module);%0A        unit.add(module2);%0A%0A        String unitId = repository.save(unit).getId();%0A%0A        mvc.perform(request(GET, %22/api/units/%22 + unitId + %22/modules%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(2)))%0A                .andExpect(jsonPath(%22$.%5B0%5D.name%22, is(%22Name%22)))%0A                .andExpect(jsonPath(%22$.%5B0%5D.avatar%22, is(%22Link to avatar%22)))%0A                .andExpect(jsonPath(%22$.%5B1%5D.name%22, is(%22Name2%22)))%0A                .andExpect(jsonPath(%22$.%5B1%5D.avatar%22, is(%22Link to avatar2%22)));%0A    %7D
 %0A%7D%0A

Add get all modules controller test