diff --git a/Jenkinsfile b/Jenkinsfile new file mode 100644 index 000000000..5936b9c34 --- /dev/null +++ b/Jenkinsfile @@ -0,0 +1,55 @@ +pipeline { + agent any + stages { + stage('Build Backend'){ + steps{ + sh 'mvn clean package -DskipTests=true' + } + } + + stage('Unit Tests'){ + steps{ + sh 'mvn test' + } + } + + stage('Deploy Backend'){ + steps{ + deploy adapters: [tomcat8(credentialsId: 'TomcatLogin', path: '', url: 'http://localhost:8080/')], contextPath: 'tasks-backend', war: 'target/tasks-backend.war' + } + } + + stage('API Tests'){ + steps{ + dir('api-test') { + git credentialsId: 'git', url: 'git@github.com:fraanpsilva/api-test-RestaAssured.git' + sh 'mvn test' + } + } + + } + + stage('Deploy Frontend'){ + steps{ + dir('frontend') { + git credentialsId: 'git', url: 'git@github.com:fraanpsilva/tasks-frontend.git' + sh 'mvn clean package' + deploy adapters: [tomcat8(credentialsId: 'TomcatLogin', path: '', url: 'http://localhost:8080/')], contextPath: 'tasks', war: 'target/tasks.war' + } + } + + } + + stage('Functional Tests'){ + steps{ + dir('functional-test'){ + git credentialsId: 'git', url: 'git@github.com:fraanpsilva/tasks-functional-testes.git' + sh 'mvn test' + + } + } + + } + + } +} diff --git a/src/main/java/br/ce/wcaquino/taskbackend/controller/TaskController.java b/src/main/java/br/ce/wcaquino/taskbackend/controller/TaskController.java index 2b9281372..9366ea2d3 100644 --- a/src/main/java/br/ce/wcaquino/taskbackend/controller/TaskController.java +++ b/src/main/java/br/ce/wcaquino/taskbackend/controller/TaskController.java @@ -17,26 +17,26 @@ import br.ce.wcaquino.taskbackend.utils.ValidationException; @RestController -@RequestMapping(value ="/todo") +@RequestMapping(value = "/todo") public class TaskController { @Autowired private TaskRepo todoRepo; - + @GetMapping public List findAll() { return todoRepo.findAll(); } - + @PostMapping public ResponseEntity save(@RequestBody Task todo) throws ValidationException { - if(todo.getTask() == null || todo.getTask() == "") { + if (todo.getTask() == null || todo.getTask() == "") { throw new ValidationException("Fill the task description"); } - if(todo.getDueDate() == null) { + if (todo.getDueDate() == null) { throw new ValidationException("Fill the due date"); } - if(!DateUtils.isEqualOrFutureDate(todo.getDueDate())) { + if (!DateUtils.isEqualOrFutureDate(todo.getDueDate())) { throw new ValidationException("Due date must not be in past"); } Task saved = todoRepo.save(todo); diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 176b13ed1..53d279a64 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,4 +1,4 @@ -server.port=8001 +server.port=8080 ## default connection pool spring.datasource.hikari.connectionTimeout=20000 @@ -7,7 +7,7 @@ spring.datasource.hikari.maximumPoolSize=5 ## PostgreSQL spring.datasource.url=jdbc:postgresql://${DATABASE_HOST:127.0.0.1}:${DATABASE_PORT:5433}/tasks spring.datasource.username=${DATABASE_USER:postgres} -spring.datasource.password=${DATABASE_PASSWD:password} +spring.datasource.password=${DATABASE_PASSWORD:password} #drop n create table again, good for testing, comment this in production spring.jpa.hibernate.ddl-auto=${DATABASE_UPDATE:create} diff --git a/src/test/java/br/ce/wcaquino/taskbackend/controller/TaskControllerTest.java b/src/test/java/br/ce/wcaquino/taskbackend/controller/TaskControllerTest.java new file mode 100644 index 000000000..06a74e1de --- /dev/null +++ b/src/test/java/br/ce/wcaquino/taskbackend/controller/TaskControllerTest.java @@ -0,0 +1,83 @@ +package br.ce.wcaquino.taskbackend.controller; + +import java.time.LocalDate; +import br.ce.wcaquino.taskbackend.utils.ValidationException; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import br.ce.wcaquino.taskbackend.model.Task; +import br.ce.wcaquino.taskbackend.repo.TaskRepo; + +public class TaskControllerTest { + + // definindo um mock + @Mock + private TaskRepo taskRepo; + + // injetando o mock na classe controller + @InjectMocks + TaskController controller = new TaskController(); + + // iniciando o mockito + @Before + public void setup() { + MockitoAnnotations.initMocks(this); + } + + @Test + public void naoDeveSalvarTarefaSemDescricao() { + Task todo = new Task(); + // todo.setTask("Descrição"); + todo.setDueDate(LocalDate.now()); + try { + controller.save(todo); + Assert.fail("Não deveria chegar nesse ponto"); + } catch (Exception e) { + Assert.assertEquals("Fill the task description", e.getMessage()); + } + + } + + @Test + public void naoDeveSalvarTarefaSemData() { + Task todo = new Task(); + // todo.setDueDate(LocalDate.now()); + todo.setTask("Descrição"); + try { + controller.save(todo); + } catch (Exception e) { + Assert.assertEquals("Fill the due date", e.getMessage()); + } + + } + + @Test + public void naoDeveSalvarTarefaComDataPassada() { + Task todo = new Task(); + todo.setDueDate(LocalDate.of(2020, 01, 01)); + todo.setTask("Descrição"); + try { + controller.save(todo); + } catch (Exception e) { + Assert.assertEquals("Due date must not be in past", e.getMessage()); + } + + } + + @Test + public void DeveSalvarTarefaComSucesso() throws ValidationException { + Task todo = new Task(); + todo.setDueDate(LocalDate.now()); + todo.setTask("Descrição"); + controller.save(todo); + + // verificando se a classe mockada foi invocada no método salvar + Mockito.verify(taskRepo).save(todo); + + } + +} \ No newline at end of file diff --git a/src/test/java/br/ce/wcaquino/taskbackend/utils/DateUtilsTest.java b/src/test/java/br/ce/wcaquino/taskbackend/utils/DateUtilsTest.java new file mode 100644 index 000000000..9fd88bc89 --- /dev/null +++ b/src/test/java/br/ce/wcaquino/taskbackend/utils/DateUtilsTest.java @@ -0,0 +1,28 @@ +package br.ce.wcaquino.taskbackend.utils; + +import org.junit.Assert; +import org.junit.Test; + +import java.time.LocalDate; + +public class DateUtilsTest { + + @Test + public void deveRetornarTrueParaDatasFuturas() { + LocalDate date = LocalDate.of(2030, 01, 01); + Assert.assertTrue(DateUtils.isEqualOrFutureDate(date)); + } + + @Test + public void deveRetornarFalseParaDatasPassadas() { + LocalDate date = LocalDate.of(2010, 01, 01); + Assert.assertFalse(DateUtils.isEqualOrFutureDate(date)); + } + + @Test + public void deveRetornarTrueParaDatasPresentes() { + LocalDate date = LocalDate.now(); + Assert.assertTrue(DateUtils.isEqualOrFutureDate(date)); + } + +}