From 755388d16bb2524c0db82918da7e862813f61b22 Mon Sep 17 00:00:00 2001 From: prodigygirl Date: Thu, 16 Mar 2023 17:15:56 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9B=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82?= =?UTF-8?q?=D0=BE=D1=80=D0=BD=D0=B0=D1=8F=203?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 4 + .../example/springip/lab3/models/Company.java | 83 +++++++++++++ .../springip/lab3/models/Employee.java | 109 ++++++++++++++++ .../springip/lab3/models/Position.java | 73 +++++++++++ .../springip/lab3/service/CompanyService.java | 91 ++++++++++++++ .../lab3/service/EmployeeService.java | 100 +++++++++++++++ .../lab3/service/PositionService.java | 69 +++++++++++ src/main/resources/application.properties | 12 +- .../example/springip/CompanyServiceTests.java | 117 ++++++++++++++++++ .../springip/EmployeeServiceTests.java | 92 ++++++++++++++ .../springip/PositionServiceTests.java | 56 +++++++++ .../springip/SpringipApplicationTests.java | 2 +- 12 files changed, 806 insertions(+), 2 deletions(-) create mode 100644 src/main/java/com/example/springip/lab3/models/Company.java create mode 100644 src/main/java/com/example/springip/lab3/models/Employee.java create mode 100644 src/main/java/com/example/springip/lab3/models/Position.java create mode 100644 src/main/java/com/example/springip/lab3/service/CompanyService.java create mode 100644 src/main/java/com/example/springip/lab3/service/EmployeeService.java create mode 100644 src/main/java/com/example/springip/lab3/service/PositionService.java create mode 100644 src/test/java/com/example/springip/CompanyServiceTests.java create mode 100644 src/test/java/com/example/springip/EmployeeServiceTests.java create mode 100644 src/test/java/com/example/springip/PositionServiceTests.java diff --git a/build.gradle b/build.gradle index bd92341..36226f9 100644 --- a/build.gradle +++ b/build.gradle @@ -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' } diff --git a/src/main/java/com/example/springip/lab3/models/Company.java b/src/main/java/com/example/springip/lab3/models/Company.java new file mode 100644 index 0000000..bf5a9ba --- /dev/null +++ b/src/main/java/com/example/springip/lab3/models/Company.java @@ -0,0 +1,83 @@ +package com.example.springip.lab3.models; + +import jakarta.persistence.*; + +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; + +@Entity +public class Company { + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private Long Id; + @Column(unique = true) + private String name; + @OneToMany(mappedBy = "company", cascade = CascadeType.ALL, fetch = FetchType.EAGER) + private List employees = new ArrayList<>(); + + public Company(String name, List employees) { + this.name = name; + this.employees = employees; + } + + public Company(String name) { + this.name = name; + } + + public Company() { + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public void addNewEmployee(Employee employee) { + employees.add(employee); + employee.setCompany(this); + } + + public void deleteEmployee(Employee employee) { + employees.remove(employee); + employee.deleteCompany(); + + } + + public Long getId() { + return Id; + } + + public List getEmployees() { + return employees; + } + + public void setEmployees(List employees) { + this.employees = employees; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Company company = (Company) o; + return Objects.equals(Id, company.Id); + } + + @Override + public int hashCode() { + return Objects.hash(Id); + } + + @Override + public String toString() { + return "Company{" + + "Id=" + Id + + ", name='" + name + '\'' + + ", employees=" + employees + + '}'; + } +} diff --git a/src/main/java/com/example/springip/lab3/models/Employee.java b/src/main/java/com/example/springip/lab3/models/Employee.java new file mode 100644 index 0000000..e6683d5 --- /dev/null +++ b/src/main/java/com/example/springip/lab3/models/Employee.java @@ -0,0 +1,109 @@ +package com.example.springip.lab3.models; + +import jakarta.persistence.*; + +import java.util.*; + +@Entity +public class Employee { + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private Long id; + private String phoneNumber; + private String name; + private String surname; + + @ManyToOne(fetch = FetchType.EAGER) + private Company company; + + @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER) + private Set positions = new HashSet<>(); + + + public Employee(String phoneNumber, String name, String surname) { + this.phoneNumber = phoneNumber; + this.name = name; + this.surname = surname; + } + + public Employee() { + } + + 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 getPositions() { + return positions.stream().toList(); + } + + public Company getCompany() { + return company; + } + + public void setCompany(Company company) { + this.company = company; + if (!company.getEmployees().contains(this)) { + company.addNewEmployee(this); + } + } + + public void deleteCompany() { + if (company.getEmployees().contains(this)) { + company.deleteEmployee(this); + } + this.company = null; + } + + public void addNewPosition(Position p) { + positions.add(p); + if (!p.getEmployees().contains(this)) { + p.addNewEmployee(this); + } + + } + + public void removePosition(Position p) { + positions.remove(p); + if (p.getEmployees().contains(this)) { + p.deleteEmployee(this); + } + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Employee e = (Employee) o; + return Objects.equals(id, e.id); + } + + @Override + public int hashCode() { + return Objects.hash(id); + } +} diff --git a/src/main/java/com/example/springip/lab3/models/Position.java b/src/main/java/com/example/springip/lab3/models/Position.java new file mode 100644 index 0000000..fe21da6 --- /dev/null +++ b/src/main/java/com/example/springip/lab3/models/Position.java @@ -0,0 +1,73 @@ +package com.example.springip.lab3.models; + +import jakarta.persistence.*; + +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; + +@Entity +public class Position { + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private Long Id; + + private String name; + + //mappedBy - атрибут, указывающий, что классом-владельцем отношений является другой класс + @ManyToMany(mappedBy = "positions", cascade = CascadeType.ALL, fetch = FetchType.EAGER) + private List employees = new ArrayList<>(); + + public Position() { + } + + public Position(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 getEmployees() { + return employees; + } + + public void setEmployees(List employees) { + this.employees = employees; + } + + public void addNewEmployee(Employee e) { + employees.add(e); + if (!e.getPositions().contains(this)) { + e.addNewPosition(this); + } + } + public void deleteEmployee(Employee e) { + employees.remove(e); + if (e.getPositions().contains(this)) { + e.removePosition(this); + } + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Position student = (Position) o; + return Objects.equals(Id, student.Id); + } + + @Override + public int hashCode() { + return Objects.hash(Id); + } +} diff --git a/src/main/java/com/example/springip/lab3/service/CompanyService.java b/src/main/java/com/example/springip/lab3/service/CompanyService.java new file mode 100644 index 0000000..08b0cd8 --- /dev/null +++ b/src/main/java/com/example/springip/lab3/service/CompanyService.java @@ -0,0 +1,91 @@ +package com.example.springip.lab3.service; + +import com.example.springip.lab3.models.Company; +import com.example.springip.lab3.models.Employee; +import jakarta.persistence.EntityManager; +import jakarta.persistence.EntityNotFoundException; +import jakarta.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 CompanyService { + @PersistenceContext + private EntityManager em; + + @Transactional + public Company addCompany(String name) { + if (!StringUtils.hasText(name)) { + throw new IllegalArgumentException("Student name is null or empty"); + } + final Company company = new Company(name); + em.persist(company); + return company; + } + + @Transactional(readOnly = true) + public Company findCompany(Long id) { + final Company company = em.find(Company.class, id); + if (company == null) { + throw new EntityNotFoundException(String.format("Company with id [%s] is not found", id)); + } + return company; + } + + @Transactional(readOnly = true) + public Company findCompany(String name) { + Company company = em.createQuery("select c from Company c WHERE c.name = :companyName", Company.class) + .setParameter("companyName",name) + .getSingleResult(); + if (company == null) { + throw new EntityNotFoundException(String.format("Company with name [%s] is not found", name)); + } + return company; + } + + @Transactional(readOnly = true) + public List findAllCompanies() { + return em.createQuery("select c from Company c", Company.class) + .getResultList(); + } + + @Transactional + public Company updateCompany(Long id, String name) { + if (!StringUtils.hasText(name)) { + throw new IllegalArgumentException("Company name is null or empty"); + } + final Company currentCompany = findCompany(id); + currentCompany.setName(name); + + return em.merge(currentCompany); + } + + @Transactional + public Company deleteCompany(Long id) { + final Company currentCompany = findCompany(id); + em.remove(currentCompany); + return currentCompany; + } + + @Transactional + public void deleteAllCompanies() { + em.createQuery("delete from Company").executeUpdate(); + } + + @Transactional + public void addNewEmployee(Long id, Employee employee) { + Company currentCompany = findCompany(id); + currentCompany.addNewEmployee(employee); + em.merge(currentCompany); + } + + @Transactional + public void deleteEmployee(Long id, Employee employee) { + Company currentCompany = findCompany(id); + currentCompany.deleteEmployee(employee); + em.merge(employee); + } +} diff --git a/src/main/java/com/example/springip/lab3/service/EmployeeService.java b/src/main/java/com/example/springip/lab3/service/EmployeeService.java new file mode 100644 index 0000000..b3c6b7b --- /dev/null +++ b/src/main/java/com/example/springip/lab3/service/EmployeeService.java @@ -0,0 +1,100 @@ +package com.example.springip.lab3.service; + +import com.example.springip.lab3.models.Company; +import com.example.springip.lab3.models.Employee; +import com.example.springip.lab3.models.Position; +import jakarta.persistence.EntityManager; +import jakarta.persistence.EntityNotFoundException; +import jakarta.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 EmployeeService { + @PersistenceContext + private EntityManager em; + + @Transactional + public Employee addEmployee(String surname, String name, String phoneNumber) { + if (!StringUtils.hasText(name) ||!StringUtils.hasText(surname) || !StringUtils.hasText(phoneNumber)) { + throw new IllegalArgumentException("Employee's data is null or empty"); + } + final Employee employee = new Employee(surname, name, phoneNumber); + em.persist(employee); + return employee; + } + + @Transactional + public Employee findEmployee(Long id) { + final Employee employee = em.find(Employee.class, id); + if (employee == null) { + throw new EntityNotFoundException(String.format("Employee with id [%s] is not found", id)); + } + return employee; + } + + @Transactional(readOnly = true) + public List findAllEmployees() { + return em.createQuery("select e from Employee e", Employee.class) + .getResultList(); + } + + @Transactional + public Employee updateCompany(Long id, String surname, String name, String phoneNumber) { + if (!StringUtils.hasText(name) ||!StringUtils.hasText(surname) || !StringUtils.hasText(phoneNumber)) { + throw new IllegalArgumentException("Employee's data is null or empty"); + } + final Employee currentEmployee = findEmployee(id); + currentEmployee.setName(name); + currentEmployee.setSurname(surname); + currentEmployee.setPhoneNumber(phoneNumber); + + return em.merge(currentEmployee); + } + + @Transactional + public Employee deleteEmployee(Long id) { + final Employee employee = findEmployee(id); + em.remove(employee); + return employee; + } + + @Transactional + public void deleteAllEmployees() { + em.createQuery("delete from Employee").executeUpdate(); + } + + @Transactional + public void addCompany(Long id, Company c) { + final Employee employee = findEmployee(id); + employee.setCompany(c); + em.merge(employee); + } + + @Transactional + public void deleteCompany(Long id) { + final Employee employee = findEmployee(id); + employee.deleteCompany(); + em.merge(employee); + } + + @Transactional + public void addPosition(Long id, Position p) { + Employee e = findEmployee(id); + e.addNewPosition(p); + System.out.println(e.getPositions().size()); + em.merge(e); + } + + @Transactional + public void deletePosition(Long id, Position p) { + Employee e = findEmployee(id); + e.removePosition(p); + System.out.println("Количество должностей после удаления: " + e.getPositions().size()); + em.merge(e); + em.flush(); + } +} diff --git a/src/main/java/com/example/springip/lab3/service/PositionService.java b/src/main/java/com/example/springip/lab3/service/PositionService.java new file mode 100644 index 0000000..b96bef2 --- /dev/null +++ b/src/main/java/com/example/springip/lab3/service/PositionService.java @@ -0,0 +1,69 @@ +package com.example.springip.lab3.service; + +import com.example.springip.lab3.models.Company; +import com.example.springip.lab3.models.Employee; +import com.example.springip.lab3.models.Position; +import jakarta.persistence.EntityManager; +import jakarta.persistence.EntityNotFoundException; +import jakarta.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 PositionService { + @PersistenceContext + private EntityManager em; + + @Transactional + public Position addPosition(String name) { + if (!StringUtils.hasText(name)) { + throw new IllegalArgumentException("Employee's data is null or empty"); + } + final Position position = new Position(name); + em.persist(position); + return position; + } + + @Transactional + public Position findPosition(Long id) { + final Position position = em.find(Position.class, id); + if (position == null) { + throw new EntityNotFoundException(String.format("Position with id [%s] is not found", id)); + } + return position; + } + + @Transactional(readOnly = true) + public List findAllPositions() { + return em.createQuery("select p from Position p", Position.class) + .getResultList(); + } + + @Transactional + public Position updatePosition(Long id, String surname, String name, String phoneNumber) { + if (!StringUtils.hasText(name) ||!StringUtils.hasText(surname) || !StringUtils.hasText(phoneNumber)) { + throw new IllegalArgumentException("Employee's data is null or empty"); + } + final Position currentPosition = findPosition(id); + currentPosition.setName(name); + + return em.merge(currentPosition); + } + + @Transactional + public Position deletePosition(Long id) { + final Position p = findPosition(id); + em.remove(p); + return p; + } + + @Transactional + public void deleteAllPositions() { + em.createQuery("delete from Position").executeUpdate(); + } + + +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 8b13789..da7b0b1 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -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 diff --git a/src/test/java/com/example/springip/CompanyServiceTests.java b/src/test/java/com/example/springip/CompanyServiceTests.java new file mode 100644 index 0000000..62166be --- /dev/null +++ b/src/test/java/com/example/springip/CompanyServiceTests.java @@ -0,0 +1,117 @@ +package com.example.springip; + +import com.example.springip.lab3.models.Company; +import com.example.springip.lab3.models.Employee; +import com.example.springip.lab3.service.CompanyService; +import com.example.springip.lab3.service.EmployeeService; +import jakarta.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; + +@SpringBootTest +public class CompanyServiceTests { + @Autowired + private CompanyService companyService; + + @Autowired + private EmployeeService employeeService; + + + @Test + public void testDeleteAllCompanies() { + companyService.deleteAllCompanies(); + int n = 3; + for (int i = 0; i < n; i++) { + String name = "Company" + i; + companyService.addCompany(name); + } + companyService.deleteAllCompanies(); + Assertions.assertEquals(companyService.findAllCompanies().size(), 0); + } + + @Test + public void testDeleteCompany() { + companyService.deleteAllCompanies(); + String name = "Company1"; + Company c = companyService.addCompany(name); + companyService.deleteCompany(c.getId()); + Assertions.assertThrows(EntityNotFoundException.class, () -> {companyService.findCompany(c.getId());}); + } + + @Test + public void testAddCompany() { + companyService.deleteAllCompanies(); + String name = "Company1"; + Company c = companyService.addCompany(name); + Assertions.assertNotNull(companyService.findCompany(c.getId())); + companyService.deleteAllCompanies(); + } + + @Test + public void testUpdateCompany() { + companyService.deleteAllCompanies(); + String name = "Company1"; + companyService.addCompany(name); + String name2 = "Company2"; + companyService.updateCompany(companyService.findCompany(name).getId(), name2); + Assertions.assertNotNull(companyService.findCompany(name2)); + companyService.deleteAllCompanies(); + } + + + @Test + public void testFindAllCompanies() { + companyService.deleteAllCompanies(); + int n = 3; + for (int i = 0; i < n; i++) { + String name = "Company" + i; + companyService.addCompany(name); + } + + Assertions.assertEquals(companyService.findAllCompanies().size(), n); + companyService.deleteAllCompanies(); + } + + @Test + public void testAddEmployee() { + + employeeService.deleteAllEmployees(); + companyService.deleteAllCompanies(); + + + final String name = "Company"; + Company c = companyService.addCompany(name); + Employee newEmployee = employeeService.addEmployee("cha", "chacha", "111"); + companyService.addNewEmployee(c.getId(), newEmployee); + + Assertions.assertTrue(companyService.findCompany(c.getId()).getEmployees().contains(newEmployee)); + + + employeeService.deleteAllEmployees(); + companyService.deleteAllCompanies(); + } + + @Test + public void testDeleteEmployee() { + employeeService.deleteAllEmployees(); + companyService.deleteAllCompanies(); + + + final String name = "Company"; + Company c = companyService.addCompany(name); + Employee newEmployee = employeeService.addEmployee("cha", "chacha", "111"); + + companyService.addNewEmployee(c.getId(), newEmployee); + Assertions.assertTrue(companyService.findCompany(c.getId()).getEmployees().contains(newEmployee)); + + companyService.deleteEmployee(c.getId(), newEmployee); + Company cFromDB = companyService.findCompany(c.getId()); + Assertions.assertFalse(companyService.findCompany(c.getId()).getEmployees().contains(newEmployee)); + + employeeService.deleteAllEmployees(); + companyService.deleteAllCompanies(); + + } +} diff --git a/src/test/java/com/example/springip/EmployeeServiceTests.java b/src/test/java/com/example/springip/EmployeeServiceTests.java new file mode 100644 index 0000000..7898fea --- /dev/null +++ b/src/test/java/com/example/springip/EmployeeServiceTests.java @@ -0,0 +1,92 @@ +package com.example.springip; + +import com.example.springip.lab3.models.Company; +import com.example.springip.lab3.models.Employee; +import com.example.springip.lab3.service.CompanyService; +import com.example.springip.lab3.service.EmployeeService; +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; + +@SpringBootTest +public class EmployeeServiceTests { + @Autowired + private EmployeeService employeeService; + + @Autowired + private CompanyService companyService; + + @Test + public void testAddEmployee() { + employeeService.deleteAllEmployees(); + + employeeService.addEmployee("name", "surname", "111"); + Assertions.assertEquals(1, employeeService.findAllEmployees().size()); + employeeService.deleteAllEmployees(); + } + + @Test + public void testUpdateEmployee() { + employeeService.deleteAllEmployees(); + final String newName = "name"; + + Employee e = employeeService.addEmployee("surname", "name", "111"); + employeeService.updateCompany(e.getId(),e.getSurname(), newName, e.getPhoneNumber()); + Assertions.assertEquals(newName, employeeService.findEmployee(e.getId()).getName()); + employeeService.deleteAllEmployees(); + } + + @Test + public void testDeleteEmployee() { + employeeService.deleteAllEmployees(); + + Employee e = employeeService.addEmployee("name", "surname", "111"); + employeeService.deleteEmployee(e.getId()); + Assertions.assertEquals(0, employeeService.findAllEmployees().size()); + employeeService.deleteAllEmployees(); + } + + @Test + public void testFindAllEmployee() { + employeeService.deleteAllEmployees(); + int n = 3; + for (int i = 0; i < n; i++) { + employeeService.addEmployee("name", "surname", "111"); + } + Assertions.assertEquals(n, employeeService.findAllEmployees().size()); + employeeService.deleteAllEmployees(); + } + + @Test + public void testAddCompany() { + employeeService.deleteAllEmployees(); + companyService.deleteAllCompanies(); + + Employee e = employeeService.addEmployee("name", "surname", "111"); + Company c = companyService.addCompany("Comp"); + companyService.addNewEmployee(c.getId(), e); + Assertions.assertEquals(c, employeeService.findEmployee(e.getId()).getCompany()); + + employeeService.deleteAllEmployees(); + companyService.deleteAllCompanies(); + } + + @Test + public void testDeleteFromCompany() { + employeeService.deleteAllEmployees(); + companyService.deleteAllCompanies(); + + Employee e = employeeService.addEmployee("name", "surname", "111"); + Company c = companyService.addCompany("Comp"); + + employeeService.addCompany(e.getId(), c); + Assertions.assertEquals(c, employeeService.findEmployee(e.getId()).getCompany()); + + employeeService.deleteCompany(e.getId()); + Assertions.assertNull(employeeService.findEmployee(e.getId()).getCompany()); + + employeeService.deleteAllEmployees(); + companyService.deleteAllCompanies(); + } +} diff --git a/src/test/java/com/example/springip/PositionServiceTests.java b/src/test/java/com/example/springip/PositionServiceTests.java new file mode 100644 index 0000000..d419b5c --- /dev/null +++ b/src/test/java/com/example/springip/PositionServiceTests.java @@ -0,0 +1,56 @@ +package com.example.springip; + +import com.example.springip.lab3.models.Employee; +import com.example.springip.lab3.models.Position; +import com.example.springip.lab3.service.EmployeeService; +import com.example.springip.lab3.service.PositionService; +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.stereotype.Service; + +@SpringBootTest +public class PositionServiceTests { + @Autowired + private PositionService positionService; + + @Autowired + private EmployeeService employeeService; + + + @Test + public void testAddEmployee() { + employeeService.deleteAllEmployees(); + positionService.deleteAllPositions(); + + Position p = positionService.addPosition("Position"); + Employee e = employeeService.addEmployee("1", "2", "33"); + + employeeService.addPosition(e.getId(), p); + Assertions.assertTrue(employeeService.findEmployee(e.getId()).getPositions().contains(p)); + Assertions.assertTrue(positionService.findPosition(p.getId()).getEmployees().contains(e)); + + employeeService.deleteAllEmployees(); + positionService.deleteAllPositions(); + + } + + @Test + public void testDeleteEmployee() { + employeeService.deleteAllEmployees(); + positionService.deleteAllPositions(); + + Position p = positionService.addPosition("Position"); + Employee e = employeeService.addEmployee("1", "2", "33"); + + employeeService.addPosition(e.getId(), p); + employeeService.deletePosition(e.getId(), p); + Assertions.assertFalse(employeeService.findEmployee(e.getId()).getPositions().contains(p)); + Assertions.assertFalse(positionService.findPosition(p.getId()).getEmployees().contains(e)); + + employeeService.deleteAllEmployees(); + positionService.deleteAllPositions(); + + } +} diff --git a/src/test/java/com/example/springip/SpringipApplicationTests.java b/src/test/java/com/example/springip/SpringipApplicationTests.java index f2f78d1..1514811 100644 --- a/src/test/java/com/example/springip/SpringipApplicationTests.java +++ b/src/test/java/com/example/springip/SpringipApplicationTests.java @@ -1,6 +1,6 @@ package com.example.springip; -import com.example.springip.service.OperationService; +import com.example.springip.lab2.service.OperationService; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired;