Groups ammounts

This commit is contained in:
Filip Izydorczyk 2021-01-21 15:05:45 +01:00
parent cd2665bd08
commit 7045844653
3 changed files with 130 additions and 0 deletions

View File

@ -0,0 +1,43 @@
package com.plannaplan.controllers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;
import com.plannaplan.App;
import com.plannaplan.responses.models.StatisticCreatedGroupsResponse;
import com.plannaplan.services.GroupService;
/**
* Rest controller to enpoint that help deveopler test the app
*/
@RestController
@CrossOrigin
@RequestMapping("/api/" + App.API_VERSION + "/statistics")
@Api(tags = {
"StatisticsController" }, value = "StatisticsController", description = "Statistics are meant to be used by deanery only so in every endpoint you need to provide DEANERY token.")
public class StatisticsController {
@Autowired
private GroupService groupService;
/**
* @return if tour was set
*/
@PreAuthorize("hasRole('ROLE_DEANERY')")
@GetMapping(path = "/groups/created")
public ResponseEntity<StatisticCreatedGroupsResponse> getGroupsAmmounts() {
final StatisticCreatedGroupsResponse response = new StatisticCreatedGroupsResponse(
this.groupService.getGroupsAmmount());
return new ResponseEntity<>(response, HttpStatus.OK);
}
}

View File

@ -0,0 +1,23 @@
package com.plannaplan.responses.models;
/**
* Api response for created groups statistics
*/
public class StatisticCreatedGroupsResponse {
private Integer ammount;
/**
* @param ammount created groups
*/
public StatisticCreatedGroupsResponse(Integer ammount) {
this.ammount = ammount;
}
/**
* @return ammount of created groups
*/
public Integer getAmmount() {
return ammount;
}
}

View File

@ -0,0 +1,64 @@
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.result.MockMvcResultMatchers.status;
import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.*;
import com.plannaplan.entities.User;
import com.plannaplan.services.UserService;
import com.plannaplan.types.UserRoles;
@RunWith(SpringRunner.class)
@SpringBootTest
@ContextConfiguration
public class StatisticsControllerTest extends AbstractControllerTest {
private static final String GROUP_AMMOUNTS_ENDPOINT = "/api/v1/statistics/groups/created";
@Autowired
private UserService userService;
/* GROUP AMMOUNTS TESTS */
@Test
public void shouldFailWithWrongAccesGroupsAmmounts() throws Exception {
final String mail = "shouldFailWithWrongAccesGroupsAmmounts@StatisticsController.test";
final User usr = this.userService.save(new User(null, null, mail, UserRoles.TEST_USER));
final String token = this.userService.login(usr).getToken();
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(get(GROUP_AMMOUNTS_ENDPOINT).header("Authorization", "Bearer " + token))
.andExpect(status().is4xxClientError());
}
@Test
public void shouldOkGettingGroupsAmmounts() throws Exception {
final String mail = "shouldOkGettingGroupsAmmounts@StatisticsController.test";
final User usr = this.userService.save(new User(null, null, mail, UserRoles.DEANERY));
final String token = this.userService.login(usr).getToken();
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(get(GROUP_AMMOUNTS_ENDPOINT).header("Authorization", "Bearer " + token))
.andExpect(status().isOk());
}
@Test
public void shouldFailWithNoTokenGroupsAmmounts() throws Exception {
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(get(GROUP_AMMOUNTS_ENDPOINT)).andExpect(status().is4xxClientError());
}
}