diff --git a/buisnesslogic/src/main/java/com/plannaplan/entities/Assignment.java b/buisnesslogic/src/main/java/com/plannaplan/entities/Assignment.java index 4af9863..7f368b4 100755 --- a/buisnesslogic/src/main/java/com/plannaplan/entities/Assignment.java +++ b/buisnesslogic/src/main/java/com/plannaplan/entities/Assignment.java @@ -23,26 +23,34 @@ public class Assignment { @ManyToOne @JoinColumn(name = "commision_id") private Commision commision; - + private boolean isPastAssignment; + /** - * Assignment - * - * @param group group you would like to assign - * @param commision commision assignment belongs to group - * + * Assignment + * @param group group we would like to assign + * @param commision commision that assignment belongs to + * @param isPastAssignment is assignment past or no */ - public Assignment(Groups group, Commision commision) { + public Assignment(Groups group, Commision commision, boolean isPastAssignment) { this.commision = commision; this.group = group; } + /** + * Assignment + * @param group group we would like to assign + * @param commision commision that assignment belongs to + */ + public Assignment(Groups group, Commision commision) { + this(group, commision, false); + } + public Assignment() { } /** - * getId - * - * @return id + * Id getter + * @return id id of assignment */ public Long getId() { @@ -57,4 +65,20 @@ public class Assignment { public Groups getGroup() { return this.group; } + + /** + * isPastAssignment getter + * @return isPastAssignment + */ + public boolean isPastAssignment() { + return isPastAssignment; + } + + /** + * setter isPastAssignment + * @param isPastAssignment + */ + public void setPastAssignment(boolean isPastAssignment) { + this.isPastAssignment = isPastAssignment; + } } \ No newline at end of file diff --git a/buisnesslogic/src/main/java/com/plannaplan/entities/Commision.java b/buisnesslogic/src/main/java/com/plannaplan/entities/Commision.java index 7f1f33e..36e3585 100755 --- a/buisnesslogic/src/main/java/com/plannaplan/entities/Commision.java +++ b/buisnesslogic/src/main/java/com/plannaplan/entities/Commision.java @@ -4,6 +4,7 @@ import java.sql.Timestamp; import java.util.List; import javax.persistence.Entity; +import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; @@ -25,13 +26,13 @@ public class Commision { private User commisionOwner; private Timestamp commisionDate; - @OneToMany(mappedBy = "commision") + @OneToMany(mappedBy = "commision", fetch = FetchType.EAGER) private List assignments; /** - * Commision - * - * @param user user assign to the group + * + * @param user owner of commission. Can not be null otherwise saving commision + * will fail. */ public Commision(User user) { this.commisionDate = new Timestamp(System.currentTimeMillis()); @@ -42,30 +43,35 @@ public class Commision { } /** - * getId - * - * @return id + * Id getter + * @return id id of commision */ public Long getId() { return this.id; } /** - * getCommisionDate - * - * @return commisionDate + * CommisionDate getter + * @return commisionDate */ public Timestamp getCommisionDate() { return commisionDate; } /** - * getCommisionOwner - * - * @return commisionOwner + * User of given commision getter + * @return User commisionOwner */ public User getCommisionOwner() { return commisionOwner; } + /** + * Assigments getter + * @return List of assignments + */ + public List getAssignments() { + return this.assignments; + } + } diff --git a/buisnesslogic/src/main/java/com/plannaplan/repositories/GroupRepository.java b/buisnesslogic/src/main/java/com/plannaplan/repositories/GroupRepository.java index 62c303b..019a6ad 100755 --- a/buisnesslogic/src/main/java/com/plannaplan/repositories/GroupRepository.java +++ b/buisnesslogic/src/main/java/com/plannaplan/repositories/GroupRepository.java @@ -31,4 +31,18 @@ public interface GroupRepository extends JpaRepository { @Query("FROM Groups WHERE course_id = ?1") List getByCourse(@Param("id") Long id); + @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)") + List getAssignedAmounts(@Param("ids") List groupIds); + } \ No newline at end of file diff --git a/buisnesslogic/src/main/java/com/plannaplan/services/AssignmentService.java b/buisnesslogic/src/main/java/com/plannaplan/services/AssignmentService.java index cb42ad6..ce21a8f 100755 --- a/buisnesslogic/src/main/java/com/plannaplan/services/AssignmentService.java +++ b/buisnesslogic/src/main/java/com/plannaplan/services/AssignmentService.java @@ -1,6 +1,7 @@ package com.plannaplan.services; import java.util.List; +import java.util.Optional; import com.plannaplan.entities.Assignment; import com.plannaplan.entities.Commision; @@ -21,12 +22,13 @@ public class AssignmentService { public AssignmentService() { } - /* - * save - * @param assignment which assignment should be save in service + /** + * Save given assignment + * @param assignment assignment to save + * @return assignment saved assignment with database id */ - public void save(Assignment assignment) { - this.repo.save(assignment); + public Assignment save(Assignment assignment) { + return this.repo.save(assignment); } /* @@ -44,4 +46,13 @@ public class AssignmentService { public long getAssignmentsAmmount() { return this.repo.count(); } + + /** + * Get assigmnent by id + * @param id id of assigmnent + * @return Optional of assignment + */ + public Optional getById(Long id) { + return this.repo.findById(id); + } } diff --git a/buisnesslogic/src/main/java/com/plannaplan/services/CommisionService.java b/buisnesslogic/src/main/java/com/plannaplan/services/CommisionService.java index 800a899..63d4c4f 100755 --- a/buisnesslogic/src/main/java/com/plannaplan/services/CommisionService.java +++ b/buisnesslogic/src/main/java/com/plannaplan/services/CommisionService.java @@ -5,6 +5,7 @@ import java.util.Optional; import com.plannaplan.entities.Commision; import com.plannaplan.entities.User; +import com.plannaplan.repositories.AssignmentRepository; import com.plannaplan.repositories.CommisionRepository; import org.springframework.beans.factory.annotation.Autowired; @@ -18,6 +19,8 @@ import org.springframework.stereotype.Service; public class CommisionService { @Autowired private CommisionRepository repo; + @Autowired + private AssignmentRepository aRepository; public CommisionService() { } @@ -29,6 +32,14 @@ public class CommisionService { * @return commision */ public Commision save(Commision commision) { + Optional lastCommision = this.getNewestCommision(commision.getCommisionOwner()); + if (lastCommision.isPresent()) { + final Commision lastCom = lastCommision.get(); + lastCom.getAssignments().forEach(assignment -> { + assignment.setPastAssignment(true); + this.aRepository.save(assignment); + }); + } this.repo.save(commision); return commision; diff --git a/buisnesslogic/src/main/java/com/plannaplan/services/GroupService.java b/buisnesslogic/src/main/java/com/plannaplan/services/GroupService.java index 2eae2fb..4d1582b 100755 --- a/buisnesslogic/src/main/java/com/plannaplan/services/GroupService.java +++ b/buisnesslogic/src/main/java/com/plannaplan/services/GroupService.java @@ -1,7 +1,11 @@ package com.plannaplan.services; +import java.util.HashMap; import java.util.List; +import java.util.Objects; import java.util.Optional; +import java.util.stream.Collectors; +import java.util.function.Function; import com.plannaplan.entities.Groups; import com.plannaplan.repositories.GroupRepository; @@ -30,8 +34,7 @@ public class GroupService { } public Groups save(Groups group) { - this.repo.save(group); - return group; + return this.repo.save(group); } public void delete(Groups groups) { @@ -55,4 +58,30 @@ public class GroupService { return Optional.empty(); } + + /** + * + * @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 respoonses = this.repo + .getAssignedAmounts(groups.stream().filter(Objects::nonNull).map(new Function() { + @Override + public Long apply(Groups p) { + final Long id = p.getId(); + response.put(id, 0); + return id; + } + }).collect(Collectors.toList())); + + 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/main/java/com/plannaplan/services/UserService.java b/buisnesslogic/src/main/java/com/plannaplan/services/UserService.java index f91121f..a9a7c3f 100755 --- a/buisnesslogic/src/main/java/com/plannaplan/services/UserService.java +++ b/buisnesslogic/src/main/java/com/plannaplan/services/UserService.java @@ -34,8 +34,8 @@ public class UserService { return token; } - public void save(User user) { - this.repo.save(user); + public User save(User user) { + return this.repo.save(user); } public User getUserByEmail(String email) throws UserNotFoundException { diff --git a/buisnesslogic/src/test/java/com/plannaplan/repositories/GroupRepositoryTest.java b/buisnesslogic/src/test/java/com/plannaplan/repositories/GroupRepositoryTest.java new file mode 100755 index 0000000..222a279 --- /dev/null +++ b/buisnesslogic/src/test/java/com/plannaplan/repositories/GroupRepositoryTest.java @@ -0,0 +1,106 @@ +package com.plannaplan.repositories; + +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 static org.junit.Assert.assertTrue; + +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.services.AssignmentService; +import com.plannaplan.services.CommisionService; +import com.plannaplan.services.GroupService; +import com.plannaplan.services.UserService; +import com.plannaplan.types.UserRoles; +import com.plannaplan.types.WeekDay; + +import org.junit.Test; + +import org.junit.runner.RunWith; + +@RunWith(SpringRunner.class) +@SpringBootTest +@ContextConfiguration +public class GroupRepositoryTest { + @Autowired + private GroupRepository repository; + @Autowired + private AssignmentService assignmentService; + @Autowired + private GroupService groupService; + @Autowired + private UserService userService; + + @Autowired + private CommisionService commisionService; + + @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(); + + 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); + + } + + @Test + public void shouldReturnGroupAssignmentTimesList() 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); + + List response = this.repository + .getAssignedAmounts(List.of(testGroup.getId(), testGroup2.getId(), testGroup3.getId())); + + assertTrue("Response should have size 3", response.size() == 3); + assertTrue("Instance of firest element should be Group", response.get(0)[0] instanceof Groups); + assertTrue("Instance of firest element should be Long", response.get(0)[1] instanceof Long); + + } + +} diff --git a/buisnesslogic/src/test/java/com/plannaplan/services/AssignmentServiceTest.java b/buisnesslogic/src/test/java/com/plannaplan/services/AssignmentServiceTest.java index b0dd705..1f4bb31 100755 --- a/buisnesslogic/src/test/java/com/plannaplan/services/AssignmentServiceTest.java +++ b/buisnesslogic/src/test/java/com/plannaplan/services/AssignmentServiceTest.java @@ -11,8 +11,9 @@ import java.util.List; import com.plannaplan.entities.Assignment; import com.plannaplan.entities.Commision; +import com.plannaplan.entities.User; +import com.plannaplan.types.UserRoles; -import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -26,24 +27,31 @@ public class AssignmentServiceTest { private AssignmentService service; @Autowired private CommisionService comServie; - private Commision com; - - @Before - public void init() { - this.com = new Commision(); - this.comServie.save(this.com); - } + @Autowired + private UserService userService; @Test public void shouldSaveAssignment() { + final User user = new User("Gibi", "Kovalsky", "shouldSaveAssignment@assignmentservice.test", + UserRoles.STUDENT); + this.userService.save(user); + long beginState = this.service.getAssignmentsAmmount(); - this.addAssignmentToCommision(this.com); + Commision com = new Commision(user); + this.comServie.save(com); + this.addAssignmentToCommision(com); assertTrue("Assign ammount should increase", this.service.getAssignmentsAmmount() > beginState); } @Test public void shouldGetCommisionAssignments() { - this.addAssignmentToCommision(this.com); + final User user = new User("Gibi", "Kovalsky", "shouldGetCommisionAssignments@assignmentservice.test", + UserRoles.STUDENT); + this.userService.save(user); + Commision com = new Commision(user); + this.comServie.save(com); + this.addAssignmentToCommision(com); + final List response = this.service.getCommisionAssignments(com); assertTrue("Returned list size should be 1", response.size() == 1); } diff --git a/buisnesslogic/src/test/java/com/plannaplan/services/CommisionServiceTest.java b/buisnesslogic/src/test/java/com/plannaplan/services/CommisionServiceTest.java index 4799e1f..138bdfd 100755 --- a/buisnesslogic/src/test/java/com/plannaplan/services/CommisionServiceTest.java +++ b/buisnesslogic/src/test/java/com/plannaplan/services/CommisionServiceTest.java @@ -7,8 +7,10 @@ import org.springframework.test.context.junit4.SpringRunner; import static org.junit.Assert.assertTrue; +import com.plannaplan.entities.Assignment; import com.plannaplan.entities.Commision; import com.plannaplan.entities.User; +import com.plannaplan.types.UserRoles; import org.junit.Test; import org.junit.runner.RunWith; @@ -24,10 +26,15 @@ public class CommisionServiceTest { @Autowired private UserService userService; + @Autowired + private AssignmentService assignmentService; + @Test public void shouldSaveCommision() { + User usr = new User(); + this.userService.save(usr); long beginState = this.service.getCommisionsAmmount(); - this.service.save(new Commision()); + this.service.save(new Commision(usr)); assertTrue("Commision ammount should have changed", this.service.getCommisionsAmmount() > beginState); } @@ -57,4 +64,31 @@ public class CommisionServiceTest { assertTrue("Wrong commision was returned", result.getId().equals(newestCommision.getId())); } + @Test + public void shouldMarkAssignmentsOfPreviousCommisionPast() { + final User usr = new User("Trevor", "Hammolt", + "shouldMarkAssignmentsOfPreviousCommisionPast@commisionservice.test", UserRoles.STUDENT); + this.userService.save(usr); + + final Commision firstCommision = new Commision(usr); + this.service.save(firstCommision); + + Assignment firstCommisionAssignment = this.assignmentService.save(new Assignment(null, firstCommision)); + + assertTrue("FirstCommisionAssignment should be flaged as present assignment", + !firstCommisionAssignment.isPastAssignment()); + + final Commision secondCommision = new Commision(usr); + this.service.save(secondCommision); + + Assignment secondCommisionAssignment = new Assignment(null, secondCommision); + this.assignmentService.save(secondCommisionAssignment); + + assertTrue("SecondCommisionAssignment should be flaged as present assignment", + !secondCommisionAssignment.isPastAssignment()); + assertTrue("FirstCommisionAssignment should be flaged as past assignment", + this.assignmentService.getById(firstCommisionAssignment.getId()).get().isPastAssignment()); + + } + } diff --git a/buisnesslogic/src/test/java/com/plannaplan/services/GroupServiceTest.java b/buisnesslogic/src/test/java/com/plannaplan/services/GroupServiceTest.java index 254957e..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() { @@ -31,4 +45,27 @@ public class GroupServiceTest { groupService.delete(group); assertTrue(this.groupService.getGroupsAmmount() == startAmmount); } + + @Test + 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/buisnesslogic/src/test/resources/application.properties b/buisnesslogic/src/test/resources/application.properties index 0477c97..55e1449 100755 --- a/buisnesslogic/src/test/resources/application.properties +++ b/buisnesslogic/src/test/resources/application.properties @@ -7,5 +7,6 @@ spring.jpa.open-in-view=true spring.jpa.hibernate.ddl-auto=create-drop spring.jackson.serialization.fail-on-empty-beans=false spring.main.allow-bean-definition-overriding=true +spring.jackson.default-property-inclusion = NON_NULL server.port=1285 \ No newline at end of file diff --git a/restservice/src/main/java/com/plannaplan/App.java b/restservice/src/main/java/com/plannaplan/App.java index d5b08eb..af81457 100755 --- a/restservice/src/main/java/com/plannaplan/App.java +++ b/restservice/src/main/java/com/plannaplan/App.java @@ -33,7 +33,7 @@ public class App { filip.setEmail("filizy@st.amu.edu.pl"); filip.setName("Filip"); filip.setSurname("Izydorczyk"); - filip.setRole(UserRoles.ADMIN); + filip.setRole(UserRoles.STUDENT); this.userService.save(filip); User hub = new User(); diff --git a/restservice/src/main/java/com/plannaplan/controllers/GroupController.java b/restservice/src/main/java/com/plannaplan/controllers/GroupController.java index 65b6c4e..42d2742 100755 --- a/restservice/src/main/java/com/plannaplan/controllers/GroupController.java +++ b/restservice/src/main/java/com/plannaplan/controllers/GroupController.java @@ -1,5 +1,6 @@ package com.plannaplan.controllers; +import java.util.HashMap; import java.util.List; import com.plannaplan.App; @@ -37,8 +38,20 @@ public class GroupController { @ApiOperation(value = "Return list of lectures and classes (if present) given course") public ResponseEntity> getCourses( @PathVariable(name = "id") Long id, - @RequestParam(name = "capacity", defaultValue = "true") @ApiParam(value = "Boolean if we want to have capacity field in response") Boolean capacity) { + @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); + + if (takenPlaces) { + HashMap ammounts = this.groupService.getTakenPlaces(groups); + if (capacity) { + return new ResponseEntity<>(GroupsMappers.mapToGetCourseGroupsWithCapacityResponse(groups, ammounts), + HttpStatus.OK); + } + return new ResponseEntity<>(GroupsMappers.mapToGetCourseGroupsDefaultResponse(groups, ammounts), + HttpStatus.OK); + } + if (capacity) { return new ResponseEntity<>(GroupsMappers.mapToGetCourseGroupsWithCapacityResponse(groups), HttpStatus.OK); } diff --git a/restservice/src/main/java/com/plannaplan/responses/mappers/GroupsMappers.java b/restservice/src/main/java/com/plannaplan/responses/mappers/GroupsMappers.java index 8fa6c0f..43779d3 100755 --- a/restservice/src/main/java/com/plannaplan/responses/mappers/GroupsMappers.java +++ b/restservice/src/main/java/com/plannaplan/responses/mappers/GroupsMappers.java @@ -1,8 +1,10 @@ package com.plannaplan.responses.mappers; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; import java.util.Objects; +import java.util.function.Function; import java.util.stream.Collectors; import com.plannaplan.entities.Groups; @@ -12,47 +14,100 @@ import com.plannaplan.responses.models.GroupWithCapacityResponse; import com.plannaplan.types.GroupType; public class GroupsMappers { + public static List mapToDefaultResponse(List groups, HashMap taken) { + return groups.stream().filter(Objects::nonNull).map(new Function() { + @Override + public GroupDefaultResponse apply(Groups p) { + if (taken != null) { + return new GroupDefaultResponse(p, taken.get(p.getId())); + } else { + return new GroupDefaultResponse(p); + } + } + }).collect(Collectors.toList()); + } + public static List mapToDefaultResponse(List groups) { - return groups.stream().filter(Objects::nonNull).map(GroupDefaultResponse::new).collect(Collectors.toList()); + return GroupsMappers.mapToDefaultResponse(groups, null); + } + + public static List mapToCapacityResponse(List groups, + HashMap taken) { + return groups.stream().filter(Objects::nonNull).map(new Function() { + @Override + public GroupWithCapacityResponse apply(Groups p) { + if (taken != null) { + return new GroupWithCapacityResponse(p, taken.get(p.getId())); + } else { + return new GroupWithCapacityResponse(p); + } + } + }).collect(Collectors.toList()); } public static List mapToCapacityResponse(List groups) { - return groups.stream().filter(Objects::nonNull).map(GroupWithCapacityResponse::new) - .collect(Collectors.toList()); + return GroupsMappers.mapToCapacityResponse(groups, null); } - - public static CourseWithGroupsResponse mapToGetCourseGroupsDefaultResponse (List groups){ + + public static CourseWithGroupsResponse mapToGetCourseGroupsDefaultResponse( + List groups, HashMap taken) { List lectures = new ArrayList<>(); List classes = new ArrayList<>(); groups.stream().forEach(group -> { if (group.getType() == GroupType.CLASS) { - classes.add(new GroupDefaultResponse(group)); + if (taken != null) { + classes.add(new GroupDefaultResponse(group, taken.get(group.getId()))); + } else { + classes.add(new GroupDefaultResponse(group)); + } } else { - lectures.add(new GroupDefaultResponse(group)); + if (taken != null) { + lectures.add(new GroupDefaultResponse(group, taken.get(group.getId()))); + } else { + lectures.add(new GroupDefaultResponse(group)); + } } }); - return new CourseWithGroupsResponse<>(classes, lectures); } - public static CourseWithGroupsResponse mapToGetCourseGroupsWithCapacityResponse (List groups){ + public static CourseWithGroupsResponse mapToGetCourseGroupsDefaultResponse( + List groups) { + return GroupsMappers.mapToGetCourseGroupsDefaultResponse(groups, null); + } + + public static CourseWithGroupsResponse mapToGetCourseGroupsWithCapacityResponse( + List groups, HashMap taken) { List lectures = new ArrayList<>(); List classes = new ArrayList<>(); groups.stream().forEach(group -> { if (group.getType() == GroupType.CLASS) { - classes.add(new GroupWithCapacityResponse(group)); + if (taken != null) { + classes.add(new GroupWithCapacityResponse(group, taken.get(group.getId()))); + } else { + classes.add(new GroupWithCapacityResponse(group)); + } + } else { - lectures.add(new GroupWithCapacityResponse(group)); + if (taken != null) { + lectures.add(new GroupWithCapacityResponse(group, taken.get(group.getId()))); + } else { + lectures.add(new GroupWithCapacityResponse(group)); + } } }); - return new CourseWithGroupsResponse<>(classes, lectures); } + public static CourseWithGroupsResponse mapToGetCourseGroupsWithCapacityResponse( + List groups) { + return GroupsMappers.mapToGetCourseGroupsWithCapacityResponse(groups, null); + } + } diff --git a/restservice/src/main/java/com/plannaplan/responses/models/GroupDefaultResponse.java b/restservice/src/main/java/com/plannaplan/responses/models/GroupDefaultResponse.java index 165be0f..c182343 100755 --- a/restservice/src/main/java/com/plannaplan/responses/models/GroupDefaultResponse.java +++ b/restservice/src/main/java/com/plannaplan/responses/models/GroupDefaultResponse.java @@ -1,5 +1,6 @@ package com.plannaplan.responses.models; +import com.fasterxml.jackson.annotation.JsonInclude; import com.plannaplan.entities.Assignment; import com.plannaplan.entities.Groups; import com.plannaplan.types.GroupType; @@ -7,6 +8,7 @@ import com.plannaplan.types.GroupType; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; +@JsonInclude(JsonInclude.Include.NON_NULL) @ApiModel(description = "Response shows information about given group.", value = "GroupDefaultResponse") public class GroupDefaultResponse { @@ -28,6 +30,9 @@ public class GroupDefaultResponse { @ApiModelProperty(value = "Value shows kind of group. The types are LECTURE or CLASS.") private GroupType type; + @ApiModelProperty(value = "Value shows how many places is already taken by other students.") + private Integer takenPlaces; + public GroupDefaultResponse(Groups group) { this.id = group.getId() != null ? group.getId() : null; this.day = group.getDay() != null ? group.getDay().label : -1; @@ -37,6 +42,11 @@ public class GroupDefaultResponse { this.type = group.getType() != null ? group.getType() : null; } + public GroupDefaultResponse(Groups group, int takenPlaces) { + this(group); + this.takenPlaces = takenPlaces; + } + public GroupDefaultResponse(Assignment assignment) { this(assignment.getGroup()); } @@ -65,4 +75,8 @@ public class GroupDefaultResponse { return id; } + public Integer getTakenPlaces() { + return this.takenPlaces; + } + } diff --git a/restservice/src/main/java/com/plannaplan/responses/models/GroupWithCapacityResponse.java b/restservice/src/main/java/com/plannaplan/responses/models/GroupWithCapacityResponse.java index 8e7a1f8..1ca9fc2 100755 --- a/restservice/src/main/java/com/plannaplan/responses/models/GroupWithCapacityResponse.java +++ b/restservice/src/main/java/com/plannaplan/responses/models/GroupWithCapacityResponse.java @@ -14,10 +14,19 @@ public class GroupWithCapacityResponse extends GroupDefaultResponse { this.capacity = group.getCapacity(); } + public GroupWithCapacityResponse(Groups group, int takenPlaces) { + super(group, takenPlaces); + this.capacity = group.getCapacity(); + } + public GroupWithCapacityResponse(Assignment assignment) { this(assignment.getGroup()); } + public GroupWithCapacityResponse(Assignment assignment, int takenPlaces) { + this(assignment.getGroup(), takenPlaces); + } + public int getCapacity() { return capacity; } diff --git a/restservice/src/test/java/com/plannaplan/controllers/ConfigControllerTest.java b/restservice/src/test/java/com/plannaplan/controllers/ConfigControllerTest.java index 2c445af..9c56c69 100755 --- a/restservice/src/test/java/com/plannaplan/controllers/ConfigControllerTest.java +++ b/restservice/src/test/java/com/plannaplan/controllers/ConfigControllerTest.java @@ -28,7 +28,6 @@ public class ConfigControllerTest extends AbstractControllerTest { private static final String FILE_NAME = "Zajecia.xlsx"; private static final String CONFIG_ENDPOINT = "/api/v1/configurator/config"; - private static final String TEST_MAIL = "notexisting@mail.domain"; @Autowired private UserService service; @@ -45,12 +44,13 @@ public class ConfigControllerTest extends AbstractControllerTest { @Test public void shouldReturnOKAuthorized() throws Exception { - final User usr = new User(null, null, TEST_MAIL, UserRoles.ADMIN); + final String mail = "shouldReturnOKAuthorized@ConfigController.test"; + final User usr = new User(null, null, mail, UserRoles.ADMIN); this.service.save(usr); final InputStream inputStream = getClass().getClassLoader().getResourceAsStream(FILE_NAME); final MockMultipartFile file = new MockMultipartFile("file", inputStream); - final String token = this.service.login(TEST_MAIL); + final String token = this.service.login(mail); MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); mockMvc.perform(multipart(CONFIG_ENDPOINT).file(file).header("Authorization", "Bearer " + token)) @@ -58,4 +58,20 @@ public class ConfigControllerTest extends AbstractControllerTest { } + @Test + public void shouldReturnDenyNoAdminAuthorized() throws Exception { + final String mail = "shouldReturnDenyNoAdminAuthorized@ConfigController.test"; + final User usr = new User(null, null, mail, UserRoles.TEST_USER); + this.service.save(usr); + + final InputStream inputStream = getClass().getClassLoader().getResourceAsStream(FILE_NAME); + final MockMultipartFile file = new MockMultipartFile("file", inputStream); + final String token = this.service.login(mail); + + MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); + mockMvc.perform(multipart(CONFIG_ENDPOINT).file(file).header("Authorization", "Bearer " + token)) + .andExpect(status().is4xxClientError()); + + } + } diff --git a/restservice/src/test/java/com/plannaplan/responses/mappers/GroupsMappersTest.java b/restservice/src/test/java/com/plannaplan/responses/mappers/GroupsMappersTest.java index e41fe0a..174c147 100755 --- a/restservice/src/test/java/com/plannaplan/responses/mappers/GroupsMappersTest.java +++ b/restservice/src/test/java/com/plannaplan/responses/mappers/GroupsMappersTest.java @@ -2,7 +2,9 @@ package com.plannaplan.responses.mappers; import static org.junit.Assert.assertTrue; +import java.lang.reflect.Field; import java.util.Arrays; +import java.util.HashMap; import java.util.List; import com.plannaplan.entities.Groups; @@ -15,38 +17,151 @@ import com.plannaplan.types.WeekDay; import org.junit.Test; public class GroupsMappersTest { - @Test - public void shouldMapToResponseIncludingCapacity() { - final List gropus = Arrays.asList( - new Groups(42, "A4-1", null, 520, WeekDay.MONDAY, new Lecturer("krul.", "Wladyslaw", "Potocki"))); - final List response = GroupsMappers.mapToCapacityResponse(gropus); + @Test + public void shouldMapToResponseIncludingCapacity() { + final List gropus = Arrays.asList(new Groups(42, "A4-1", null, 520, WeekDay.MONDAY, + new Lecturer("krul.", "Wladyslaw", "Potocki"))); + final List response = GroupsMappers.mapToCapacityResponse(gropus); - assert (response.get(0).getCapacity() == 42); - assert (response.get(0) instanceof GroupWithCapacityResponse); - assert (response.size() == 1); - } + assert (response.get(0).getCapacity() == 42); + assert (response.get(0) instanceof GroupWithCapacityResponse); + assert (response.size() == 1); + } - @Test - public void shouldMapToResponseWiothoutCapacity() { - final List gropus = Arrays.asList( - new Groups(42, "A4-1", null, 520, WeekDay.MONDAY, new Lecturer("krul.", "Wladyslaw", "Potocki"))); - final List response = GroupsMappers.mapToDefaultResponse(gropus); + @Test + public void shouldMapToResponseWiothoutCapacity() { + final List gropus = Arrays.asList(new Groups(42, "A4-1", null, 520, WeekDay.MONDAY, + new Lecturer("krul.", "Wladyslaw", "Potocki"))); + final List response = GroupsMappers.mapToDefaultResponse(gropus); - assert (response.get(0) instanceof GroupDefaultResponse); - assert (response.size() == 1); - } + assert (response.get(0) instanceof GroupDefaultResponse); + assert (response.size() == 1); + } - @Test - public void shouldMapToGetCourseGroupsResponse(){ - final List groups = Arrays.asList( - new Groups(150, "A4-1", null, 520, WeekDay.MONDAY, new Lecturer("krul.", "Wladyslaw", "Potocki")), new Groups(24, "A4-1", null, 520, WeekDay.MONDAY, new Lecturer("krul.", "Wladyslaw", "Potocki"))); - - final CourseWithGroupsResponse response = GroupsMappers.mapToGetCourseGroupsDefaultResponse(groups); + @Test + public void shouldMapToGetCourseGroupsResponse() { + final List groups = Arrays.asList( + new Groups(150, "A4-1", null, 520, WeekDay.MONDAY, + new Lecturer("krul.", "Wladyslaw", "Potocki")), + new Groups(24, "A4-1", null, 520, WeekDay.MONDAY, + new Lecturer("krul.", "Wladyslaw", "Potocki"))); - assertTrue(response.getClasses().size() == 1); - assertTrue(response.getLectures().size() == 1); + final CourseWithGroupsResponse response = GroupsMappers + .mapToGetCourseGroupsDefaultResponse(groups); + + assertTrue(response.getClasses().size() == 1); + assertTrue(response.getLectures().size() == 1); } - -} + @Test + public void shouldMapToGetCourseGroupsResponseWithTakenPlaces() throws NoSuchFieldException, SecurityException, + IllegalArgumentException, IllegalAccessException { + final Field reader = Groups.class.getDeclaredField("id"); + reader.setAccessible(true); + + final List groups = Arrays.asList( + new Groups(150, "A4-1", null, 520, WeekDay.MONDAY, + new Lecturer("krul.", "Wladyslaw", "Potocki")), + new Groups(24, "A4-1", null, 520, WeekDay.MONDAY, + new Lecturer("krul.", "Wladyslaw", "Potocki"))); + reader.set(groups.get(0), Long.valueOf(0)); + reader.set(groups.get(1), Long.valueOf(1)); + + final HashMap placeMap = new HashMap<>(); + placeMap.put(Long.valueOf(0), 0); + placeMap.put(Long.valueOf(1), 21); + + final CourseWithGroupsResponse response = GroupsMappers + .mapToGetCourseGroupsDefaultResponse(groups, placeMap); + + assertTrue(response.getClasses().size() == 1); + assertTrue(response.getLectures().size() == 1); + assertTrue(response.getLectures().get(0).getTakenPlaces() == 0); + assertTrue(response.getClasses().get(0).getTakenPlaces() == 21); + } + + @Test + public void shouldMapToGetCourseCapacityGroupsResponseWithTakenPlaces() throws NoSuchFieldException, + SecurityException, IllegalArgumentException, IllegalAccessException { + final Field reader = Groups.class.getDeclaredField("id"); + reader.setAccessible(true); + + final List groups = Arrays.asList( + new Groups(150, "A4-1", null, 520, WeekDay.MONDAY, + new Lecturer("krul.", "Wladyslaw", "Potocki")), + new Groups(24, "A4-1", null, 520, WeekDay.MONDAY, + new Lecturer("krul.", "Wladyslaw", "Potocki"))); + reader.set(groups.get(0), Long.valueOf(0)); + reader.set(groups.get(1), Long.valueOf(1)); + + final HashMap placeMap = new HashMap<>(); + placeMap.put(Long.valueOf(0), 0); + placeMap.put(Long.valueOf(1), 21); + + final CourseWithGroupsResponse response = GroupsMappers + .mapToGetCourseGroupsWithCapacityResponse(groups, placeMap); + + assertTrue(response.getClasses().size() == 1); + assertTrue(response.getLectures().size() == 1); + assertTrue(response.getLectures().get(0).getTakenPlaces() == 0); + assertTrue(response.getLectures().get(0).getCapacity() == 150); + assertTrue(response.getClasses().get(0).getTakenPlaces() == 21); + assertTrue(response.getClasses().get(0).getCapacity() == 24); + } + + @Test + public void shouldMapToDefaultResponseWithTakenPlace() throws NoSuchFieldException, SecurityException, + IllegalArgumentException, IllegalAccessException { + final Field reader = Groups.class.getDeclaredField("id"); + reader.setAccessible(true); + + final Groups group1 = new Groups(150, "A4-1", null, 520, WeekDay.MONDAY, + new Lecturer("krul.", "Wladyslaw", "Potocki")); + final Groups group2 = new Groups(24, "A4-1", null, 520, WeekDay.MONDAY, + new Lecturer("krul.", "Wladyslaw", "Potocki")); + + reader.set(group1, Long.valueOf(0)); + reader.set(group2, Long.valueOf(1)); + + final HashMap placeMap = new HashMap<>(); + placeMap.put(Long.valueOf(0), 5); + placeMap.put(Long.valueOf(1), 56); + + final List response = GroupsMappers.mapToDefaultResponse(List.of(group1, group2), + placeMap); + + assertTrue(response.size() == 2); + assertTrue(response.get(0).getTakenPlaces() == 5); + assertTrue(response.get(1).getTakenPlaces() == 56); + } + + @Test + public void shouldMapToCapacityResponseWithTakenPlace() throws NoSuchFieldException, SecurityException, + IllegalArgumentException, IllegalAccessException { + final Field reader = Groups.class.getDeclaredField("id"); + reader.setAccessible(true); + + final Groups group1 = new Groups(150, "A4-1", null, 520, WeekDay.MONDAY, + new Lecturer("krul.", "Wladyslaw", "Potocki")); + final Groups group2 = new Groups(24, "A4-1", null, 520, WeekDay.MONDAY, + new Lecturer("krul.", "Wladyslaw", "Potocki")); + + reader.set(group1, Long.valueOf(0)); + reader.set(group2, Long.valueOf(1)); + + final HashMap placeMap = new HashMap<>(); + placeMap.put(Long.valueOf(0), 5); + placeMap.put(Long.valueOf(1), 56); + + final List response = GroupsMappers + .mapToCapacityResponse(List.of(group1, group2), placeMap); + + assertTrue(response.size() == 2); + assertTrue(response.get(0).getTakenPlaces() == 5); + assertTrue(response.get(0).getCapacity() == 150); + assertTrue(response.get(1).getTakenPlaces() == 56); + assertTrue(response.get(1).getCapacity() == 24); + } + +}