Checkpoint: ExchangeServiceTest

Signed-off-by: Marcin Woźniak <y0rune@aol.com>
This commit is contained in:
Marcin Woźniak 2021-01-13 12:23:00 +01:00
parent f9a27abb32
commit ed528ad9a0
Signed by: y0rune
GPG Key ID: F204C385F57EB348
4 changed files with 207 additions and 142 deletions

View File

@ -1,5 +1,8 @@
package com.plannaplan.entities; package com.plannaplan.entities;
import java.sql.Date;
import java.sql.Timestamp;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.GeneratedValue; import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType; import javax.persistence.GenerationType;
@ -18,7 +21,7 @@ public class Exchange {
private Long id; private Long id;
@OneToOne @OneToOne
@JoinColumn(name = "owned_id", unique = true) @JoinColumn(name = "owned_id")
private Assignment ownedAssignment; private Assignment ownedAssignment;
@OneToOne @OneToOne
@ -27,67 +30,74 @@ public class Exchange {
private Long ownerId; private Long ownerId;
private Timestamp dateExchange;
public Exchange(){ public Exchange(){
} }
/** /**
* @return Long ID of exchange trader * @return Long ID of exchange trader
*/ */
public Long getOwnerId() { public Long getOwnerId() {
return ownerId; return ownerId;
} }
/** /**
* @param ownerId ID of exchange trader * @param ownerId ID of exchange trader
*/ */
public void setOwnerId(Long ownerId) { public void setOwnerId(Long ownerId) {
this.ownerId = ownerId; this.ownerId = ownerId;
} }
public Timestamp getDataExchange() {
return this.dateExchange;
}
/** /**
* *
* @param ownedAssignment Assignment which owner would like to trade * @param ownedAssignment Assignment which owner would like to trade
* @param desiredAssignment Groups instance that trader wants * @param desiredAssignment Groups instance that trader wants
*/ */
public Exchange(Assignment ownedAssignment, Groups desiredAssignment) { public Exchange(Assignment ownedAssignment, Groups desiredAssignment) {
this.ownedAssignment = ownedAssignment; this.ownedAssignment = ownedAssignment;
this.desiredAssignment = desiredAssignment; 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 * @return Long ID in database
*/ */
public Long getId(){ public Long getId(){
return this.id; return this.id;
} }
/** /**
* @return Groups Target group * @return Groups Target group
*/ */
public Groups getDesiredAssignment() { public Groups getDesiredAssignment() {
return desiredAssignment; return desiredAssignment;
} }
/** /**
* @param desiredAssignment Target group * @param desiredAssignment Target group
*/ */
public void setDesiredAssignment(Groups desiredAssignment) { public void setDesiredAssignment(Groups desiredAssignment) {
this.desiredAssignment = desiredAssignment; this.desiredAssignment = desiredAssignment;
} }
/** /**
* @return Assignment Owned assignment * @return Assignment Owned assignment
*/ */
public Assignment getOwnedAssignment() { public Assignment getOwnedAssignment() {
return ownedAssignment; return ownedAssignment;
} }
/** /**
* @param ownedAssignment Owned assignment * @param ownedAssignment Owned assignment
*/ */
public void setOwnedAssignment(Assignment ownedAssignment) { public void setOwnedAssignment(Assignment ownedAssignment) {
this.ownedAssignment = ownedAssignment; this.ownedAssignment = ownedAssignment;
} }
} }

View File

@ -1,5 +1,7 @@
package com.plannaplan.models; package com.plannaplan.models;
import java.sql.Timestamp;
import com.plannaplan.entities.Assignment; import com.plannaplan.entities.Assignment;
import com.plannaplan.entities.Exchange; import com.plannaplan.entities.Exchange;
@ -55,4 +57,12 @@ public class MatchData {
// Compare the data members and return accordingly // 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())); 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();
}
} }

View File

@ -1,10 +1,13 @@
package com.plannaplan.services; package com.plannaplan.services;
import java.sql.Timestamp;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.Set; import java.util.Set;
import java.util.TreeSet;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import com.plannaplan.entities.Assignment; import com.plannaplan.entities.Assignment;
@ -116,9 +119,11 @@ public class ExchangeService {
final Exchange exchangeOne = (Exchange) m[0]; final Exchange exchangeOne = (Exchange) m[0];
final Exchange exchangeTwo = (Exchange) m[1]; final Exchange exchangeTwo = (Exchange) m[1];
return new MatchData(exchangeOne, exchangeTwo); return new MatchData(exchangeOne, exchangeTwo);
}).collect(Collectors.toList()); }).collect(Collectors.toList());
final Set<MatchData> filterMatches = new HashSet<>(matches); Set<MatchData> filterMatches = new TreeSet<>((m1, m2) -> m1.compare(m2));
filterMatches.addAll(matches);
return filterMatches; return filterMatches;
} }
} }

View File

@ -2,8 +2,11 @@ package com.plannaplan.services;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.TreeSet;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import com.plannaplan.entities.Assignment; import com.plannaplan.entities.Assignment;
@ -16,7 +19,8 @@ import com.plannaplan.repositories.CommisionRepository;
import com.plannaplan.types.UserRoles; import com.plannaplan.types.UserRoles;
import com.plannaplan.types.WeekDay; 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.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -31,141 +35,177 @@ import org.springframework.test.context.junit4.SpringRunner;
@ContextConfiguration @ContextConfiguration
public class ExchangeServiceTest { public class ExchangeServiceTest {
@Autowired @Autowired
private AssignmentService assignmentService; private AssignmentService assignmentService;
@Autowired @Autowired
private GroupService groupService; private GroupService groupService;
@Autowired @Autowired
private UserService userService; private UserService userService;
@Autowired @Autowired
private CommisionRepository commisionRepository; private CommisionRepository commisionRepository;
@Autowired @Autowired
private ExchangeService exchangeService; 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<MatchData> 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<Long> listGroupsOfUser1 = user1.getStudentRegisteredGrups().stream().map(Groups::getId)
.collect(Collectors.toList());
final List<Long> listGroupsOfUser2 = user2.getStudentRegisteredGrups().stream().map(Groups::getId)
.collect(Collectors.toList());
final List<Long> listGroupsOfUser3 = user3.getStudentRegisteredGrups().stream().map(Groups::getId)
.collect(Collectors.toList());
final List<Long> 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<Long> listGroupsOfUser1 = user1.getStudentRegisteredGrups().stream().map(Groups::getId)
.collect(Collectors.toList());
final List<Long> 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 @Test
@DirtiesContext(methodMode = MethodMode.BEFORE_METHOD) public void shouldSortExchanges() throws Exception{
public void shouldReturnUniqMatches() { final List<MatchData> listMatrix = new ArrayList<>();
final User user1 = this.userService.save(new User(null, null, "shouldReturnMatches@ExchangeRepository.test", "123454", UserRoles.STUDENT , 451)); final Exchange exchange1 = new Exchange(null, null);
final Groups group1 = this.groupService.save(new Groups(123, "A2-3", null, 430, WeekDay.MONDAY, null)); Thread.sleep(1000);
final Commision commision1 = this.commisionRepository.save(new Commision(user1)); final Exchange exchange2 = new Exchange(null, null);
final User user2 = this.userService.save(new User(null, null, "shouldReturnMatches@ExchangeRepository.test", "123454", UserRoles.STUDENT , 451)); Thread.sleep(1000);
final Groups group2 = this.groupService.save(new Groups(123, "A2-3", null, 430, WeekDay.MONDAY, null)); final Exchange exchange3 = new Exchange(null, null);
final Commision commision2 = this.commisionRepository.save(new Commision(user2)); Thread.sleep(1000);
final Assignment assignmentUser1 = this.assignmentService.save(new Assignment(group1, commision1)); final Exchange exchange4 = new Exchange(null, null);
final Assignment assignmentUser2 = this.assignmentService.save(new Assignment(group2, commision2)); 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)); Set<MatchData> filterMatches = new TreeSet<>((m1, m2) -> m1.compare(m2));
this.exchangeService.save(new Exchange(assignmentUser2, group1)); filterMatches.addAll(listMatrix);
Iterator<MatchData> iterator = filterMatches.iterator();
while (iterator.hasNext()) {
MatchData matchData = iterator.next();
System.out.println(matchData.getExchangesMsValue());
}
final Set<MatchData> uniqList = this.exchangeService.getMatches(); final Set<MatchData> uniqList = this.exchangeService.getMatches();
assertTrue(uniqList.size() == 1); assertTrue(uniqList.size() == 2);
}
@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<Long> listGroupsOfUser1 = user1.getStudentRegisteredGrups().stream().map(Groups::getId)
.collect(Collectors.toList());
final List<Long> listGroupsOfUser2 = user2.getStudentRegisteredGrups().stream().map(Groups::getId)
.collect(Collectors.toList());
final List<Long> listGroupsOfUser3 = user3.getStudentRegisteredGrups().stream().map(Groups::getId)
.collect(Collectors.toList());
final List<Long> 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<Long> listGroupsOfUser1 = user1.getStudentRegisteredGrups().stream().map(Groups::getId).collect(Collectors.toList());
final List<Long> 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);
} }
} }