Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/add test to courserepositorytest and curriculumrepositorytest #143

Open
wants to merge 9 commits into
base: develop
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

@SpringBootApplication
@EnableScheduling
@Import(RequestExpiresVerification.class)
public class Application {

public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
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.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;
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.List;
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 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 = entityManager.persistAndFlush(CampusFactoryUtils.sampleCampus(city));
department = entityManager.persistAndFlush(DepartmentFactoryUtils.sampleDepartment(campus));
}

@Test
public void disableAllByDepartmentId() {
department = entityManager.persistAndFlush(DepartmentFactoryUtils.sampleDepartment(campus));
Department department1 = entityManager.persistAndFlush(DepartmentFactoryUtils.sampleDepartment(campus));
entityManager.persistAndFlush(CourseFactoryUtils.sampleCourse(department));
entityManager.persistAndFlush(CourseFactoryUtils.sampleCourse(department));
Course course3 = entityManager.persistAndFlush(CourseFactoryUtils.sampleCourse(department1));

courseRepository.disableAllByDepartmentId(department.getId());

List<Course> courses = courseRepository.findAllByStatus(EntityStatus.ENABLED);
assertThat(courses)
.hasSize(1)
.contains(course3);
}

@Test
public void notDisableAllByDepartmentId() {
department = entityManager.persistAndFlush(DepartmentFactoryUtils.sampleDepartment(campus));
Department department1 = entityManager.persistAndFlush(DepartmentFactoryUtils.sampleDepartment(campus));
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<Course> courses = courseRepository.findAllByStatus(EntityStatus.ENABLED);
assertThat(courses)
.hasSize(3)
.contains(course, course2, course3);
}

@Test
public void existsByDepartmentId() {
course = CourseFactoryUtils.sampleCourse(department);
entityManager.persistAndFlush(course);

boolean result = courseRepository.existsByDepartmentId(course.getDepartment().getId());

assertThat(result).isTrue();
}

@Test
public void notExistsByDepartmentId() {
course = CourseFactoryUtils.sampleCourse(department);
entityManager.persistAndFlush(course);

boolean result = courseRepository.existsByDepartmentId(UUID.randomUUID());

assertThat(result).isFalse();
}

@Test
public void findByIdIn() {
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<UUID> coursesIds = List.of(course1.getId(), course2.getId());

List<Course> result = courseRepository.findByIdIn(coursesIds);

assertThat(result)
.hasSize(2)
.contains(course1, course2);
}

@Test
public void isEmptyFindByIdIn() {
department = entityManager.persistAndFlush(DepartmentFactoryUtils.sampleDepartment(campus));
entityManager.persistAndFlush(CourseFactoryUtils.sampleCourse(department));
entityManager.persistAndFlush(CourseFactoryUtils.sampleCourse(department));
entityManager.persistAndFlush(CourseFactoryUtils.sampleCourse(department));
List<UUID> coursesIds = List.of(UUID.randomUUID(), UUID.randomUUID());

List<Course> result = courseRepository.findByIdIn(coursesIds);

assertThat(result).isEmpty();
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
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.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
private TestEntityManager entityManager;

@Autowired
private CurriculumRepository curriculumRepository;

private Course course;
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 = entityManager.persistAndFlush(CourseFactoryUtils.sampleCourse(department));
}

@Test
public void findAllByCourseIdAndId() {
curriculum = CurriculumFactoryUtils.sampleCurriculum(course);
entityManager.persistAndFlush(curriculum);

Optional<Curriculum> result = curriculumRepository.findAllByCourseIdAndId(curriculum.getCourse().getId(), curriculum.getId());

assertThat(result)
.isPresent()
.get()
.extracting(Curriculum::getId)
.isEqualTo(curriculum.getId());
}

@Test
public void isEmptyFindAllByCourseIdAndId() {
curriculum = CurriculumFactoryUtils.sampleCurriculum(course);
entityManager.persistAndFlush(curriculum);

Optional<Curriculum> result = curriculumRepository.findAllByCourseIdAndId(UUID.randomUUID(), curriculum.getId());

assertThat(result).isEmpty();
}
}
Original file line number Diff line number Diff line change
@@ -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);
}

}