From 7fa6c9ea843ecdc7de307995881d7d51e9712c18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9ssica=20Sobral?= Date: Wed, 29 Jun 2022 21:01:12 -0300 Subject: [PATCH 1/9] test: add courserepositorytest class --- .../gestaoestagiosbackend/Application.java | 1 - .../course/CourseFactoryUtils.java | 29 +++++++------- .../course/CourseRepositoryTest.java | 39 +++++++++++++++++++ .../department/DepartmentFactoryUtils.java | 29 +++++++------- 4 files changed, 65 insertions(+), 33 deletions(-) create mode 100644 src/test/java/br/edu/ifsp/ifspcodelab/gestaoestagiosbackend/course/CourseRepositoryTest.java diff --git a/src/main/java/br/edu/ifsp/ifspcodelab/gestaoestagiosbackend/Application.java b/src/main/java/br/edu/ifsp/ifspcodelab/gestaoestagiosbackend/Application.java index 218eb187..1a94cd0e 100644 --- a/src/main/java/br/edu/ifsp/ifspcodelab/gestaoestagiosbackend/Application.java +++ b/src/main/java/br/edu/ifsp/ifspcodelab/gestaoestagiosbackend/Application.java @@ -14,7 +14,6 @@ @SpringBootApplication @EnableScheduling -@Import(RequestExpiresVerification.class) public class Application { public static void main(String[] args) { diff --git a/src/test/java/br/edu/ifsp/ifspcodelab/gestaoestagiosbackend/course/CourseFactoryUtils.java b/src/test/java/br/edu/ifsp/ifspcodelab/gestaoestagiosbackend/course/CourseFactoryUtils.java index 1d6d09e3..f778a4cc 100644 --- a/src/test/java/br/edu/ifsp/ifspcodelab/gestaoestagiosbackend/course/CourseFactoryUtils.java +++ b/src/test/java/br/edu/ifsp/ifspcodelab/gestaoestagiosbackend/course/CourseFactoryUtils.java @@ -1,16 +1,13 @@ -//package br.edu.ifsp.ifspcodelab.gestaoestagiosbackend.course; -// -// -//import br.edu.ifsp.ifspcodelab.gestaoestagiosbackend.department.Department; -//import br.edu.ifsp.ifspcodelab.gestaoestagiosbackend.department.DepartmentFactoryUtils; -// -//public class CourseFactoryUtils { -// public static Course sampleCourse() { -// String name = "Test Course"; -// String abbreviation = "TC"; -// Integer numberOfPeriods = 6; -// Department department = DepartmentFactoryUtils.sampleDepartment(); -// -// return new Course(name, abbreviation, numberOfPeriods, department); -// } -//} +package br.edu.ifsp.ifspcodelab.gestaoestagiosbackend.course; + +import br.edu.ifsp.ifspcodelab.gestaoestagiosbackend.department.Department; + +public class CourseFactoryUtils { + public static Course sampleCourse(Department department) { + String name = "Test Course"; + String abbreviation = "TC"; + Integer numberOfPeriods = 6; + + return new Course(name, abbreviation, numberOfPeriods, department); + } +} diff --git a/src/test/java/br/edu/ifsp/ifspcodelab/gestaoestagiosbackend/course/CourseRepositoryTest.java b/src/test/java/br/edu/ifsp/ifspcodelab/gestaoestagiosbackend/course/CourseRepositoryTest.java new file mode 100644 index 00000000..8a4e2fc0 --- /dev/null +++ b/src/test/java/br/edu/ifsp/ifspcodelab/gestaoestagiosbackend/course/CourseRepositoryTest.java @@ -0,0 +1,39 @@ +package br.edu.ifsp.ifspcodelab.gestaoestagiosbackend.course; + +import br.edu.ifsp.ifspcodelab.gestaoestagiosbackend.campus.Campus; +import br.edu.ifsp.ifspcodelab.gestaoestagiosbackend.campus.CampusFactoryUtils; +import br.edu.ifsp.ifspcodelab.gestaoestagiosbackend.city.City; +import br.edu.ifsp.ifspcodelab.gestaoestagiosbackend.city.CityFactoryUtils; +import br.edu.ifsp.ifspcodelab.gestaoestagiosbackend.department.Department; +import br.edu.ifsp.ifspcodelab.gestaoestagiosbackend.department.DepartmentFactoryUtils; +import br.edu.ifsp.ifspcodelab.gestaoestagiosbackend.state.State; +import br.edu.ifsp.ifspcodelab.gestaoestagiosbackend.state.StateFactoryUtils; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; +import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager; + +import java.util.UUID; + +import static org.assertj.core.api.Assertions.assertThat; + +@DataJpaTest +public class CourseRepositoryTest { + @Autowired + private TestEntityManager entityManager; + @Autowired + private CourseRepository courseRepository; + + private Course course; + + @BeforeEach + public void setUp() { + State state = entityManager.persistAndFlush(StateFactoryUtils.sampleState()); + City city = entityManager.persistAndFlush(CityFactoryUtils.sampleCity(state)); + Campus campus = entityManager.persistAndFlush(CampusFactoryUtils.sampleCampus(city)); + Department department = entityManager.persistAndFlush(DepartmentFactoryUtils.sampleDepartment(campus)); + course = CourseFactoryUtils.sampleCourse(department); + } + +} diff --git a/src/test/java/br/edu/ifsp/ifspcodelab/gestaoestagiosbackend/department/DepartmentFactoryUtils.java b/src/test/java/br/edu/ifsp/ifspcodelab/gestaoestagiosbackend/department/DepartmentFactoryUtils.java index 885f1c57..ee911206 100644 --- a/src/test/java/br/edu/ifsp/ifspcodelab/gestaoestagiosbackend/department/DepartmentFactoryUtils.java +++ b/src/test/java/br/edu/ifsp/ifspcodelab/gestaoestagiosbackend/department/DepartmentFactoryUtils.java @@ -1,16 +1,13 @@ -//package br.edu.ifsp.ifspcodelab.gestaoestagiosbackend.department; -// -//import br.edu.ifsp.ifspcodelab.gestaoestagiosbackend.campus.Campus; -//import br.edu.ifsp.ifspcodelab.gestaoestagiosbackend.campus.CampusFactoryUtils; -// -//public class DepartmentFactoryUtils { -// public static Department sampleDepartment() { -// Campus campus = CampusFactoryUtils.sampleCampus(); -// return new Department("Test Department", "TDP", campus); -// } -// -// public static Department sampleDepartment(String name, String abbreviation, Campus campus) { -// return new Department(name, abbreviation, campus); -// } -// -//} +package br.edu.ifsp.ifspcodelab.gestaoestagiosbackend.department; + +import br.edu.ifsp.ifspcodelab.gestaoestagiosbackend.campus.Campus; + +public class DepartmentFactoryUtils { + public static Department sampleDepartment(Campus campus) + { + String name = "Test Department"; + String abbreviation = "TDP"; + return new Department(name, abbreviation, campus); + } + +} From 8f9efc501f943d69910684fea03cc847a3e9d93d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9ssica=20Sobral?= Date: Wed, 29 Jun 2022 21:07:23 -0300 Subject: [PATCH 2/9] test: add test for existsbydepartmentid method --- .../course/CourseRepositoryTest.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/test/java/br/edu/ifsp/ifspcodelab/gestaoestagiosbackend/course/CourseRepositoryTest.java b/src/test/java/br/edu/ifsp/ifspcodelab/gestaoestagiosbackend/course/CourseRepositoryTest.java index 8a4e2fc0..80dc9884 100644 --- a/src/test/java/br/edu/ifsp/ifspcodelab/gestaoestagiosbackend/course/CourseRepositoryTest.java +++ b/src/test/java/br/edu/ifsp/ifspcodelab/gestaoestagiosbackend/course/CourseRepositoryTest.java @@ -36,4 +36,21 @@ public void setUp() { course = CourseFactoryUtils.sampleCourse(department); } + @Test + public void existsByDepartmentId() { + entityManager.persistAndFlush(course); + + boolean result = courseRepository.existsByDepartmentId(course.getDepartment().getId()); + + assertThat(result).isTrue(); + } + + @Test + public void notExistsByDepartmentId() { + entityManager.persistAndFlush(course); + + boolean result = courseRepository.existsByDepartmentId(UUID.randomUUID()); + + assertThat(result).isFalse(); + } } From 4d676da0ac994b17f844ebe6ebde253c09a86b3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9ssica=20Sobral?= Date: Wed, 29 Jun 2022 21:51:29 -0300 Subject: [PATCH 3/9] test: add curriculumrepositorytest class --- .../curriculum/CurriculumFactoryUtils.java | 15 ++++++++ .../curriculum/CurriculumRepositoryTest.java | 38 +++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 src/test/java/br/edu/ifsp/ifspcodelab/gestaoestagiosbackend/curriculum/CurriculumFactoryUtils.java create mode 100644 src/test/java/br/edu/ifsp/ifspcodelab/gestaoestagiosbackend/curriculum/CurriculumRepositoryTest.java diff --git a/src/test/java/br/edu/ifsp/ifspcodelab/gestaoestagiosbackend/curriculum/CurriculumFactoryUtils.java b/src/test/java/br/edu/ifsp/ifspcodelab/gestaoestagiosbackend/curriculum/CurriculumFactoryUtils.java new file mode 100644 index 00000000..a7ef05f6 --- /dev/null +++ b/src/test/java/br/edu/ifsp/ifspcodelab/gestaoestagiosbackend/curriculum/CurriculumFactoryUtils.java @@ -0,0 +1,15 @@ +package br.edu.ifsp.ifspcodelab.gestaoestagiosbackend.curriculum; + +import br.edu.ifsp.ifspcodelab.gestaoestagiosbackend.course.Course; + +public class CurriculumFactoryUtils { + public static Curriculum sampleCurriculum(Course course){ + String code = "1001"; + Integer courseLoad = 1001; + Integer internshipCourseLoad = 360; + String internshipStartCriteria = "Test internship start criteria"; + String internshipAllowedActivities = "Test internship allowed activities"; + + return new Curriculum(code, courseLoad, internshipCourseLoad, internshipStartCriteria, internshipAllowedActivities, course); + } +} diff --git a/src/test/java/br/edu/ifsp/ifspcodelab/gestaoestagiosbackend/curriculum/CurriculumRepositoryTest.java b/src/test/java/br/edu/ifsp/ifspcodelab/gestaoestagiosbackend/curriculum/CurriculumRepositoryTest.java new file mode 100644 index 00000000..6caf5d19 --- /dev/null +++ b/src/test/java/br/edu/ifsp/ifspcodelab/gestaoestagiosbackend/curriculum/CurriculumRepositoryTest.java @@ -0,0 +1,38 @@ +package br.edu.ifsp.ifspcodelab.gestaoestagiosbackend.curriculum; + +import br.edu.ifsp.ifspcodelab.gestaoestagiosbackend.campus.Campus; +import br.edu.ifsp.ifspcodelab.gestaoestagiosbackend.campus.CampusFactoryUtils; +import br.edu.ifsp.ifspcodelab.gestaoestagiosbackend.city.City; +import br.edu.ifsp.ifspcodelab.gestaoestagiosbackend.city.CityFactoryUtils; +import br.edu.ifsp.ifspcodelab.gestaoestagiosbackend.course.Course; +import br.edu.ifsp.ifspcodelab.gestaoestagiosbackend.course.CourseFactoryUtils; +import br.edu.ifsp.ifspcodelab.gestaoestagiosbackend.department.Department; +import br.edu.ifsp.ifspcodelab.gestaoestagiosbackend.department.DepartmentFactoryUtils; +import br.edu.ifsp.ifspcodelab.gestaoestagiosbackend.state.State; +import br.edu.ifsp.ifspcodelab.gestaoestagiosbackend.state.StateFactoryUtils; +import org.junit.jupiter.api.BeforeEach; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; +import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager; + +@DataJpaTest +public class CurriculumRepositoryTest { + @Autowired + private TestEntityManager entityManager; + + @Autowired + private CurriculumRepository curriculumRepository; + + private Curriculum curriculum; + + @BeforeEach + public void setUp() { + State state = entityManager.persistAndFlush(StateFactoryUtils.sampleState()); + City city = entityManager.persistAndFlush(CityFactoryUtils.sampleCity(state)); + Campus campus = entityManager.persistAndFlush(CampusFactoryUtils.sampleCampus(city)); + Department department = entityManager.persistAndFlush(DepartmentFactoryUtils.sampleDepartment(campus)); + Course course = entityManager.persistAndFlush(CourseFactoryUtils.sampleCourse(department)); + curriculum = CurriculumFactoryUtils.sampleCurriculum(course); + } + +} \ No newline at end of file From d7f711da94fa622f3c5f753dce5a3486fc51b295 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9ssica=20Sobral?= Date: Wed, 29 Jun 2022 21:54:49 -0300 Subject: [PATCH 4/9] test: add test for findallbycourseidandid method --- .../curriculum/CurriculumRepositoryTest.java | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/test/java/br/edu/ifsp/ifspcodelab/gestaoestagiosbackend/curriculum/CurriculumRepositoryTest.java b/src/test/java/br/edu/ifsp/ifspcodelab/gestaoestagiosbackend/curriculum/CurriculumRepositoryTest.java index 6caf5d19..a9658f0e 100644 --- a/src/test/java/br/edu/ifsp/ifspcodelab/gestaoestagiosbackend/curriculum/CurriculumRepositoryTest.java +++ b/src/test/java/br/edu/ifsp/ifspcodelab/gestaoestagiosbackend/curriculum/CurriculumRepositoryTest.java @@ -11,10 +11,16 @@ import br.edu.ifsp.ifspcodelab.gestaoestagiosbackend.state.State; import br.edu.ifsp.ifspcodelab.gestaoestagiosbackend.state.StateFactoryUtils; import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager; +import java.util.Optional; +import java.util.UUID; + +import static org.assertj.core.api.Assertions.assertThat; + @DataJpaTest public class CurriculumRepositoryTest { @Autowired @@ -35,4 +41,22 @@ public void setUp() { curriculum = CurriculumFactoryUtils.sampleCurriculum(course); } + @Test + public void findAllByCourseIdAndId() { + entityManager.persistAndFlush(curriculum); + + Optional result = curriculumRepository.findAllByCourseIdAndId(curriculum.getCourse().getId(), curriculum.getId()); + + assertThat(result.get().getId()).isEqualTo(curriculum.getId()); + } + + @Test + public void isEmptyFindAllByCourseIdAndId() { + entityManager.persistAndFlush(curriculum); + + Optional result = curriculumRepository.findAllByCourseIdAndId(UUID.randomUUID(), curriculum.getId()); + + assertThat(result).isEmpty(); + } + } \ No newline at end of file From 4ce61259112a6529695a3ebe64e485416775950c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9ssica=20Sobral?= Date: Thu, 30 Jun 2022 19:22:34 -0300 Subject: [PATCH 5/9] test: add test for disableallbydepartmentid method --- .../course/CourseRepositoryTest.java | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/test/java/br/edu/ifsp/ifspcodelab/gestaoestagiosbackend/course/CourseRepositoryTest.java b/src/test/java/br/edu/ifsp/ifspcodelab/gestaoestagiosbackend/course/CourseRepositoryTest.java index 80dc9884..181c9d2e 100644 --- a/src/test/java/br/edu/ifsp/ifspcodelab/gestaoestagiosbackend/course/CourseRepositoryTest.java +++ b/src/test/java/br/edu/ifsp/ifspcodelab/gestaoestagiosbackend/course/CourseRepositoryTest.java @@ -4,6 +4,7 @@ import br.edu.ifsp.ifspcodelab.gestaoestagiosbackend.campus.CampusFactoryUtils; import br.edu.ifsp.ifspcodelab.gestaoestagiosbackend.city.City; import br.edu.ifsp.ifspcodelab.gestaoestagiosbackend.city.CityFactoryUtils; +import br.edu.ifsp.ifspcodelab.gestaoestagiosbackend.common.enums.EntityStatus; import br.edu.ifsp.ifspcodelab.gestaoestagiosbackend.department.Department; import br.edu.ifsp.ifspcodelab.gestaoestagiosbackend.department.DepartmentFactoryUtils; import br.edu.ifsp.ifspcodelab.gestaoestagiosbackend.state.State; @@ -36,6 +37,28 @@ public void setUp() { course = CourseFactoryUtils.sampleCourse(department); } + @Test + public void disableAllByDepartmentId() { + entityManager.persistAndFlush(course); + + courseRepository.disableAllByDepartmentId(course.getDepartment().getId()); + entityManager.clear(); + Course course1 = entityManager.find(Course.class, course.getId()); + + assertThat(course1.getStatus()).isEqualTo(EntityStatus.DISABLED); + } + + @Test + public void notDisableAllByDepartmentId() { + entityManager.persistAndFlush(course); + + courseRepository.disableAllByDepartmentId(UUID.randomUUID()); + entityManager.clear(); + Course course1 = entityManager.find(Course.class, course.getId()); + + assertThat(course1.getStatus()).isEqualTo(EntityStatus.ENABLED); + } + @Test public void existsByDepartmentId() { entityManager.persistAndFlush(course); From 00b121a45db8599f95e2e060bea8ebd9daa72e95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9ssica=20Sobral?= Date: Thu, 30 Jun 2022 19:39:15 -0300 Subject: [PATCH 6/9] test: add test for findbyidin method --- .../course/CourseRepositoryTest.java | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/test/java/br/edu/ifsp/ifspcodelab/gestaoestagiosbackend/course/CourseRepositoryTest.java b/src/test/java/br/edu/ifsp/ifspcodelab/gestaoestagiosbackend/course/CourseRepositoryTest.java index 181c9d2e..ef988bd5 100644 --- a/src/test/java/br/edu/ifsp/ifspcodelab/gestaoestagiosbackend/course/CourseRepositoryTest.java +++ b/src/test/java/br/edu/ifsp/ifspcodelab/gestaoestagiosbackend/course/CourseRepositoryTest.java @@ -9,12 +9,15 @@ import br.edu.ifsp.ifspcodelab.gestaoestagiosbackend.department.DepartmentFactoryUtils; import br.edu.ifsp.ifspcodelab.gestaoestagiosbackend.state.State; import br.edu.ifsp.ifspcodelab.gestaoestagiosbackend.state.StateFactoryUtils; +import org.assertj.core.util.Arrays; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager; +import java.util.ArrayList; +import java.util.List; import java.util.UUID; import static org.assertj.core.api.Assertions.assertThat; @@ -76,4 +79,24 @@ public void notExistsByDepartmentId() { assertThat(result).isFalse(); } + + @Test + public void findByIdIn() { + entityManager.persistAndFlush(course); + List coursesIds = List.of(course.getId()); + + List result = courseRepository.findByIdIn(coursesIds); + + assertThat(result).hasSize(1); + } + + @Test + public void isEmptyFindByIdIn() { + entityManager.persistAndFlush(course); + List coursesIds = List.of(UUID.randomUUID()); + + List result = courseRepository.findByIdIn(coursesIds); + + assertThat(result).isEmpty(); + } } From 4c52bf79dcc4438f4b47f8138de8a1dfa4cea839 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9ssica=20Sobral?= Date: Thu, 30 Jun 2022 19:54:37 -0300 Subject: [PATCH 7/9] fix: remove unused import --- .../gestaoestagiosbackend/course/CourseRepositoryTest.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/test/java/br/edu/ifsp/ifspcodelab/gestaoestagiosbackend/course/CourseRepositoryTest.java b/src/test/java/br/edu/ifsp/ifspcodelab/gestaoestagiosbackend/course/CourseRepositoryTest.java index ef988bd5..d338df37 100644 --- a/src/test/java/br/edu/ifsp/ifspcodelab/gestaoestagiosbackend/course/CourseRepositoryTest.java +++ b/src/test/java/br/edu/ifsp/ifspcodelab/gestaoestagiosbackend/course/CourseRepositoryTest.java @@ -9,14 +9,12 @@ import br.edu.ifsp.ifspcodelab.gestaoestagiosbackend.department.DepartmentFactoryUtils; import br.edu.ifsp.ifspcodelab.gestaoestagiosbackend.state.State; import br.edu.ifsp.ifspcodelab.gestaoestagiosbackend.state.StateFactoryUtils; -import org.assertj.core.util.Arrays; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager; -import java.util.ArrayList; import java.util.List; import java.util.UUID; From 1f8abe3d714d02583336469bfe24ba5cf9b4549d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9ssica=20Sobral?= Date: Fri, 8 Jul 2022 15:41:57 -0300 Subject: [PATCH 8/9] refactor: bringing the scenarios for the tests of the courserepositorytest and curriculumrepositorytest classes --- .../course/CourseRepositoryTest.java | 61 ++++++++++++------- .../curriculum/CurriculumRepositoryTest.java | 13 ++-- 2 files changed, 48 insertions(+), 26 deletions(-) diff --git a/src/test/java/br/edu/ifsp/ifspcodelab/gestaoestagiosbackend/course/CourseRepositoryTest.java b/src/test/java/br/edu/ifsp/ifspcodelab/gestaoestagiosbackend/course/CourseRepositoryTest.java index d338df37..48fd6fa1 100644 --- a/src/test/java/br/edu/ifsp/ifspcodelab/gestaoestagiosbackend/course/CourseRepositoryTest.java +++ b/src/test/java/br/edu/ifsp/ifspcodelab/gestaoestagiosbackend/course/CourseRepositoryTest.java @@ -14,10 +14,8 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager; - import java.util.List; import java.util.UUID; - import static org.assertj.core.api.Assertions.assertThat; @DataJpaTest @@ -27,41 +25,53 @@ public class CourseRepositoryTest { @Autowired private CourseRepository courseRepository; + private Campus campus; + private Department department; private Course course; @BeforeEach public void setUp() { State state = entityManager.persistAndFlush(StateFactoryUtils.sampleState()); City city = entityManager.persistAndFlush(CityFactoryUtils.sampleCity(state)); - Campus campus = entityManager.persistAndFlush(CampusFactoryUtils.sampleCampus(city)); - Department department = entityManager.persistAndFlush(DepartmentFactoryUtils.sampleDepartment(campus)); - course = CourseFactoryUtils.sampleCourse(department); + campus = entityManager.persistAndFlush(CampusFactoryUtils.sampleCampus(city)); + department = entityManager.persistAndFlush(DepartmentFactoryUtils.sampleDepartment(campus)); } @Test public void disableAllByDepartmentId() { - entityManager.persistAndFlush(course); - - courseRepository.disableAllByDepartmentId(course.getDepartment().getId()); - entityManager.clear(); - Course course1 = entityManager.find(Course.class, course.getId()); - - assertThat(course1.getStatus()).isEqualTo(EntityStatus.DISABLED); + Department department1 = entityManager.persistAndFlush(DepartmentFactoryUtils.sampleDepartment(campus)); + Department department2 = entityManager.persistAndFlush(DepartmentFactoryUtils.sampleDepartment(campus)); + entityManager.persistAndFlush(CourseFactoryUtils.sampleCourse(department1)); + entityManager.persistAndFlush(CourseFactoryUtils.sampleCourse(department1)); + Course course3 = entityManager.persistAndFlush(CourseFactoryUtils.sampleCourse(department2)); + + courseRepository.disableAllByDepartmentId(department1.getId()); + + List courses = courseRepository.findAllByStatus(EntityStatus.ENABLED); + assertThat(courses) + .hasSize(1) + .contains(course3); } @Test public void notDisableAllByDepartmentId() { - entityManager.persistAndFlush(course); + Department department1 = entityManager.persistAndFlush(DepartmentFactoryUtils.sampleDepartment(campus)); + Department department2 = entityManager.persistAndFlush(DepartmentFactoryUtils.sampleDepartment(campus)); + Course course1 = entityManager.persistAndFlush(CourseFactoryUtils.sampleCourse(department1)); + Course course2 = entityManager.persistAndFlush(CourseFactoryUtils.sampleCourse(department1)); + Course course3 = entityManager.persistAndFlush(CourseFactoryUtils.sampleCourse(department2)); courseRepository.disableAllByDepartmentId(UUID.randomUUID()); - entityManager.clear(); - Course course1 = entityManager.find(Course.class, course.getId()); - assertThat(course1.getStatus()).isEqualTo(EntityStatus.ENABLED); + List courses = courseRepository.findAllByStatus(EntityStatus.ENABLED); + assertThat(courses) + .hasSize(3) + .contains(course1, course2, course3); } @Test public void existsByDepartmentId() { + course = CourseFactoryUtils.sampleCourse(department); entityManager.persistAndFlush(course); boolean result = courseRepository.existsByDepartmentId(course.getDepartment().getId()); @@ -71,6 +81,7 @@ public void existsByDepartmentId() { @Test public void notExistsByDepartmentId() { + course = CourseFactoryUtils.sampleCourse(department); entityManager.persistAndFlush(course); boolean result = courseRepository.existsByDepartmentId(UUID.randomUUID()); @@ -80,18 +91,26 @@ public void notExistsByDepartmentId() { @Test public void findByIdIn() { - entityManager.persistAndFlush(course); - List coursesIds = List.of(course.getId()); + Department department1 = entityManager.persistAndFlush(DepartmentFactoryUtils.sampleDepartment(campus)); + Course course1 = entityManager.persistAndFlush(CourseFactoryUtils.sampleCourse(department1)); + Course course2 = entityManager.persistAndFlush(CourseFactoryUtils.sampleCourse(department1)); + entityManager.persistAndFlush(CourseFactoryUtils.sampleCourse(department1)); + List coursesIds = List.of(course1.getId(), course2.getId()); List result = courseRepository.findByIdIn(coursesIds); - assertThat(result).hasSize(1); + assertThat(result) + .hasSize(2) + .contains(course1, course2); } @Test public void isEmptyFindByIdIn() { - entityManager.persistAndFlush(course); - List coursesIds = List.of(UUID.randomUUID()); + Department department1 = entityManager.persistAndFlush(DepartmentFactoryUtils.sampleDepartment(campus)); + entityManager.persistAndFlush(CourseFactoryUtils.sampleCourse(department1)); + entityManager.persistAndFlush(CourseFactoryUtils.sampleCourse(department1)); + entityManager.persistAndFlush(CourseFactoryUtils.sampleCourse(department1)); + List coursesIds = List.of(UUID.randomUUID(), UUID.randomUUID()); List result = courseRepository.findByIdIn(coursesIds); diff --git a/src/test/java/br/edu/ifsp/ifspcodelab/gestaoestagiosbackend/curriculum/CurriculumRepositoryTest.java b/src/test/java/br/edu/ifsp/ifspcodelab/gestaoestagiosbackend/curriculum/CurriculumRepositoryTest.java index a9658f0e..4b105f3a 100644 --- a/src/test/java/br/edu/ifsp/ifspcodelab/gestaoestagiosbackend/curriculum/CurriculumRepositoryTest.java +++ b/src/test/java/br/edu/ifsp/ifspcodelab/gestaoestagiosbackend/curriculum/CurriculumRepositoryTest.java @@ -15,10 +15,8 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager; - import java.util.Optional; import java.util.UUID; - import static org.assertj.core.api.Assertions.assertThat; @DataJpaTest @@ -29,6 +27,7 @@ public class CurriculumRepositoryTest { @Autowired private CurriculumRepository curriculumRepository; + private Course course; private Curriculum curriculum; @BeforeEach @@ -37,21 +36,25 @@ public void setUp() { City city = entityManager.persistAndFlush(CityFactoryUtils.sampleCity(state)); Campus campus = entityManager.persistAndFlush(CampusFactoryUtils.sampleCampus(city)); Department department = entityManager.persistAndFlush(DepartmentFactoryUtils.sampleDepartment(campus)); - Course course = entityManager.persistAndFlush(CourseFactoryUtils.sampleCourse(department)); - curriculum = CurriculumFactoryUtils.sampleCurriculum(course); + course = entityManager.persistAndFlush(CourseFactoryUtils.sampleCourse(department)); } @Test public void findAllByCourseIdAndId() { + curriculum = CurriculumFactoryUtils.sampleCurriculum(course); entityManager.persistAndFlush(curriculum); Optional result = curriculumRepository.findAllByCourseIdAndId(curriculum.getCourse().getId(), curriculum.getId()); - assertThat(result.get().getId()).isEqualTo(curriculum.getId()); + assertThat(result) + .isPresent() + .map(Curriculum::getId) + .isEqualTo(curriculum.getId()); } @Test public void isEmptyFindAllByCourseIdAndId() { + curriculum = CurriculumFactoryUtils.sampleCurriculum(course); entityManager.persistAndFlush(curriculum); Optional result = curriculumRepository.findAllByCourseIdAndId(UUID.randomUUID(), curriculum.getId()); From 4d52f6ff88066e564b80a488a7480256c78d3207 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9ssica=20Sobral?= Date: Fri, 8 Jul 2022 15:44:25 -0300 Subject: [PATCH 9/9] fix: bringing the scenarios for the tests --- .../course/CourseRepositoryTest.java | 36 +++++++++---------- .../curriculum/CurriculumRepositoryTest.java | 4 +-- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/src/test/java/br/edu/ifsp/ifspcodelab/gestaoestagiosbackend/course/CourseRepositoryTest.java b/src/test/java/br/edu/ifsp/ifspcodelab/gestaoestagiosbackend/course/CourseRepositoryTest.java index 48fd6fa1..02f1b9aa 100644 --- a/src/test/java/br/edu/ifsp/ifspcodelab/gestaoestagiosbackend/course/CourseRepositoryTest.java +++ b/src/test/java/br/edu/ifsp/ifspcodelab/gestaoestagiosbackend/course/CourseRepositoryTest.java @@ -39,13 +39,13 @@ public void setUp() { @Test public void disableAllByDepartmentId() { + department = entityManager.persistAndFlush(DepartmentFactoryUtils.sampleDepartment(campus)); Department department1 = entityManager.persistAndFlush(DepartmentFactoryUtils.sampleDepartment(campus)); - Department department2 = entityManager.persistAndFlush(DepartmentFactoryUtils.sampleDepartment(campus)); - entityManager.persistAndFlush(CourseFactoryUtils.sampleCourse(department1)); - entityManager.persistAndFlush(CourseFactoryUtils.sampleCourse(department1)); - Course course3 = entityManager.persistAndFlush(CourseFactoryUtils.sampleCourse(department2)); + entityManager.persistAndFlush(CourseFactoryUtils.sampleCourse(department)); + entityManager.persistAndFlush(CourseFactoryUtils.sampleCourse(department)); + Course course3 = entityManager.persistAndFlush(CourseFactoryUtils.sampleCourse(department1)); - courseRepository.disableAllByDepartmentId(department1.getId()); + courseRepository.disableAllByDepartmentId(department.getId()); List courses = courseRepository.findAllByStatus(EntityStatus.ENABLED); assertThat(courses) @@ -55,18 +55,18 @@ public void disableAllByDepartmentId() { @Test public void notDisableAllByDepartmentId() { + department = entityManager.persistAndFlush(DepartmentFactoryUtils.sampleDepartment(campus)); Department department1 = entityManager.persistAndFlush(DepartmentFactoryUtils.sampleDepartment(campus)); - Department department2 = entityManager.persistAndFlush(DepartmentFactoryUtils.sampleDepartment(campus)); - Course course1 = entityManager.persistAndFlush(CourseFactoryUtils.sampleCourse(department1)); - Course course2 = entityManager.persistAndFlush(CourseFactoryUtils.sampleCourse(department1)); - Course course3 = entityManager.persistAndFlush(CourseFactoryUtils.sampleCourse(department2)); + course = entityManager.persistAndFlush(CourseFactoryUtils.sampleCourse(department)); + Course course2 = entityManager.persistAndFlush(CourseFactoryUtils.sampleCourse(department)); + Course course3 = entityManager.persistAndFlush(CourseFactoryUtils.sampleCourse(department1)); courseRepository.disableAllByDepartmentId(UUID.randomUUID()); List courses = courseRepository.findAllByStatus(EntityStatus.ENABLED); assertThat(courses) .hasSize(3) - .contains(course1, course2, course3); + .contains(course, course2, course3); } @Test @@ -91,10 +91,10 @@ public void notExistsByDepartmentId() { @Test public void findByIdIn() { - Department department1 = entityManager.persistAndFlush(DepartmentFactoryUtils.sampleDepartment(campus)); - Course course1 = entityManager.persistAndFlush(CourseFactoryUtils.sampleCourse(department1)); - Course course2 = entityManager.persistAndFlush(CourseFactoryUtils.sampleCourse(department1)); - entityManager.persistAndFlush(CourseFactoryUtils.sampleCourse(department1)); + department = entityManager.persistAndFlush(DepartmentFactoryUtils.sampleDepartment(campus)); + Course course1 = entityManager.persistAndFlush(CourseFactoryUtils.sampleCourse(department)); + Course course2 = entityManager.persistAndFlush(CourseFactoryUtils.sampleCourse(department)); + entityManager.persistAndFlush(CourseFactoryUtils.sampleCourse(department)); List coursesIds = List.of(course1.getId(), course2.getId()); List result = courseRepository.findByIdIn(coursesIds); @@ -106,10 +106,10 @@ public void findByIdIn() { @Test public void isEmptyFindByIdIn() { - Department department1 = entityManager.persistAndFlush(DepartmentFactoryUtils.sampleDepartment(campus)); - entityManager.persistAndFlush(CourseFactoryUtils.sampleCourse(department1)); - entityManager.persistAndFlush(CourseFactoryUtils.sampleCourse(department1)); - entityManager.persistAndFlush(CourseFactoryUtils.sampleCourse(department1)); + department = entityManager.persistAndFlush(DepartmentFactoryUtils.sampleDepartment(campus)); + entityManager.persistAndFlush(CourseFactoryUtils.sampleCourse(department)); + entityManager.persistAndFlush(CourseFactoryUtils.sampleCourse(department)); + entityManager.persistAndFlush(CourseFactoryUtils.sampleCourse(department)); List coursesIds = List.of(UUID.randomUUID(), UUID.randomUUID()); List result = courseRepository.findByIdIn(coursesIds); diff --git a/src/test/java/br/edu/ifsp/ifspcodelab/gestaoestagiosbackend/curriculum/CurriculumRepositoryTest.java b/src/test/java/br/edu/ifsp/ifspcodelab/gestaoestagiosbackend/curriculum/CurriculumRepositoryTest.java index 4b105f3a..7ae60226 100644 --- a/src/test/java/br/edu/ifsp/ifspcodelab/gestaoestagiosbackend/curriculum/CurriculumRepositoryTest.java +++ b/src/test/java/br/edu/ifsp/ifspcodelab/gestaoestagiosbackend/curriculum/CurriculumRepositoryTest.java @@ -48,7 +48,8 @@ public void findAllByCourseIdAndId() { assertThat(result) .isPresent() - .map(Curriculum::getId) + .get() + .extracting(Curriculum::getId) .isEqualTo(curriculum.getId()); } @@ -61,5 +62,4 @@ public void isEmptyFindAllByCourseIdAndId() { assertThat(result).isEmpty(); } - } \ No newline at end of file