package com.plannaplan.controllers; 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.http.MediaType; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import java.nio.charset.Charset; import com.plannaplan.entities.User; import com.plannaplan.services.UserService; import com.plannaplan.types.UserRoles; import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.*; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; @RunWith(SpringRunner.class) @SpringBootTest @ContextConfiguration public class CommisionControllerTest { @Autowired private WebApplicationContext webApplicationContext; @Autowired private UserService service; private static User user; private static final String ADD_COMMISION_ENDPOINT = "/api/v1/commisions/user"; private static final String GET_COMMISIONS_ENDPOINT = "/api/v1/commisions/user"; private static final String TEST_COMMISIONS_EMAIL = "commisions@notexisting.domain"; private static final MediaType APPLICATION_JSON_UTF8 = new MediaType(MediaType.APPLICATION_JSON.getType(), MediaType.APPLICATION_JSON.getSubtype(), Charset.forName("utf8")); @Test public void shouldReturn4xxAddingCommision() throws Exception { MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); mockMvc.perform(post(ADD_COMMISION_ENDPOINT)).andExpect(status().is4xxClientError()); } @Test public void shouldFailedAddingCommisionDueToNoArgs() throws Exception { this.checkUser(); final String token = this.service.login(TEST_COMMISIONS_EMAIL); MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); mockMvc.perform(post(ADD_COMMISION_ENDPOINT).header("Authorization", "Bearer " + token)) .andExpect(status().is4xxClientError()); } @Test public void shouldReturnOkAddingCommision() throws Exception { this.checkUser(); final String token = this.service.login(TEST_COMMISIONS_EMAIL); MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); mockMvc.perform(post(ADD_COMMISION_ENDPOINT).header("Authorization", "Bearer " + token) .contentType(APPLICATION_JSON_UTF8).content("[]")).andExpect(status().isOk()); } @Test public void shouldReturn4xxGettingAllCommisions() throws Exception { MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); mockMvc.perform(get(GET_COMMISIONS_ENDPOINT)).andExpect(status().is4xxClientError()); } @Test public void shouldReturnOkGettingAllCommisions() throws Exception { this.checkUser(); final String token = this.service.login(TEST_COMMISIONS_EMAIL); MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); mockMvc.perform(get(GET_COMMISIONS_ENDPOINT).header("Authorization", "Bearer " + token)) .andExpect(status().isOk()); } private void checkUser() { if (CommisionControllerTest.user == null) { CommisionControllerTest.user = new User(null, null, TEST_COMMISIONS_EMAIL, UserRoles.TEST_USER); this.service.save(user); } } }