From b4bb13f2ddc14628378ae72fedd9cdc41f0b50a8 Mon Sep 17 00:00:00 2001 From: BuildTools Date: Fri, 20 Nov 2020 15:56:43 +0100 Subject: [PATCH] Added tests and service --- .../repositories/GroupRepository.java | 9 +-- .../com/plannaplan/services/GroupService.java | 3 +- .../repositories/GroupRepositoryTest.java | 67 +++++++++++++------ .../controllers/ConfigController.java | 2 +- 4 files changed, 50 insertions(+), 31 deletions(-) diff --git a/buisnesslogic/src/main/java/com/plannaplan/repositories/GroupRepository.java b/buisnesslogic/src/main/java/com/plannaplan/repositories/GroupRepository.java index 9a23383..8c474f2 100755 --- a/buisnesslogic/src/main/java/com/plannaplan/repositories/GroupRepository.java +++ b/buisnesslogic/src/main/java/com/plannaplan/repositories/GroupRepository.java @@ -18,14 +18,7 @@ public interface GroupRepository extends JpaRepository { @Query("FROM Groups WHERE course_id = ?1") List getByCourse(@Param("id") Long id); - // SELECT group_id, COUNT(*) AS assinged_times FROM assignment WHERE - // is_past_assignment=0 GROUP BY group_id HAVING group_id=7; - @Query("SELECT COUNT(*) AS assinged_times FROM Assignment WHERE isPastAssignment=false GROUP BY group HAVING group_id=?1") - Number getAssignedAmount(Long groupId); - - // @Query("SELECT group_id, COUNT(*) AS assinged_times FROM assignment WHERE - // is_past_assignment=0 GROUP BY group_id HAVING group_id=7;") - // Number getTmpSqlAssignedAmount(Long groupId); + Optional getAssignedAmount(Long groupId); } \ 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 d202f8b..024b7ed 100755 --- a/buisnesslogic/src/main/java/com/plannaplan/services/GroupService.java +++ b/buisnesslogic/src/main/java/com/plannaplan/services/GroupService.java @@ -52,7 +52,6 @@ public class GroupService { } public int getAssignedAmount(Long groupId) { - this.repo.getAssignedAmount(groupId); - return 0; + return this.repo.getAssignedAmount(groupId).orElse(Integer.valueOf(0)).intValue(); } } \ 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 5a09f6e..2d49130 100755 --- a/buisnesslogic/src/test/java/com/plannaplan/repositories/GroupRepositoryTest.java +++ b/buisnesslogic/src/test/java/com/plannaplan/repositories/GroupRepositoryTest.java @@ -26,27 +26,54 @@ import org.junit.runner.RunWith; @SpringBootTest @ContextConfiguration public class GroupRepositoryTest { - @Autowired - private GroupRepository repository; - @Autowired - private AssignmentService assignmentService; - @Autowired - private GroupService groupService; - @Autowired - private UserService userService; + @Autowired + private GroupRepository repository; + @Autowired + private AssignmentService assignmentService; + @Autowired + private GroupService groupService; + @Autowired + private UserService userService; - @Autowired - private CommisionService commisionService; + @Autowired + private CommisionService commisionService; - @Test - public void shouldReturnGroupAssignmentTimes() { - final Groups testGroup = groupService.save(new Groups(43, "A-41", null, 235, WeekDay.MONDAY, null)); - final User user = this.userService.save( - new User("Luis", "Vita", "shouldReturnGroupAssignmentTimes@grouprepository.test", UserRoles.STUDENT)); - final Commision commision = this.commisionService.save(new Commision(user)); - this.assignmentService.save(new Assignment(testGroup, commision)); + @Test + public void shouldReturnGroupAssignmentTimes() throws InterruptedException { + final Groups testGroup = groupService.save(new Groups(43, "A-41", null, 235, WeekDay.MONDAY, null)); + int startGroupAmmount = this.repository.getAssignedAmount(testGroup.getId()).orElse(Integer.valueOf(0)) + .intValue(); - System.out.println(this.repository.getAssignedAmount(testGroup.getId())); - assertTrue(false); - } + final User user = this.userService.save(new User("Luis", "Vita", + "shouldReturnGroupAssignmentTimes@grouprepository.test", UserRoles.STUDENT)); + final Commision commision = this.commisionService.save(new Commision(user)); + + this.assignmentService.save(new Assignment(testGroup, commision)); + Thread.sleep(1000); + + int afterAssignedGroupAmmount = this.repository.getAssignedAmount(testGroup.getId()) + .orElse(Integer.valueOf(0)).intValue(); + + assertTrue(afterAssignedGroupAmmount > startGroupAmmount); + + final Commision recommision = this.commisionService.save(new Commision(user)); + this.assignmentService.save(new Assignment(testGroup, recommision)); + + Thread.sleep(1000); + + int afterreAssignedGroupAmmount = this.repository.getAssignedAmount(testGroup.getId()) + .orElse(Integer.valueOf(0)).intValue(); + + assertTrue(afterAssignedGroupAmmount == afterreAssignedGroupAmmount); + + this.commisionService.save(new Commision(user)); + + Thread.sleep(1000); + + int afterdeAssignedGroupAmmount = this.repository.getAssignedAmount(testGroup.getId()) + .orElse(Integer.valueOf(0)).intValue(); + + assertTrue(afterdeAssignedGroupAmmount < afterreAssignedGroupAmmount); + + } } diff --git a/restservice/src/main/java/com/plannaplan/controllers/ConfigController.java b/restservice/src/main/java/com/plannaplan/controllers/ConfigController.java index f9e3b0e..58b94e1 100755 --- a/restservice/src/main/java/com/plannaplan/controllers/ConfigController.java +++ b/restservice/src/main/java/com/plannaplan/controllers/ConfigController.java @@ -34,7 +34,7 @@ public class ConfigController { private ConfiguratorService contrl; @PostMapping("/config") - // @PreAuthorize("hasRole('ROLE_ADMIN')") + @PreAuthorize("hasRole('ROLE_ADMIN')") @ApiOperation("Imports data to system. To call you need to provide ADMIN token") public ResponseEntity configApp( @RequestParam("file") @ApiParam(value = "file .xlsx that contains courses and groups with apoinnted rules") MultipartFile file) {