From bfa8eb6e3ede89f1be90f3cedcd26e1697557994 Mon Sep 17 00:00:00 2001 From: Filip Izydorczyk Date: Thu, 21 Jan 2021 16:22:14 +0100 Subject: [PATCH] full groups statistics --- .../com/plannaplan/services/GroupService.java | 19 ++++++++++ .../controllers/StatisticsController.java | 11 ++++++ .../controllers/StatisticsControllerTest.java | 36 +++++++++++++++++++ 3 files changed, 66 insertions(+) diff --git a/buisnesslogic/src/main/java/com/plannaplan/services/GroupService.java b/buisnesslogic/src/main/java/com/plannaplan/services/GroupService.java index d3d9656..713cc00 100755 --- a/buisnesslogic/src/main/java/com/plannaplan/services/GroupService.java +++ b/buisnesslogic/src/main/java/com/plannaplan/services/GroupService.java @@ -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 = this.repo.findAll().iterator(); + + while (groups.hasNext()) { + final Groups group = groups.next(); + if (group.getCapacity() <= group.getRegisteredStudents().size()) { + response += 1; + } + + } + + return response; + } } \ No newline at end of file diff --git a/restservice/src/main/java/com/plannaplan/controllers/StatisticsController.java b/restservice/src/main/java/com/plannaplan/controllers/StatisticsController.java index e60fa71..a6d37e8 100755 --- a/restservice/src/main/java/com/plannaplan/controllers/StatisticsController.java +++ b/restservice/src/main/java/com/plannaplan/controllers/StatisticsController.java @@ -44,6 +44,17 @@ public class StatisticsController { return new ResponseEntity<>(response, HttpStatus.OK); } + /** + * @return if tour was set + */ + @PreAuthorize("hasRole('ROLE_DEANERY')") + @GetMapping(path = "/groups/full") + public ResponseEntity getGroupsFullAmmounts() { + final StatisticSimpleNumberResponse response = new StatisticSimpleNumberResponse( + this.groupService.getFullgroupsAmmount()); + return new ResponseEntity<>(response, HttpStatus.OK); + } + /** * @return amount of registered to some groups */ diff --git a/restservice/src/test/java/com/plannaplan/controllers/StatisticsControllerTest.java b/restservice/src/test/java/com/plannaplan/controllers/StatisticsControllerTest.java index fd380b3..e72cab4 100755 --- a/restservice/src/test/java/com/plannaplan/controllers/StatisticsControllerTest.java +++ b/restservice/src/test/java/com/plannaplan/controllers/StatisticsControllerTest.java @@ -23,6 +23,7 @@ import com.plannaplan.types.UserRoles; 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"; @@ -205,4 +206,39 @@ public class StatisticsControllerTest extends AbstractControllerTest { 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()); + + } }