From 3707fe1014dd915b90f15baa3a720bbc2614526b Mon Sep 17 00:00:00 2001 From: fraanpsilva Date: Mon, 15 Aug 2022 16:32:10 +0100 Subject: [PATCH 01/15] Testes Classe DateUtils --- .../controller/TaskController.java | 20 +++++++------ src/main/resources/application.properties | 4 +-- .../taskbackend/utils/DateUtilsTest.java | 28 +++++++++++++++++++ 3 files changed, 41 insertions(+), 11 deletions(-) create mode 100644 src/test/java/br/ce/wcaquino/taskbackend/utils/DateUtilsTest.java 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..b97284847 100644 --- a/src/main/java/br/ce/wcaquino/taskbackend/controller/TaskController.java +++ b/src/main/java/br/ce/wcaquino/taskbackend/controller/TaskController.java @@ -2,7 +2,6 @@ import java.util.List; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; @@ -17,26 +16,29 @@ import br.ce.wcaquino.taskbackend.utils.ValidationException; @RestController -@RequestMapping(value ="/todo") +@RequestMapping(value = "/todo") public class TaskController { - @Autowired - private TaskRepo todoRepo; - + private final TaskRepo todoRepo; + + public TaskController(TaskRepo todoRepo) { + this.todoRepo = 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/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)); + } + +} From 42602b36a54d4fce286162c31f76e581a576d8c9 Mon Sep 17 00:00:00 2001 From: fraanpsilva Date: Mon, 15 Aug 2022 17:50:55 +0100 Subject: [PATCH 02/15] Testes Classe TaskController --- .../controller/TaskController.java | 8 +- .../controller/TaskControllerTest.java | 83 +++++++++++++++++++ 2 files changed, 86 insertions(+), 5 deletions(-) create mode 100644 src/test/java/br/ce/wcaquino/taskbackend/controller/TaskControllerTest.java 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 b97284847..9366ea2d3 100644 --- a/src/main/java/br/ce/wcaquino/taskbackend/controller/TaskController.java +++ b/src/main/java/br/ce/wcaquino/taskbackend/controller/TaskController.java @@ -2,6 +2,7 @@ import java.util.List; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; @@ -19,11 +20,8 @@ @RequestMapping(value = "/todo") public class TaskController { - private final TaskRepo todoRepo; - - public TaskController(TaskRepo todoRepo) { - this.todoRepo = todoRepo; - } + @Autowired + private TaskRepo todoRepo; @GetMapping public List findAll() { 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 From e1c3f45ab35ae95a437ef3f47fa351e815609864 Mon Sep 17 00:00:00 2001 From: fraanpsilva Date: Sun, 4 Sep 2022 22:24:27 +0100 Subject: [PATCH 03/15] add jenkinsfile --- Jenkinsfile | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 Jenkinsfile diff --git a/Jenkinsfile b/Jenkinsfile new file mode 100644 index 000000000..b3e6a69b1 --- /dev/null +++ b/Jenkinsfile @@ -0,0 +1,10 @@ +pipeline { + agent any + stages { + stage('Just Test'){ + steps{ + sh 'echo deu certo' + } + } + } +} \ No newline at end of file From 19a67f11937de2d3c25b9659b38fa150f0b6b7fd Mon Sep 17 00:00:00 2001 From: fraanpsilva Date: Mon, 5 Sep 2022 16:43:30 +0100 Subject: [PATCH 04/15] config jenkinsfile --- Jenkinsfile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index b3e6a69b1..abf1ec2fb 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -1,9 +1,9 @@ pipeline { agent any stages { - stage('Just Test'){ + stage('Build Backend'){ steps{ - sh 'echo deu certo' + sh 'mvn clean package -DskipTests=true' } } } From 1df086e0192b4b6792abf67f488b4f1c6e212ea1 Mon Sep 17 00:00:00 2001 From: fraanpsilva Date: Mon, 5 Sep 2022 16:47:09 +0100 Subject: [PATCH 05/15] config jenkinsfile --- Jenkinsfile | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Jenkinsfile b/Jenkinsfile index abf1ec2fb..d7e1421ec 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -6,5 +6,11 @@ pipeline { sh 'mvn clean package -DskipTests=true' } } + + stage('Unit Tests'){ + steps{ + sh 'mvn test' + } + } } } \ No newline at end of file From 16ff24cc5abc3562e4cd9022667d0043d321f5c9 Mon Sep 17 00:00:00 2001 From: fraanpsilva Date: Mon, 5 Sep 2022 16:59:14 +0100 Subject: [PATCH 06/15] config jenkinsfile --- Jenkinsfile | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/Jenkinsfile b/Jenkinsfile index d7e1421ec..d26ae1ddf 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -12,5 +12,17 @@ pipeline { sh 'mvn test' } } + +// config Sonarqube + stage('Sonar Analysis'){ + environment { + scannerHome = tool 'SONAR_SCANNER' + } + steps{ + withSonarQubeEnv('SONAR_LOCAL'){ + sh "${scannerHomer}/bin/sonar-scanner -e -Dsonar.projectKey=DeployBack -Dsonar.host.url=http://localhost:9000 -Dsonar.login=sqp_5973eb7788fffbba0060355cbb3c7d77a57bd5b0 -Dsonar.java.binaries=target -Dsonar.coverage.exclusions=**/mvn/**,**/src/test/**,**/model/**,**Application.java" + } + } + } } -} \ No newline at end of file +} From 5b76b5b79f46d50e9c156669c485586f6b82a333 Mon Sep 17 00:00:00 2001 From: fraanpsilva Date: Mon, 5 Sep 2022 18:23:41 +0100 Subject: [PATCH 07/15] config jenkinsfile --- Jenkinsfile | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index d26ae1ddf..7aeaac748 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -14,15 +14,15 @@ pipeline { } // config Sonarqube - stage('Sonar Analysis'){ - environment { - scannerHome = tool 'SONAR_SCANNER' - } - steps{ - withSonarQubeEnv('SONAR_LOCAL'){ - sh "${scannerHomer}/bin/sonar-scanner -e -Dsonar.projectKey=DeployBack -Dsonar.host.url=http://localhost:9000 -Dsonar.login=sqp_5973eb7788fffbba0060355cbb3c7d77a57bd5b0 -Dsonar.java.binaries=target -Dsonar.coverage.exclusions=**/mvn/**,**/src/test/**,**/model/**,**Application.java" - } - } - } + //stage('Sonar Analysis'){ + //environment { + // scannerHome = tool 'SONAR_SCANNER' + //} + //steps{ + //withSonarQubeEnv('SONAR_LOCAL'){ + //sh "${scannerHomer}/bin/sonar-scanner -e -Dsonar.projectKey=DeployBack -Dsonar.host.url=http://localhost:9000 -Dsonar.login=sqp_5973eb7788fffbba0060355cbb3c7d77a57bd5b0 -Dsonar.java.binaries=target -Dsonar.coverage.exclusions=**/mvn/**,**/src/test/**,**/model/**,**Application.java" + //} + //} + //} } } From 9a4d6736321f4133f7ab1e3dc39977f739f19f81 Mon Sep 17 00:00:00 2001 From: fraanpsilva Date: Mon, 12 Sep 2022 20:02:40 +0100 Subject: [PATCH 08/15] config jenkinsfile --- Jenkinsfile | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index 7aeaac748..4f91ab20c 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -13,6 +13,13 @@ pipeline { } } + stage('Deploy Backend'){ + steps{ + deploy adapters: [tomcat8(credentialsId: 'TomcatLogin', path: '', url: 'http://localhost:8080/')], contextPath: 'tasks-backend', war: 'target/tasks-backend.war' + } + + } + // config Sonarqube //stage('Sonar Analysis'){ //environment { @@ -20,9 +27,19 @@ pipeline { //} //steps{ //withSonarQubeEnv('SONAR_LOCAL'){ - //sh "${scannerHomer}/bin/sonar-scanner -e -Dsonar.projectKey=DeployBack -Dsonar.host.url=http://localhost:9000 -Dsonar.login=sqp_5973eb7788fffbba0060355cbb3c7d77a57bd5b0 -Dsonar.java.binaries=target -Dsonar.coverage.exclusions=**/mvn/**,**/src/test/**,**/model/**,**Application.java" + //sh "${scannerHome}/bin/sonar-scanner -e -Dsonar.projectKey=DeployBack -Dsonar.host.url=http://localhost:9000 -Dsonar.login=sqp_5973eb7788fffbba0060355cbb3c7d77a57bd5b0 -Dsonar.java.binaries=target -Dsonar.coverage.exclusions=**/mvn/**,**/src/test/**,**/model/**,**Application.java" //} //} //} - } + //} + + //stage('Quality Gate'){ + //steps{ + //sleep(5) + //timeout(time: 1, unit: 'MINUTES'){ + //waitForQualityGate abortPipeline: true + // } + + //} + // } } From 0dccca2dece52ceb34e6cfaeb8f28f79c4fc09c1 Mon Sep 17 00:00:00 2001 From: fraanpsilva Date: Mon, 12 Sep 2022 20:08:10 +0100 Subject: [PATCH 09/15] config jenkinsfile --- Jenkinsfile | 40 ++++++++++------------------------------ 1 file changed, 10 insertions(+), 30 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index 4f91ab20c..b44c0bb10 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -6,40 +6,20 @@ pipeline { sh 'mvn clean package -DskipTests=true' } } + } - stage('Unit Tests'){ - steps{ - sh 'mvn test' - } + 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('Deploy Backend'){ + steps{ + deploy adapters: [tomcat8(credentialsId: 'TomcatLogin', path: '', url: 'http://localhost:8080/')], contextPath: 'tasks-backend', war: 'target/tasks-backend.war' } -// config Sonarqube - //stage('Sonar Analysis'){ - //environment { - // scannerHome = tool 'SONAR_SCANNER' - //} - //steps{ - //withSonarQubeEnv('SONAR_LOCAL'){ - //sh "${scannerHome}/bin/sonar-scanner -e -Dsonar.projectKey=DeployBack -Dsonar.host.url=http://localhost:9000 -Dsonar.login=sqp_5973eb7788fffbba0060355cbb3c7d77a57bd5b0 -Dsonar.java.binaries=target -Dsonar.coverage.exclusions=**/mvn/**,**/src/test/**,**/model/**,**Application.java" - //} - //} - //} - //} + } + - //stage('Quality Gate'){ - //steps{ - //sleep(5) - //timeout(time: 1, unit: 'MINUTES'){ - //waitForQualityGate abortPipeline: true - // } - - //} - // } } From 0e1c34d265f55b4904d34be34223e9b9c26a5de6 Mon Sep 17 00:00:00 2001 From: fraanpsilva Date: Mon, 12 Sep 2022 20:09:40 +0100 Subject: [PATCH 10/15] config jenkinsfile --- Jenkinsfile | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index b44c0bb10..8f95a1acf 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -6,20 +6,20 @@ pipeline { sh 'mvn clean package -DskipTests=true' } } - } + - stage('Unit Tests'){ - steps{ - sh 'mvn test' + 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('Deploy Backend'){ + steps{ + deploy adapters: [tomcat8(credentialsId: 'TomcatLogin', path: '', url: 'http://localhost:8080/')], contextPath: 'tasks-backend', war: 'target/tasks-backend.war' + } + } } - } From e2b0d26d6e7c8a27b09da10052e8e8b7bbae6ec8 Mon Sep 17 00:00:00 2001 From: fraanpsilva Date: Mon, 12 Sep 2022 20:14:53 +0100 Subject: [PATCH 11/15] config jenkinsfile --- Jenkinsfile | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Jenkinsfile b/Jenkinsfile index 8f95a1acf..4bc566b99 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -20,6 +20,15 @@ pipeline { } } + + stage('API Tests'){ + steps{ + git credentialsId: 'git', url: 'git@github.com:fraanpsilva/api-test-RestaAssured.git' + bash 'mvn test' + + } + + } } } From f584836e3ff3c10711110d749c43fa91d978f2c4 Mon Sep 17 00:00:00 2001 From: fraanpsilva Date: Mon, 12 Sep 2022 20:19:42 +0100 Subject: [PATCH 12/15] config jenkinsfile --- Jenkinsfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Jenkinsfile b/Jenkinsfile index 4bc566b99..2a3bd1e23 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -24,7 +24,7 @@ pipeline { stage('API Tests'){ steps{ git credentialsId: 'git', url: 'git@github.com:fraanpsilva/api-test-RestaAssured.git' - bash 'mvn test' + sh 'mvn test' } From ea6ca01d822a1bbc2c6c155408af7a9500f3b85a Mon Sep 17 00:00:00 2001 From: fraanpsilva Date: Tue, 13 Sep 2022 12:01:37 +0100 Subject: [PATCH 13/15] config jenkinsfile --- Jenkinsfile | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index 2a3bd1e23..998a17e58 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -23,8 +23,11 @@ pipeline { stage('API Tests'){ steps{ - git credentialsId: 'git', url: 'git@github.com:fraanpsilva/api-test-RestaAssured.git' - sh 'mvn test' + dir('') { + git credentialsId: 'git', url: 'git@github.com:fraanpsilva/api-test-RestaAssured.git' + sh 'mvn test' + + } } From 33a2ff0f7daee818550269a5dba5e2de589d099f Mon Sep 17 00:00:00 2001 From: fraanpsilva Date: Tue, 13 Sep 2022 12:24:45 +0100 Subject: [PATCH 14/15] config jenkinsfile --- Jenkinsfile | 63 +++++++++++++++++++++++++++++------------------------ 1 file changed, 35 insertions(+), 28 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index 998a17e58..4f5d95c0a 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -1,37 +1,44 @@ pipeline { - agent any - stages { - stage('Build Backend'){ - steps{ - sh 'mvn clean package -DskipTests=true' - } - } + agent any + stages { + stage('Build Backend'){ + steps{ + sh 'mvn clean package -DskipTests=true' + } + } + stage('Unit Tests'){ + steps{ + sh 'mvn test' + } + } - 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('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('API Tests'){ - steps{ - dir('') { - 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' } - } + } + } + + } } From 70fafd9c0f0a16c0431e3a56db8287ba5860c4fd Mon Sep 17 00:00:00 2001 From: fraanpsilva Date: Tue, 13 Sep 2022 12:28:12 +0100 Subject: [PATCH 15/15] config jenkinsfile --- Jenkinsfile | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/Jenkinsfile b/Jenkinsfile index 4f5d95c0a..5936b9c34 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -39,6 +39,17 @@ pipeline { } } - + + stage('Functional Tests'){ + steps{ + dir('functional-test'){ + git credentialsId: 'git', url: 'git@github.com:fraanpsilva/tasks-functional-testes.git' + sh 'mvn test' + + } + } + + } + } }