From 34f762bde8315cc1674270e36db2d06f1fac9108 Mon Sep 17 00:00:00 2001 From: petruki <31597636+petruki@users.noreply.github.com> Date: Tue, 15 Jul 2025 21:52:05 -0700 Subject: [PATCH] chore: bump okhttp@5.1.0, updated tests based on MockWebServer --- .github/workflows/master.yml | 6 +- .github/workflows/sonar.yml | 52 +++++++++++ pom.xml | 17 +++- .../switcherapi/ac/AcMockWebServer.java | 86 +++++++++++++++++++ ...s.java => AdminAccountControllerTest.java} | 2 +- ...ests.java => AdminAuthControllerTest.java} | 2 +- ...ava => AdminGitHubAuthControllerTest.java} | 57 +----------- ...java => AdminPlanControllerErrorTest.java} | 2 +- ...ollerTests.java => ApiControllerTest.java} | 2 +- ...ourcesTests.java => ApiResourcesTest.java} | 2 +- ...llerTests.java => PlanControllerTest.java} | 2 +- ...tcherRelayCacheLimiterControllerTest.java} | 2 +- ... => SwitcherRelayControllerErrorTest.java} | 2 +- ....java => SwitcherRelayControllerTest.java} | 2 +- .../ac/service/GitHubServiceTest.java | 52 +---------- 15 files changed, 169 insertions(+), 119 deletions(-) create mode 100644 .github/workflows/sonar.yml create mode 100644 src/test/java/com/github/switcherapi/ac/AcMockWebServer.java rename src/test/java/com/github/switcherapi/ac/controller/{AdminAccountControllerTests.java => AdminAccountControllerTest.java} (99%) rename src/test/java/com/github/switcherapi/ac/controller/{AdminAuthControllerTests.java => AdminAuthControllerTest.java} (99%) rename src/test/java/com/github/switcherapi/ac/controller/{AdminGitHubAuthControllerTests.java => AdminGitHubAuthControllerTest.java} (70%) rename src/test/java/com/github/switcherapi/ac/controller/{AdminPlanControllerErrorTests.java => AdminPlanControllerErrorTest.java} (98%) rename src/test/java/com/github/switcherapi/ac/controller/{ApiControllerTests.java => ApiControllerTest.java} (97%) rename src/test/java/com/github/switcherapi/ac/controller/{ApiResourcesTests.java => ApiResourcesTest.java} (99%) rename src/test/java/com/github/switcherapi/ac/controller/{PlanControllerTests.java => PlanControllerTest.java} (99%) rename src/test/java/com/github/switcherapi/ac/controller/{SwitcherRelayCacheLimiterControllerTests.java => SwitcherRelayCacheLimiterControllerTest.java} (97%) rename src/test/java/com/github/switcherapi/ac/controller/{SwitcherRelayControllerErrorTests.java => SwitcherRelayControllerErrorTest.java} (98%) rename src/test/java/com/github/switcherapi/ac/controller/{SwitcherRelayControllerTests.java => SwitcherRelayControllerTest.java} (99%) diff --git a/.github/workflows/master.yml b/.github/workflows/master.yml index 8d3aaf4..8f1c00f 100644 --- a/.github/workflows/master.yml +++ b/.github/workflows/master.yml @@ -10,7 +10,6 @@ jobs: build-test: name: Build & Test runs-on: ubuntu-latest - if: "! contains(toJSON(github.event.commits.*.message), '[skip ci]')" steps: - name: Git checkout @@ -26,9 +25,10 @@ jobs: cache: maven - name: Build/Test & SonarCloud Scan - run: mvn -B clean verify -Pcoverage,sonar -Dsonar.token=${{ secrets.SONAR_TOKEN }} env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} + if: env.SONAR_TOKEN != '' + run: mvn -B clean verify -Pcoverage,sonar -Dsonar.token=${{ secrets.SONAR_TOKEN }} docker: name: Publish Docker Image diff --git a/.github/workflows/sonar.yml b/.github/workflows/sonar.yml new file mode 100644 index 0000000..b4e9bf1 --- /dev/null +++ b/.github/workflows/sonar.yml @@ -0,0 +1,52 @@ +name: Manual SonarCloud Analysis + +on: + workflow_dispatch: + inputs: + pr_id: + description: 'Pull Request ID to analyze' + required: true + type: string + +jobs: + sonar-analysis: + name: SonarCloud Analysis for PR + runs-on: ubuntu-latest + + steps: + - name: Get PR details + id: pr + uses: actions/github-script@v7 + with: + script: | + const pr = await github.rest.pulls.get({ + owner: context.repo.owner, + repo: context.repo.repo, + pull_number: ${{ inputs.pr_id }} + }); + core.setOutput('head_ref', pr.data.head.ref); + core.setOutput('base_ref', pr.data.base.ref); + core.setOutput('head_sha', pr.data.head.sha); + + - uses: actions/checkout@v4 + with: + ref: ${{ steps.pr.outputs.head_sha }} + fetch-depth: 0 + + - name: Set up JDK 21 + uses: actions/setup-java@v4 + with: + java-version: 21 + distribution: 'temurin' + cache: maven + + - name: Build/Test & SonarCloud Scan + env: + SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} + if: env.SONAR_TOKEN != '' + run: | + mvn -B clean verify -Pcoverage,sonar \ + -Dsonar.token=${{ secrets.SONAR_TOKEN }} \ + -Dsonar.pullrequest.key=${{ inputs.pr_id }} \ + -Dsonar.pullrequest.branch=${{ steps.pr.outputs.head_ref }} \ + -Dsonar.pullrequest.base=${{ steps.pr.outputs.base_ref }} \ No newline at end of file diff --git a/pom.xml b/pom.xml index 1111d58..2cc189f 100644 --- a/pom.xml +++ b/pom.xml @@ -64,9 +64,12 @@ 2.13.1 2.8.9 + + 3.18.0 + 4.20.0 - 4.12.0 + 5.1.0 3.7.7 @@ -274,7 +277,7 @@ com.squareup.okhttp3 - okhttp + mockwebserver3-junit5 ${okhttp.version} test @@ -294,6 +297,16 @@ + + + + org.apache.commons + commons-lang3 + ${commons-lang3.version} + + + + diff --git a/src/test/java/com/github/switcherapi/ac/AcMockWebServer.java b/src/test/java/com/github/switcherapi/ac/AcMockWebServer.java new file mode 100644 index 0000000..3a4448b --- /dev/null +++ b/src/test/java/com/github/switcherapi/ac/AcMockWebServer.java @@ -0,0 +1,86 @@ +package com.github.switcherapi.ac; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.github.switcherapi.ac.model.GitHubDetail; +import lombok.extern.slf4j.Slf4j; +import mockwebserver3.MockResponse; +import mockwebserver3.MockWebServer; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; + +import java.io.IOException; +import java.net.HttpURLConnection; + +@Slf4j +public class AcMockWebServer { + + private final ObjectMapper mapper = new ObjectMapper(); + + protected static MockWebServer mockBackend; + + @BeforeAll + static void setup() throws IOException { + mockBackend = new MockWebServer(); + mockBackend.start(); + } + + @AfterAll + static void tearDown() { + mockBackend.close(); + } + + protected void givenResponse401() { + mockBackend.enqueue(new MockResponse.Builder() + .code(HttpURLConnection.HTTP_UNAUTHORIZED) + .build()); + } + + protected void givenResponse503() { + mockBackend.enqueue(new MockResponse.Builder() + .code(HttpURLConnection.HTTP_UNAVAILABLE) + .build()); + } + + protected void givenResponseInvalidCode() { + mockBackend.enqueue(new MockResponse.Builder() + .body("{ \"error\": \"Invalid code\" }") + .build()); + } + + protected void givenResponseSuccess() throws JsonProcessingException { + final var githubAccountDetail = new GitHubDetail("123", "UserName", "login", "http://avatar.com"); + + mockBackend.enqueue(new MockResponse.Builder() + .code(HttpURLConnection.HTTP_OK) + .body(mapper.writeValueAsString(githubAccountDetail)) + .build()); + } + + protected void givenGitHubToken() { + mockBackend.enqueue(new MockResponse.Builder() + .code(HttpURLConnection.HTTP_OK) + .body("{\"access_token\":\"123\",\"token_type\":\"bearer\",\"scope\":\"\"}") + .build()); + } + + protected void givenGitHubDetails() { + var githubAccountDetail = new GitHubDetail( + "123", "UserName", "login", "http://avatar.com"); + + try { + mockBackend.enqueue(new MockResponse.Builder() + .code(HttpURLConnection.HTTP_OK) + .body(mapper.writeValueAsString(githubAccountDetail)) + .build()); + } catch (JsonProcessingException e) { + log.error("Error on parsing GitHubDetail", e); + } + } + + protected void givenGitHubTokenInvalid() { + mockBackend.enqueue(new MockResponse.Builder() + .code(HttpURLConnection.HTTP_BAD_REQUEST) + .build()); + } +} diff --git a/src/test/java/com/github/switcherapi/ac/controller/AdminAccountControllerTests.java b/src/test/java/com/github/switcherapi/ac/controller/AdminAccountControllerTest.java similarity index 99% rename from src/test/java/com/github/switcherapi/ac/controller/AdminAccountControllerTests.java rename to src/test/java/com/github/switcherapi/ac/controller/AdminAccountControllerTest.java index 99e7c83..4b68f27 100644 --- a/src/test/java/com/github/switcherapi/ac/controller/AdminAccountControllerTests.java +++ b/src/test/java/com/github/switcherapi/ac/controller/AdminAccountControllerTest.java @@ -37,7 +37,7 @@ @AutoConfigureDataMongo @AutoConfigureWebTestClient @Execution(ExecutionMode.CONCURRENT) -class AdminAccountControllerTests { +class AdminAccountControllerTest { @Autowired JwtTokenService jwtTokenService; @Autowired PlanService planService; diff --git a/src/test/java/com/github/switcherapi/ac/controller/AdminAuthControllerTests.java b/src/test/java/com/github/switcherapi/ac/controller/AdminAuthControllerTest.java similarity index 99% rename from src/test/java/com/github/switcherapi/ac/controller/AdminAuthControllerTests.java rename to src/test/java/com/github/switcherapi/ac/controller/AdminAuthControllerTest.java index 47fa7a1..b18509e 100644 --- a/src/test/java/com/github/switcherapi/ac/controller/AdminAuthControllerTests.java +++ b/src/test/java/com/github/switcherapi/ac/controller/AdminAuthControllerTest.java @@ -33,7 +33,7 @@ @SpringBootTest @AutoConfigureDataMongo @AutoConfigureWebTestClient -class AdminAuthControllerTests { +class AdminAuthControllerTest { @Autowired AdminRepository adminRepository; @Autowired JwtTokenService jwtTokenService; diff --git a/src/test/java/com/github/switcherapi/ac/controller/AdminGitHubAuthControllerTests.java b/src/test/java/com/github/switcherapi/ac/controller/AdminGitHubAuthControllerTest.java similarity index 70% rename from src/test/java/com/github/switcherapi/ac/controller/AdminGitHubAuthControllerTests.java rename to src/test/java/com/github/switcherapi/ac/controller/AdminGitHubAuthControllerTest.java index cb2fe88..da4d209 100644 --- a/src/test/java/com/github/switcherapi/ac/controller/AdminGitHubAuthControllerTests.java +++ b/src/test/java/com/github/switcherapi/ac/controller/AdminGitHubAuthControllerTest.java @@ -1,16 +1,10 @@ package com.github.switcherapi.ac.controller; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; +import com.github.switcherapi.ac.AcMockWebServer; import com.github.switcherapi.ac.config.SwitcherFeatures; -import com.github.switcherapi.ac.model.GitHubDetail; import com.github.switcherapi.ac.model.dto.GitHubAuthDTO; import com.github.switcherapi.ac.service.facades.GitHubFacade; import com.github.switcherapi.client.test.SwitcherTest; -import okhttp3.mockwebserver.MockResponse; -import okhttp3.mockwebserver.MockWebServer; -import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.parallel.Execution; @@ -24,9 +18,6 @@ import org.springframework.http.MediaType; import org.springframework.test.web.reactive.server.WebTestClient; -import java.io.IOException; -import java.net.HttpURLConnection; - import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; @@ -34,25 +25,11 @@ @AutoConfigureDataMongo @AutoConfigureWebTestClient @Execution(ExecutionMode.CONCURRENT) -class AdminGitHubAuthControllerTests { +class AdminGitHubAuthControllerTest extends AcMockWebServer { @Autowired WebTestClient webTestClient; @Autowired ApplicationContext applicationContext; - public static MockWebServer mockBackend; - private final ObjectMapper mapper = new ObjectMapper(); - - @BeforeAll - static void setup() throws IOException { - mockBackend = new MockWebServer(); - mockBackend.start(); - } - - @AfterAll - static void tearDown() throws IOException { - mockBackend.shutdown(); - } - @BeforeEach void initialize() { var baseUrl = String.format("http://localhost:%s", mockBackend.getPort()); @@ -163,34 +140,4 @@ void shouldNotLoginWithGitHub_invalidUrl() { .expectStatus().isUnauthorized(); } - private void givenGitHubToken() { - mockBackend.enqueue(new MockResponse() - .setBody("{\"access_token\":\"123\",\"token_type\":\"bearer\",\"scope\":\"\"}") - .addHeader("Content-Type", MediaType.APPLICATION_JSON)); - } - - private void givenResponse401() { - mockBackend.enqueue(new MockResponse().setResponseCode( - HttpURLConnection.HTTP_UNAUTHORIZED)); - } - - private void givenResponse503() { - mockBackend.enqueue(new MockResponse().setResponseCode( - HttpURLConnection.HTTP_UNAVAILABLE)); - } - - private void givenResponseInvalidCode() { - mockBackend.enqueue(new MockResponse() - .setBody("{ \"error\": \"Invalid code\" }") - .addHeader("Content-Type", MediaType.APPLICATION_JSON)); - } - - private void givenResponseSuccess() throws JsonProcessingException { - final var githubAccountDetail = new GitHubDetail("123", "UserName", "login", "http://avatar.com"); - - mockBackend.enqueue(new MockResponse() - .setBody(mapper.writeValueAsString(githubAccountDetail)) - .addHeader("Content-Type", MediaType.APPLICATION_JSON)); - } - } diff --git a/src/test/java/com/github/switcherapi/ac/controller/AdminPlanControllerErrorTests.java b/src/test/java/com/github/switcherapi/ac/controller/AdminPlanControllerErrorTest.java similarity index 98% rename from src/test/java/com/github/switcherapi/ac/controller/AdminPlanControllerErrorTests.java rename to src/test/java/com/github/switcherapi/ac/controller/AdminPlanControllerErrorTest.java index 6796d73..e98c0d2 100644 --- a/src/test/java/com/github/switcherapi/ac/controller/AdminPlanControllerErrorTests.java +++ b/src/test/java/com/github/switcherapi/ac/controller/AdminPlanControllerErrorTest.java @@ -23,7 +23,7 @@ @AutoConfigureDataMongo @AutoConfigureWebTestClient @Execution(ExecutionMode.CONCURRENT) -class AdminPlanControllerErrorTests { +class AdminPlanControllerErrorTest { @Mock private PlanService mockPlanService; diff --git a/src/test/java/com/github/switcherapi/ac/controller/ApiControllerTests.java b/src/test/java/com/github/switcherapi/ac/controller/ApiControllerTest.java similarity index 97% rename from src/test/java/com/github/switcherapi/ac/controller/ApiControllerTests.java rename to src/test/java/com/github/switcherapi/ac/controller/ApiControllerTest.java index c7c87f5..c84ab43 100644 --- a/src/test/java/com/github/switcherapi/ac/controller/ApiControllerTests.java +++ b/src/test/java/com/github/switcherapi/ac/controller/ApiControllerTest.java @@ -13,7 +13,7 @@ @SpringBootTest @AutoConfigureWebTestClient @Execution(ExecutionMode.CONCURRENT) -class ApiControllerTests { +class ApiControllerTest { @Autowired WebTestClient webTestClient; diff --git a/src/test/java/com/github/switcherapi/ac/controller/ApiResourcesTests.java b/src/test/java/com/github/switcherapi/ac/controller/ApiResourcesTest.java similarity index 99% rename from src/test/java/com/github/switcherapi/ac/controller/ApiResourcesTests.java rename to src/test/java/com/github/switcherapi/ac/controller/ApiResourcesTest.java index 409318e..a02aba5 100644 --- a/src/test/java/com/github/switcherapi/ac/controller/ApiResourcesTests.java +++ b/src/test/java/com/github/switcherapi/ac/controller/ApiResourcesTest.java @@ -26,7 +26,7 @@ @SpringBootTest @AutoConfigureWebTestClient @Execution(ExecutionMode.CONCURRENT) -class ApiResourcesTests { +class ApiResourcesTest { @Autowired JwtTokenService jwtService; @Autowired WebTestClient webTestClient; diff --git a/src/test/java/com/github/switcherapi/ac/controller/PlanControllerTests.java b/src/test/java/com/github/switcherapi/ac/controller/PlanControllerTest.java similarity index 99% rename from src/test/java/com/github/switcherapi/ac/controller/PlanControllerTests.java rename to src/test/java/com/github/switcherapi/ac/controller/PlanControllerTest.java index 443e598..08f0271 100644 --- a/src/test/java/com/github/switcherapi/ac/controller/PlanControllerTests.java +++ b/src/test/java/com/github/switcherapi/ac/controller/PlanControllerTest.java @@ -36,7 +36,7 @@ @AutoConfigureDataMongo @AutoConfigureWebTestClient @Execution(ExecutionMode.CONCURRENT) -class PlanControllerTests extends ControllerTestUtils { +class PlanControllerTest extends ControllerTestUtils { @Autowired JwtTokenService jwtTokenService; @Autowired PlanService planService; diff --git a/src/test/java/com/github/switcherapi/ac/controller/SwitcherRelayCacheLimiterControllerTests.java b/src/test/java/com/github/switcherapi/ac/controller/SwitcherRelayCacheLimiterControllerTest.java similarity index 97% rename from src/test/java/com/github/switcherapi/ac/controller/SwitcherRelayCacheLimiterControllerTests.java rename to src/test/java/com/github/switcherapi/ac/controller/SwitcherRelayCacheLimiterControllerTest.java index b1be1dc..f127f49 100644 --- a/src/test/java/com/github/switcherapi/ac/controller/SwitcherRelayCacheLimiterControllerTests.java +++ b/src/test/java/com/github/switcherapi/ac/controller/SwitcherRelayCacheLimiterControllerTest.java @@ -30,7 +30,7 @@ "service.cache.enabled=true", "service.cache.duration=1" }) -class SwitcherRelayCacheLimiterControllerTests extends ControllerTestUtils { +class SwitcherRelayCacheLimiterControllerTest extends ControllerTestUtils { private static final String TEST_PLAN = "TEST"; diff --git a/src/test/java/com/github/switcherapi/ac/controller/SwitcherRelayControllerErrorTests.java b/src/test/java/com/github/switcherapi/ac/controller/SwitcherRelayControllerErrorTest.java similarity index 98% rename from src/test/java/com/github/switcherapi/ac/controller/SwitcherRelayControllerErrorTests.java rename to src/test/java/com/github/switcherapi/ac/controller/SwitcherRelayControllerErrorTest.java index d0367b9..1c07c03 100644 --- a/src/test/java/com/github/switcherapi/ac/controller/SwitcherRelayControllerErrorTests.java +++ b/src/test/java/com/github/switcherapi/ac/controller/SwitcherRelayControllerErrorTest.java @@ -21,7 +21,7 @@ @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @AutoConfigureDataMongo -class SwitcherRelayControllerErrorTests { +class SwitcherRelayControllerErrorTest { @Mock private AccountService mockAccountService; @Mock private ValidatorBuilderService mockValidatorBuilderService; diff --git a/src/test/java/com/github/switcherapi/ac/controller/SwitcherRelayControllerTests.java b/src/test/java/com/github/switcherapi/ac/controller/SwitcherRelayControllerTest.java similarity index 99% rename from src/test/java/com/github/switcherapi/ac/controller/SwitcherRelayControllerTests.java rename to src/test/java/com/github/switcherapi/ac/controller/SwitcherRelayControllerTest.java index ba15d94..9be0f46 100644 --- a/src/test/java/com/github/switcherapi/ac/controller/SwitcherRelayControllerTests.java +++ b/src/test/java/com/github/switcherapi/ac/controller/SwitcherRelayControllerTest.java @@ -29,7 +29,7 @@ @AutoConfigureDataMongo @AutoConfigureWebTestClient @Execution(ExecutionMode.CONCURRENT) -class SwitcherRelayControllerTests extends ControllerTestUtils { +class SwitcherRelayControllerTest extends ControllerTestUtils { @Autowired AccountService accountService; @Autowired PlanService planService; diff --git a/src/test/java/com/github/switcherapi/ac/service/GitHubServiceTest.java b/src/test/java/com/github/switcherapi/ac/service/GitHubServiceTest.java index 93e3478..2e3a682 100644 --- a/src/test/java/com/github/switcherapi/ac/service/GitHubServiceTest.java +++ b/src/test/java/com/github/switcherapi/ac/service/GitHubServiceTest.java @@ -1,46 +1,20 @@ package com.github.switcherapi.ac.service; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.github.switcherapi.ac.model.GitHubDetail; +import com.github.switcherapi.ac.AcMockWebServer; import com.github.switcherapi.ac.service.facades.GitHubFacade; -import lombok.extern.slf4j.Slf4j; -import okhttp3.mockwebserver.MockResponse; -import okhttp3.mockwebserver.MockWebServer; -import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.parallel.Execution; import org.junit.jupiter.api.parallel.ExecutionMode; -import org.springframework.http.MediaType; import org.springframework.web.server.ResponseStatusException; -import java.io.IOException; - import static org.junit.jupiter.api.Assertions.*; -@Slf4j @Execution(ExecutionMode.CONCURRENT) -public class GitHubServiceTest { - - private final ObjectMapper mapper = new ObjectMapper(); - - public static MockWebServer mockBackend; +class GitHubServiceTest extends AcMockWebServer { private GitHubService gitHubService; - @BeforeAll - static void setup() throws IOException { - mockBackend = new MockWebServer(); - mockBackend.start(); - } - - @AfterAll - static void tearDown() throws IOException { - mockBackend.shutdown(); - } - @BeforeEach void initialize() { var baseUrl = String.format("http://localhost:%s", mockBackend.getPort()); @@ -103,26 +77,4 @@ void shouldNotGetGitHubDetail_whenURIIsInvalid() { assertEquals("401 UNAUTHORIZED \"Invalid GitHub account\"", ex.getMessage()); } - private void givenGitHubToken() { - mockBackend.enqueue(new MockResponse() - .setBody("{\"access_token\":\"123\",\"token_type\":\"bearer\",\"scope\":\"\"}") - .addHeader("Content-Type", MediaType.APPLICATION_JSON)); - } - - private void givenGitHubDetails() { - final var githubAccountDetail = new GitHubDetail("123", "UserName", "login", "http://avatar.com"); - - try { - mockBackend.enqueue(new MockResponse() - .setBody(mapper.writeValueAsString(githubAccountDetail)) - .addHeader("Content-Type", MediaType.APPLICATION_JSON)); - } catch (JsonProcessingException e) { - log.error("Error on parsing GitHubDetail", e); - } - } - - private void givenGitHubTokenInvalid() { - mockBackend.enqueue(new MockResponse().setResponseCode(400)); - } - }