From 6f218f576031013714a78106a5b92d93b7798c5c 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 01/12] feat: Publish API methods --- Dockerfile | 17 +++++ README.md | 73 ++++++++++++++++++- docker-compose.yml | 6 ++ pom.xml | 60 ++++++++++++++- .../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 +++ .../calculator/rest/CalculatorAPITestIT.java | 60 +++++++++++++++ 10 files changed, 312 insertions(+), 3 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/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 da63004..79c6de2 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,14 @@ Create a service to wrap those methods * 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 @@ -25,7 +33,7 @@ Create a service to wrap those methods ## 1. Manually Build, Test, and Deploy By Maven -### 1.2 Run JUnit Test +### 1.1 Run JUnit Test Maven execution: @@ -45,6 +53,29 @@ Execute (linux/macos mvnw or windows mvnw.cmd): $ ./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: @@ -68,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 99c0aae..5798d0c 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ 4.0.0 com.geekshubs.javawebapp java-maven-calculator-web-app - jar + war 1.1-SNAPSHOT Calculator Web A Java Maven Calculator Web Application @@ -16,6 +16,9 @@ 17 2.2 5.8.2 + 4.5.13 + 11.0.7 + 3.0.3 3.10.1 3.2.0 2.22.2 @@ -23,6 +26,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 @@ -35,6 +58,12 @@ ${junit.version} test + + org.apache.httpcomponents + httpclient + ${apache.httpcomponents.version} + test + org.apache.maven.plugins maven-resources-plugin @@ -54,6 +83,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/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 0011602450bfd80c179216edd6d1636553576d88 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 02/12] 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 f190b0e9447bac51bf4fbdd5a7e520dc5825b04f 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 03/12] 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 c1d5a19..4559fdb 100644 --- a/.github/workflows/test-report.yml +++ b/.github/workflows/test-report.yml @@ -3,7 +3,7 @@ on: workflow_call: workflow_dispatch: workflow_run: - workflows: [ 'CI' ] # runs after CI workflow + workflows: [CI] # runs after CI workflow types: - completed jobs: From ebf9f6bf061f66a4dc5c59d76b98ae03ad5a513c 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 04/12] 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 f603c7e..6a3f6b4 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 4559fdb..89924e5 100644 --- a/.github/workflows/test-report.yml +++ b/.github/workflows/test-report.yml @@ -3,7 +3,8 @@ on: workflow_call: workflow_dispatch: workflow_run: - workflows: [CI] # runs after CI workflow + workflows: + - CI types: - completed jobs: From adc179c1eb7bee40608d986a9990311ac3d19c84 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 05/12] 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 6a3f6b4..f603c7e 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 89924e5..c1d5a19 100644 --- a/.github/workflows/test-report.yml +++ b/.github/workflows/test-report.yml @@ -3,8 +3,7 @@ on: workflow_call: workflow_dispatch: workflow_run: - workflows: - - CI + workflows: [ 'CI' ] # runs after CI workflow types: - completed jobs: From f07ef0ae088a7c687769dbb936486657de90973d 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 06/12] 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 f603c7e..116dc5f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -29,15 +29,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 4f7c2062a3ce42c5b32336c2b6d18017bcc0958b 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 07/12] 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 c33b23340aadd01b96c7d27d6581b5f3220f7b7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Garc=C3=ADa=20Fern=C3=A1ndez?= Date: Fri, 26 Jan 2024 18:07:33 +0000 Subject: [PATCH 08/12] feat: update IT for API --- .github/workflows/ci.yml | 2 +- .../calculator/rest/CalculatorAPI.java | 12 ++++++++---- .../integration/ITCalculatorAPITest.java | 16 ++++++++-------- .../calculator/rest/CalculatorAPITest.java | 19 ------------------- 4 files changed, 17 insertions(+), 32 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 116dc5f..d5260b3 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -33,7 +33,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@v3 # upload test results if: success() || failure() # run this step even if previous step failed diff --git a/src/main/java/com/geekshubs/calculator/rest/CalculatorAPI.java b/src/main/java/com/geekshubs/calculator/rest/CalculatorAPI.java index bb7b70d..8d60dd0 100644 --- a/src/main/java/com/geekshubs/calculator/rest/CalculatorAPI.java +++ b/src/main/java/com/geekshubs/calculator/rest/CalculatorAPI.java @@ -1,6 +1,8 @@ package com.geekshubs.calculator.rest; import com.geekshubs.calculator.Calculator; +import com.geekshubs.calculator.service.CalculatorService; + import jakarta.ws.rs.GET; import jakarta.ws.rs.Path; import jakarta.ws.rs.Produces; @@ -12,6 +14,8 @@ @Path("/calculator") public class CalculatorAPI { + CalculatorService calculatorService = new CalculatorService(); + @GET @Path("ping") @Produces(MediaType.TEXT_PLAIN) @@ -23,27 +27,27 @@ public String ping() { @Path("add") @Produces(MediaType.APPLICATION_JSON) public Calculator Add(@QueryParam("x") int x, @QueryParam("y") int y) { - return new Calculator(x, y, x + y); + return calculatorService.Add(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); + return calculatorService.Sub(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); + return calculatorService.Mul(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); + return calculatorService.Div(x, y); } } diff --git a/src/test/java/com/geekshubs/calculator/integration/ITCalculatorAPITest.java b/src/test/java/com/geekshubs/calculator/integration/ITCalculatorAPITest.java index 171c602..75f03d9 100644 --- a/src/test/java/com/geekshubs/calculator/integration/ITCalculatorAPITest.java +++ b/src/test/java/com/geekshubs/calculator/integration/ITCalculatorAPITest.java @@ -25,36 +25,36 @@ public void testPing() throws Exception { @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"); + 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("5")); } @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"); + 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("-1")); } @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"); + 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("6")); } @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"); + HttpGet httpGet = new HttpGet("http://localhost:8080/calculator/api/calculator/div?x=6&y=3"); HttpResponse response = httpclient.execute(httpGet); assertEquals(200, response.getStatusLine().getStatusCode()); - assertThat(EntityUtils.toString(response.getEntity()), containsString("\"result\":1")); + assertThat(EntityUtils.toString(response.getEntity()), containsString("2")); } } diff --git a/src/test/java/com/geekshubs/calculator/rest/CalculatorAPITest.java b/src/test/java/com/geekshubs/calculator/rest/CalculatorAPITest.java index 578153f..e7c4a9c 100644 --- a/src/test/java/com/geekshubs/calculator/rest/CalculatorAPITest.java +++ b/src/test/java/com/geekshubs/calculator/rest/CalculatorAPITest.java @@ -13,23 +13,4 @@ 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 b54b2eb983d04e85d7404c81634f42e15c317ddc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Garc=C3=ADa=20Fern=C3=A1ndez?= Date: Fri, 26 Jan 2024 18:10:33 +0000 Subject: [PATCH 09/12] chore: check test report results --- .github/workflows/test-report.yml | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/.github/workflows/test-report.yml b/.github/workflows/test-report.yml index c1d5a19..1edc962 100644 --- a/.github/workflows/test-report.yml +++ b/.github/workflows/test-report.yml @@ -3,9 +3,8 @@ on: workflow_call: workflow_dispatch: workflow_run: - workflows: [ 'CI' ] # runs after CI workflow - types: - - completed + workflows: 'CI' # runs after CI workflow + jobs: report: runs-on: ubuntu-latest From de01839738cb8609fbdbc2dbd93d9166fe9be5fb 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 10/12] 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 79a6660d51c749efe7951b4a06785b97ec83651c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Garc=C3=ADa=20Fern=C3=A1ndez?= Date: Fri, 21 Jun 2024 17:16:58 +0000 Subject: [PATCH 11/12] test: refactor IT calculator tests --- .../integration/ITCalculatorAPITest.java | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/test/java/com/geekshubs/calculator/integration/ITCalculatorAPITest.java b/src/test/java/com/geekshubs/calculator/integration/ITCalculatorAPITest.java index 75f03d9..a8e32fc 100644 --- a/src/test/java/com/geekshubs/calculator/integration/ITCalculatorAPITest.java +++ b/src/test/java/com/geekshubs/calculator/integration/ITCalculatorAPITest.java @@ -10,12 +10,14 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.containsString; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; public class ITCalculatorAPITest { + CloseableHttpClient httpclient = HttpClients.createDefault(); + @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()); @@ -24,7 +26,6 @@ public void testPing() throws Exception { @Test public void testAdd() throws Exception { - CloseableHttpClient httpclient = HttpClients.createDefault(); 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()); @@ -33,7 +34,6 @@ public void testAdd() throws Exception { @Test public void testSub() throws Exception { - CloseableHttpClient httpclient = HttpClients.createDefault(); 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()); @@ -42,7 +42,6 @@ public void testSub() throws Exception { @Test public void testMul() throws Exception { - CloseableHttpClient httpclient = HttpClients.createDefault(); 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()); @@ -51,10 +50,16 @@ public void testMul() throws Exception { @Test public void testDiv() throws Exception { - CloseableHttpClient httpclient = HttpClients.createDefault(); HttpGet httpGet = new HttpGet("http://localhost:8080/calculator/api/calculator/div?x=6&y=3"); HttpResponse response = httpclient.execute(httpGet); assertEquals(200, response.getStatusLine().getStatusCode()); assertThat(EntityUtils.toString(response.getEntity()), containsString("2")); } + + @Test + public void testDivByZero() throws Exception { + HttpGet httpGet = new HttpGet("http://localhost:8080/calculator/api/calculator/div?x=6&y=0"); + HttpResponse response = httpclient.execute(httpGet); + assertEquals(500, response.getStatusLine().getStatusCode()); + } } From 807076b47fbd2fb0d0c480c9fb4a037ed973944a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Garc=C3=ADa=20Fern=C3=A1ndez?= Date: Fri, 21 Jun 2024 17:20:56 +0000 Subject: [PATCH 12/12] feat: cover API REST test cases --- .../calculator/rest/CalculatorAPITest.java | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/test/java/com/geekshubs/calculator/rest/CalculatorAPITest.java b/src/test/java/com/geekshubs/calculator/rest/CalculatorAPITest.java index e7c4a9c..a7d2e34 100644 --- a/src/test/java/com/geekshubs/calculator/rest/CalculatorAPITest.java +++ b/src/test/java/com/geekshubs/calculator/rest/CalculatorAPITest.java @@ -8,9 +8,30 @@ public class CalculatorAPITest { + CalculatorAPI calculatorAPI = new CalculatorAPI(); + @Test public void testPing() { assertThat(new CalculatorAPI().ping(), containsString("Welcome to Java Maven Calculator Web App!!!")); } + @Test + public void testAdd() { + assertEquals(34, calculatorAPI.Add(8, 26).getResult()); + } + + @Test + public void testSub() { + assertEquals(4, calculatorAPI.Sub(12, 8).getResult()); + } + + @Test + public void testMul() { + assertEquals(88, calculatorAPI.Mul(11, 8).getResult()); + } + + @Test + public void testDiv() { + assertEquals(1, calculatorAPI.Div(12, 12).getResult()); + } }