full groups statistics

This commit is contained in:
Filip Izydorczyk 2021-01-21 16:22:14 +01:00
parent a910709798
commit bfa8eb6e3e
3 changed files with 66 additions and 0 deletions

View File

@ -1,6 +1,7 @@
package com.plannaplan.services; package com.plannaplan.services;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.Optional; import java.util.Optional;
@ -153,4 +154,22 @@ public class GroupService {
return response; 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

@ -44,6 +44,17 @@ public class StatisticsController {
return new ResponseEntity<>(response, HttpStatus.OK); 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 * @return amount of registered to some groups
*/ */

View File

@ -23,6 +23,7 @@ import com.plannaplan.types.UserRoles;
public class StatisticsControllerTest extends AbstractControllerTest { public class StatisticsControllerTest extends AbstractControllerTest {
private static final String GROUP_AMMOUNTS_ENDPOINT = "/api/v1/statistics/groups/created"; 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_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_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_ACCEPTED_AMMOUNTS_ENDPOINT = "/api/v1/statistics/users/accepted";
@ -205,4 +206,39 @@ public class StatisticsControllerTest extends AbstractControllerTest {
mockMvc.perform(get(USER_PARTLY_ACCEPTED_AMMOUNTS_ENDPOINT)).andExpect(status().is4xxClientError()); 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());
}
} }