From 92e9b29a1e15aafcf9b1fe639df3adaf849f69ae Mon Sep 17 00:00:00 2001 From: AnnZhimol Date: Sun, 2 Apr 2023 17:10:24 +0400 Subject: [PATCH] Lab 3 --- .../ulstu/is/cbapp/models/DrivingSchool.java | 2 +- .../ru/ulstu/is/cbapp/models/Student.java | 2 +- .../cbapp/service/DrivingSchoolService.java | 9 +++ .../ulstu/is/cbapp/CategoryServiceTests.java | 10 +-- .../is/cbapp/DrivingSchoolServiceTests.java | 65 +++++++++++++++---- 5 files changed, 70 insertions(+), 18 deletions(-) diff --git a/src/main/java/ru/ulstu/is/cbapp/models/DrivingSchool.java b/src/main/java/ru/ulstu/is/cbapp/models/DrivingSchool.java index b6bbca3..53bb06e 100644 --- a/src/main/java/ru/ulstu/is/cbapp/models/DrivingSchool.java +++ b/src/main/java/ru/ulstu/is/cbapp/models/DrivingSchool.java @@ -11,7 +11,7 @@ public class DrivingSchool { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long Id; - @Column(unique = true) + private String name; @OneToMany(mappedBy = "drivingSchool", cascade = CascadeType.ALL, fetch = FetchType.EAGER) private List students = new ArrayList<>(); diff --git a/src/main/java/ru/ulstu/is/cbapp/models/Student.java b/src/main/java/ru/ulstu/is/cbapp/models/Student.java index 5e39ad9..8f020ed 100644 --- a/src/main/java/ru/ulstu/is/cbapp/models/Student.java +++ b/src/main/java/ru/ulstu/is/cbapp/models/Student.java @@ -13,7 +13,7 @@ public class Student { private String name; private String surname; - @ManyToOne(fetch = FetchType.EAGER) + @ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER) private DrivingSchool drivingSchool; @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER) diff --git a/src/main/java/ru/ulstu/is/cbapp/service/DrivingSchoolService.java b/src/main/java/ru/ulstu/is/cbapp/service/DrivingSchoolService.java index f2b80b9..998f050 100644 --- a/src/main/java/ru/ulstu/is/cbapp/service/DrivingSchoolService.java +++ b/src/main/java/ru/ulstu/is/cbapp/service/DrivingSchoolService.java @@ -82,6 +82,15 @@ public class DrivingSchoolService { em.merge(currentDrivingSchool); } + @Transactional + public void addAllStudent(Long id, List students) { + DrivingSchool currentDrivingSchool = findDrivingSchool(id); + for(int i=0; i 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(); drivingSchoolService.deleteAllDrivingSchools(); @@ -99,20 +111,51 @@ public class DrivingSchoolServiceTests { studentService.deleteAllStudents(); drivingSchoolService.deleteAllDrivingSchools(); - final String name = "DrivingSchool"; 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); - Assertions.assertTrue(drivingSchoolService.findDrivingSchool(d.getId()).getStudents().contains(newStudent)); + drivingSchoolService.addNewStudent(d.getId(), student1); + drivingSchoolService.addNewStudent(d.getId(), student2); + drivingSchoolService.addNewStudent(d.getId(), student3); - drivingSchoolService.deleteStudent(d.getId(), newStudent); - DrivingSchool cFromDB = drivingSchoolService.findDrivingSchool(d.getId()); - Assertions.assertFalse(drivingSchoolService.findDrivingSchool(d.getId()).getStudents().contains(newStudent)); + 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)); + + 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(); 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(); } }