diff --git a/Jenkinsfile b/Jenkinsfile new file mode 100644 index 000000000..8f37fa4ad --- /dev/null +++ b/Jenkinsfile @@ -0,0 +1,45 @@ +pipeline { + agent any + stages { + stage ('Build Backend') { + steps { + bat 'mvn clean package -DskipTests=true' + } + } + stage ('Unit Tests') { + steps { + bat 'mvn test' + } + } + stage ('Deploy Backend') { + steps { + deploy adapters: [tomcat8(credentialsId: 'TomcatLogin', path: '', url: 'http://localhost:8001/')], contextPath: 'tasks-backend', war: 'target/tasks-backend.war' + } + } + stage ('API Tests') { + steps { + dir('api-tests'){ + git credentialsId: 'GitHubLogin', url: 'https://github.com/elyfranmedeiros/tasks-api-test' + bat 'mvn test' + } + } + } + stage ('Deploy Frontend') { + steps { + dir('frontend') { + git credentialsId: 'GitHubLogin', url: 'https://github.com/elyfranmedeiros/tasks-frontend' + bat 'mvn clean package' + deploy adapters: [tomcat8(credentialsId: 'TomcatLogin', path: '', url: 'http://localhost:8001/')], contextPath: 'tasks', war: 'target/tasks.war' + } + } + } + stage ('Functional Tests') { + steps { + dir('functional-tests'){ + git credentialsId: 'GitHubLogin', url: 'https://github.com/elyfranmedeiros/tasks-functional-tests' + bat 'mvn test' + } + } + } + } +} \ No newline at end of file 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..1e01b23cf 100644 --- a/src/main/java/br/ce/wcaquino/taskbackend/controller/TaskController.java +++ b/src/main/java/br/ce/wcaquino/taskbackend/controller/TaskController.java @@ -5,10 +5,13 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.bind.annotation.RestController; import br.ce.wcaquino.taskbackend.model.Task; @@ -42,4 +45,10 @@ public ResponseEntity save(@RequestBody Task todo) throws ValidationExcept Task saved = todoRepo.save(todo); return new ResponseEntity(saved, HttpStatus.CREATED); } + + @DeleteMapping(value = "/{id}") + @ResponseStatus(code = HttpStatus.NO_CONTENT) + public void delete(@PathVariable Long id) { + todoRepo.deleteById(id); + } } 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..a71d65357 --- /dev/null +++ b/src/test/java/br/ce/wcaquino/taskbackend/controller/TaskControllerTest.java @@ -0,0 +1,81 @@ +package br.ce.wcaquino.taskbackend.controller; + +import static org.junit.Assert.*; + +import java.time.LocalDate; + +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; +import br.ce.wcaquino.taskbackend.utils.ValidationException; + +public class TaskControllerTest { + + @Mock + private TaskRepo todoRepo; + + @InjectMocks + private TaskController controller; + + private LocalDate date; + private Task task; + + @Before + public void before() { + MockitoAnnotations.initMocks(this); + task = new Task(); + } + + @Test + public void testEmptyDescription() { + date = LocalDate.now(); + task.setDueDate(date); + try { + controller.save(task); + Assert.fail("Não deveria chegar a esse ponto"); + } catch (ValidationException e) { + assertEquals("Fill the task description", e.getMessage()); + } + } + + @Test + public void testEmptyDueDate() { + task.setTask("Desc"); + try { + controller.save(task); + Assert.fail("Não deveria chegar a esse ponto"); + } catch (ValidationException e) { + assertEquals("Fill the due date", e.getMessage()); + } + } + + @Test + public void testOldDate() { + date = LocalDate.of(2005, 10, 10); + task.setDueDate(date); + task.setTask("Desc"); + try { + controller.save(task); + Assert.fail("Não deveria chegar a esse ponto"); + } catch (ValidationException e) { + assertEquals("Due date must not be in past", e.getMessage()); + } + } + + @Test + public void testTaskSavedSuccessfully() throws ValidationException { + date = LocalDate.now(); + task.setDueDate(date); + task.setTask("Desc"); + controller.save(task); + Mockito.verify(todoRepo).save(task); + } + +} 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..7982592ba --- /dev/null +++ b/src/test/java/br/ce/wcaquino/taskbackend/utils/DateUtilsTest.java @@ -0,0 +1,29 @@ +package br.ce.wcaquino.taskbackend.utils; + +import static org.junit.Assert.*; + +import java.time.LocalDate; + +import org.junit.Test; + +public class DateUtilsTest { + + @Test + public void testFutureDate() { + LocalDate date = LocalDate.of(2030, 10, 12); + assertTrue(DateUtils.isEqualOrFutureDate(date)); + } + + @Test + public void testCurrentDate() { + LocalDate date = LocalDate.now(); + assertTrue(DateUtils.isEqualOrFutureDate(date)); + } + + @Test + public void testOldDate() { + LocalDate date = LocalDate.of(2010, 10, 12); + assertFalse(DateUtils.isEqualOrFutureDate(date)); + } + +}