Checkpoint - added many to many user group relations and test cerating users and groups to accept test

This commit is contained in:
Filip Izydorczyk 2021-01-02 15:26:05 +01:00
parent 4b096a50bf
commit 2c0008afe1
3 changed files with 70 additions and 4 deletions

View File

@ -1,10 +1,14 @@
package com.plannaplan.entities;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import com.plannaplan.types.GroupType;
@ -36,23 +40,36 @@ public class Groups {
private Lecturer lecturer;
private Integer zajCykId;
private Integer grNr;
@ManyToMany(mappedBy = "studentRegisteredGrups")
private Set<User> registeredStudents;
public Set<User> getRegisteredStudents() {
return this.registeredStudents;
}
public void assignUser(User user) {
if (this.registeredStudents == null) {
this.registeredStudents = new HashSet<>();
}
this.registeredStudents.add(user);
}
public Groups() {
}
public Integer getGr_nr() {
public Integer getGrNr() {
return grNr;
}
public void setGr_nr(Integer grNr) {
public void setGrNr(Integer grNr) {
this.grNr = grNr;
}
public Integer getZaj_cyk_id() {
public Integer getZajCykId() {
return zajCykId;
}
public void setZaj_cyk_id(Integer zajCykId) {
public void setZajCykId(Integer zajCykId) {
this.zajCykId = zajCykId;
}

View File

@ -1,13 +1,18 @@
package com.plannaplan.entities;
import java.sql.Timestamp;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import com.plannaplan.models.UserApiResponse;
import com.plannaplan.types.UserRoles;
@ -33,6 +38,20 @@ public class User {
private String refreshToken;
private Timestamp tokenUsageDate;
private Integer ranking;
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable
private Set<Groups> studentRegisteredGrups;
public Set<Groups> getStudentRegisteredGrups() {
return this.studentRegisteredGrups;
}
public void claimGroup(Groups group) {
if (this.studentRegisteredGrups == null) {
this.studentRegisteredGrups = new HashSet<>();
}
this.studentRegisteredGrups.add(group);
}
public User() {
}

View File

@ -18,8 +18,10 @@ import javax.management.relation.Role;
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.Test;
@ -36,6 +38,8 @@ public class AssignmentServiceTest {
private CommisionService comServie;
@Autowired
private UserService userService;
@Autowired
private GroupService groupService;
@Test
public void shouldSaveAssignment() {
@ -67,11 +71,37 @@ public class AssignmentServiceTest {
@DirtiesContext(methodMode = MethodMode.BEFORE_METHOD)
public void shouldPerformAcceptAlgorythm() {
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 -> {
this.userService.save(new User(null, null,
"shouldPerformAcceptAlgorythm-" + UUID.randomUUID().toString() + "@AssignmentService.test", null,
UserRoles.STUDENT, generator.nextInt(400) + 100));
});
IntStream.range(0, 418).forEach(i -> {
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, 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();
System.out.println("-X_-x-x-x-_X-x-x-x-X_-x-x-x-");
}