Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/master.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
52 changes: 52 additions & 0 deletions .github/workflows/sonar.yml
Original file line number Diff line number Diff line change
@@ -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 }}
17 changes: 15 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,12 @@
<gson.version>2.13.1</gson.version>
<springdoc.version>2.8.9</springdoc.version>

<!-- Patch -->
<commons-lang3.version>3.18.0</commons-lang3.version>

<!-- Test-->
<flapdoodle.embed.mongo.version>4.20.0</flapdoodle.embed.mongo.version>
<okhttp.version>4.12.0</okhttp.version>
<okhttp.version>5.1.0</okhttp.version>
<reactor-test.version>3.7.7</reactor-test.version>

<!-- Plugins -->
Expand Down Expand Up @@ -274,7 +277,7 @@

<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<artifactId>mockwebserver3-junit5</artifactId>
<version>${okhttp.version}</version>
<scope>test</scope>
</dependency>
Expand All @@ -294,6 +297,16 @@
</dependency>
</dependencies>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
</dependency>
</dependencies>
</dependencyManagement>

<build>
<pluginManagement>
<plugins>
Expand Down
86 changes: 86 additions & 0 deletions src/test/java/com/github/switcherapi/ac/AcMockWebServer.java
Original file line number Diff line number Diff line change
@@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
@AutoConfigureDataMongo
@AutoConfigureWebTestClient
@Execution(ExecutionMode.CONCURRENT)
class AdminAccountControllerTests {
class AdminAccountControllerTest {

@Autowired JwtTokenService jwtTokenService;
@Autowired PlanService planService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
@SpringBootTest
@AutoConfigureDataMongo
@AutoConfigureWebTestClient
class AdminAuthControllerTests {
class AdminAuthControllerTest {

@Autowired AdminRepository adminRepository;
@Autowired JwtTokenService jwtTokenService;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -24,35 +18,18 @@
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;

@SpringBootTest
@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());
Expand Down Expand Up @@ -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));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
@AutoConfigureDataMongo
@AutoConfigureWebTestClient
@Execution(ExecutionMode.CONCURRENT)
class AdminPlanControllerErrorTests {
class AdminPlanControllerErrorTest {

@Mock private PlanService mockPlanService;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
@SpringBootTest
@AutoConfigureWebTestClient
@Execution(ExecutionMode.CONCURRENT)
class ApiControllerTests {
class ApiControllerTest {

@Autowired WebTestClient webTestClient;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
@SpringBootTest
@AutoConfigureWebTestClient
@Execution(ExecutionMode.CONCURRENT)
class ApiResourcesTests {
class ApiResourcesTest {

@Autowired JwtTokenService jwtService;
@Autowired WebTestClient webTestClient;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
@AutoConfigureDataMongo
@AutoConfigureWebTestClient
@Execution(ExecutionMode.CONCURRENT)
class PlanControllerTests extends ControllerTestUtils {
class PlanControllerTest extends ControllerTestUtils {

@Autowired JwtTokenService jwtTokenService;
@Autowired PlanService planService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureDataMongo
class SwitcherRelayControllerErrorTests {
class SwitcherRelayControllerErrorTest {

@Mock private AccountService mockAccountService;
@Mock private ValidatorBuilderService mockValidatorBuilderService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
@AutoConfigureDataMongo
@AutoConfigureWebTestClient
@Execution(ExecutionMode.CONCURRENT)
class SwitcherRelayControllerTests extends ControllerTestUtils {
class SwitcherRelayControllerTest extends ControllerTestUtils {

@Autowired AccountService accountService;
@Autowired PlanService planService;
Expand Down
Loading