|
| 1 | +name: 'Java SDK - Unit Testing' |
| 2 | + |
| 3 | +# This workflow runs ONLY unit tests (excludes integration tests ending with IT.java) |
| 4 | +# Integration tests require network access and valid .env credentials |
| 5 | + |
| 6 | +on: |
| 7 | + pull_request: |
| 8 | + branches: |
| 9 | + - development |
| 10 | + - staging |
| 11 | + - main |
| 12 | + |
| 13 | +jobs: |
| 14 | + coverage: |
| 15 | + name: Unit Test Coverage Check |
| 16 | + runs-on: ubuntu-latest |
| 17 | + permissions: |
| 18 | + contents: read |
| 19 | + pull-requests: write |
| 20 | + checks: write |
| 21 | + |
| 22 | + steps: |
| 23 | + - name: Checkout code |
| 24 | + uses: actions/checkout@v4 |
| 25 | + |
| 26 | + - name: Set up JDK 8 |
| 27 | + uses: actions/setup-java@v4 |
| 28 | + with: |
| 29 | + java-version: '8' |
| 30 | + distribution: 'temurin' |
| 31 | + cache: 'maven' |
| 32 | + |
| 33 | + - name: Run unit tests with coverage (excluding integration tests) |
| 34 | + run: | |
| 35 | + echo "Running unit tests only (excluding *IT.java files)..." |
| 36 | + mvn clean test -Dtest='!*IT' jacoco:report -Dgpg.skip=true |
| 37 | + echo "Unit tests completed" |
| 38 | + continue-on-error: true |
| 39 | + |
| 40 | + - name: Generate JaCoCo Badge |
| 41 | + id: jacoco |
| 42 | + uses: cicirello/jacoco-badge-generator@v2 |
| 43 | + with: |
| 44 | + badges-directory: .github/badges |
| 45 | + generate-branches-badge: true |
| 46 | + generate-summary: true |
| 47 | + |
| 48 | + - name: Check coverage thresholds |
| 49 | + id: coverage-check |
| 50 | + run: | |
| 51 | + echo "Checking coverage thresholds (unit tests only)..." |
| 52 | + # Extract coverage percentages from JaCoCo XML report (using sed for cross-platform compatibility) |
| 53 | + INSTRUCTION_COVERAGE=$(sed -n 's/.*type="INSTRUCTION".*covered="\([0-9]*\)".*/\1/p' target/site/jacoco/jacoco.xml | tail -1) |
| 54 | + INSTRUCTION_MISSED=$(sed -n 's/.*type="INSTRUCTION".*missed="\([0-9]*\)".*/\1/p' target/site/jacoco/jacoco.xml | tail -1) |
| 55 | + BRANCH_COVERAGE=$(sed -n 's/.*type="BRANCH".*covered="\([0-9]*\)".*/\1/p' target/site/jacoco/jacoco.xml | tail -1) |
| 56 | + BRANCH_MISSED=$(sed -n 's/.*type="BRANCH".*missed="\([0-9]*\)".*/\1/p' target/site/jacoco/jacoco.xml | tail -1) |
| 57 | + |
| 58 | + # Calculate percentages |
| 59 | + INSTRUCTION_TOTAL=$((INSTRUCTION_COVERAGE + INSTRUCTION_MISSED)) |
| 60 | + BRANCH_TOTAL=$((BRANCH_COVERAGE + BRANCH_MISSED)) |
| 61 | + |
| 62 | + if [ $INSTRUCTION_TOTAL -gt 0 ]; then |
| 63 | + INSTRUCTION_PCT=$((INSTRUCTION_COVERAGE * 100 / INSTRUCTION_TOTAL)) |
| 64 | + else |
| 65 | + INSTRUCTION_PCT=0 |
| 66 | + fi |
| 67 | + |
| 68 | + if [ $BRANCH_TOTAL -gt 0 ]; then |
| 69 | + BRANCH_PCT=$((BRANCH_COVERAGE * 100 / BRANCH_TOTAL)) |
| 70 | + else |
| 71 | + BRANCH_PCT=0 |
| 72 | + fi |
| 73 | + |
| 74 | + echo "instruction_pct=$INSTRUCTION_PCT" >> $GITHUB_OUTPUT |
| 75 | + echo "branch_pct=$BRANCH_PCT" >> $GITHUB_OUTPUT |
| 76 | + |
| 77 | + # Check thresholds |
| 78 | + THRESHOLD_MET=true |
| 79 | + MESSAGES="" |
| 80 | + |
| 81 | + if [ $INSTRUCTION_PCT -lt 90 ]; then |
| 82 | + THRESHOLD_MET=false |
| 83 | + MESSAGES="${MESSAGES}❌ Overall instruction coverage is ${INSTRUCTION_PCT}% (threshold: 90%)\n" |
| 84 | + else |
| 85 | + MESSAGES="${MESSAGES}✅ Overall instruction coverage is ${INSTRUCTION_PCT}% (threshold: 90%)\n" |
| 86 | + fi |
| 87 | + |
| 88 | + if [ $BRANCH_PCT -lt 80 ]; then |
| 89 | + THRESHOLD_MET=false |
| 90 | + MESSAGES="${MESSAGES}❌ Branch coverage is ${BRANCH_PCT}% (threshold: 80%)\n" |
| 91 | + else |
| 92 | + MESSAGES="${MESSAGES}✅ Branch coverage is ${BRANCH_PCT}% (threshold: 80%)\n" |
| 93 | + fi |
| 94 | + |
| 95 | + echo "threshold_met=$THRESHOLD_MET" >> $GITHUB_OUTPUT |
| 96 | + echo -e "$MESSAGES" |
| 97 | + echo "messages<<EOF" >> $GITHUB_OUTPUT |
| 98 | + echo -e "$MESSAGES" >> $GITHUB_OUTPUT |
| 99 | + echo "EOF" >> $GITHUB_OUTPUT |
| 100 | + |
| 101 | + if [ "$THRESHOLD_MET" = "false" ]; then |
| 102 | + exit 1 |
| 103 | + fi |
| 104 | + |
| 105 | + - name: Add coverage comment to PR |
| 106 | + uses: madrapps/jacoco-report@v1.6.1 |
| 107 | + if: always() |
| 108 | + with: |
| 109 | + paths: | |
| 110 | + ${{ github.workspace }}/target/site/jacoco/jacoco.xml |
| 111 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 112 | + min-coverage-overall: 90 |
| 113 | + min-coverage-changed-files: 80 |
| 114 | + title: '📊 Unit Test Coverage Report' |
| 115 | + update-comment: true |
| 116 | + |
| 117 | + - name: Upload JaCoCo coverage report |
| 118 | + uses: actions/upload-artifact@v4 |
| 119 | + if: always() |
| 120 | + with: |
| 121 | + name: jacoco-report |
| 122 | + path: target/site/jacoco/ |
| 123 | + |
| 124 | + - name: Fail if coverage thresholds not met |
| 125 | + if: steps.coverage-check.outputs.threshold_met == 'false' |
| 126 | + run: | |
| 127 | + echo "Coverage thresholds not met:" |
| 128 | + echo "${{ steps.coverage-check.outputs.messages }}" |
| 129 | + exit 1 |
| 130 | +
|
0 commit comments