diff --git a/buisnesslogic/src/main/java/com/plannaplan/entities/Exchange.java b/buisnesslogic/src/main/java/com/plannaplan/entities/Exchange.java index 9ee37b8..44a4d3f 100644 --- a/buisnesslogic/src/main/java/com/plannaplan/entities/Exchange.java +++ b/buisnesslogic/src/main/java/com/plannaplan/entities/Exchange.java @@ -1,5 +1,8 @@ package com.plannaplan.entities; +import java.sql.Date; +import java.sql.Timestamp; + import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; @@ -18,7 +21,7 @@ public class Exchange { private Long id; @OneToOne - @JoinColumn(name = "owned_id", unique = true) + @JoinColumn(name = "owned_id") private Assignment ownedAssignment; @OneToOne @@ -27,67 +30,74 @@ public class Exchange { private Long ownerId; + private Timestamp dateExchange; + public Exchange(){ - + } - /** + /** * @return Long ID of exchange trader */ public Long getOwnerId() { return ownerId; } - - /** + + /** * @param ownerId ID of exchange trader */ public void setOwnerId(Long ownerId) { this.ownerId = ownerId; } + public Timestamp getDataExchange() { + return this.dateExchange; + } + /** - * + * * @param ownedAssignment Assignment which owner would like to trade * @param desiredAssignment Groups instance that trader wants */ public Exchange(Assignment ownedAssignment, Groups desiredAssignment) { this.ownedAssignment = ownedAssignment; this.desiredAssignment = desiredAssignment; - this.ownerId = this.ownedAssignment.getCommision().getCommisionOwner().getId(); + this.ownerId = this.ownedAssignment != null ? this.ownedAssignment.getCommision().getCommisionOwner().getId() : null; + this.dateExchange = new Timestamp(System.currentTimeMillis()); } - /** + /** * @return Long ID in database */ public Long getId(){ return this.id; } - - /** + + /** * @return Groups Target group */ public Groups getDesiredAssignment() { return desiredAssignment; } - /** + /** * @param desiredAssignment Target group */ public void setDesiredAssignment(Groups desiredAssignment) { this.desiredAssignment = desiredAssignment; } - /** + /** * @return Assignment Owned assignment */ public Assignment getOwnedAssignment() { return ownedAssignment; } - /** + /** * @param ownedAssignment Owned assignment */ public void setOwnedAssignment(Assignment ownedAssignment) { this.ownedAssignment = ownedAssignment; } -} \ No newline at end of file +} diff --git a/buisnesslogic/src/main/java/com/plannaplan/models/MatchData.java b/buisnesslogic/src/main/java/com/plannaplan/models/MatchData.java index 320cf46..93dbec6 100644 --- a/buisnesslogic/src/main/java/com/plannaplan/models/MatchData.java +++ b/buisnesslogic/src/main/java/com/plannaplan/models/MatchData.java @@ -1,5 +1,7 @@ package com.plannaplan.models; +import java.sql.Timestamp; + import com.plannaplan.entities.Assignment; import com.plannaplan.entities.Exchange; @@ -55,4 +57,12 @@ public class MatchData { // Compare the data members and return accordingly return (this.getAssignmentOne().equals(c.getAssignmentOne()) && this.getAssignmentTwo().equals(c.getAssignmentTwo())) || (this.getAssignmentOne().equals(c.getAssignmentTwo()) && this.getAssignmentTwo().equals(c.getAssignmentOne())); } + + public int compare(MatchData m1) { + return Float.compare(m1.getExchangesMsValue(), this.getExchangesMsValue()); + } + + public float getExchangesMsValue(){ + return this.exchangeOne.getDataExchange().getTime() + this.exchangeTwo.getDataExchange().getTime(); + } } diff --git a/buisnesslogic/src/main/java/com/plannaplan/services/ExchangeService.java b/buisnesslogic/src/main/java/com/plannaplan/services/ExchangeService.java index d142bef..c87c0fa 100644 --- a/buisnesslogic/src/main/java/com/plannaplan/services/ExchangeService.java +++ b/buisnesslogic/src/main/java/com/plannaplan/services/ExchangeService.java @@ -1,10 +1,13 @@ package com.plannaplan.services; +import java.sql.Timestamp; import java.util.ArrayList; +import java.util.Comparator; import java.util.HashSet; import java.util.List; import java.util.Optional; import java.util.Set; +import java.util.TreeSet; import java.util.stream.Collectors; import com.plannaplan.entities.Assignment; @@ -116,9 +119,11 @@ public class ExchangeService { final Exchange exchangeOne = (Exchange) m[0]; final Exchange exchangeTwo = (Exchange) m[1]; return new MatchData(exchangeOne, exchangeTwo); + }).collect(Collectors.toList()); - final Set filterMatches = new HashSet<>(matches); + Set filterMatches = new TreeSet<>((m1, m2) -> m1.compare(m2)); + filterMatches.addAll(matches); return filterMatches; } } diff --git a/buisnesslogic/src/test/java/com/plannaplan/services/ExchangeServiceTest.java b/buisnesslogic/src/test/java/com/plannaplan/services/ExchangeServiceTest.java index 2669b89..7336772 100644 --- a/buisnesslogic/src/test/java/com/plannaplan/services/ExchangeServiceTest.java +++ b/buisnesslogic/src/test/java/com/plannaplan/services/ExchangeServiceTest.java @@ -2,8 +2,11 @@ package com.plannaplan.services; import static org.junit.Assert.assertTrue; +import java.util.ArrayList; +import java.util.Iterator; import java.util.List; import java.util.Set; +import java.util.TreeSet; import java.util.stream.Collectors; import com.plannaplan.entities.Assignment; @@ -16,7 +19,8 @@ import com.plannaplan.repositories.CommisionRepository; import com.plannaplan.types.UserRoles; import com.plannaplan.types.WeekDay; -import org.junit.Ignore; +import org.apache.commons.compress.utils.Lists; +import org.assertj.core.util.Arrays; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; @@ -31,141 +35,177 @@ import org.springframework.test.context.junit4.SpringRunner; @ContextConfiguration public class ExchangeServiceTest { - @Autowired - private AssignmentService assignmentService; + @Autowired + private AssignmentService assignmentService; - @Autowired - private GroupService groupService; + @Autowired + private GroupService groupService; - @Autowired - private UserService userService; + @Autowired + private UserService userService; - @Autowired - private CommisionRepository commisionRepository; + @Autowired + private CommisionRepository commisionRepository; - @Autowired - private ExchangeService exchangeService; + @Autowired + private ExchangeService exchangeService; + + @Test + @DirtiesContext(methodMode = MethodMode.BEFORE_METHOD) + public void shouldReturnUniqMatches() { + final User user1 = this.userService.save( + new User(null, null, "shouldReturnMatches@ExchangeRepository.test", "123454", UserRoles.STUDENT, 451)); + final Groups group1 = this.groupService.save(new Groups(123, "A2-3", null, 430, WeekDay.MONDAY, null)); + final Commision commision1 = this.commisionRepository.save(new Commision(user1)); + final User user2 = this.userService.save( + new User(null, null, "shouldReturnMatches@ExchangeRepository.test", "123454", UserRoles.STUDENT, 451)); + final Groups group2 = this.groupService.save(new Groups(123, "A2-3", null, 430, WeekDay.MONDAY, null)); + final Commision commision2 = this.commisionRepository.save(new Commision(user2)); + final Assignment assignmentUser1 = this.assignmentService.save(new Assignment(group1, commision1)); + final Assignment assignmentUser2 = this.assignmentService.save(new Assignment(group2, commision2)); + + this.assignmentService.callAcceptAlgorythm(); + + this.exchangeService.save(new Exchange(assignmentUser1, group2)); + this.exchangeService.save(new Exchange(assignmentUser2, group1)); + + final Set uniqList = this.exchangeService.getMatches(); + assertTrue(uniqList.size() == 1); + } + + @Test + @DirtiesContext(methodMode = MethodMode.BEFORE_METHOD) + public void shouldPerformExchange() throws Exception{ + User user1 = this.userService.save( + new User(null, null, "1shouldReturnMatches@ExchangeRepository.test", "123454", UserRoles.STUDENT, 451)); + final Long user1Id = user1.getId(); + final Groups group1 = this.groupService.save(new Groups(123, "A2-3", null, 430, WeekDay.MONDAY, null)); + final Commision commision1 = this.commisionRepository.save(new Commision(user1)); + + User user2 = this.userService.save( + new User(null, null, "2shouldReturnMatches@ExchangeRepository.test", "123455", UserRoles.STUDENT, 452)); + final Long user2Id = user2.getId(); + final Groups group2 = this.groupService.save(new Groups(123, "A2-3", null, 430, WeekDay.THURSDAY, null)); + final Commision commision2 = this.commisionRepository.save(new Commision(user2)); + + User user3 = this.userService.save( + new User(null, null, "3shouldReturnMatches@ExchangeRepository.test", "123456", UserRoles.STUDENT, 453)); + final Long user3Id = user3.getId(); + final Groups group3 = this.groupService.save(new Groups(123, "A2-3", null, 430, WeekDay.WEDNESDAY, null)); + final Commision commision3 = this.commisionRepository.save(new Commision(user3)); + + User user4 = this.userService.save( + new User(null, null, "2shouldReturnMatches@ExchangeRepository.test", "123457", UserRoles.STUDENT, 455)); + final Long user4Id = user4.getId(); + final Groups group4 = this.groupService.save(new Groups(123, "A2-3", null, 430, WeekDay.FRIDAY, null)); + final Commision commision4 = this.commisionRepository.save(new Commision(user4)); + + final Assignment assignmentUser1 = this.assignmentService.save(new Assignment(group1, commision1)); + final Assignment assignmentUser2 = this.assignmentService.save(new Assignment(group2, commision2)); + final Assignment assignmentUser3 = this.assignmentService.save(new Assignment(group2, commision3)); + final Assignment assignmentUser4 = this.assignmentService.save(new Assignment(group4, commision4)); + + this.assignmentService.callAcceptAlgorythm(); + + this.exchangeService.save(new Exchange(assignmentUser1, group2)); + Thread.sleep(1000); + this.exchangeService.save(new Exchange(assignmentUser2, group1)); + Thread.sleep(1000); + this.exchangeService.save(new Exchange(assignmentUser3, group1)); + Thread.sleep(1000); + this.exchangeService.save(new Exchange(assignmentUser4, group3)); + + this.exchangeService.performExchange(); + + user1 = this.userService.getById(user1Id).get(); + user2 = this.userService.getById(user2Id).get(); + user3 = this.userService.getById(user3Id).get(); + user4 = this.userService.getById(user4Id).get(); + + final List listGroupsOfUser1 = user1.getStudentRegisteredGrups().stream().map(Groups::getId) + .collect(Collectors.toList()); + final List listGroupsOfUser2 = user2.getStudentRegisteredGrups().stream().map(Groups::getId) + .collect(Collectors.toList()); + final List listGroupsOfUser3 = user3.getStudentRegisteredGrups().stream().map(Groups::getId) + .collect(Collectors.toList()); + final List listGroupsOfUser4 = user4.getStudentRegisteredGrups().stream().map(Groups::getId) + .collect(Collectors.toList()); + + assertTrue(listGroupsOfUser1.contains(group2.getId())); + assertTrue(listGroupsOfUser2.contains(group1.getId())); + assertTrue(listGroupsOfUser3.contains(group2.getId())); + assertTrue(listGroupsOfUser4.contains(group4.getId())); + } + + @Test + @DirtiesContext(methodMode = MethodMode.BEFORE_METHOD) + public void shouldRemoveOutDatedExchnages() throws Exception { + User user1 = this.userService.save( + new User(null, null, "1shouldReturnMatches@ExchangeRepository.test", "123454", UserRoles.STUDENT, 451)); + final Long user1Id = user1.getId(); + final Groups group1 = this.groupService.save(new Groups(123, "A2-3", null, 430, WeekDay.MONDAY, null)); + final Commision commision1 = this.commisionRepository.save(new Commision(user1)); + + User user2 = this.userService.save( + new User(null, null, "2shouldReturnMatches@ExchangeRepository.test", "123455", UserRoles.STUDENT, 452)); + final Long user2Id = user2.getId(); + final Groups group2 = this.groupService.save(new Groups(123, "A2-3", null, 430, WeekDay.THURSDAY, null)); + final Commision commision2 = this.commisionRepository.save(new Commision(user2)); + + final Groups group3 = this.groupService.save(new Groups(123, "A2-3", null, 430, WeekDay.THURSDAY, null)); + final Groups group4 = this.groupService.save(new Groups(123, "A2-3", null, 430, WeekDay.THURSDAY, null)); + + final Assignment assignmentUser1 = this.assignmentService.save(new Assignment(group1, commision1)); + final Assignment assignmentUser2 = this.assignmentService.save(new Assignment(group2, commision2)); + + this.assignmentService.callAcceptAlgorythm(); + + this.exchangeService.save(new Exchange(assignmentUser1, group2)); + this.exchangeService.save(new Exchange(assignmentUser2, group1)); + this.exchangeService.save(new Exchange(assignmentUser1, group3)); + this.exchangeService.save(new Exchange(assignmentUser1, group4)); + + this.exchangeService.performExchange(); + + user1 = this.userService.getById(user1Id).get(); + user2 = this.userService.getById(user2Id).get(); + + final List listGroupsOfUser1 = user1.getStudentRegisteredGrups().stream().map(Groups::getId) + .collect(Collectors.toList()); + final List listGroupsOfUser2 = user2.getStudentRegisteredGrups().stream().map(Groups::getId) + .collect(Collectors.toList()); + + assertTrue(listGroupsOfUser1.contains(group2.getId())); + assertTrue(listGroupsOfUser2.contains(group1.getId())); + assertTrue(this.exchangeService.getAllExchanges().size() == 0); + } @Test - @DirtiesContext(methodMode = MethodMode.BEFORE_METHOD) - public void shouldReturnUniqMatches() { - final User user1 = this.userService.save(new User(null, null, "shouldReturnMatches@ExchangeRepository.test", "123454", UserRoles.STUDENT , 451)); - final Groups group1 = this.groupService.save(new Groups(123, "A2-3", null, 430, WeekDay.MONDAY, null)); - final Commision commision1 = this.commisionRepository.save(new Commision(user1)); - final User user2 = this.userService.save(new User(null, null, "shouldReturnMatches@ExchangeRepository.test", "123454", UserRoles.STUDENT , 451)); - final Groups group2 = this.groupService.save(new Groups(123, "A2-3", null, 430, WeekDay.MONDAY, null)); - final Commision commision2 = this.commisionRepository.save(new Commision(user2)); - final Assignment assignmentUser1 = this.assignmentService.save(new Assignment(group1, commision1)); - final Assignment assignmentUser2 = this.assignmentService.save(new Assignment(group2, commision2)); + public void shouldSortExchanges() throws Exception{ + final List listMatrix = new ArrayList<>(); + final Exchange exchange1 = new Exchange(null, null); + Thread.sleep(1000); + final Exchange exchange2 = new Exchange(null, null); + Thread.sleep(1000); + final Exchange exchange3 = new Exchange(null, null); + Thread.sleep(1000); + final Exchange exchange4 = new Exchange(null, null); + Thread.sleep(1000); - this.assignmentService.callAcceptAlgorythm(); + listMatrix.add(new MatchData(exchange1, exchange3)); + listMatrix.add(new MatchData(exchange2, exchange4)); - this.exchangeService.save(new Exchange(assignmentUser1, group2)); - this.exchangeService.save(new Exchange(assignmentUser2, group1)); + Set filterMatches = new TreeSet<>((m1, m2) -> m1.compare(m2)); + filterMatches.addAll(listMatrix); + + Iterator iterator = filterMatches.iterator(); + + while (iterator.hasNext()) { + MatchData matchData = iterator.next(); + System.out.println(matchData.getExchangesMsValue()); + } final Set uniqList = this.exchangeService.getMatches(); - assertTrue(uniqList.size() == 1); - } - - @Test - @DirtiesContext(methodMode = MethodMode.BEFORE_METHOD) - @Ignore - public void shouldPerformExchange() { - User user1 = this.userService.save( - new User(null, null, "1shouldReturnMatches@ExchangeRepository.test", "123454", UserRoles.STUDENT, 451)); - final Long user1Id = user1.getId(); - final Groups group1 = this.groupService.save(new Groups(123, "A2-3", null, 430, WeekDay.MONDAY, null)); - final Commision commision1 = this.commisionRepository.save(new Commision(user1)); - - User user2 = this.userService.save( - new User(null, null, "2shouldReturnMatches@ExchangeRepository.test", "123455", UserRoles.STUDENT, 452)); - final Long user2Id = user2.getId(); - final Groups group2 = this.groupService.save(new Groups(123, "A2-3", null, 430, WeekDay.THURSDAY, null)); - final Commision commision2 = this.commisionRepository.save(new Commision(user2)); - - User user3 = this.userService.save( - new User(null, null, "3shouldReturnMatches@ExchangeRepository.test", "123456", UserRoles.STUDENT, 453)); - final Long user3Id = user3.getId(); - final Groups group3 = this.groupService.save(new Groups(123, "A2-3", null, 430, WeekDay.WEDNESDAY, null)); - final Commision commision3 = this.commisionRepository.save(new Commision(user3)); - - User user4 = this.userService.save( - new User(null, null, "2shouldReturnMatches@ExchangeRepository.test", "123457", UserRoles.STUDENT, 455)); - final Long user4Id = user4.getId(); - final Groups group4 = this.groupService.save(new Groups(123, "A2-3", null, 430, WeekDay.FRIDAY, null)); - final Commision commision4 = this.commisionRepository.save(new Commision(user4)); - - final Assignment assignmentUser1 = this.assignmentService.save(new Assignment(group1, commision1)); - final Assignment assignmentUser2 = this.assignmentService.save(new Assignment(group2, commision2)); - final Assignment assignmentUser3 = this.assignmentService.save(new Assignment(group2, commision3)); - final Assignment assignmentUser4 = this.assignmentService.save(new Assignment(group4, commision4)); - - this.assignmentService.callAcceptAlgorythm(); - - this.exchangeService.save(new Exchange(assignmentUser1, group2)); - this.exchangeService.save(new Exchange(assignmentUser2, group1)); - this.exchangeService.save(new Exchange(assignmentUser3, group1)); - - this.exchangeService.save(new Exchange(assignmentUser4, group3)); - - this.exchangeService.performExchange(); - - user1 = this.userService.getById(user1Id).get(); - user2 = this.userService.getById(user2Id).get(); - user3 = this.userService.getById(user3Id).get(); - user4 = this.userService.getById(user4Id).get(); - - final List listGroupsOfUser1 = user1.getStudentRegisteredGrups().stream().map(Groups::getId) - .collect(Collectors.toList()); - final List listGroupsOfUser2 = user2.getStudentRegisteredGrups().stream().map(Groups::getId) - .collect(Collectors.toList()); - final List listGroupsOfUser3 = user3.getStudentRegisteredGrups().stream().map(Groups::getId) - .collect(Collectors.toList()); - final List listGroupsOfUser4 = user4.getStudentRegisteredGrups().stream().map(Groups::getId) - .collect(Collectors.toList()); - - assertTrue(listGroupsOfUser1.contains(group2.getId())); - assertTrue(listGroupsOfUser2.contains(group1.getId())); - assertTrue(listGroupsOfUser3.contains(group2.getId())); - assertTrue(listGroupsOfUser4.contains(group4.getId())); - } - - @Test - @DirtiesContext(methodMode = MethodMode.BEFORE_METHOD) - public void shouldRemoveOutDatedExchnages() { - User user1 = this.userService.save(new User(null, null, "1shouldReturnMatches@ExchangeRepository.test", "123454", UserRoles.STUDENT , 451)); - final Long user1Id = user1.getId(); - final Groups group1 = this.groupService.save(new Groups(123, "A2-3", null, 430, WeekDay.MONDAY, null)); - final Commision commision1 = this.commisionRepository.save(new Commision(user1)); - - User user2 = this.userService.save(new User(null, null, "2shouldReturnMatches@ExchangeRepository.test", "123455", UserRoles.STUDENT, 452)); - final Long user2Id = user2.getId(); - final Groups group2 = this.groupService.save(new Groups(123, "A2-3", null, 430, WeekDay.THURSDAY, null)); - final Commision commision2 = this.commisionRepository.save(new Commision(user2)); - - final Groups group3 = this.groupService.save(new Groups(123, "A2-3", null, 430, WeekDay.THURSDAY, null)); - final Groups group4 = this.groupService.save(new Groups(123, "A2-3", null, 430, WeekDay.THURSDAY, null)); - - final Assignment assignmentUser1 = this.assignmentService.save(new Assignment(group1, commision1)); - final Assignment assignmentUser2 = this.assignmentService.save(new Assignment(group2, commision2)); - - this.assignmentService.callAcceptAlgorythm(); - - this.exchangeService.save(new Exchange(assignmentUser1, group2)); - this.exchangeService.save(new Exchange(assignmentUser2, group1)); - this.exchangeService.save(new Exchange(assignmentUser1, group3)); - this.exchangeService.save(new Exchange(assignmentUser1, group4)); - - this.exchangeService.performExchange(); - - user1 = this.userService.getById(user1Id).get(); - user2 = this.userService.getById(user2Id).get(); - - final List listGroupsOfUser1 = user1.getStudentRegisteredGrups().stream().map(Groups::getId).collect(Collectors.toList()); - final List listGroupsOfUser2 = user2.getStudentRegisteredGrups().stream().map(Groups::getId).collect(Collectors.toList()); - - assertTrue(listGroupsOfUser1.contains(group2.getId())); - assertTrue(listGroupsOfUser2.contains(group1.getId())); - assertTrue(this.exchangeService.getAllExchanges().size() == 0); + assertTrue(uniqList.size() == 2); } }