Compare commits

...

15 Commits
main ... Lab_03

Author SHA1 Message Date
81de225039 Загрузил(а) файлы в 'Отчеты' 2023-06-15 18:57:59 +04:00
d3f90d1251 Загрузил(а) файлы в 'Отчеты' 2023-06-15 18:25:26 +04:00
db982e9cf3 Загрузил(а) файлы в 'Отчеты' 2023-06-15 17:58:50 +04:00
1825f3b28d Отчет 3 2023-06-15 17:28:07 +04:00
c6b490b5ee Отчет 2 2023-06-15 17:11:06 +04:00
d6136f8e3c Lab 3 2023-04-04 12:34:53 +04:00
2683ba9907 Lab 3 2023-04-02 17:59:58 +04:00
92e9b29a1e Lab 3 2023-04-02 17:10:24 +04:00
d90389cfbe Lab 3 2023-03-19 22:43:59 +04:00
70ec81dc5c Lab2 2023-03-06 14:51:29 +04:00
eab61da355 Lab2 2023-03-04 12:57:40 +04:00
6dfb0655bb Lab1 2023-02-19 17:07:43 +04:00
f0fec374ff Lab1 2023-02-06 16:52:22 +04:00
a6d16481da Lab1 2023-02-06 16:42:57 +04:00
815967146e Controller(calc) 2023-02-06 14:41:17 +04:00
20 changed files with 955 additions and 16 deletions

View File

@ -14,6 +14,10 @@ repositories {
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'com.h2database:h2:2.1.210'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

View File

@ -5,9 +5,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class CbappApplication {
public static void main(String[] args) {
SpringApplication.run(CbappApplication.class, args);
}
}

View File

@ -0,0 +1,73 @@
package ru.ulstu.is.cbapp.models;
import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
@Entity
public class Category {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long Id;
private String name;
//mappedBy - атрибут, указывающий, что классом-владельцем отношений является другой класс
@ManyToMany(mappedBy = "categories", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private List<Student> students = new ArrayList<>();
public Category() {
}
public Category(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Long getId() {
return Id;
}
public List<Student> getStudents() {
return students;
}
public void setStudents(List<Student> students) {
this.students = students;
}
public void addNewStudent(Student e) {
students.add(e);
if (!e.getCategories().contains(this)) {
e.addNewCategory(this);
}
}
public void deleteStudent(Student e) {
students.remove(e);
if (e.getCategories().contains(this)) {
e.removeCategory(this);
}
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Category student = (Category) o;
return Objects.equals(Id, student.Id);
}
@Override
public int hashCode() {
return Objects.hash(Id);
}
}

View File

@ -0,0 +1,83 @@
package ru.ulstu.is.cbapp.models;
import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
@Entity
public class DrivingSchool {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long Id;
private String name;
@OneToMany(mappedBy = "drivingSchool", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private List<Student> students = new ArrayList<>();
public DrivingSchool(String name, List<Student> students) {
this.name = name;
this.students = students;
}
public DrivingSchool(String name) {
this.name = name;
}
public DrivingSchool() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void addNewStudent(Student student) {
students.add(student);
student.setDrivingSchool(this);
}
public void deleteStudent(Student student) {
students.remove(student);
student.deleteDrivingSchool();
}
public Long getId() {
return Id;
}
public List<Student> getStudents() {
return students;
}
public void setStudents(List<Student> students) {
this.students = students;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
DrivingSchool drivingSchool = (DrivingSchool) o;
return Objects.equals(Id, drivingSchool.Id);
}
@Override
public int hashCode() {
return Objects.hash(Id);
}
@Override
public String toString() {
return "DrivingSchool{" +
"Id=" + Id +
", name='" + name + '\'' +
", students=" + students +
'}';
}
}

View File

@ -0,0 +1,109 @@
package ru.ulstu.is.cbapp.models;
import javax.persistence.*;
import java.util.*;
@Entity
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String phoneNumber;
private String name;
private String surname;
@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private DrivingSchool drivingSchool;
@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private Set<Category> categories = new HashSet<>();
public Student(String phoneNumber, String name, String surname) {
this.phoneNumber = phoneNumber;
this.name = name;
this.surname = surname;
}
public Student() {
}
public Long getId() {
return id;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
public List<Category> getCategories() {
return categories.stream().toList();
}
public DrivingSchool getDrivingSchool() {
return drivingSchool;
}
public void setDrivingSchool(DrivingSchool drivingSchool) {
this.drivingSchool = drivingSchool;
if (!drivingSchool.getStudents().contains(this)) {
drivingSchool.addNewStudent(this);
}
}
public void deleteDrivingSchool() {
if (drivingSchool.getStudents().contains(this)) {
drivingSchool.deleteStudent(this);
}
this.drivingSchool = null;
}
public void addNewCategory(Category p) {
categories.add(p);
if (!p.getStudents().contains(this)) {
p.addNewStudent(this);
}
}
public void removeCategory(Category p) {
categories.remove(p);
if (p.getStudents().contains(this)) {
p.deleteStudent(this);
}
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student e = (Student) o;
return Objects.equals(id, e.id);
}
@Override
public int hashCode() {
return Objects.hash(id);
}
}

View File

@ -0,0 +1,30 @@
package ru.ulstu.is.cbapp.models;
import javax.persistence.*;
@Entity
@Table(name = "StudentCategory")
public class StudentCategory {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToOne
private Student student;
@ManyToOne
private Category category;
public StudentCategory(Student student, Category Category) {
this.student = student;
this.category = category;
}
public StudentCategory() {
}
public Long getId() {
return id;
}
}

View File

@ -0,0 +1,67 @@
package ru.ulstu.is.cbapp.service;
import ru.ulstu.is.cbapp.models.Category;
import javax.persistence.EntityManager;
import javax.persistence.EntityNotFoundException;
import javax.persistence.PersistenceContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import java.util.List;
@Service
public class CategoryService {
@PersistenceContext
private EntityManager em;
@Transactional
public Category addCategory(String name) {
if (!StringUtils.hasText(name)) {
throw new IllegalArgumentException("Student's data is null or empty");
}
final Category category = new Category(name);
em.persist(category);
return category;
}
@Transactional
public Category findCategory(Long id) {
final Category category = em.find(Category.class, id);
if (category == null) {
throw new EntityNotFoundException(String.format("Category with id [%s] is not found", id));
}
return category;
}
@Transactional(readOnly = true)
public List<Category> findAllCategories() {
return em.createQuery("select p from Category p", Category.class)
.getResultList();
}
@Transactional
public Category updateCategory(Long id, String surname, String name, String phoneNumber) {
if (!StringUtils.hasText(name) ||!StringUtils.hasText(surname) || !StringUtils.hasText(phoneNumber)) {
throw new IllegalArgumentException("Student's data is null or empty");
}
final Category currentCategory = findCategory(id);
currentCategory.setName(name);
return em.merge(currentCategory);
}
@Transactional
public Category deleteCategory(Long id) {
final Category p = findCategory(id);
em.remove(p);
return p;
}
@Transactional
public void deleteAllCategories() {
em.createQuery("delete from Category").executeUpdate();
}
}

View File

@ -0,0 +1,128 @@
package ru.ulstu.is.cbapp.service;
import javax.persistence.EntityManager;
import javax.persistence.EntityNotFoundException;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import org.springframework.data.util.Pair;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import ru.ulstu.is.cbapp.models.Category;
import ru.ulstu.is.cbapp.models.DrivingSchool;
import ru.ulstu.is.cbapp.models.Student;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
@Service
public class DrivingSchoolService {
@PersistenceContext
private EntityManager em;
@Transactional
public DrivingSchool addDrivingSchool(String name) {
if (!StringUtils.hasText(name)) {
throw new IllegalArgumentException("Student name is null or empty");
}
final DrivingSchool drivingSchool = new DrivingSchool(name);
em.persist(drivingSchool);
return drivingSchool;
}
@Transactional(readOnly = true)
public DrivingSchool findDrivingSchool(Long id) {
final DrivingSchool drivingSchool = em.find(DrivingSchool.class, id);
if (drivingSchool == null) {
throw new EntityNotFoundException(String.format("DrivingSchool with id [%s] is not found", id));
}
return drivingSchool;
}
@Transactional(readOnly = true)
public DrivingSchool findDrivingSchool(String name) {
DrivingSchool drivingSchool = em.createQuery("select d from DrivingSchool d WHERE d.name = :drivingSchoolName", DrivingSchool.class)
.setParameter("drivingSchoolName",name)
.getSingleResult();
if (drivingSchool == null) {
throw new EntityNotFoundException(String.format("DrivingSchool with name [%s] is not found", name));
}
return drivingSchool;
}
@Transactional(readOnly = true)
public List<DrivingSchool> findAllDrivingSchools() {
return em.createQuery("select d from DrivingSchool d", DrivingSchool.class)
.getResultList();
}
@Transactional
public DrivingSchool updateDrivingSchool(Long id, String name) {
if (!StringUtils.hasText(name)) {
throw new IllegalArgumentException("DrivingSchool name is null or empty");
}
final DrivingSchool currentDrivingSchool = findDrivingSchool(id);
currentDrivingSchool.setName(name);
return em.merge(currentDrivingSchool);
}
@Transactional
public DrivingSchool deleteDrivingSchool(Long id) {
final DrivingSchool currentDrivingSchool = findDrivingSchool(id);
em.remove(currentDrivingSchool);
return currentDrivingSchool;
}
@Transactional
public void deleteAllDrivingSchools() {
em.createQuery("delete from DrivingSchool").executeUpdate();
}
@Transactional
public void addNewStudent(Long id, Student student) {
DrivingSchool currentDrivingSchool = findDrivingSchool(id);
currentDrivingSchool.addNewStudent(student);
em.merge(currentDrivingSchool);
}
@Transactional
public List<Pair<Long, Integer>> CountStudentsCategory(Long dsID) {
TypedQuery<Object[]> query = em.createQuery("SELECT sc.category.Id, COUNT(sc.category.Id) AS count " +
"FROM StudentCategory sc " +
"JOIN sc.student s " +
"JOIN s.drivingSchool ds " +
"WHERE ds.Id=:dsID " +
"GROUP BY sc.category.Id", Object[].class)
.setParameter("dsID", dsID);
List<Object[]> resultList = query.getResultList();
List<Pair<Long, Integer>> pairs = new ArrayList<>();
for (Object[] result : resultList) {
Long categoryId = (Long) result[0];
Integer count = ((Number) result[1]).intValue();
Pair<Long, Integer> pair = Pair.of(categoryId, count);
pairs.add(pair);
}
return pairs;
}
@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
public void deleteStudent(Long id, Student student) {
DrivingSchool currentDrivingSchool = findDrivingSchool(id);
currentDrivingSchool.deleteStudent(student);
em.merge(student);
}
}

View File

@ -0,0 +1,100 @@
package ru.ulstu.is.cbapp.service;
import javax.persistence.EntityManager;
import javax.persistence.EntityNotFoundException;
import javax.persistence.PersistenceContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import ru.ulstu.is.cbapp.models.DrivingSchool;
import ru.ulstu.is.cbapp.models.Student;
import ru.ulstu.is.cbapp.models.Category;
import java.util.List;
@Service
public class StudentService {
@PersistenceContext
private EntityManager em;
@Transactional
public Student addStudent(String surname, String name, String phoneNumber) {
if (!StringUtils.hasText(name) ||!StringUtils.hasText(surname) || !StringUtils.hasText(phoneNumber)) {
throw new IllegalArgumentException("Student's data is null or empty");
}
final Student student = new Student(surname, name, phoneNumber);
em.persist(student);
return student;
}
@Transactional
public Student findStudent(Long id) {
final Student student = em.find(Student.class, id);
if (student == null) {
throw new EntityNotFoundException(String.format("Student with id [%s] is not found", id));
}
return student;
}
@Transactional(readOnly = true)
public List<Student> findAllStudents() {
return em.createQuery("select e from Student e", Student.class)
.getResultList();
}
@Transactional
public Student updateStudent(Long id, String surname, String name, String phoneNumber) {
if (!StringUtils.hasText(name) ||!StringUtils.hasText(surname) || !StringUtils.hasText(phoneNumber)) {
throw new IllegalArgumentException("Student's data is null or empty");
}
final Student currentStudent = findStudent(id);
currentStudent.setName(name);
currentStudent.setSurname(surname);
currentStudent.setPhoneNumber(phoneNumber);
return em.merge(currentStudent);
}
@Transactional
public Student deleteStudent(Long id) {
final Student student = findStudent(id);
em.remove(student);
return student;
}
@Transactional
public void deleteAllStudents() {
em.createQuery("delete from Student").executeUpdate();
}
@Transactional
public void addDrivingSchool(Long id, DrivingSchool d) {
final Student student = findStudent(id);
student.setDrivingSchool(d);
em.merge(student);
}
@Transactional
public void deleteDrivingSchool(Long id) {
final Student student = findStudent(id);
student.deleteDrivingSchool();
em.merge(student);
}
@Transactional
public void addCategory(Long id, Category p) {
Student e = findStudent(id);
e.addNewCategory(p);
System.out.println(e.getCategories().size());
em.merge(e);
}
@Transactional
public void deleteCategory(Long id, Category p) {
Student e = findStudent(id);
e.removeCategory(p);
System.out.println("Количество должностей после удаления: " + e.getCategories().size());
em.merge(e);
em.flush();
}
}

View File

@ -1 +1,11 @@
spring.main.banner-mode=off
server.port=8080
spring.datasource.url=jdbc:h2:file:./data
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=password
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.jpa.hibernate.ddl-auto=update
spring.h2.console.enabled=true
spring.h2.console.settings.trace=false
spring.h2.console.settings.web-allow-others=false

View File

@ -0,0 +1,55 @@
package ru.ulstu.is.cbapp;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import ru.ulstu.is.cbapp.models.Student;
import ru.ulstu.is.cbapp.models.Category;
import ru.ulstu.is.cbapp.service.StudentService;
import ru.ulstu.is.cbapp.service.CategoryService;
@SpringBootTest
public class CategoryServiceTests {
@Autowired
private CategoryService categoryService;
@Autowired
private StudentService studentService;
@Test
public void testAddStudent() {
studentService.deleteAllStudents();
categoryService.deleteAllCategories();
Category p = categoryService.addCategory("Category");
Student e = studentService.addStudent("1", "2", "33");
studentService.addCategory(e.getId(), p);
Assertions.assertTrue(studentService.findStudent(e.getId()).getCategories().contains(p));
Assertions.assertTrue(categoryService.findCategory(p.getId()).getStudents().contains(e));
studentService.deleteAllStudents();
categoryService.deleteAllCategories();
}
@Test
public void testDeleteStudent() {
studentService.deleteAllStudents();
categoryService.deleteAllCategories();
Category p = categoryService.addCategory("Category");
Student e1 = studentService.addStudent("1", "2", "33");
studentService.addCategory(e1.getId(), p);
studentService.deleteCategory(e1.getId(), p);
Assertions.assertFalse(studentService.findStudent(e1.getId()).getCategories().contains(p));
Assertions.assertFalse(categoryService.findCategory(p.getId()).getStudents().contains(e1));
studentService.deleteAllStudents();
categoryService.deleteAllCategories();
}
}

View File

@ -1,13 +0,0 @@
package ru.ulstu.is.cbapp;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class CbappApplicationTests {
@Test
void contextLoads() {
}
}

View File

@ -0,0 +1,203 @@
package ru.ulstu.is.cbapp;
import javax.persistence.EntityNotFoundException;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.util.Pair;
import ru.ulstu.is.cbapp.models.Category;
import ru.ulstu.is.cbapp.models.DrivingSchool;
import ru.ulstu.is.cbapp.models.Student;
import ru.ulstu.is.cbapp.service.CategoryService;
import ru.ulstu.is.cbapp.service.DrivingSchoolService;
import ru.ulstu.is.cbapp.service.StudentService;
import java.util.ArrayList;
import java.util.List;
@SpringBootTest
public class DrivingSchoolServiceTests {
@Autowired
private DrivingSchoolService drivingSchoolService;
@Autowired
private CategoryService categoryService;
@Autowired
private StudentService studentService;
@Test
public void testDeleteAllDrivingSchools() {
drivingSchoolService.deleteAllDrivingSchools();
int n = 3;
for (int i = 0; i < n; i++) {
String name = "DrivingSchool" + i;
drivingSchoolService.addDrivingSchool(name);
}
drivingSchoolService.deleteAllDrivingSchools();
Assertions.assertEquals(drivingSchoolService.findAllDrivingSchools().size(), 0);
}
@Test
public void testDeleteDrivingSchool() {
drivingSchoolService.deleteAllDrivingSchools();
String name = "DrivingSchool1";
DrivingSchool d = drivingSchoolService.addDrivingSchool(name);
drivingSchoolService.deleteDrivingSchool(d.getId());
Assertions.assertThrows(EntityNotFoundException.class, () -> {
drivingSchoolService.findDrivingSchool(d.getId());});
}
@Test
public void testAddDrivingSchool() {
drivingSchoolService.deleteAllDrivingSchools();
String name = "DrivingSchool1";
DrivingSchool d = drivingSchoolService.addDrivingSchool(name);
Assertions.assertNotNull(drivingSchoolService.findDrivingSchool(d.getId()));
drivingSchoolService.deleteAllDrivingSchools();
}
@Test
public void testUpdateDrivingSchool() {
drivingSchoolService.deleteAllDrivingSchools();
String name = "DrivingSchool1";
DrivingSchool ds = drivingSchoolService.addDrivingSchool(name);
String name2 = "DrivingSchool2";
drivingSchoolService.updateDrivingSchool(ds.getId(), name2);
Assertions.assertNotNull(drivingSchoolService.findDrivingSchool(ds.getId()));
drivingSchoolService.deleteAllDrivingSchools();
}
@Test
public void testFindAllDrivingSchools() {
drivingSchoolService.deleteAllDrivingSchools();
int n = 3;
for (int i = 0; i < n; i++) {
String name = "DrivingSchool" + i;
drivingSchoolService.addDrivingSchool(name);
}
Assertions.assertEquals(drivingSchoolService.findAllDrivingSchools().size(), n);
drivingSchoolService.deleteAllDrivingSchools();
}
@Test
public void testGroupStudentCategory() {
studentService.deleteAllStudents();
drivingSchoolService.deleteAllDrivingSchools();
categoryService.deleteAllCategories();
final String name = "DrivingSchool";
DrivingSchool d = drivingSchoolService.addDrivingSchool(name);
Student s1 = studentService.addStudent("cha", "chacha", "111");
Student s2 = studentService.addStudent("cha", "chacha", "111");
Student s3 = studentService.addStudent("cha", "chacha", "111");
Category c1 = categoryService.addCategory("Category1");
Category c2 = categoryService.addCategory("Category2");
Category c3 = categoryService.addCategory("Category3");
studentService.addCategory(s1.getId(), c1);
studentService.addCategory(s2.getId(), c2);
studentService.addCategory(s3.getId(), c3);
List<Student> studs = new ArrayList<>();
studs.add(s1);
studs.add(s2);
studs.add(s3);
drivingSchoolService.addAllStudent(d.getId(), studs);
List<Pair<Long,Integer>> list=drivingSchoolService.CountStudentsCategory(d.getId());
Assertions.assertNotNull(list);
studentService.deleteAllStudents();
drivingSchoolService.deleteAllDrivingSchools();
categoryService.deleteAllCategories();
}
@Test
public void testAddStudent() {
studentService.deleteAllStudents();
drivingSchoolService.deleteAllDrivingSchools();
final String name = "DrivingSchool";
DrivingSchool d = drivingSchoolService.addDrivingSchool(name);
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();
drivingSchoolService.deleteAllDrivingSchools();
}
@Test
public void testDeleteStudent() {
studentService.deleteAllStudents();
drivingSchoolService.deleteAllDrivingSchools();
final String name = "DrivingSchool";
DrivingSchool d = drivingSchoolService.addDrivingSchool(name);
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(), 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));
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();
}
}

View File

@ -0,0 +1,92 @@
package ru.ulstu.is.cbapp;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import ru.ulstu.is.cbapp.models.DrivingSchool;
import ru.ulstu.is.cbapp.models.Student;
import ru.ulstu.is.cbapp.service.DrivingSchoolService;
import ru.ulstu.is.cbapp.service.StudentService;
@SpringBootTest
public class StudentServiceTests {
@Autowired
private StudentService studentService;
@Autowired
private DrivingSchoolService drivingSchoolService;
@Test
public void testAddStudent() {
studentService.deleteAllStudents();
studentService.addStudent("name", "surname", "111");
Assertions.assertEquals(1, studentService.findAllStudents().size());
studentService.deleteAllStudents();
}
@Test
public void testUpdateStudent() {
studentService.deleteAllStudents();
final String newName = "name";
Student e = studentService.addStudent("surname", "name", "111");
studentService.updateStudent(e.getId(),e.getSurname(), newName, e.getPhoneNumber());
Assertions.assertEquals(newName, studentService.findStudent(e.getId()).getName());
studentService.deleteAllStudents();
}
@Test
public void testDeleteStudent() {
studentService.deleteAllStudents();
Student e = studentService.addStudent("name", "surname", "111");
studentService.deleteStudent(e.getId());
Assertions.assertEquals(0, studentService.findAllStudents().size());
studentService.deleteAllStudents();
}
@Test
public void testFindAllStudent() {
studentService.deleteAllStudents();
int n = 3;
for (int i = 0; i < n; i++) {
studentService.addStudent("name", "surname", "111");
}
Assertions.assertEquals(n, studentService.findAllStudents().size());
studentService.deleteAllStudents();
}
@Test
public void testAddDrivingSchool() {
studentService.deleteAllStudents();
drivingSchoolService.deleteAllDrivingSchools();
Student e = studentService.addStudent("name", "surname", "111");
DrivingSchool d = drivingSchoolService.addDrivingSchool("Comp");
drivingSchoolService.addNewStudent(d.getId(), e);
Assertions.assertEquals(d, studentService.findStudent(e.getId()).getDrivingSchool());
studentService.deleteAllStudents();
drivingSchoolService.deleteAllDrivingSchools();
}
@Test
public void testDeleteFromDrivingSchool() {
studentService.deleteAllStudents();
drivingSchoolService.deleteAllDrivingSchools();
Student e = studentService.addStudent("name", "surname", "111");
DrivingSchool d = drivingSchoolService.addDrivingSchool("Comp");
studentService.addDrivingSchool(e.getId(), d);
Assertions.assertEquals(d, studentService.findStudent(e.getId()).getDrivingSchool());
studentService.deleteDrivingSchool(e.getId());
Assertions.assertNull(studentService.findStudent(e.getId()).getDrivingSchool());
studentService.deleteAllStudents();
drivingSchoolService.deleteAllDrivingSchools();
}
}