diff --git a/.idea/hatcher-api.iml b/.idea/hatcher-api.iml index 79157c7..2c84192 100644 --- a/.idea/hatcher-api.iml +++ b/.idea/hatcher-api.iml @@ -147,5 +147,7 @@ + + \ No newline at end of file diff --git a/pom.xml b/pom.xml index ec6a559..1082c39 100644 --- a/pom.xml +++ b/pom.xml @@ -107,6 +107,11 @@ hibernate-validator 6.1.6.Final + + junit + junit + test + diff --git a/src/main/resources/db/migration/data/.gitkeep b/src/main/resources/db/migration/data/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/src/main/resources/db/migration/data/V20210203154851__course_migration.sql b/src/main/resources/db/migration/data/V20210203154851__course_migration.sql index bb14dc7..0b49a99 100644 --- a/src/main/resources/db/migration/data/V20210203154851__course_migration.sql +++ b/src/main/resources/db/migration/data/V20210203154851__course_migration.sql @@ -3,8 +3,4 @@ CREATE TABLE public.registration_course ( course_name varchar(100) NOT NULL, description varchar NULL, CONSTRAINT registration_course_pkey PRIMARY KEY (id) -); - -INSERT INTO public.registration_course -(course_name, description) -VALUES('Sistemas', 'Description'); \ No newline at end of file +); \ No newline at end of file diff --git a/src/test/java/org/ayty/hatcher/api/v1/competence/service/CourseRepositoryTest.java b/src/test/java/org/ayty/hatcher/api/v1/competence/service/CourseRepositoryTest.java new file mode 100644 index 0000000..b71926b --- /dev/null +++ b/src/test/java/org/ayty/hatcher/api/v1/competence/service/CourseRepositoryTest.java @@ -0,0 +1,70 @@ +package org.ayty.hatcher.api.v1.competence.service; + +import org.assertj.core.api.Assertions; +import org.ayty.hatcher.api.v1.competence.dto.Course; +import org.ayty.hatcher.api.v1.competence.jpa.CourseRepository; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; +import org.springframework.test.context.junit4.SpringRunner; + +import javax.persistence.GeneratedValue; +import javax.validation.ConstraintViolationException; + +import java.util.List; + +import static org.assertj.core.api.Assertions.*; + + +@RunWith(SpringRunner.class) +@DataJpaTest +public class CourseRepositoryTest { + @Autowired + private CourseRepository courseRepository; + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @Test + public void createShouldPersistData(){ + Course course = new Course(null, "name", "desc"); + this.courseRepository.save(course); + assertThat(course.getId()).isNotNull(); + assertThat(course.getName()).isEqualTo("name"); + assertThat(course.getDescription()).isEqualTo("desc"); + + } + + @Test + public void deleteShouldRemoveData() { + Course course = new Course(null, "name", "desc"); + this.courseRepository.save(course); + this.courseRepository.delete(course); + assertThat(courseRepository.findById(course.getId())).isEmpty(); + + } + + @Test + public void updateShouldChangeAndPersistData(){ + Course course = new Course(null, "name", "desc"); + this.courseRepository.save(course); + course.setName("name2"); + course.setDescription("Desc2"); + this.courseRepository.save(course); + this.courseRepository.findById(course.getId()); + assertThat(course.getId()).isNotNull(); + assertThat(course.getName()).isEqualTo("name2"); + assertThat(course.getDescription()).isEqualTo("Desc2"); + + } + + @Test + public void nameNullShouldThrownConstraintViolationException(){ + thrown.expect(ConstraintViolationException.class); + thrown.expectMessage("Não pode ser deixado vazio"); + this.courseRepository.save(new Course()); + } + +}