Checkpint - algorythm works

This commit is contained in:
Filip Izydorczyk 2021-01-03 16:21:06 +01:00
parent 2c0008afe1
commit 0f8b4eafb5
5 changed files with 59 additions and 34 deletions

View File

@ -25,7 +25,6 @@ public class Assignment {
@JoinColumn(name = "commision_id") @JoinColumn(name = "commision_id")
private Commision commision; private Commision commision;
private boolean isPastAssignment; private boolean isPastAssignment;
private boolean isAccepted;
/** /**
* Assignment * Assignment
@ -37,7 +36,6 @@ public class Assignment {
public Assignment(Groups group, Commision commision, boolean isPastAssignment) { public Assignment(Groups group, Commision commision, boolean isPastAssignment) {
this.commision = commision; this.commision = commision;
this.group = group; this.group = group;
this.isAccepted = false;
} }
/** /**
@ -46,16 +44,7 @@ public class Assignment {
* @return boolean isAccepted * @return boolean isAccepted
*/ */
public boolean isAccepted() { public boolean isAccepted() {
return isAccepted; return this.group.getRegisteredStudents().contains(this.commision.getCommisionOwner());
}
/**
* Set is accepted by algorythm
*
* @param isAccepted boolean isAccepted
*/
public void setAccepted(boolean isAccepted) {
this.isAccepted = isAccepted;
} }
/** /**

View File

@ -4,6 +4,7 @@ import java.util.HashSet;
import java.util.Set; import java.util.Set;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue; import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType; import javax.persistence.GenerationType;
import javax.persistence.Id; import javax.persistence.Id;
@ -40,7 +41,7 @@ public class Groups {
private Lecturer lecturer; private Lecturer lecturer;
private Integer zajCykId; private Integer zajCykId;
private Integer grNr; private Integer grNr;
@ManyToMany(mappedBy = "studentRegisteredGrups") @ManyToMany(mappedBy = "studentRegisteredGrups", fetch = FetchType.EAGER)
private Set<User> registeredStudents; private Set<User> registeredStudents;
public Set<User> getRegisteredStudents() { public Set<User> getRegisteredStudents() {

View File

@ -8,6 +8,7 @@ import java.util.concurrent.TimeUnit;
import javax.persistence.CascadeType; import javax.persistence.CascadeType;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue; import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType; import javax.persistence.GenerationType;
import javax.persistence.Id; import javax.persistence.Id;
@ -38,7 +39,7 @@ public class User {
private String refreshToken; private String refreshToken;
private Timestamp tokenUsageDate; private Timestamp tokenUsageDate;
private Integer ranking; private Integer ranking;
@ManyToMany(cascade = CascadeType.ALL) @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinTable @JoinTable
private Set<Groups> studentRegisteredGrups; private Set<Groups> studentRegisteredGrups;

View File

@ -5,6 +5,7 @@ import java.util.Optional;
import com.plannaplan.entities.Assignment; import com.plannaplan.entities.Assignment;
import com.plannaplan.entities.Commision; import com.plannaplan.entities.Commision;
import com.plannaplan.entities.Groups;
import com.plannaplan.entities.User; import com.plannaplan.entities.User;
import com.plannaplan.repositories.AssignmentRepository; import com.plannaplan.repositories.AssignmentRepository;
@ -21,6 +22,12 @@ public class AssignmentService {
@Autowired @Autowired
private AssignmentRepository repo; private AssignmentRepository repo;
@Autowired
private CommisionService service;
@Autowired
private GroupService groupService;
@Autowired @Autowired
private UserService userService; private UserService userService;
@ -65,7 +72,17 @@ public class AssignmentService {
final List<User> students = this.userService.getStudentsSortedByRanking(); final List<User> students = this.userService.getStudentsSortedByRanking();
students.forEach(e -> { students.forEach(e -> {
System.out.println(e.getRanking()); final Optional<Commision> com = this.service.getNewestCommision(e);
if (com.isPresent()) {
final List<Assignment> assignments = this.getCommisionAssignments(com.get());
assignments.forEach(a -> {
final Groups group = a.getGroup();
if (group.getCapacity() > group.getRegisteredStudents().size()) {
e.claimGroup(group);
this.userService.save(e);
}
});
}
}); });
} }
} }

View File

@ -9,6 +9,7 @@ import org.springframework.test.context.junit4.SpringRunner;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Random; import java.util.Random;
import java.util.UUID; import java.util.UUID;
@ -71,13 +72,6 @@ public class AssignmentServiceTest {
@DirtiesContext(methodMode = MethodMode.BEFORE_METHOD) @DirtiesContext(methodMode = MethodMode.BEFORE_METHOD)
public void shouldPerformAcceptAlgorythm() { public void shouldPerformAcceptAlgorythm() {
final Random generator = new Random(); final Random generator = new Random();
final User user1 = this.userService.save(new User(null, null,
"shouldPerformAcceptAlgorythm-" + UUID.randomUUID().toString() + "@AssignmentService.test", null,
UserRoles.STUDENT, generator.nextInt(400) + 100));
final User user2 = this.userService.save(new User(null, null,
"shouldPerformAcceptAlgorythm-" + UUID.randomUUID().toString() + "@AssignmentService.test", null,
UserRoles.STUDENT, generator.nextInt(400) + 100));
IntStream.range(0, 1700).forEach(i -> { IntStream.range(0, 1700).forEach(i -> {
this.userService.save(new User(null, null, this.userService.save(new User(null, null,
@ -90,22 +84,45 @@ public class AssignmentServiceTest {
generator.nextInt(9) * 30 + 9 * 30, WeekDay.getDay(generator.nextInt(5)), null)); generator.nextInt(9) * 30 + 9 * 30, WeekDay.getDay(generator.nextInt(5)), null));
}); });
IntStream.range(0, 12).forEach(i -> {
user1.claimGroup(this.groupService.save(new Groups(generator.nextInt(80) + 20, null, null,
generator.nextInt(9) * 30 + 9 * 30, WeekDay.getDay(generator.nextInt(5)), null)));
});
this.userService.save(user1);
IntStream.range(0, 12).forEach(i -> {
user2.claimGroup(this.groupService.save(new Groups(generator.nextInt(80) + 20, null, null,
generator.nextInt(9) * 30 + 9 * 30, WeekDay.getDay(generator.nextInt(5)), null)));
});
this.userService.save(user2);
this.service.callAcceptAlgorythm(); this.service.callAcceptAlgorythm();
System.out.println("-X_-x-x-x-_X-x-x-x-X_-x-x-x-"); System.out.println("-X_-x-x-x-_X-x-x-x-X_-x-x-x-");
} }
@Test
@DirtiesContext(methodMode = MethodMode.BEFORE_METHOD)
public void shouldNotAcceptForOnePerson() {
final Random generator = new Random();
final Groups group = this.groupService.save(new Groups(5, null, null, 840, WeekDay.MONDAY, null));
IntStream.range(0, 6).forEach(i -> {
final User user = this.userService.save(new User(null, null,
"shouldNotAcceptForOnePerson-" + UUID.randomUUID().toString() + "@AssignmentService.test", null,
UserRoles.STUDENT, generator.nextInt(400) + 100));
final Commision com = this.comServie.save(new Commision(user));
this.service.save(new Assignment(group, com));
});
this.service.callAcceptAlgorythm();
final List<User> users = this.userService.getStudentsSortedByRanking();
final User loser = users.get(users.size() - 1);
assertTrue(loser.getStudentRegisteredGrups().size() == 0);
users.forEach(u -> {
final Commision com = this.comServie.getNewestCommision(u).get();
final List<Assignment> assignments = com.getAssignments();
if (u.getId() == loser.getId()) {
assertTrue(!assignments.get(0).isAccepted());
} else {
assertTrue(assignments.get(0).isAccepted());
}
});
}
private void addAssignmentToCommision(Commision com) { private void addAssignmentToCommision(Commision com) {
Assignment a = new Assignment(null, com); Assignment a = new Assignment(null, com);
this.service.save(a); this.service.save(a);