168 lines
6.0 KiB
Java
Executable File
168 lines
6.0 KiB
Java
Executable File
package com.plannaplan.services;
|
|
|
|
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;
|
|
|
|
import static org.junit.Assert.assertTrue;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.Random;
|
|
import java.util.UUID;
|
|
import java.util.stream.IntStream;
|
|
|
|
import com.plannaplan.entities.Assignment;
|
|
import com.plannaplan.entities.Commision;
|
|
import com.plannaplan.entities.Groups;
|
|
import com.plannaplan.entities.User;
|
|
import com.plannaplan.types.UserRoles;
|
|
import com.plannaplan.types.WeekDay;
|
|
|
|
import org.junit.Ignore;
|
|
import org.junit.Test;
|
|
|
|
import org.junit.runner.RunWith;
|
|
|
|
@RunWith(SpringRunner.class)
|
|
@SpringBootTest
|
|
@ContextConfiguration
|
|
public class AssignmentServiceTest {
|
|
|
|
@Autowired
|
|
private AssignmentService service;
|
|
@Autowired
|
|
private CommisionService comServie;
|
|
@Autowired
|
|
private UserService userService;
|
|
@Autowired
|
|
private GroupService groupService;
|
|
|
|
@Test
|
|
public void shouldSaveAssignment() {
|
|
final User user = new User("Gibi", "Kovalsky", "shouldSaveAssignment@assignmentservice.test",
|
|
UserRoles.STUDENT);
|
|
this.userService.save(user);
|
|
|
|
long beginState = this.service.getAssignmentsAmmount();
|
|
Commision com = new Commision(user);
|
|
this.comServie.save(com);
|
|
this.addAssignmentToCommision(com);
|
|
assertTrue("Assign ammount should increase", this.service.getAssignmentsAmmount() > beginState);
|
|
}
|
|
|
|
@Test
|
|
public void shouldGetCommisionAssignments() {
|
|
final User user = new User("Gibi", "Kovalsky", "shouldGetCommisionAssignments@assignmentservice.test",
|
|
UserRoles.STUDENT);
|
|
this.userService.save(user);
|
|
Commision com = new Commision(user);
|
|
this.comServie.save(com);
|
|
this.addAssignmentToCommision(com);
|
|
|
|
final List<Assignment> response = this.service.getCommisionAssignments(com);
|
|
assertTrue("Returned list size should be 1", response.size() == 1);
|
|
}
|
|
|
|
@Test
|
|
@Ignore
|
|
@DirtiesContext(methodMode = MethodMode.BEFORE_METHOD)
|
|
public void shouldPerformAcceptAlgorythm() {
|
|
final Random generator = new Random();
|
|
|
|
final List<Groups> groups = new ArrayList<>();
|
|
|
|
IntStream.range(0, 418).forEach(i -> {
|
|
groups.add(this.groupService.save(new Groups(generator.nextInt(80) + 20, null, null,
|
|
generator.nextInt(9) * 30 + 9 * 30, WeekDay.getDay(generator.nextInt(5)), null)));
|
|
});
|
|
|
|
IntStream.range(0, 1700).forEach(i -> {
|
|
final User user = this.userService.save(new User(null, null,
|
|
"shouldPerformAcceptAlgorythm-" + UUID.randomUUID().toString() + "@AssignmentService.test", null,
|
|
UserRoles.STUDENT, generator.nextInt(400) + 100));
|
|
final Commision com = this.comServie.save(new Commision(user));
|
|
IntStream.range(0, 5).forEach(j -> {
|
|
this.service.save(new Assignment(groups.get(generator.nextInt(groups.size())), com));
|
|
});
|
|
});
|
|
this.service.callAcceptAlgorythm();
|
|
}
|
|
|
|
/**
|
|
* This test will also sand a mail to users as a side effect. U can check them
|
|
* in mailcater
|
|
*/
|
|
@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());
|
|
}
|
|
});
|
|
|
|
}
|
|
|
|
@Test
|
|
@DirtiesContext(methodMode = MethodMode.BEFORE_METHOD)
|
|
public void shouldAcceptTwoTours() throws InterruptedException {
|
|
final Groups group = this.groupService.save(new Groups(5, null, null, 840, WeekDay.MONDAY, null));
|
|
final Groups group2 = this.groupService.save(new Groups(5, null, null, 840, WeekDay.MONDAY, null));
|
|
|
|
User user = this.userService.save(new User(null, null,
|
|
"shouldNotAcceptForOnePerson-" + UUID.randomUUID().toString() + "@AssignmentService.test", null,
|
|
UserRoles.STUDENT, 234));
|
|
|
|
final Commision com = this.comServie.save(new Commision(user));
|
|
|
|
this.service.save(new Assignment(group, com));
|
|
|
|
this.service.callAcceptAlgorythm();
|
|
|
|
user = this.userService.getById(user.getId()).get();
|
|
assertTrue(user.getStudentRegisteredGrups().size() == 1);
|
|
|
|
this.service.save(new Assignment(group2, com));
|
|
|
|
this.service.callAcceptAlgorythm();
|
|
|
|
user = this.userService.getById(user.getId()).get();
|
|
assertTrue(user.getStudentRegisteredGrups().size() == 2);
|
|
|
|
}
|
|
|
|
private void addAssignmentToCommision(Commision com) {
|
|
Assignment a = new Assignment(null, com);
|
|
this.service.save(a);
|
|
}
|
|
|
|
}
|