From 73795a39b2355406559bead76a3c513495aa21e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Garc=C3=ADa=20Fern=C3=A1ndez?= Date: Wed, 19 Jan 2022 12:32:24 +0100 Subject: [PATCH 01/26] feat: Add integration components tests --- README.md | 10 +++++-- .../calculator/service/CalculatorService.java | 22 +++++++++++++++ .../service/CalculatorServiceTest.java | 28 +++++++++++++++++++ 3 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 src/main/java/com/geekshubs/calculator/service/CalculatorService.java create mode 100644 src/test/java/com/geekshubs/calculator/service/CalculatorServiceTest.java diff --git a/README.md b/README.md index bbf8b06..83fee7a 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,13 @@ A Java calculator web app, build by Maven, CI/CD Create calculator App * Sum: sum two numbers and return result -* Substract: substract two numbers and return result +* Subtract: subtract two numbers and return result +* Multiply: multiply two numbers and return result +* Divide: divide two numbers and return result + +Create a service to wrap those methods +* Sum: sum two numbers and return result +* Subtract: subtract two numbers and return result * Multiply: multiply two numbers and return result * Divide: divide two numbers and return result @@ -14,7 +20,7 @@ Create calculator App JDK-11 Maven 3.8.4 -## 1. Manualy Build, Test, and Deploy By Maven +## 1. Manually Build, Test, and Deploy By Maven ### 1.2 Run JUnit Test diff --git a/src/main/java/com/geekshubs/calculator/service/CalculatorService.java b/src/main/java/com/geekshubs/calculator/service/CalculatorService.java new file mode 100644 index 0000000..8607cfb --- /dev/null +++ b/src/main/java/com/geekshubs/calculator/service/CalculatorService.java @@ -0,0 +1,22 @@ +package com.geekshubs.calculator.service; + +import com.geekshubs.calculator.Calculator; + +public class CalculatorService { + + public Calculator Add(int x, int y) { + return new Calculator(x, y, x + y); + } + + public Calculator Sub(int x, int y) { + return new Calculator(x, y, x - y); + } + + public Calculator Mul(int x, int y) { + return new Calculator(x, y, x * y); + } + + public Calculator Div(int x, int y) { + return new Calculator(x, y, x / y); + } +} \ No newline at end of file diff --git a/src/test/java/com/geekshubs/calculator/service/CalculatorServiceTest.java b/src/test/java/com/geekshubs/calculator/service/CalculatorServiceTest.java new file mode 100644 index 0000000..a5450b1 --- /dev/null +++ b/src/test/java/com/geekshubs/calculator/service/CalculatorServiceTest.java @@ -0,0 +1,28 @@ +package com.geekshubs.calculator.service; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class CalculatorServiceTest { + + @Test + public void testAdd() { + assertEquals(34, new CalculatorService().Add(8, 26).getResult()); + } + + @Test + public void testSub() { + assertEquals(4, new CalculatorService().Sub(12, 8).getResult()); + } + + @Test + public void testMul() { + assertEquals(88, new CalculatorService().Mul(11, 8).getResult()); + } + + @Test + public void testDiv() { + assertEquals(1, new CalculatorService().Div(12, 12).getResult()); + } +} From 4767a007dc1276ad3a254a2fdd10e02d49bb0549 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Garc=C3=ADa=20Fern=C3=A1ndez?= Date: Wed, 19 Jan 2022 12:36:13 +0100 Subject: [PATCH 02/26] feat: Run actions on all pull requests --- .github/workflows/ci.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 4aa72f5..10fe402 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -9,7 +9,6 @@ on: push: branches: [ master ] pull_request: - branches: [ master ] jobs: continuous-testing: From 5af203e0a4cd1ff193c644fdb8556a85c0d13293 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Garc=C3=ADa=20Fern=C3=A1ndez?= Date: Wed, 19 Jan 2022 13:22:31 +0100 Subject: [PATCH 03/26] feat: Extract service instance --- .../calculator/service/CalculatorServiceTest.java | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/test/java/com/geekshubs/calculator/service/CalculatorServiceTest.java b/src/test/java/com/geekshubs/calculator/service/CalculatorServiceTest.java index a5450b1..b4f2790 100644 --- a/src/test/java/com/geekshubs/calculator/service/CalculatorServiceTest.java +++ b/src/test/java/com/geekshubs/calculator/service/CalculatorServiceTest.java @@ -6,23 +6,25 @@ public class CalculatorServiceTest { + CalculatorService calculatorService = new CalculatorService(); + @Test public void testAdd() { - assertEquals(34, new CalculatorService().Add(8, 26).getResult()); + assertEquals(34, calculatorService.Add(8, 26).getResult()); } @Test public void testSub() { - assertEquals(4, new CalculatorService().Sub(12, 8).getResult()); + assertEquals(4, calculatorService.Sub(12, 8).getResult()); } @Test public void testMul() { - assertEquals(88, new CalculatorService().Mul(11, 8).getResult()); + assertEquals(88, calculatorService.Mul(11, 8).getResult()); } @Test public void testDiv() { - assertEquals(1, new CalculatorService().Div(12, 12).getResult()); + assertEquals(1, calculatorService.Div(12, 12).getResult()); } } From 23d7dc8f658f81929dfb0c7c6677ba51bcec6d9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Garc=C3=ADa=20Fern=C3=A1ndez?= Date: Wed, 19 Jan 2022 13:53:57 +0100 Subject: [PATCH 04/26] feat: Add JaCoCo and sonarcloud.io configuration --- .github/workflows/ci.yml | 5 +++++ pom.xml | 20 ++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 10fe402..27d097c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -34,3 +34,8 @@ jobs: with: name: test-results path: target/surefire-reports/TEST*.xml + - name: Analyze with SonarCloud + run: mvn sonar:sonar -Dsonar.host.url=https://sonarcloud.io -Dsonar.login=$SONAR_TOKEN -Dsonar.organization=sonarqubegeekshubs + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} diff --git a/pom.xml b/pom.xml index 940c7c3..6c2ec5a 100644 --- a/pom.xml +++ b/pom.xml @@ -17,6 +17,7 @@ 3.9.0 3.2.0 2.22.2 + 0.8.7 @@ -70,6 +71,25 @@ + + org.jacoco + jacoco-maven-plugin + ${jacoco.maven.plugin.version} + + + + prepare-agent + + + + report + test + + report + + + + From ef4cec1e86c3c17d7d6a9620d4972970cebab39f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Garc=C3=ADa=20Fern=C3=A1ndez?= Date: Wed, 19 Jan 2022 16:58:22 +0100 Subject: [PATCH 05/26] feat: Publish API methods --- .github/workflows/test-report.yml | 2 +- Dockerfile | 17 ++++ README.md | 84 ++++++++++++++++++- docker-compose.yml | 6 ++ pom.xml | 65 +++++++++++++- .../calculator/rest/CalculatorAPI.java | 49 +++++++++++ src/main/webapp/META-INF/context.xml | 2 + src/main/webapp/WEB-INF/web.xml | 27 ++++++ src/main/webapp/index.html | 9 ++ src/main/webapp/index.jsp | 12 +++ .../geekshubs/calculator/CalculatorTest.java | 2 - .../calculator/rest/CalculatorAPITestIT.java | 60 +++++++++++++ 12 files changed, 325 insertions(+), 10 deletions(-) create mode 100644 Dockerfile create mode 100644 docker-compose.yml create mode 100644 src/main/java/com/geekshubs/calculator/rest/CalculatorAPI.java create mode 100644 src/main/webapp/META-INF/context.xml create mode 100644 src/main/webapp/WEB-INF/web.xml create mode 100644 src/main/webapp/index.html create mode 100644 src/main/webapp/index.jsp create mode 100644 src/test/java/com/geekshubs/calculator/rest/CalculatorAPITestIT.java diff --git a/.github/workflows/test-report.yml b/.github/workflows/test-report.yml index 0fe69f3..9abae61 100644 --- a/.github/workflows/test-report.yml +++ b/.github/workflows/test-report.yml @@ -1,7 +1,7 @@ name: 'Test Report' on: workflow_run: - workflows: ['CI'] # runs after CI workflow + workflows: [ 'CI' ] # runs after CI workflow types: - completed jobs: diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..fc66aa7 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,17 @@ +FROM maven:3.8.4-jdk-11 +MAINTAINER ricardogarfe + +RUN adduser --gecos "First Last,RoomNumber,WorkPhone,HomePhone" --disabled-password jetty +USER jetty +WORKDIR /home/jetty + +EXPOSE 8080 +# Run subsequent commands as jetty user +USER jetty + +ADD pom.xml /home/jetty +ADD src /home/jetty/src + +RUN mvn package +# Run script +CMD mvn jetty:run diff --git a/README.md b/README.md index 83fee7a..79c6de2 100644 --- a/README.md +++ b/README.md @@ -1,47 +1,85 @@ # A Java Maven Calculator Web App + A Java calculator web app, build by Maven, CI/CD # Requirements Create calculator App + * Sum: sum two numbers and return result * Subtract: subtract two numbers and return result * Multiply: multiply two numbers and return result * Divide: divide two numbers and return result Create a service to wrap those methods + * Sum: sum two numbers and return result * Subtract: subtract two numbers and return result * Multiply: multiply two numbers and return result * Divide: divide two numbers and return result +Create a REST service to expose those methods with URLs in browser: + +- /api/calculator/ping +- /api/calculator/add?x=8&y=26 +- /api/calculator/sub?x=12&y=8 +- /api/calculator/mul?x=11&y=8 +- /api/calculator/div?x=12&y=12 + ## System requirements -JDK-11 -Maven 3.8.4 +* JDK-11 +* Maven 3.8.4 ## 1. Manually Build, Test, and Deploy By Maven -### 1.2 Run JUnit Test +### 1.1 Run JUnit Test Maven execution: + ```console $ mvn clean test ``` Maven wrapper: + ```console $ mvn wrapper:wrapper -Dmaven=3.8.4 ``` Execute (linux/macos mvnw or windows mvnw.cmd): + ```console $ ./mvnw clean test ``` +### 1.2 Run Integration Test + +```console +$ mvn clean integration-test +``` + +### 1.3 Run Locally + +```console +$ mvn jetty:run +[INFO] Started Jetty Server +``` + +By default, the jetty port is 8080, so you should visit following urls in browser: + +- http://localhost:8080/calculator/api/calculator/ping +- http://localhost:8080/calculator/api/calculator/add?x=8&y=26 +- http://localhost:8080/calculator/api/calculator/sub?x=12&y=8 +- http://localhost:8080/calculator/api/calculator/mul?x=11&y=8 +- http://localhost:8080/calculator/api/calculator/div?x=12&y=12 + +To run in a different port, `mvn jetty:run -Djetty.port=`. + ## 2. Automatically Build and Test [Github action ci](.github/workflows/ci.yml) step definition: + ```yaml steps: # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it @@ -61,4 +99,42 @@ steps: ## 3. Containerize Your Web App -**TODO** +### 3.1. Build a docker image using Dockerfile: + +```console +$ docker build -t myjetty --no-cache -f Dockerfile . +[+] Building 18.9s (11/11) FINISHED +``` + +### 3.2. Run docker image locally + +```console +$ docker run --rm -p 8080:8080 myjetty +``` + +> Explain: --rm means delete the container after stopping it. + +Access the web app at http://localhost:8080/api/calculator/ping in browser. + +Press Control-C to stop and remove the container. + +### 3.3. Run docker-compose environment + +Define a service to use repository Docker image: + +```yaml +version: '3.8' +services: + calculator: + build: . + ports: + - "8080:8080" +``` + +Run docker-compose environment: + +```console +$ docker-compse up +``` + +Access the web app at http://localhost:8080/api/calculator/ping in browser. diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..08a02cd --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,6 @@ +version: '3.8' +services: + calculator: + build: . + ports: + - "8080:8080" diff --git a/pom.xml b/pom.xml index 6c2ec5a..6992c14 100644 --- a/pom.xml +++ b/pom.xml @@ -1,9 +1,10 @@ - + 4.0.0 com.geekshubs.javawebapp java-maven-calculator-web-app - jar + war 1.1-SNAPSHOT Calculator Web A Java Maven Calculator Web Application @@ -14,6 +15,9 @@ 11 2.2 5.8.2 + 4.5.13 + 11.0.7 + 3.0.3 3.9.0 3.2.0 2.22.2 @@ -21,6 +25,26 @@ + + org.glassfish.jersey.containers + jersey-container-servlet-core + ${jersey.version} + + + org.glassfish.jersey.media + jersey-media-json-jackson + ${jersey.version} + + + org.glassfish.jersey.core + jersey-server + ${jersey.version} + + + org.glassfish.jersey.inject + jersey-hk2 + ${jersey.version} + org.hamcrest hamcrest-library @@ -33,6 +57,12 @@ ${junit.version} test + + org.apache.httpcomponents + httpclient + ${apache.httpcomponents.version} + test + org.apache.maven.plugins maven-resources-plugin @@ -52,6 +82,35 @@ ${maven.compiler.target} + + org.eclipse.jetty + jetty-maven-plugin + ${jetty.maven.plugin.version} + + + /calculator + + 8079 + stop-jetty-for-it + 10 + + + + start-jetty + pre-integration-test + + start + + + + stop-jetty + post-integration-test + + stop + + + + org.apache.maven.plugins maven-surefire-plugin diff --git a/src/main/java/com/geekshubs/calculator/rest/CalculatorAPI.java b/src/main/java/com/geekshubs/calculator/rest/CalculatorAPI.java new file mode 100644 index 0000000..bb7b70d --- /dev/null +++ b/src/main/java/com/geekshubs/calculator/rest/CalculatorAPI.java @@ -0,0 +1,49 @@ +package com.geekshubs.calculator.rest; + +import com.geekshubs.calculator.Calculator; +import jakarta.ws.rs.GET; +import jakarta.ws.rs.Path; +import jakarta.ws.rs.Produces; +import jakarta.ws.rs.QueryParam; +import jakarta.ws.rs.core.MediaType; + +import java.util.Date; + +@Path("/calculator") +public class CalculatorAPI { + + @GET + @Path("ping") + @Produces(MediaType.TEXT_PLAIN) + public String ping() { + return "Welcome to Java Maven Calculator Web App!!!\n" + new Date(); + } + + @GET + @Path("add") + @Produces(MediaType.APPLICATION_JSON) + public Calculator Add(@QueryParam("x") int x, @QueryParam("y") int y) { + return new Calculator(x, y, x + y); + } + + @GET + @Path("sub") + @Produces(MediaType.APPLICATION_JSON) + public Calculator Sub(@QueryParam("x") int x, @QueryParam("y") int y) { + return new Calculator(x, y, x - y); + } + + @GET + @Path("mul") + @Produces(MediaType.APPLICATION_JSON) + public Calculator Mul(@QueryParam("x") int x, @QueryParam("y") int y) { + return new Calculator(x, y, x * y); + } + + @GET + @Path("div") + @Produces(MediaType.APPLICATION_JSON) + public Calculator Div(@QueryParam("x") int x, @QueryParam("y") int y) { + return new Calculator(x, y, x / y); + } +} diff --git a/src/main/webapp/META-INF/context.xml b/src/main/webapp/META-INF/context.xml new file mode 100644 index 0000000..6c7356e --- /dev/null +++ b/src/main/webapp/META-INF/context.xml @@ -0,0 +1,2 @@ + + diff --git a/src/main/webapp/WEB-INF/web.xml b/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000..06ccf0b --- /dev/null +++ b/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,27 @@ + + Calculator Web + + + index.html + index.jsp + + + + Calculator Service + org.glassfish.jersey.servlet.ServletContainer + + + jersey.config.server.provider.packages + com.geekshubs.calculator.rest + + + jersey.config.server.provider.scanning.recursive + false + + 1 + + + Calculator Service + /api/* + + diff --git a/src/main/webapp/index.html b/src/main/webapp/index.html new file mode 100644 index 0000000..14dee0b --- /dev/null +++ b/src/main/webapp/index.html @@ -0,0 +1,9 @@ + + + HTML - Calculator API + + + +
Hello, Calculator!
+ + diff --git a/src/main/webapp/index.jsp b/src/main/webapp/index.jsp new file mode 100644 index 0000000..12a74c3 --- /dev/null +++ b/src/main/webapp/index.jsp @@ -0,0 +1,12 @@ +<%@ page language="java" contentType="text/html; charset=ISO-8859-1" + pageEncoding="ISO-8859-1"%> + + + + + JSP - Calculator API + + + <%="Hello,JSP!" %> + + \ No newline at end of file diff --git a/src/test/java/com/geekshubs/calculator/CalculatorTest.java b/src/test/java/com/geekshubs/calculator/CalculatorTest.java index 815f3a1..e4b85c4 100644 --- a/src/test/java/com/geekshubs/calculator/CalculatorTest.java +++ b/src/test/java/com/geekshubs/calculator/CalculatorTest.java @@ -2,8 +2,6 @@ import org.junit.jupiter.api.Test; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.containsString; import static org.junit.jupiter.api.Assertions.assertEquals; public class CalculatorTest { diff --git a/src/test/java/com/geekshubs/calculator/rest/CalculatorAPITestIT.java b/src/test/java/com/geekshubs/calculator/rest/CalculatorAPITestIT.java new file mode 100644 index 0000000..5f20125 --- /dev/null +++ b/src/test/java/com/geekshubs/calculator/rest/CalculatorAPITestIT.java @@ -0,0 +1,60 @@ +package com.geekshubs.calculator.rest; + +import org.apache.http.HttpResponse; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.util.EntityUtils; +import org.junit.jupiter.api.Test; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.containsString; +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class CalculatorAPITestIT { + + @Test + public void testPing() throws Exception { + CloseableHttpClient httpclient = HttpClients.createDefault(); + HttpGet httpGet = new HttpGet("http://localhost:8080/calculator/api/calculator/ping"); + HttpResponse response = httpclient.execute(httpGet); + assertEquals(200, response.getStatusLine().getStatusCode()); + assertThat(EntityUtils.toString(response.getEntity()), containsString("Welcome to Java Maven Calculator Web App!!!")); + } + + @Test + public void testAdd() throws Exception { + CloseableHttpClient httpclient = HttpClients.createDefault(); + HttpGet httpGet = new HttpGet("http://localhost:8080/calculator/api/calculator/add?x=8&y=26"); + HttpResponse response = httpclient.execute(httpGet); + assertEquals(200, response.getStatusLine().getStatusCode()); + assertThat(EntityUtils.toString(response.getEntity()), containsString("\"result\":34")); + } + + @Test + public void testSub() throws Exception { + CloseableHttpClient httpclient = HttpClients.createDefault(); + HttpGet httpGet = new HttpGet("http://localhost:8080/calculator/api/calculator/sub?x=12&y=8"); + HttpResponse response = httpclient.execute(httpGet); + assertEquals(200, response.getStatusLine().getStatusCode()); + assertThat(EntityUtils.toString(response.getEntity()), containsString("\"result\":4")); + } + + @Test + public void testMul() throws Exception { + CloseableHttpClient httpclient = HttpClients.createDefault(); + HttpGet httpGet = new HttpGet("http://localhost:8080/calculator/api/calculator/mul?x=11&y=8"); + HttpResponse response = httpclient.execute(httpGet); + assertEquals(200, response.getStatusLine().getStatusCode()); + assertThat(EntityUtils.toString(response.getEntity()), containsString("\"result\":88")); + } + + @Test + public void testDiv() throws Exception { + CloseableHttpClient httpclient = HttpClients.createDefault(); + HttpGet httpGet = new HttpGet("http://localhost:8080/calculator/api/calculator/div?x=12&y=12"); + HttpResponse response = httpclient.execute(httpGet); + assertEquals(200, response.getStatusLine().getStatusCode()); + assertThat(EntityUtils.toString(response.getEntity()), containsString("\"result\":1")); + } +} From d48c83d98c65356bc362506fb4e102dec47a6b26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Garc=C3=ADa=20Fern=C3=A1ndez?= Date: Wed, 19 Jan 2022 17:11:11 +0100 Subject: [PATCH 06/26] fix: fix html bugs --- src/main/webapp/index.html | 5 +++-- src/main/webapp/index.jsp | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/main/webapp/index.html b/src/main/webapp/index.html index 14dee0b..45ea54c 100644 --- a/src/main/webapp/index.html +++ b/src/main/webapp/index.html @@ -1,5 +1,6 @@ - - + + + HTML - Calculator API diff --git a/src/main/webapp/index.jsp b/src/main/webapp/index.jsp index 12a74c3..f01dce8 100644 --- a/src/main/webapp/index.jsp +++ b/src/main/webapp/index.jsp @@ -1,7 +1,7 @@ <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> - + JSP - Calculator API @@ -9,4 +9,4 @@ <%="Hello,JSP!" %> - \ No newline at end of file + From a61a67cb05e32571465b02cc89ce1402ffd7e1f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Garc=C3=ADa=20Fern=C3=A1ndez?= Date: Wed, 19 Jan 2022 17:16:55 +0100 Subject: [PATCH 07/26] fix: fix test workflow --- .github/workflows/test-report.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test-report.yml b/.github/workflows/test-report.yml index 9abae61..aa92b13 100644 --- a/.github/workflows/test-report.yml +++ b/.github/workflows/test-report.yml @@ -1,7 +1,7 @@ name: 'Test Report' on: workflow_run: - workflows: [ 'CI' ] # runs after CI workflow + workflows: [CI] # runs after CI workflow types: - completed jobs: From b0f8c0290054a50a4f46492977de5c972e5ef270 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Garc=C3=ADa=20Fern=C3=A1ndez?= Date: Wed, 19 Jan 2022 17:21:10 +0100 Subject: [PATCH 08/26] fix: workflow from all branches --- .github/workflows/ci.yml | 2 +- .github/workflows/test-report.yml | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 27d097c..520a861 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,7 +1,7 @@ # This workflow will build a Java project with Maven # For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-maven -name: 'CI' +name: CI # Controls when the action will run. on: diff --git a/.github/workflows/test-report.yml b/.github/workflows/test-report.yml index aa92b13..fdc2819 100644 --- a/.github/workflows/test-report.yml +++ b/.github/workflows/test-report.yml @@ -1,7 +1,8 @@ name: 'Test Report' on: workflow_run: - workflows: [CI] # runs after CI workflow + workflows: + - CI types: - completed jobs: From a90d4efd16711a3d9b8cc0bd0fb02efa3cc5c827 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Garc=C3=ADa=20Fern=C3=A1ndez?= Date: Wed, 19 Jan 2022 17:27:41 +0100 Subject: [PATCH 09/26] fix: Revert workflow workaround --- .github/workflows/ci.yml | 2 +- .github/workflows/test-report.yml | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 520a861..27d097c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,7 +1,7 @@ # This workflow will build a Java project with Maven # For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-maven -name: CI +name: 'CI' # Controls when the action will run. on: diff --git a/.github/workflows/test-report.yml b/.github/workflows/test-report.yml index fdc2819..9abae61 100644 --- a/.github/workflows/test-report.yml +++ b/.github/workflows/test-report.yml @@ -1,8 +1,7 @@ name: 'Test Report' on: workflow_run: - workflows: - - CI + workflows: [ 'CI' ] # runs after CI workflow types: - completed jobs: From 6a63cf61fe832f1275ed438ec660a081ab295145 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Garc=C3=ADa=20Fern=C3=A1ndez?= Date: Wed, 19 Jan 2022 18:04:08 +0100 Subject: [PATCH 10/26] feat: Divide tests execution --- .github/workflows/ci.yml | 12 +- pom.xml | 129 +++++++++++++++++- .../ITCalculatorAPITest.java} | 4 +- .../calculator/rest/CalculatorAPITest.java | 35 +++++ 4 files changed, 167 insertions(+), 13 deletions(-) rename src/test/java/com/geekshubs/calculator/{rest/CalculatorAPITestIT.java => integration/ITCalculatorAPITest.java} (97%) create mode 100644 src/test/java/com/geekshubs/calculator/rest/CalculatorAPITest.java diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 27d097c..b72a5c1 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -25,15 +25,19 @@ jobs: distribution: 'zulu' cache: 'maven' - name: Compile - run: mvn compile - - name: Test - run: mvn verify + run: mvn clean compile + - name: Unit Test + run: mvn test + - name: Integration API Test + run: mvn verify -P integration-test - name: Upload test results uses: actions/upload-artifact@v2 # upload test results if: success() || failure() # run this step even if previous step failed with: name: test-results - path: target/surefire-reports/TEST*.xml + path: | + target/surefire-reports/TEST*.xml + target/failsafe-reports/TEST*.xml - name: Analyze with SonarCloud run: mvn sonar:sonar -Dsonar.host.url=https://sonarcloud.io -Dsonar.login=$SONAR_TOKEN -Dsonar.organization=sonarqubegeekshubs env: diff --git a/pom.xml b/pom.xml index 6992c14..395edd5 100644 --- a/pom.xml +++ b/pom.xml @@ -10,6 +10,9 @@ A Java Maven Calculator Web Application + + dev + UTF-8 11 11 @@ -23,6 +26,30 @@ 2.22.2 0.8.7 + + + all-tests + + all-tests + + false + false + + + + dev + + + integration-test + + + integration-test + + false + true + + + @@ -115,17 +142,40 @@ org.apache.maven.plugins maven-surefire-plugin ${maven.surefire.plugin.version} + + + ${surefireArgLine} + + ${skip.unit.tests} + + + **/IT*.java + + +
+ + org.apache.maven.plugins + maven-failsafe-plugin + 2.22.2 + - run-integration-test - integration-test + integration-tests - test + integration-test + verify - - **/*IT.java - + + ${failsafeArgLine} + + ${skip.integration.tests} @@ -135,17 +185,82 @@ jacoco-maven-plugin ${jacoco.maven.plugin.version} + + pre-unit-test prepare-agent + + + ${project.build.directory}/coverage-reports/jacoco.exec + + surefireArgLine + + + - report + post-unit-test test report + + + ${project.build.directory}/coverage-reports/jacoco.exec + + ${project.reporting.outputDirectory}/jacoco + + + + + + + + pre-integration-test + pre-integration-test + + prepare-agent + + + + ${project.build.directory}/coverage-reports/jacoco-it.exec + + failsafeArgLine + + + + + post-integration-test + post-integration-test + + report + + + + ${project.build.directory}/coverage-reports/jacoco-it.exec + + ${project.reporting.outputDirectory}/jacoco-it + diff --git a/src/test/java/com/geekshubs/calculator/rest/CalculatorAPITestIT.java b/src/test/java/com/geekshubs/calculator/integration/ITCalculatorAPITest.java similarity index 97% rename from src/test/java/com/geekshubs/calculator/rest/CalculatorAPITestIT.java rename to src/test/java/com/geekshubs/calculator/integration/ITCalculatorAPITest.java index 5f20125..171c602 100644 --- a/src/test/java/com/geekshubs/calculator/rest/CalculatorAPITestIT.java +++ b/src/test/java/com/geekshubs/calculator/integration/ITCalculatorAPITest.java @@ -1,4 +1,4 @@ -package com.geekshubs.calculator.rest; +package com.geekshubs.calculator.integration; import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpGet; @@ -11,7 +11,7 @@ import static org.hamcrest.Matchers.containsString; import static org.junit.jupiter.api.Assertions.assertEquals; -public class CalculatorAPITestIT { +public class ITCalculatorAPITest { @Test public void testPing() throws Exception { diff --git a/src/test/java/com/geekshubs/calculator/rest/CalculatorAPITest.java b/src/test/java/com/geekshubs/calculator/rest/CalculatorAPITest.java new file mode 100644 index 0000000..578153f --- /dev/null +++ b/src/test/java/com/geekshubs/calculator/rest/CalculatorAPITest.java @@ -0,0 +1,35 @@ +package com.geekshubs.calculator.rest; + +import org.junit.jupiter.api.Test; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.containsString; +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class CalculatorAPITest { + + @Test + public void testPing() { + assertThat(new CalculatorAPI().ping(), containsString("Welcome to Java Maven Calculator Web App!!!")); + } + + @Test + public void testAdd() { + assertEquals(34, new CalculatorAPI().Add(8, 26).getResult()); + } + + @Test + public void testSub() { + assertEquals(4, new CalculatorAPI().Sub(12, 8).getResult()); + } + + @Test + public void testMul() { + assertEquals(88, new CalculatorAPI().Mul(11, 8).getResult()); + } + + @Test + public void testDiv() { + assertEquals(1, new CalculatorAPI().Div(12, 12).getResult()); + } +} From 2e142005e0cc92a36404c606428cc32e0eb2eb2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Garc=C3=ADa=20Fern=C3=A1ndez?= Date: Wed, 19 Jan 2022 19:59:10 +0100 Subject: [PATCH 11/26] feat: Add cucumber tests first draft --- .github/workflows/ci.yml | 2 +- pom.xml | 73 +++++++++++++++++++ .../aceptance/api/ITRunCucumberTest.java | 17 +++++ .../calculator/aceptance/api/PingSteps.java | 41 +++++++++++ .../calculator/acceptance/api/ping.feature | 7 ++ 5 files changed, 139 insertions(+), 1 deletion(-) create mode 100644 src/test/java/com/geekshubs/calculator/aceptance/api/ITRunCucumberTest.java create mode 100644 src/test/java/com/geekshubs/calculator/aceptance/api/PingSteps.java create mode 100644 src/test/resources/com/geekshubs/calculator/acceptance/api/ping.feature diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index b72a5c1..110f4ed 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -29,7 +29,7 @@ jobs: - name: Unit Test run: mvn test - name: Integration API Test - run: mvn verify -P integration-test + run: mvn verify -Pintegration-test - name: Upload test results uses: actions/upload-artifact@v2 # upload test results if: success() || failure() # run this step even if previous step failed diff --git a/pom.xml b/pom.xml index 395edd5..8533e12 100644 --- a/pom.xml +++ b/pom.xml @@ -18,6 +18,9 @@ 11 2.2 5.8.2 + 7.0.0 + 3.0.0 + 1.8.2 4.5.13 11.0.7 3.0.3 @@ -84,6 +87,67 @@ ${junit.version} test
+ + io.cucumber + cucumber-java + ${cucumber.version} + test + + + io.cucumber + cucumber-junit-platform-engine + ${cucumber.version} + test + + + org.junit.platform + junit-platform-console + ${junit.platform.version} + test + + + org.junit.platform + junit-platform-runner + ${junit.platform.version} + test + + + + org.junit.platform + junit-platform-launcher + ${junit.platform.version} + test + + + org.junit.platform + junit-platform-suite + ${junit.platform.version} + test + + + org.junit.platform + junit-platform-suite-engine + ${junit.platform.version} + test + + + org.junit.platform + junit-platform-suite-api + ${junit.platform.version} + test + + + org.junit.jupiter + junit-jupiter-api + ${junit.version} + test + + + org.junit.jupiter + junit-jupiter-engine + ${junit.version} + test + org.apache.httpcomponents httpclient @@ -150,7 +214,16 @@ **/IT*.java + **/ITRunCucumberTest.java + + + + cucumber.junit-platform.naming-strategy=long + + diff --git a/src/test/java/com/geekshubs/calculator/aceptance/api/ITRunCucumberTest.java b/src/test/java/com/geekshubs/calculator/aceptance/api/ITRunCucumberTest.java new file mode 100644 index 0000000..76ea5df --- /dev/null +++ b/src/test/java/com/geekshubs/calculator/aceptance/api/ITRunCucumberTest.java @@ -0,0 +1,17 @@ +package com.geekshubs.calculator.aceptance.api; + +import org.junit.platform.suite.api.ConfigurationParameter; +import org.junit.platform.suite.api.IncludeEngines; +import org.junit.platform.suite.api.SelectClasspathResource; +import org.junit.platform.suite.api.Suite; + +import static io.cucumber.junit.platform.engine.Constants.PLUGIN_PROPERTY_NAME; +import static io.cucumber.junit.platform.engine.Constants.GLUE_PROPERTY_NAME; + +@Suite +@IncludeEngines("cucumber") +@SelectClasspathResource("com/geekshubs/calculator/acceptance/api/ping.feature") +@ConfigurationParameter(key = PLUGIN_PROPERTY_NAME, value = "pretty") +@ConfigurationParameter(key = GLUE_PROPERTY_NAME, value = "com.geekshubs.calculator.aceptance.api") +public class ITRunCucumberTest { +} diff --git a/src/test/java/com/geekshubs/calculator/aceptance/api/PingSteps.java b/src/test/java/com/geekshubs/calculator/aceptance/api/PingSteps.java new file mode 100644 index 0000000..07451c2 --- /dev/null +++ b/src/test/java/com/geekshubs/calculator/aceptance/api/PingSteps.java @@ -0,0 +1,41 @@ +package com.geekshubs.calculator.aceptance.api; + +import io.cucumber.java.en.Then; +import io.cucumber.java.en.When; +import org.apache.http.HttpResponse; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.util.EntityUtils; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.containsString; +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class PingSteps { + + HttpResponse response; + + @When("^I make a GET call on ([^\"]*)$") + public void iMakeAGETCallOn(String path) throws Exception { + CloseableHttpClient httpclient = HttpClients.createDefault(); + HttpGet httpGet = new HttpGet(String.format("http://localhost:8080%s",path)); + response = httpclient.execute(httpGet); + } + + @Then("^I should receive (\\d+) response status code$") + public void iShouldReceiveStatusCodeResponse(int code) { + assertEquals(code, response.getStatusLine().getStatusCode()); + } + + @Then("^should receive a welcome message$") + public void shouldReceiveAWelcomeMessage() throws Exception { + assertThat(EntityUtils.toString(response.getEntity()), containsString("Welcome to Java Maven Calculator Web App!!!")); + } + + @Then("^should receive result (\\d+)$") + public void shouldReceiveResultCorrect(int result) throws Exception { + assertThat(EntityUtils.toString(response.getEntity()), containsString(String.format("\"result\":%d", result))); + } + +} \ No newline at end of file diff --git a/src/test/resources/com/geekshubs/calculator/acceptance/api/ping.feature b/src/test/resources/com/geekshubs/calculator/acceptance/api/ping.feature new file mode 100644 index 0000000..bbb0c5d --- /dev/null +++ b/src/test/resources/com/geekshubs/calculator/acceptance/api/ping.feature @@ -0,0 +1,7 @@ +@API +Feature: Ping check + + Scenario: Should have a working ping check + When I make a GET call on /calculator/api/calculator/ping + Then I should receive 200 response status code + And should receive a welcome message From 389ad4c3649373d8f2f77e4fd677c2294b773b30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Garc=C3=ADa=20Fern=C3=A1ndez?= Date: Wed, 19 Jan 2022 20:29:39 +0100 Subject: [PATCH 12/26] feat: Refactor cucumber runner test Class --- pom.xml | 2 +- .../api/{ITRunCucumberTest.java => ITCucumberRunner.java} | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) rename src/test/java/com/geekshubs/calculator/aceptance/api/{ITRunCucumberTest.java => ITCucumberRunner.java} (95%) diff --git a/pom.xml b/pom.xml index 8533e12..5a25842 100644 --- a/pom.xml +++ b/pom.xml @@ -214,7 +214,7 @@ **/IT*.java - **/ITRunCucumberTest.java + **/ITCucumberRunner.java + + jersey.config.server.provider.packages + com.geekshubs.calculator.rest + + + jersey.config.server.provider.scanning.recursive + false + + 1 + + + Calculator Service + /api/* + + diff --git a/src/main/webapp/index.html b/src/main/webapp/index.html new file mode 100644 index 0000000..14dee0b --- /dev/null +++ b/src/main/webapp/index.html @@ -0,0 +1,9 @@ + + + HTML - Calculator API + + + +
Hello, Calculator!
+ + diff --git a/src/main/webapp/index.jsp b/src/main/webapp/index.jsp new file mode 100644 index 0000000..12a74c3 --- /dev/null +++ b/src/main/webapp/index.jsp @@ -0,0 +1,12 @@ +<%@ page language="java" contentType="text/html; charset=ISO-8859-1" + pageEncoding="ISO-8859-1"%> + + + + + JSP - Calculator API + + + <%="Hello,JSP!" %> + + \ No newline at end of file diff --git a/src/test/java/com/geekshubs/calculator/CalculatorTest.java b/src/test/java/com/geekshubs/calculator/CalculatorTest.java index 815f3a1..e4b85c4 100644 --- a/src/test/java/com/geekshubs/calculator/CalculatorTest.java +++ b/src/test/java/com/geekshubs/calculator/CalculatorTest.java @@ -2,8 +2,6 @@ import org.junit.jupiter.api.Test; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.containsString; import static org.junit.jupiter.api.Assertions.assertEquals; public class CalculatorTest { diff --git a/src/test/java/com/geekshubs/calculator/rest/CalculatorAPITestIT.java b/src/test/java/com/geekshubs/calculator/rest/CalculatorAPITestIT.java new file mode 100644 index 0000000..5f20125 --- /dev/null +++ b/src/test/java/com/geekshubs/calculator/rest/CalculatorAPITestIT.java @@ -0,0 +1,60 @@ +package com.geekshubs.calculator.rest; + +import org.apache.http.HttpResponse; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.util.EntityUtils; +import org.junit.jupiter.api.Test; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.containsString; +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class CalculatorAPITestIT { + + @Test + public void testPing() throws Exception { + CloseableHttpClient httpclient = HttpClients.createDefault(); + HttpGet httpGet = new HttpGet("http://localhost:8080/calculator/api/calculator/ping"); + HttpResponse response = httpclient.execute(httpGet); + assertEquals(200, response.getStatusLine().getStatusCode()); + assertThat(EntityUtils.toString(response.getEntity()), containsString("Welcome to Java Maven Calculator Web App!!!")); + } + + @Test + public void testAdd() throws Exception { + CloseableHttpClient httpclient = HttpClients.createDefault(); + HttpGet httpGet = new HttpGet("http://localhost:8080/calculator/api/calculator/add?x=8&y=26"); + HttpResponse response = httpclient.execute(httpGet); + assertEquals(200, response.getStatusLine().getStatusCode()); + assertThat(EntityUtils.toString(response.getEntity()), containsString("\"result\":34")); + } + + @Test + public void testSub() throws Exception { + CloseableHttpClient httpclient = HttpClients.createDefault(); + HttpGet httpGet = new HttpGet("http://localhost:8080/calculator/api/calculator/sub?x=12&y=8"); + HttpResponse response = httpclient.execute(httpGet); + assertEquals(200, response.getStatusLine().getStatusCode()); + assertThat(EntityUtils.toString(response.getEntity()), containsString("\"result\":4")); + } + + @Test + public void testMul() throws Exception { + CloseableHttpClient httpclient = HttpClients.createDefault(); + HttpGet httpGet = new HttpGet("http://localhost:8080/calculator/api/calculator/mul?x=11&y=8"); + HttpResponse response = httpclient.execute(httpGet); + assertEquals(200, response.getStatusLine().getStatusCode()); + assertThat(EntityUtils.toString(response.getEntity()), containsString("\"result\":88")); + } + + @Test + public void testDiv() throws Exception { + CloseableHttpClient httpclient = HttpClients.createDefault(); + HttpGet httpGet = new HttpGet("http://localhost:8080/calculator/api/calculator/div?x=12&y=12"); + HttpResponse response = httpclient.execute(httpGet); + assertEquals(200, response.getStatusLine().getStatusCode()); + assertThat(EntityUtils.toString(response.getEntity()), containsString("\"result\":1")); + } +} From 1e29f833b6302858a27e45b3ecaab843341338c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Garc=C3=ADa=20Fern=C3=A1ndez?= Date: Wed, 19 Jan 2022 17:11:11 +0100 Subject: [PATCH 18/26] fix: fix html bugs --- src/main/webapp/index.html | 5 +++-- src/main/webapp/index.jsp | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/main/webapp/index.html b/src/main/webapp/index.html index 14dee0b..45ea54c 100644 --- a/src/main/webapp/index.html +++ b/src/main/webapp/index.html @@ -1,5 +1,6 @@ - - + + + HTML - Calculator API diff --git a/src/main/webapp/index.jsp b/src/main/webapp/index.jsp index 12a74c3..f01dce8 100644 --- a/src/main/webapp/index.jsp +++ b/src/main/webapp/index.jsp @@ -1,7 +1,7 @@ <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> - + JSP - Calculator API @@ -9,4 +9,4 @@ <%="Hello,JSP!" %> - \ No newline at end of file + From e86623aa73ba437b0af7223286a219f0b10a4cfc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Garc=C3=ADa=20Fern=C3=A1ndez?= Date: Wed, 19 Jan 2022 17:16:55 +0100 Subject: [PATCH 19/26] fix: fix test workflow --- .github/workflows/test-report.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test-report.yml b/.github/workflows/test-report.yml index 9abae61..aa92b13 100644 --- a/.github/workflows/test-report.yml +++ b/.github/workflows/test-report.yml @@ -1,7 +1,7 @@ name: 'Test Report' on: workflow_run: - workflows: [ 'CI' ] # runs after CI workflow + workflows: [CI] # runs after CI workflow types: - completed jobs: From 5d733c898f2207dc9f0b8b5a483cb0ccaf83fec5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Garc=C3=ADa=20Fern=C3=A1ndez?= Date: Wed, 19 Jan 2022 17:21:10 +0100 Subject: [PATCH 20/26] fix: workflow from all branches --- .github/workflows/ci.yml | 2 +- .github/workflows/test-report.yml | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d5dab2b..c6d9e7d 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,7 +1,7 @@ # This workflow will build a Java project with Maven # For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-maven -name: 'CI' +name: CI # Controls when the action will run. on: diff --git a/.github/workflows/test-report.yml b/.github/workflows/test-report.yml index aa92b13..fdc2819 100644 --- a/.github/workflows/test-report.yml +++ b/.github/workflows/test-report.yml @@ -1,7 +1,8 @@ name: 'Test Report' on: workflow_run: - workflows: [CI] # runs after CI workflow + workflows: + - CI types: - completed jobs: From dadd91610c02c722c08c55bc718fcab2707f6f44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Garc=C3=ADa=20Fern=C3=A1ndez?= Date: Wed, 19 Jan 2022 17:27:41 +0100 Subject: [PATCH 21/26] fix: Revert workflow workaround --- .github/workflows/ci.yml | 2 +- .github/workflows/test-report.yml | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index c6d9e7d..d5dab2b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,7 +1,7 @@ # This workflow will build a Java project with Maven # For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-maven -name: CI +name: 'CI' # Controls when the action will run. on: diff --git a/.github/workflows/test-report.yml b/.github/workflows/test-report.yml index fdc2819..9abae61 100644 --- a/.github/workflows/test-report.yml +++ b/.github/workflows/test-report.yml @@ -1,8 +1,7 @@ name: 'Test Report' on: workflow_run: - workflows: - - CI + workflows: [ 'CI' ] # runs after CI workflow types: - completed jobs: From 7050632d05beb836c0d6254b6b2b1437dde336f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Garc=C3=ADa=20Fern=C3=A1ndez?= Date: Wed, 19 Jan 2022 18:04:08 +0100 Subject: [PATCH 22/26] feat: Divide tests execution --- .github/workflows/ci.yml | 12 +- pom.xml | 129 +++++++++++++++++- .../ITCalculatorAPITest.java} | 4 +- .../calculator/rest/CalculatorAPITest.java | 35 +++++ 4 files changed, 167 insertions(+), 13 deletions(-) rename src/test/java/com/geekshubs/calculator/{rest/CalculatorAPITestIT.java => integration/ITCalculatorAPITest.java} (97%) create mode 100644 src/test/java/com/geekshubs/calculator/rest/CalculatorAPITest.java diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d5dab2b..389c3ea 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -26,15 +26,19 @@ jobs: java-version: '17' cache: 'maven' - name: Compile - run: mvn compile - - name: Test - run: mvn verify + run: mvn clean compile + - name: Unit Test + run: mvn test + - name: Integration API Test + run: mvn verify -P integration-test - name: Upload test results uses: actions/upload-artifact@v3 # upload test results if: success() || failure() # run this step even if previous step failed with: name: test-results - path: target/surefire-reports/TEST*.xml + path: | + target/surefire-reports/TEST*.xml + target/failsafe-reports/TEST*.xml - name: Analyze with SonarCloud run: mvn sonar:sonar -Dsonar.host.url=https://sonarcloud.io -Dsonar.login=$SONAR_TOKEN -Dsonar.organization=sonarqubegeekshubs env: diff --git a/pom.xml b/pom.xml index 5798d0c..07324f4 100644 --- a/pom.xml +++ b/pom.xml @@ -10,6 +10,9 @@ A Java Maven Calculator Web Application + + dev + UTF-8 17 17 @@ -24,6 +27,30 @@ 2.22.2 0.8.7 + + + all-tests + + all-tests + + false + false + + + + dev + + + integration-test + + + integration-test + + false + true + + + @@ -116,17 +143,40 @@ org.apache.maven.plugins maven-surefire-plugin ${maven.surefire.plugin.version} + + + ${surefireArgLine} + + ${skip.unit.tests} + + + **/IT*.java + + +
+ + org.apache.maven.plugins + maven-failsafe-plugin + 2.22.2 + - run-integration-test - integration-test + integration-tests - test + integration-test + verify - - **/*IT.java - + + ${failsafeArgLine} + + ${skip.integration.tests} @@ -136,17 +186,82 @@ jacoco-maven-plugin ${jacoco.maven.plugin.version} + + pre-unit-test prepare-agent + + + ${project.build.directory}/coverage-reports/jacoco.exec + + surefireArgLine + + + - report + post-unit-test test report + + + ${project.build.directory}/coverage-reports/jacoco.exec + + ${project.reporting.outputDirectory}/jacoco + + + + + + + + pre-integration-test + pre-integration-test + + prepare-agent + + + + ${project.build.directory}/coverage-reports/jacoco-it.exec + + failsafeArgLine + + + + + post-integration-test + post-integration-test + + report + + + + ${project.build.directory}/coverage-reports/jacoco-it.exec + + ${project.reporting.outputDirectory}/jacoco-it + diff --git a/src/test/java/com/geekshubs/calculator/rest/CalculatorAPITestIT.java b/src/test/java/com/geekshubs/calculator/integration/ITCalculatorAPITest.java similarity index 97% rename from src/test/java/com/geekshubs/calculator/rest/CalculatorAPITestIT.java rename to src/test/java/com/geekshubs/calculator/integration/ITCalculatorAPITest.java index 5f20125..171c602 100644 --- a/src/test/java/com/geekshubs/calculator/rest/CalculatorAPITestIT.java +++ b/src/test/java/com/geekshubs/calculator/integration/ITCalculatorAPITest.java @@ -1,4 +1,4 @@ -package com.geekshubs.calculator.rest; +package com.geekshubs.calculator.integration; import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpGet; @@ -11,7 +11,7 @@ import static org.hamcrest.Matchers.containsString; import static org.junit.jupiter.api.Assertions.assertEquals; -public class CalculatorAPITestIT { +public class ITCalculatorAPITest { @Test public void testPing() throws Exception { diff --git a/src/test/java/com/geekshubs/calculator/rest/CalculatorAPITest.java b/src/test/java/com/geekshubs/calculator/rest/CalculatorAPITest.java new file mode 100644 index 0000000..578153f --- /dev/null +++ b/src/test/java/com/geekshubs/calculator/rest/CalculatorAPITest.java @@ -0,0 +1,35 @@ +package com.geekshubs.calculator.rest; + +import org.junit.jupiter.api.Test; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.containsString; +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class CalculatorAPITest { + + @Test + public void testPing() { + assertThat(new CalculatorAPI().ping(), containsString("Welcome to Java Maven Calculator Web App!!!")); + } + + @Test + public void testAdd() { + assertEquals(34, new CalculatorAPI().Add(8, 26).getResult()); + } + + @Test + public void testSub() { + assertEquals(4, new CalculatorAPI().Sub(12, 8).getResult()); + } + + @Test + public void testMul() { + assertEquals(88, new CalculatorAPI().Mul(11, 8).getResult()); + } + + @Test + public void testDiv() { + assertEquals(1, new CalculatorAPI().Div(12, 12).getResult()); + } +} From 8037ed04aed38dde74348a1e4bc2587315b24c7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Garc=C3=ADa=20Fern=C3=A1ndez?= Date: Thu, 25 Jan 2024 23:03:42 +0100 Subject: [PATCH 23/26] feat: set maven-war-plugin version --- pom.xml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/pom.xml b/pom.xml index 07324f4..392ff37 100644 --- a/pom.xml +++ b/pom.xml @@ -23,6 +23,7 @@ 11.0.7 3.0.3 3.10.1 + 3.3.2 3.2.0 2.22.2 0.8.7 @@ -110,6 +111,11 @@ ${maven.compiler.target} + + org.apache.maven.plugins + maven-war-plugin + ${maven-war-plugin.version} + org.eclipse.jetty jetty-maven-plugin From ed43938f549b3ed6996c673db84dcc16da9cb696 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Garc=C3=ADa=20Fern=C3=A1ndez?= Date: Wed, 19 Jun 2024 20:24:14 +0200 Subject: [PATCH 24/26] chore: enable run workflow manually --- .github/workflows/ci.yml | 3 +++ .github/workflows/test-report.yml | 2 ++ 2 files changed, 5 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1d6db2f..d5260b3 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -5,6 +5,9 @@ name: 'CI' # Controls when the action will run. on: + workflow_call: + workflow_dispatch: + # Triggers the workflow on push or pull request events but only for the master branch push: branches: diff --git a/.github/workflows/test-report.yml b/.github/workflows/test-report.yml index 9abae61..c1d5a19 100644 --- a/.github/workflows/test-report.yml +++ b/.github/workflows/test-report.yml @@ -1,5 +1,7 @@ name: 'Test Report' on: + workflow_call: + workflow_dispatch: workflow_run: workflows: [ 'CI' ] # runs after CI workflow types: From 518a4412d4538c0f57c46cb0451d1fb2591aa71e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Garc=C3=ADa=20Fern=C3=A1ndez?= Date: Thu, 20 Jan 2022 23:28:46 +0000 Subject: [PATCH 25/26] feat: enable gitpod --- .gitpod.Dockerfile | 7 +++++++ .gitpod.yml | 24 ++++++++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 .gitpod.Dockerfile create mode 100644 .gitpod.yml diff --git a/.gitpod.Dockerfile b/.gitpod.Dockerfile new file mode 100644 index 0000000..eb553cb --- /dev/null +++ b/.gitpod.Dockerfile @@ -0,0 +1,7 @@ +FROM gitpod/workspace-full + +USER gitpod + +RUN bash -c ". /home/gitpod/.sdkman/bin/sdkman-init.sh && \ + sdk install java 17.0.9-tem && \ + sdk default java 17.0.9-tem" diff --git a/.gitpod.yml b/.gitpod.yml new file mode 100644 index 0000000..0c92254 --- /dev/null +++ b/.gitpod.yml @@ -0,0 +1,24 @@ +# This configuration file was automatically generated by Gitpod. +# Please adjust to your needs (see https://www.gitpod.io/docs/config-gitpod-file) +# and commit this file to your remote git repository to share the goodness with others. + +image: + file: .gitpod.Dockerfile + +tasks: + - init: mvn package -DskipTests=false + +vscode: + extensions: + - redhat.java + - vscjava.vscode-java-debug + - vscjava.vscode-maven + +# Ports to expose on workspace startup +ports: + - port: 8080 + onOpen: open-preview + name: Calculator API + description: Calculator API init + visibility: private + protocol: http From 1ff8adb00788bbabba5e735e1a43be0de24e80bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Garc=C3=ADa=20Fern=C3=A1ndez?= Date: Fri, 21 Jun 2024 17:58:15 +0000 Subject: [PATCH 26/26] test: fix expected results --- .../calculator/integration/ITCalculatorAPITest.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/test/java/com/geekshubs/calculator/integration/ITCalculatorAPITest.java b/src/test/java/com/geekshubs/calculator/integration/ITCalculatorAPITest.java index 8db9db8..7d7754f 100644 --- a/src/test/java/com/geekshubs/calculator/integration/ITCalculatorAPITest.java +++ b/src/test/java/com/geekshubs/calculator/integration/ITCalculatorAPITest.java @@ -29,7 +29,7 @@ public void testAdd() throws Exception { HttpGet httpGet = new HttpGet("http://localhost:8080/calculator/api/calculator/add?x=2&y=3"); HttpResponse response = httpclient.execute(httpGet); assertEquals(200, response.getStatusLine().getStatusCode()); - assertThat(EntityUtils.toString(response.getEntity()), containsString("\"result\":34")); + assertThat(EntityUtils.toString(response.getEntity()), containsString("\"result\":5")); } @Test @@ -37,7 +37,7 @@ public void testSub() throws Exception { HttpGet httpGet = new HttpGet("http://localhost:8080/calculator/api/calculator/sub?x=2&y=3"); HttpResponse response = httpclient.execute(httpGet); assertEquals(200, response.getStatusLine().getStatusCode()); - assertThat(EntityUtils.toString(response.getEntity()), containsString("\"result\":4")); + assertThat(EntityUtils.toString(response.getEntity()), containsString("\"result\":-1")); } @Test @@ -45,7 +45,7 @@ public void testMul() throws Exception { HttpGet httpGet = new HttpGet("http://localhost:8080/calculator/api/calculator/mul?x=2&y=3"); HttpResponse response = httpclient.execute(httpGet); assertEquals(200, response.getStatusLine().getStatusCode()); - assertThat(EntityUtils.toString(response.getEntity()), containsString("\"result\":88")); + assertThat(EntityUtils.toString(response.getEntity()), containsString("\"result\":6")); } @Test