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.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 static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity; import com.plannaplan.entities.Groups; import com.plannaplan.entities.User; import com.plannaplan.services.GroupService; import com.plannaplan.services.UserService; import com.plannaplan.types.UserRoles; import com.plannaplan.types.WeekDay; @RunWith(SpringRunner.class) @SpringBootTest @ContextConfiguration public class GroupControllerTest extends AbstractControllerTest { private static final String GROUPS_BY_COURSE_ENDPOINT = "/api/v1/groups/course"; private static final String UPDATE_GROUPS_ENDPOINT = "/api/v1/groups/521/capacity"; @Autowired private UserService service; @Autowired private GroupService groupService; @Test public void shouldFailWithNoParaeter() throws Exception { MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build(); mockMvc.perform(get(GROUPS_BY_COURSE_ENDPOINT)).andExpect(status().isNotFound()); } @Test public void shouldReturnGroupsOk() throws Exception { MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build(); mockMvc.perform(get(GROUPS_BY_COURSE_ENDPOINT + "/2")).andExpect(status().isOk()); } @Test public void shouldFailWithNoToken() throws Exception { MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); mockMvc.perform(put(UPDATE_GROUPS_ENDPOINT)).andExpect(status().is4xxClientError()); } @Test public void shouldFailWithStudentToken() throws Exception { final User user = this.service .save(new User(null, null, "shouldFailWithStudentToken@GroupController.test", UserRoles.STUDENT)); final String token = this.service.login(user).getToken(); MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); mockMvc.perform(put(UPDATE_GROUPS_ENDPOINT).header("Authorization", "Bearer " + token)) .andExpect(status().is4xxClientError()); } @Test public void shouldFailWithNoParams() throws Exception { final User user = this.service .save(new User(null, null, "shouldFailWithNoParams@GroupController.test", UserRoles.DEANERY)); final String token = this.service.login(user).getToken(); MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); mockMvc.perform(put(UPDATE_GROUPS_ENDPOINT).header("Authorization", "Bearer " + token)) .andExpect(status().is4xxClientError()); } @Test public void shouldFailWithWrongId() throws Exception { final User user = this.service .save(new User(null, null, "shouldFailWithWrongId@GroupController.test", UserRoles.DEANERY)); final String token = this.service.login(user).getToken(); MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); mockMvc.perform(put("/api/v1/groups/" + user.getId() + "/capacity").param("newcapacity", "23") .header("Authorization", "Bearer " + token)).andExpect(status().isNotFound()); } @Test public void shouldChangeCapacity() throws Exception { final User user = this.service .save(new User(null, null, "shouldChangeCapacity@GroupController.test", UserRoles.DEANERY)); final String token = this.service.login(user).getToken(); final Groups newGroup = this.groupService.save(new Groups(43, "A-Nigzdie", null, 436, WeekDay.FRIDAY, null)); MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); mockMvc.perform(put("/api/v1/groups/" + newGroup.getId() + "/capacity").param("newcapacity", "23") .header("Authorization", "Bearer " + token)).andExpect(status().isOk()); assertTrue(this.groupService.getGroupById(newGroup.getId()).get().getCapacity() == 23); } }