From 13eb8dae772fbef4a64041b643bf2dc6b4a9f682 Mon Sep 17 00:00:00 2001 From: BuildTools Date: Fri, 27 Nov 2020 16:55:15 +0100 Subject: [PATCH] Added service method tests + javadocs --- .../repositories/GroupRepository.java | 12 ++++--- .../com/plannaplan/services/GroupService.java | 18 +++++++--- .../repositories/GroupRepositoryTest.java | 2 -- .../plannaplan/services/GroupServiceTest.java | 34 ++++++++++++++++++- .../controllers/GroupController.java | 2 +- 5 files changed, 56 insertions(+), 12 deletions(-) diff --git a/buisnesslogic/src/main/java/com/plannaplan/repositories/GroupRepository.java b/buisnesslogic/src/main/java/com/plannaplan/repositories/GroupRepository.java index 6e66a42..79d4e30 100755 --- a/buisnesslogic/src/main/java/com/plannaplan/repositories/GroupRepository.java +++ b/buisnesslogic/src/main/java/com/plannaplan/repositories/GroupRepository.java @@ -21,11 +21,15 @@ public interface GroupRepository extends JpaRepository { @Query("SELECT COUNT(*) AS assinged_times FROM Assignment WHERE isPastAssignment=false GROUP BY group HAVING group_id=?1") Optional getAssignedAmount(Long groupId); + /** + * PLAIN SQL QUERY: SELECT group_id, COUNT(*) assinged_times FROM assignment + * WHERE is_past_assignment=0 GROUP BY group_id HAVING group_id IN (:ids)") + * + * @param groupIds list of groups ids + * @return list of objects arrays where first object is Groups instance and + * second is Long that is taken places value + */ @Query("SELECT group, COUNT(*) AS assinged_times FROM Assignment a WHERE a.isPastAssignment=false GROUP BY a.group HAVING group_id IN (:ids)") - // @Query(nativeQuery = true, value = "SELECT group_id, COUNT(*) AS - // assinged_times FROM assignment WHERE is_past_assignment=0 GROUP BY group_id - // HAVING group_id IN (:ids)") - List getAssignedAmounts(@Param("ids") List groupIds); } \ No newline at end of file diff --git a/buisnesslogic/src/main/java/com/plannaplan/services/GroupService.java b/buisnesslogic/src/main/java/com/plannaplan/services/GroupService.java index 1952d53..1cd4971 100755 --- a/buisnesslogic/src/main/java/com/plannaplan/services/GroupService.java +++ b/buisnesslogic/src/main/java/com/plannaplan/services/GroupService.java @@ -54,12 +54,22 @@ public class GroupService { } - public HashMap getTakenPlaces(List groups) { - HashMap respoonse = new HashMap<>(); + /** + * + * @param groups list of groups you want to get taken places ammount + * @return HashMap where Long is group id and Integer is how many + * places in gorup is already taken + */ + public HashMap getTakenPlaces(List groups) { + HashMap response = new HashMap<>(); - List respoonse2 = this.repo.getAssignedAmounts( + List respoonses = this.repo.getAssignedAmounts( groups.stream().filter(Objects::nonNull).map(Groups::getId).collect(Collectors.toList())); - return respoonse; + for (Object[] element : respoonses) { + response.put(((Groups) element[0]).getId(), ((Long) element[1]).intValue()); + } + + return response; } } \ No newline at end of file diff --git a/buisnesslogic/src/test/java/com/plannaplan/repositories/GroupRepositoryTest.java b/buisnesslogic/src/test/java/com/plannaplan/repositories/GroupRepositoryTest.java index d0e4dc7..222a279 100755 --- a/buisnesslogic/src/test/java/com/plannaplan/repositories/GroupRepositoryTest.java +++ b/buisnesslogic/src/test/java/com/plannaplan/repositories/GroupRepositoryTest.java @@ -9,8 +9,6 @@ import static org.junit.Assert.assertTrue; import java.util.List; -import javax.swing.GroupLayout.Group; - import com.plannaplan.entities.Assignment; import com.plannaplan.entities.Commision; import com.plannaplan.entities.Groups; diff --git a/buisnesslogic/src/test/java/com/plannaplan/services/GroupServiceTest.java b/buisnesslogic/src/test/java/com/plannaplan/services/GroupServiceTest.java index 3b9f4bb..0dc9715 100755 --- a/buisnesslogic/src/test/java/com/plannaplan/services/GroupServiceTest.java +++ b/buisnesslogic/src/test/java/com/plannaplan/services/GroupServiceTest.java @@ -2,7 +2,15 @@ package com.plannaplan.services; import static org.junit.Assert.assertTrue; +import java.util.HashMap; +import java.util.List; + +import com.plannaplan.entities.Assignment; +import com.plannaplan.entities.Commision; import com.plannaplan.entities.Groups; +import com.plannaplan.entities.User; +import com.plannaplan.types.UserRoles; +import com.plannaplan.types.WeekDay; import org.junit.Test; import org.junit.runner.RunWith; @@ -18,6 +26,12 @@ public class GroupServiceTest { @Autowired private GroupService groupService; + @Autowired + private AssignmentService assignmentService; + @Autowired + private CommisionService commisionService; + @Autowired + private UserService userService; @Test public void createAndDeleteGroup() { @@ -33,7 +47,25 @@ public class GroupServiceTest { } @Test - public void shouldGetGroupsAssignmentsAmmounts() { + public void shouldGetGroupsAssignmentsAmmounts() throws InterruptedException { + final Groups testGroup = groupService.save(new Groups(43, "A-41", null, 645, WeekDay.MONDAY, null)); + final Groups testGroup2 = groupService.save(new Groups(433, "A-41", null, 235, WeekDay.TUESDAY, null)); + final Groups testGroup3 = groupService.save(new Groups(23, "A-41", null, 340, WeekDay.MONDAY, null)); + final User user = this.userService.save( + new User("Dare", "Oc", "shouldReturnGroupAssignmentTimesList@grouprepository.test", UserRoles.STUDENT)); + final Commision commision = this.commisionService.save(new Commision(user)); + this.assignmentService.save(new Assignment(testGroup, commision)); + this.assignmentService.save(new Assignment(testGroup2, commision)); + this.assignmentService.save(new Assignment(testGroup3, commision)); + + Thread.sleep(1000); + + HashMap response = this.groupService.getTakenPlaces(List.of(testGroup, testGroup2, testGroup3)); + + assertTrue(response.size() == 3); + assertTrue(response.get(testGroup.getId()) == 1); + assertTrue(response.get(testGroup2.getId()) == 1); + assertTrue(response.get(testGroup3.getId()) == 1); } } diff --git a/restservice/src/main/java/com/plannaplan/controllers/GroupController.java b/restservice/src/main/java/com/plannaplan/controllers/GroupController.java index 7bff8df..6be9f55 100755 --- a/restservice/src/main/java/com/plannaplan/controllers/GroupController.java +++ b/restservice/src/main/java/com/plannaplan/controllers/GroupController.java @@ -41,7 +41,7 @@ public class GroupController { @RequestParam(name = "capacity", defaultValue = "true") @ApiParam(value = "Boolean if we want to have capacity field in response") Boolean capacity, @RequestParam(name = "takenPlaces", defaultValue = "false") @ApiParam(value = "Boolean if we want to have respoonse with information about taken places by other students") Boolean takenPlaces) { List groups = this.groupService.getGroupsByCourse(id); - HashMap ammounts; + HashMap ammounts; if (takenPlaces) { ammounts = this.groupService.getTakenPlaces(groups);