package com.plannaplan.services; import static org.junit.Assert.assertTrue; import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; import com.plannaplan.entities.Assignment; import com.plannaplan.entities.Commision; import com.plannaplan.entities.Exchange; import com.plannaplan.entities.Groups; import com.plannaplan.entities.User; import com.plannaplan.models.MatchData; import com.plannaplan.repositories.CommisionRepository; import com.plannaplan.types.UserRoles; import com.plannaplan.types.WeekDay; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.annotation.DirtiesContext.MethodMode; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) @SpringBootTest @ContextConfiguration public class ExchangeServiceTest { @Autowired private AssignmentService assignmentService; @Autowired private GroupService groupService; @Autowired private UserService userService; @Autowired private CommisionRepository commisionRepository; @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 List 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(); Thread.sleep(1000); user2 = this.userService.getById(user2Id).get(); Thread.sleep(1000); user3 = this.userService.getById(user3Id).get(); Thread.sleep(1000); user4 = this.userService.getById(user4Id).get(); Thread.sleep(1000); 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)); Thread.sleep(1000); this.exchangeService.save(new Exchange(assignmentUser2, group1)); Thread.sleep(1000); this.exchangeService.save(new Exchange(assignmentUser1, group3)); Thread.sleep(1000); this.exchangeService.save(new Exchange(assignmentUser1, group4)); Thread.sleep(1000); 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() == 2); } @Test @DirtiesContext(methodMode = MethodMode.BEFORE_METHOD) 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); listMatrix.add(new MatchData(exchange2, exchange4)); listMatrix.add(new MatchData(exchange1, exchange3)); final List matchDataListSorted = listMatrix.stream().sorted((m1, m2) -> -1 * m1.compare(m2)).collect(Collectors.toList()); assertTrue(listMatrix.get(0).equals(matchDataListSorted.get(1))); assertTrue(listMatrix.get(1).equals(matchDataListSorted.get(0))); assertTrue(matchDataListSorted.size() == 2); } }