Skip to content
55 changes: 55 additions & 0 deletions Jenkinsfile
Original file line number Diff line number Diff line change
@@ -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'

}
}

}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Task> findAll() {
return todoRepo.findAll();
}

@PostMapping
public ResponseEntity<Task> 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);
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
server.port=8001
server.port=8080

## default connection pool
spring.datasource.hikari.connectionTimeout=20000
Expand All @@ -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}
Expand Down
Original file line number Diff line number Diff line change
@@ -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);

}

}
28 changes: 28 additions & 0 deletions src/test/java/br/ce/wcaquino/taskbackend/utils/DateUtilsTest.java
Original file line number Diff line number Diff line change
@@ -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));
}

}