Lab 3
This commit is contained in:
parent
d90389cfbe
commit
92e9b29a1e
@ -11,7 +11,7 @@ public class DrivingSchool {
|
|||||||
@Id
|
@Id
|
||||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||||
private Long Id;
|
private Long Id;
|
||||||
@Column(unique = true)
|
|
||||||
private String name;
|
private String name;
|
||||||
@OneToMany(mappedBy = "drivingSchool", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
|
@OneToMany(mappedBy = "drivingSchool", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
|
||||||
private List<Student> students = new ArrayList<>();
|
private List<Student> students = new ArrayList<>();
|
||||||
|
@ -13,7 +13,7 @@ public class Student {
|
|||||||
private String name;
|
private String name;
|
||||||
private String surname;
|
private String surname;
|
||||||
|
|
||||||
@ManyToOne(fetch = FetchType.EAGER)
|
@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
|
||||||
private DrivingSchool drivingSchool;
|
private DrivingSchool drivingSchool;
|
||||||
|
|
||||||
@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
|
@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
|
||||||
|
@ -82,6 +82,15 @@ public class DrivingSchoolService {
|
|||||||
em.merge(currentDrivingSchool);
|
em.merge(currentDrivingSchool);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public void addAllStudent(Long id, List<Student> students) {
|
||||||
|
DrivingSchool currentDrivingSchool = findDrivingSchool(id);
|
||||||
|
for(int i=0; i<students.size(); i++) {
|
||||||
|
currentDrivingSchool.addNewStudent(students.get(i));
|
||||||
|
em.merge(currentDrivingSchool);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public void deleteStudent(Long id, Student student) {
|
public void deleteStudent(Long id, Student student) {
|
||||||
DrivingSchool currentDrivingSchool = findDrivingSchool(id);
|
DrivingSchool currentDrivingSchool = findDrivingSchool(id);
|
||||||
|
@ -41,12 +41,12 @@ public class CategoryServiceTests {
|
|||||||
categoryService.deleteAllCategories();
|
categoryService.deleteAllCategories();
|
||||||
|
|
||||||
Category p = categoryService.addCategory("Category");
|
Category p = categoryService.addCategory("Category");
|
||||||
Student e = studentService.addStudent("1", "2", "33");
|
Student e1 = studentService.addStudent("1", "2", "33");
|
||||||
|
|
||||||
studentService.addCategory(e.getId(), p);
|
studentService.addCategory(e1.getId(), p);
|
||||||
studentService.deleteCategory(e.getId(), p);
|
studentService.deleteCategory(e1.getId(), p);
|
||||||
Assertions.assertFalse(studentService.findStudent(e.getId()).getCategories().contains(p));
|
Assertions.assertFalse(studentService.findStudent(e1.getId()).getCategories().contains(p));
|
||||||
Assertions.assertFalse(categoryService.findCategory(p.getId()).getStudents().contains(e));
|
Assertions.assertFalse(categoryService.findCategory(p.getId()).getStudents().contains(e1));
|
||||||
|
|
||||||
studentService.deleteAllStudents();
|
studentService.deleteAllStudents();
|
||||||
categoryService.deleteAllCategories();
|
categoryService.deleteAllCategories();
|
||||||
|
@ -10,6 +10,9 @@ import ru.ulstu.is.cbapp.models.Student;
|
|||||||
import ru.ulstu.is.cbapp.service.DrivingSchoolService;
|
import ru.ulstu.is.cbapp.service.DrivingSchoolService;
|
||||||
import ru.ulstu.is.cbapp.service.StudentService;
|
import ru.ulstu.is.cbapp.service.StudentService;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
@SpringBootTest
|
@SpringBootTest
|
||||||
public class DrivingSchoolServiceTests {
|
public class DrivingSchoolServiceTests {
|
||||||
@Autowired
|
@Autowired
|
||||||
@ -81,14 +84,23 @@ public class DrivingSchoolServiceTests {
|
|||||||
studentService.deleteAllStudents();
|
studentService.deleteAllStudents();
|
||||||
drivingSchoolService.deleteAllDrivingSchools();
|
drivingSchoolService.deleteAllDrivingSchools();
|
||||||
|
|
||||||
|
|
||||||
final String name = "DrivingSchool";
|
final String name = "DrivingSchool";
|
||||||
DrivingSchool d = drivingSchoolService.addDrivingSchool(name);
|
DrivingSchool d = drivingSchoolService.addDrivingSchool(name);
|
||||||
Student newStudent = studentService.addStudent("cha", "chacha", "111");
|
|
||||||
drivingSchoolService.addNewStudent(d.getId(), newStudent);
|
|
||||||
|
|
||||||
Assertions.assertTrue(drivingSchoolService.findDrivingSchool(d.getId()).getStudents().contains(newStudent));
|
Student s1 = studentService.addStudent("cha", "chacha", "111");
|
||||||
|
Student s2 = studentService.addStudent("cha", "chacha", "111");
|
||||||
|
Student s3 = studentService.addStudent("cha", "chacha", "111");
|
||||||
|
|
||||||
|
List<Student> studs = new ArrayList<>();
|
||||||
|
studs.add(s1);
|
||||||
|
studs.add(s2);
|
||||||
|
studs.add(s3);
|
||||||
|
|
||||||
|
drivingSchoolService.addAllStudent(d.getId(), studs);
|
||||||
|
|
||||||
|
Assertions.assertTrue(drivingSchoolService.findDrivingSchool(d.getId()).getStudents().contains(s1));
|
||||||
|
Assertions.assertTrue(drivingSchoolService.findDrivingSchool(d.getId()).getStudents().contains(s2));
|
||||||
|
Assertions.assertTrue(drivingSchoolService.findDrivingSchool(d.getId()).getStudents().contains(s3));
|
||||||
|
|
||||||
studentService.deleteAllStudents();
|
studentService.deleteAllStudents();
|
||||||
drivingSchoolService.deleteAllDrivingSchools();
|
drivingSchoolService.deleteAllDrivingSchools();
|
||||||
@ -99,20 +111,51 @@ public class DrivingSchoolServiceTests {
|
|||||||
studentService.deleteAllStudents();
|
studentService.deleteAllStudents();
|
||||||
drivingSchoolService.deleteAllDrivingSchools();
|
drivingSchoolService.deleteAllDrivingSchools();
|
||||||
|
|
||||||
|
|
||||||
final String name = "DrivingSchool";
|
final String name = "DrivingSchool";
|
||||||
DrivingSchool d = drivingSchoolService.addDrivingSchool(name);
|
DrivingSchool d = drivingSchoolService.addDrivingSchool(name);
|
||||||
Student newStudent = studentService.addStudent("cha", "chacha", "111");
|
Student student1 = studentService.addStudent("cha1", "chacha1", "111");
|
||||||
|
Student student2 = studentService.addStudent("cha2", "chacha2", "1111");
|
||||||
|
Student student3 = studentService.addStudent("cha3", "chacha3", "11111");
|
||||||
|
|
||||||
drivingSchoolService.addNewStudent(d.getId(), newStudent);
|
drivingSchoolService.addNewStudent(d.getId(), student1);
|
||||||
Assertions.assertTrue(drivingSchoolService.findDrivingSchool(d.getId()).getStudents().contains(newStudent));
|
drivingSchoolService.addNewStudent(d.getId(), student2);
|
||||||
|
drivingSchoolService.addNewStudent(d.getId(), student3);
|
||||||
|
|
||||||
drivingSchoolService.deleteStudent(d.getId(), newStudent);
|
Assertions.assertTrue(drivingSchoolService.findDrivingSchool(d.getId()).getStudents().contains(student1));
|
||||||
DrivingSchool cFromDB = drivingSchoolService.findDrivingSchool(d.getId());
|
Assertions.assertTrue(drivingSchoolService.findDrivingSchool(d.getId()).getStudents().contains(student2));
|
||||||
Assertions.assertFalse(drivingSchoolService.findDrivingSchool(d.getId()).getStudents().contains(newStudent));
|
Assertions.assertTrue(drivingSchoolService.findDrivingSchool(d.getId()).getStudents().contains(student3));
|
||||||
|
|
||||||
|
DrivingSchool ds=drivingSchoolService.findDrivingSchool(d.getId());
|
||||||
|
|
||||||
|
Assertions.assertNotNull(ds);
|
||||||
|
|
||||||
|
Assertions.assertTrue(ds.getStudents().contains(student1));
|
||||||
|
Assertions.assertTrue(ds.getStudents().contains(student2));
|
||||||
|
Assertions.assertTrue(ds.getStudents().contains(student3));
|
||||||
|
|
||||||
studentService.deleteAllStudents();
|
studentService.deleteAllStudents();
|
||||||
drivingSchoolService.deleteAllDrivingSchools();
|
drivingSchoolService.deleteAllDrivingSchools();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testFindAllStudent(){
|
||||||
|
studentService.deleteAllStudents();
|
||||||
|
drivingSchoolService.deleteAllDrivingSchools();
|
||||||
|
|
||||||
|
DrivingSchool d = drivingSchoolService.addDrivingSchool("ds");
|
||||||
|
Student student1 = studentService.addStudent("cha1", "chacha1", "1111");
|
||||||
|
Student student2 = studentService.addStudent("cha2", "chacha2", "11111");
|
||||||
|
Student student3 = studentService.addStudent("cha3", "chacha3", "111111");
|
||||||
|
|
||||||
|
drivingSchoolService.addNewStudent(d.getId(), student1);
|
||||||
|
drivingSchoolService.addNewStudent(d.getId(), student2);
|
||||||
|
drivingSchoolService.addNewStudent(d.getId(), student3);
|
||||||
|
|
||||||
|
Assertions.assertTrue(drivingSchoolService.findDrivingSchool(d.getId()).getStudents().contains(student1));
|
||||||
|
Assertions.assertTrue(drivingSchoolService.findDrivingSchool(d.getId()).getStudents().contains(student2));
|
||||||
|
Assertions.assertTrue(drivingSchoolService.findDrivingSchool(d.getId()).getStudents().contains(student3));
|
||||||
|
|
||||||
|
studentService.deleteAllStudents();
|
||||||
|
drivingSchoolService.deleteAllDrivingSchools();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user