diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..7679f0d --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,39 @@ +# 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' + +# 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: + - main + pull_request: + +jobs: + continuous-testing: + runs-on: ubuntu-latest + # Steps represent a sequence of tasks that will be executed as part of the job + steps: + # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it + - uses: actions/checkout@v4 + # Runs a single command using the runners shell + - name: Set up JDK 1.17 + uses: actions/setup-java@v4 + with: + distribution: "liberica" + java-version: "17" + cache: "maven" + - name: Compile + run: mvn compile + - name: Test + run: mvn verify + - uses: dorny/test-reporter@v1 + with: + name: Test Results + path: "target/**/TEST*.xml" + reporter: java-junit diff --git a/.github/workflows/test-report.yml b/.github/workflows/test-report.yml new file mode 100644 index 0000000..5abf5eb --- /dev/null +++ b/.github/workflows/test-report.yml @@ -0,0 +1,18 @@ +name: 'Test Report' +on: + workflow_call: + workflow_dispatch: + workflow_run: + workflows: ['CI'] # runs after CI workflow + types: + - completed +jobs: + report: + runs-on: ubuntu-latest + steps: + - uses: dorny/test-reporter@v1 + with: + artifact: test-results # artifact name + name: Test Results # Name of the check run which will be created + path: '*.xml' # Path to test results (inside artifact .zip) + reporter: java-junit # Format of test results 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 diff --git a/README.md b/README.md index 1433fac..6ba5d2b 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,7 @@ Create calculator App * Substract: substract two numbers and return result * Multiply: multiply two numbers and return result * Divide: divide two numbers and return result + * divide by zero should return error message ## System requirements @@ -35,7 +36,23 @@ $ ./mvnw clean test ## 2. Automatically Build and Test -**TODO** +[Github action ci](.github/workflows/ci.yml) step definition: +```yaml +steps: + # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it + - uses: actions/checkout@v2 + # Runs a single command using the runners shell + - name: Set up JDK 1.11 + uses: actions/setup-java@v2.5.0 + with: + java-version: '11' + distribution: 'zulu' + cache: 'maven' + - name: Compile + run: mvn compile + - name: Test + run: mvn verify +``` ## 3. Containerize Your Web App diff --git a/pom.xml b/pom.xml index 4453f86..940c7c3 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ 4.0.0 com.geekshubs.javawebapp java-maven-calculator-web-app - war + jar 1.1-SNAPSHOT Calculator Web A Java Maven Calculator Web Application @@ -17,7 +17,6 @@ 3.9.0 3.2.0 2.22.2 - 0.8.7 @@ -71,25 +70,6 @@ - - org.jacoco - jacoco-maven-plugin - ${jacoco.maven.plugin.version} - - - - prepare-agent - - - - report - test - - report - - - - diff --git a/src/main/java/com/geekshubs/calculator/Calculator.java b/src/main/java/com/geekshubs/calculator/Calculator.java index 9f099f2..11e690c 100644 --- a/src/main/java/com/geekshubs/calculator/Calculator.java +++ b/src/main/java/com/geekshubs/calculator/Calculator.java @@ -2,4 +2,25 @@ public class Calculator { + int _x; + int _y; + int _result; + + public Calculator(int x, int y, int result) { + _x = x; + _y = y; + _result = result; + } + + public int getX() { + return _x; + } + + public int getY() { + return _y; + } + + public int getResult() { + return _result; + } } diff --git a/src/test/java/com/geekshubs/calculator/CalculatorTest.java b/src/test/java/com/geekshubs/calculator/CalculatorTest.java index 32f3b9f..93e8871 100644 --- a/src/test/java/com/geekshubs/calculator/CalculatorTest.java +++ b/src/test/java/com/geekshubs/calculator/CalculatorTest.java @@ -1,5 +1,34 @@ package com.geekshubs.calculator; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + public class CalculatorTest { + @Test + public void testSum() { + assertEquals(34, new Calculator(8, 26, 8 + 26).getResult()); + } + + @Test + public void testSub() { + assertEquals(4, new Calculator(12, 8, 12 - 8).getResult()); + } + + @Test + public void testMul() { + assertEquals(88, new Calculator(11, 8, 11 * 8).getResult()); + } + + @Test + public void testDiv() { + assertEquals(1, new Calculator(12, 12, 12 / 12).getResult()); + } + + @Test + public void testDivByZero() { + assertThrows(ArithmeticException.class, () -> new Calculator(12, 0, 12 / 0).getResult()); + } }