new endpoint testd

This commit is contained in:
Filip Izydorczyk 2020-12-18 17:23:41 +01:00
parent 4a12dceab6
commit b93d44346c
2 changed files with 80 additions and 5 deletions

View File

@ -2,6 +2,7 @@ package com.plannaplan.controllers;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Optional;
import com.plannaplan.App; import com.plannaplan.App;
import com.plannaplan.entities.Groups; import com.plannaplan.entities.Groups;
@ -60,18 +61,19 @@ public class GroupController {
return new ResponseEntity<>(GroupsMappers.mapToGetCourseGroupsDefaultResponse(groups), HttpStatus.OK); return new ResponseEntity<>(GroupsMappers.mapToGetCourseGroupsDefaultResponse(groups), HttpStatus.OK);
} }
@PreAuthorize("hasRole('ROLE_DEANERY')")
@PutMapping("/{id}/capacity") @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") @ApiOperation(value = "Change capacity of given group. You need to provide DEANERY token to be ale to change capacity")
public ResponseEntity<String> updateCapacity( public ResponseEntity<String> updateCapacity(
@PathVariable(name = "id", required = true) @ApiParam(value = "id of group to change capacity") Long id, @PathVariable(name = "id", required = true) @ApiParam(value = "id of group to change capacity") Long id,
@RequestParam(name = "newcapacity", required = true) Integer newcapacity) { @RequestParam(name = "newcapacity", required = true) Integer newcapacity) {
final Groups group = this.groupService.getGroupById(id).get(); final Optional<Groups> group = this.groupService.getGroupById(id);
if (group == null) { if (group.isEmpty()) {
return new ResponseEntity<>("Given group doens't exist", HttpStatus.NOT_FOUND); return new ResponseEntity<>("Given group doens't exist", HttpStatus.NOT_FOUND);
} }
group.setCapacity(newcapacity); final Groups gr = group.get();
this.groupService.save(group); gr.setCapacity(newcapacity);
this.groupService.save(gr);
return new ResponseEntity<>("Success", HttpStatus.OK); return new ResponseEntity<>("Success", HttpStatus.OK);
} }
} }

View File

@ -2,6 +2,7 @@ package com.plannaplan.controllers;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner; 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 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.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; 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) @RunWith(SpringRunner.class)
@SpringBootTest @SpringBootTest
@ -17,6 +28,13 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
public class GroupControllerTest extends AbstractControllerTest { public class GroupControllerTest extends AbstractControllerTest {
private static final String GROUPS_BY_COURSE_ENDPOINT = "/api/v1/groups/course"; 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 @Test
public void shouldFailWithNoParaeter() throws Exception { public void shouldFailWithNoParaeter() throws Exception {
@ -29,4 +47,59 @@ public class GroupControllerTest extends AbstractControllerTest {
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build(); MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
mockMvc.perform(get(GROUPS_BY_COURSE_ENDPOINT + "/2")).andExpect(status().isOk()); 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);
}
} }