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;
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,6 +30,8 @@ public class Exchange {
private Long ownerId;
private Timestamp dateExchange;
public Exchange(){
}
@ -45,6 +50,10 @@ public class Exchange {
this.ownerId = ownerId;
}
public Timestamp getDataExchange() {
return this.dateExchange;
}
/**
*
* @param ownedAssignment Assignment which owner would like to trade
@ -53,7 +62,8 @@ public class Exchange {
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());
}
/**

View File

@ -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();
}
}

View File

@ -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<MatchData> filterMatches = new HashSet<>(matches);
Set<MatchData> filterMatches = new TreeSet<>((m1, m2) -> m1.compare(m2));
filterMatches.addAll(matches);
return filterMatches;
}
}

View File

@ -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;
@ -49,10 +53,12 @@ public class ExchangeServiceTest {
@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 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 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));
@ -69,8 +75,7 @@ public class ExchangeServiceTest {
@Test
@DirtiesContext(methodMode = MethodMode.BEFORE_METHOD)
@Ignore
public void shouldPerformExchange() {
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();
@ -103,9 +108,11 @@ public class ExchangeServiceTest {
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();
@ -132,13 +139,15 @@ public class ExchangeServiceTest {
@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));
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));
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));
@ -161,11 +170,42 @@ public class ExchangeServiceTest {
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());
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
public void shouldSortExchanges() throws Exception{
final List<MatchData> 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(exchange1, exchange3));
listMatrix.add(new MatchData(exchange2, exchange4));
Set<MatchData> filterMatches = new TreeSet<>((m1, m2) -> m1.compare(m2));
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();
assertTrue(uniqList.size() == 2);
}
}