From b93d44346c79bb065f73e4c63fab15b8ec742147 Mon Sep 17 00:00:00 2001 From: Filip Izydorczyk Date: Fri, 18 Dec 2020 17:23:41 +0100 Subject: [PATCH] new endpoint testd --- .../controllers/GroupController.java | 12 +-- .../controllers/GroupControllerTest.java | 73 +++++++++++++++++++ 2 files changed, 80 insertions(+), 5 deletions(-) diff --git a/restservice/src/main/java/com/plannaplan/controllers/GroupController.java b/restservice/src/main/java/com/plannaplan/controllers/GroupController.java index 2e73b76..0af2431 100755 --- a/restservice/src/main/java/com/plannaplan/controllers/GroupController.java +++ b/restservice/src/main/java/com/plannaplan/controllers/GroupController.java @@ -2,6 +2,7 @@ package com.plannaplan.controllers; import java.util.HashMap; import java.util.List; +import java.util.Optional; import com.plannaplan.App; import com.plannaplan.entities.Groups; @@ -60,18 +61,19 @@ public class GroupController { return new ResponseEntity<>(GroupsMappers.mapToGetCourseGroupsDefaultResponse(groups), HttpStatus.OK); } - @PreAuthorize("hasRole('ROLE_DEANERY')") @PutMapping("/{id}/capacity") + @PreAuthorize("hasRole('ROLE_DEANERY')") @ApiOperation(value = "Change capacity of given group. You need to provide DEANERY token to be ale to change capacity") public ResponseEntity updateCapacity( @PathVariable(name = "id", required = true) @ApiParam(value = "id of group to change capacity") Long id, @RequestParam(name = "newcapacity", required = true) Integer newcapacity) { - final Groups group = this.groupService.getGroupById(id).get(); - if (group == null) { + final Optional group = this.groupService.getGroupById(id); + if (group.isEmpty()) { return new ResponseEntity<>("Given group doens't exist", HttpStatus.NOT_FOUND); } - group.setCapacity(newcapacity); - this.groupService.save(group); + final Groups gr = group.get(); + gr.setCapacity(newcapacity); + this.groupService.save(gr); return new ResponseEntity<>("Success", HttpStatus.OK); } } \ No newline at end of file diff --git a/restservice/src/test/java/com/plannaplan/controllers/GroupControllerTest.java b/restservice/src/test/java/com/plannaplan/controllers/GroupControllerTest.java index 756f046..0d6dde5 100755 --- a/restservice/src/test/java/com/plannaplan/controllers/GroupControllerTest.java +++ b/restservice/src/test/java/com/plannaplan/controllers/GroupControllerTest.java @@ -2,6 +2,7 @@ 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; @@ -9,7 +10,17 @@ 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 @@ -17,6 +28,13 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. 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 { @@ -29,4 +47,59 @@ public class GroupControllerTest extends AbstractControllerTest { 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); + } + }