Merge pull request 'statistics' (#50) from statistics into master

Reviewed-on: http://git.plannaplan.pl/filipizydorczyk/backend/pulls/50
This commit is contained in:
Marcin Woźniak 2021-01-21 16:49:37 +01:00
commit dd82acc1de
6 changed files with 457 additions and 8 deletions

View File

@ -10,17 +10,15 @@ import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
/**
* CommisionRepository.getUsers:
* Return list of:
* SELECT * FROM Commision WHERE owner_id = i .
* CommisionRepository.getUsers: Return list of: SELECT * FROM Commision WHERE
* owner_id = i .
*
* Where i, ?1 are equale to variables.
* Where i, ?1 are equale to variables.
*
* CommisionRepository.getNewestCommision
* Return list of:
* SELECT * FROM Commision WHERE owner_id = i Order by commisionDate desc.
* CommisionRepository.getNewestCommision Return list of: SELECT * FROM
* Commision WHERE owner_id = i Order by commisionDate desc.
*
* Where i, ?1 are equale to variables.
* Where i, ?1 are equale to variables.
*/
@Repository
public interface CommisionRepository extends JpaRepository<Commision, Long> {
@ -30,4 +28,10 @@ public interface CommisionRepository extends JpaRepository<Commision, Long> {
@Query("FROM Commision WHERE owner_id = ?1 order by commisionDate desc")
List<Commision> getNewestCommision(@Param("owner_id") Long id);
/**
* @return ammount of uniqe users that have a commision placed on first array
* element
*/
@Query("SELECT COUNT(DISTINCT owner_id) AS count FROM Commision")
Object[] getUsersAssigned();
}

View File

@ -1,6 +1,7 @@
package com.plannaplan.services;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
@ -153,4 +154,22 @@ public class GroupService {
return response;
}
/**
* @return amount of groups with full capacity taken
*/
public Integer getFullgroupsAmmount() {
Integer response = 0;
final Iterator<Groups> groups = this.repo.findAll().iterator();
while (groups.hasNext()) {
final Groups group = groups.next();
if (group.getCapacity() <= group.getRegisteredStudents().size()) {
response += 1;
}
}
return response;
}
}

View File

@ -1,13 +1,16 @@
package com.plannaplan.services;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.stream.Collectors;
import com.plannaplan.entities.Commision;
import com.plannaplan.entities.User;
import com.plannaplan.exceptions.UserNotFoundException;
import com.plannaplan.models.UserApiResponse;
import com.plannaplan.repositories.CommisionRepository;
import com.plannaplan.repositories.UserRepository;
import com.plannaplan.types.UserRoles;
@ -25,6 +28,12 @@ public class UserService {
@Autowired
private UsosApiService service;
@Autowired
private CommisionRepository comRepo;
@Autowired
private CommisionService comService;
public UserService() {
super();
}
@ -169,4 +178,52 @@ public class UserService {
}).collect(Collectors.toList());
}
/**
* @return ammount of how many users created an assignment
*/
public int getAmmountOfUsersWithAssignedGroups() {
int response = 0;
final Object dbResponse = this.comRepo.getUsersAssigned()[0];
if (dbResponse != null) {
response = ((Long) dbResponse).intValue();
}
return response;
}
/**
* @return ammount of how many users haven't created an assignment yet
*/
public Integer getAmmountOfUsersWithNoAssignedGroups() {
return this.getAllStudents().size() - this.getAmmountOfUsersWithAssignedGroups();
}
/**
* @return ammount of how many users have full schedule accepted
*/
public Integer getAmmountOfUsersWithAcceptedSchedules() {
final List<User> students = this.getAllStudents();
Integer accepted = 0;
final Iterator<User> it = students.iterator();
while (it.hasNext()) {
final User user = it.next();
final Optional<Commision> com = this.comService.getNewestCommision(user);
if (com.isPresent() && user.getStudentRegisteredGrups().size() == com.get().getAssignments().size()) {
accepted += 1;
}
}
return accepted;
}
/**
* @return ammount of how many users have partly or none schedule accepted
*/
public Integer getAmmountOfUsersWithNoAcceptedSchedules() {
return this.getAllStudents().size() - this.getAmmountOfUsersWithAcceptedSchedules();
}
}

View File

@ -0,0 +1,102 @@
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.StatisticSimpleNumberResponse;
import com.plannaplan.services.GroupService;
import com.plannaplan.services.UserService;
/**
* 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;
@Autowired
private UserService userService;
/**
* @return if tour was set
*/
@PreAuthorize("hasRole('ROLE_DEANERY')")
@GetMapping(path = "/groups/created")
public ResponseEntity<StatisticSimpleNumberResponse> getGroupsAmmounts() {
final StatisticSimpleNumberResponse response = new StatisticSimpleNumberResponse(
this.groupService.getGroupsAmmount());
return new ResponseEntity<>(response, HttpStatus.OK);
}
/**
* @return if tour was set
*/
@PreAuthorize("hasRole('ROLE_DEANERY')")
@GetMapping(path = "/groups/full")
public ResponseEntity<StatisticSimpleNumberResponse> getGroupsFullAmmounts() {
final StatisticSimpleNumberResponse response = new StatisticSimpleNumberResponse(
this.groupService.getFullgroupsAmmount());
return new ResponseEntity<>(response, HttpStatus.OK);
}
/**
* @return amount of registered to some groups
*/
@PreAuthorize("hasRole('ROLE_DEANERY')")
@GetMapping(path = "/users/registered")
public ResponseEntity<StatisticSimpleNumberResponse> getCommisionsAmmounts() {
final StatisticSimpleNumberResponse response = new StatisticSimpleNumberResponse(
this.userService.getAmmountOfUsersWithAssignedGroups());
return new ResponseEntity<>(response, HttpStatus.OK);
}
/**
* @return amount of students not registered to any groups
*/
@PreAuthorize("hasRole('ROLE_DEANERY')")
@GetMapping(path = "/users/noregistered")
public ResponseEntity<StatisticSimpleNumberResponse> getNonCommisionsAmmounts() {
final StatisticSimpleNumberResponse response = new StatisticSimpleNumberResponse(
this.userService.getAmmountOfUsersWithNoAssignedGroups());
return new ResponseEntity<>(response, HttpStatus.OK);
}
/**
* @return amount of students that have fully accepted schedules
*/
@PreAuthorize("hasRole('ROLE_DEANERY')")
@GetMapping(path = "/users/accepted")
public ResponseEntity<StatisticSimpleNumberResponse> getAcceptedAmmounts() {
final StatisticSimpleNumberResponse response = new StatisticSimpleNumberResponse(
this.userService.getAmmountOfUsersWithAcceptedSchedules());
return new ResponseEntity<>(response, HttpStatus.OK);
}
/**
* @return amount of students that have purtly accepted schedules
*/
@PreAuthorize("hasRole('ROLE_DEANERY')")
@GetMapping(path = "/users/accepted/partly")
public ResponseEntity<StatisticSimpleNumberResponse> getAcceptedPartlyAmmounts() {
final StatisticSimpleNumberResponse response = new StatisticSimpleNumberResponse(
this.userService.getAmmountOfUsersWithNoAcceptedSchedules());
return new ResponseEntity<>(response, HttpStatus.OK);
}
}

View File

@ -0,0 +1,23 @@
package com.plannaplan.responses.models;
/**
* Simple api response for number statistics
*/
public class StatisticSimpleNumberResponse {
private Integer ammount;
/**
* @param ammount to return as api response
*/
public StatisticSimpleNumberResponse(Integer ammount) {
this.ammount = ammount;
}
/**
* @return ammount
*/
public Integer getAmmount() {
return ammount;
}
}

View File

@ -0,0 +1,244 @@
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";
private static final String GROUP_FULL_AMMOUNTS_ENDPOINT = "/api/v1/statistics/groups/full";
private static final String USER_ASSIGNED_AMMOUNTS_ENDPOINT = "/api/v1/statistics/users/registered";
private static final String USER_NO_ASSIGNED_AMMOUNTS_ENDPOINT = "/api/v1/statistics/users/noregistered";
private static final String USER_ACCEPTED_AMMOUNTS_ENDPOINT = "/api/v1/statistics/users/accepted";
private static final String USER_PARTLY_ACCEPTED_AMMOUNTS_ENDPOINT = "/api/v1/statistics/users/accepted/partly";
@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());
}
/* USERS ASSIGNED TESTS */
@Test
public void shouldFailWithWrongAccesRegisteredStudentsAmmount() throws Exception {
final String mail = "shouldFailWithWrongAccesRegisteredStudentsAmmount@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(USER_ASSIGNED_AMMOUNTS_ENDPOINT).header("Authorization", "Bearer " + token))
.andExpect(status().is4xxClientError());
}
@Test
public void shouldOkGettingRegisteredStudentsAmmount() throws Exception {
final String mail = "shouldOkGettingRegisteredStudentsAmmount@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(USER_ASSIGNED_AMMOUNTS_ENDPOINT).header("Authorization", "Bearer " + token))
.andExpect(status().isOk());
}
@Test
public void shouldFailWithNoTokenRegisteredStudentsAmmount() throws Exception {
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(get(USER_ASSIGNED_AMMOUNTS_ENDPOINT)).andExpect(status().is4xxClientError());
}
/* USERS NO ASSIGNED TESTS */
@Test
public void shouldFailWithWrongAccesNoRegisteredStudentsAmmount() throws Exception {
final String mail = "shouldFailWithWrongAccesNoRegisteredStudentsAmmount@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(USER_NO_ASSIGNED_AMMOUNTS_ENDPOINT).header("Authorization", "Bearer " + token))
.andExpect(status().is4xxClientError());
}
@Test
public void shouldOkGettingNoRegisteredStudentsAmmount() throws Exception {
final String mail = "shouldOkGettingNoRegisteredStudentsAmmount@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(USER_NO_ASSIGNED_AMMOUNTS_ENDPOINT).header("Authorization", "Bearer " + token))
.andExpect(status().isOk());
}
@Test
public void shouldFailWithNoTokenNoRegisteredStudentsAmmount() throws Exception {
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(get(USER_NO_ASSIGNED_AMMOUNTS_ENDPOINT)).andExpect(status().is4xxClientError());
}
/* USERS FULL ACCPTED TESTS */
@Test
public void shouldFailWithWrongAccessAcceptedStudentsAmmount() throws Exception {
final String mail = "shouldFailWithWrongAccessAcceptedStudentsAmmount@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(USER_ACCEPTED_AMMOUNTS_ENDPOINT).header("Authorization", "Bearer " + token))
.andExpect(status().is4xxClientError());
}
@Test
public void shouldOkGettingAcceptedStudentsAmmount() throws Exception {
final String mail = "shouldOkGettingAcceptedStudentsAmmount@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(USER_ACCEPTED_AMMOUNTS_ENDPOINT).header("Authorization", "Bearer " + token))
.andExpect(status().isOk());
}
@Test
public void shouldFailWithNoTokenAcceptedStudentsAmmount() throws Exception {
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(get(USER_ACCEPTED_AMMOUNTS_ENDPOINT)).andExpect(status().is4xxClientError());
}
/* USERS PARTLY ACCPTED TESTS */
@Test
public void shouldFailWithWrongAccessPartlyAcceptedStudentsAmmount() throws Exception {
final String mail = "shouldFailWithWrongAccessPartlyAcceptedStudentsAmmount@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(USER_PARTLY_ACCEPTED_AMMOUNTS_ENDPOINT).header("Authorization", "Bearer " + token))
.andExpect(status().is4xxClientError());
}
@Test
public void shouldOkGettingPartlyAcceptedStudentsAmmount() throws Exception {
final String mail = "shouldOkGettingPartlyAcceptedStudentsAmmount@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(USER_PARTLY_ACCEPTED_AMMOUNTS_ENDPOINT).header("Authorization", "Bearer " + token))
.andExpect(status().isOk());
}
@Test
public void shouldFailWithNoTokenPartlyAcceptedStudentsAmmount() throws Exception {
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(get(USER_PARTLY_ACCEPTED_AMMOUNTS_ENDPOINT)).andExpect(status().is4xxClientError());
}
/* USERS FULL TAKEN GROUPS TESTS */
@Test
public void shouldFailWithWrongAccessFullGroupsAmmount() throws Exception {
final String mail = "shouldFailWithWrongAccessFullGroupsAmmount@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_FULL_AMMOUNTS_ENDPOINT).header("Authorization", "Bearer " + token))
.andExpect(status().is4xxClientError());
}
@Test
public void shouldOkGettingFullGroupsAmmount() throws Exception {
final String mail = "shouldOkGettingFullGroupsAmmount@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_FULL_AMMOUNTS_ENDPOINT).header("Authorization", "Bearer " + token))
.andExpect(status().isOk());
}
@Test
public void shouldFailWithNoTokenFullGroupsAmmount() throws Exception {
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(get(GROUP_FULL_AMMOUNTS_ENDPOINT)).andExpect(status().is4xxClientError());
}
}