diff --git a/pom.xml b/pom.xml
index 626b9ba..93738e3 100644
--- a/pom.xml
+++ b/pom.xml
@@ -58,7 +58,6 @@
${java.version}
${java.version}
- 4.0.0-M1
2.2.2
0.12.6
2.13.0
@@ -210,38 +209,6 @@
runtime
-
-
- org.glassfish.jersey.core
- jersey-client
-
-
-
- org.glassfish.jersey.core
- jersey-common
-
-
-
- org.glassfish.jersey.ext
- jersey-entity-filtering
-
-
-
- org.glassfish.jersey.inject
- jersey-hk2
-
-
- jersey-common
- org.glassfish.jersey.core
-
-
-
-
-
- org.glassfish.jersey.media
- jersey-media-json-jackson
-
-
org.projectlombok
diff --git a/src/main/java/com/github/switcherapi/ac/config/JwtRequestFilter.java b/src/main/java/com/github/switcherapi/ac/config/JwtRequestFilter.java
index 21961fe..f7ef5e5 100644
--- a/src/main/java/com/github/switcherapi/ac/config/JwtRequestFilter.java
+++ b/src/main/java/com/github/switcherapi/ac/config/JwtRequestFilter.java
@@ -6,9 +6,9 @@
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
-import jakarta.ws.rs.core.HttpHeaders;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
+import org.springframework.http.HttpHeaders;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
diff --git a/src/main/java/com/github/switcherapi/ac/config/SwitcherFeatures.java b/src/main/java/com/github/switcherapi/ac/config/SwitcherFeatures.java
index a9b18e6..8847c35 100644
--- a/src/main/java/com/github/switcherapi/ac/config/SwitcherFeatures.java
+++ b/src/main/java/com/github/switcherapi/ac/config/SwitcherFeatures.java
@@ -3,7 +3,6 @@
import com.github.switcherapi.ac.util.FileUtil;
import com.github.switcherapi.client.SnapshotCallback;
import com.github.switcherapi.client.SwitcherContextBase;
-import com.github.switcherapi.client.SwitcherKey;
import jakarta.annotation.PostConstruct;
import lombok.Getter;
import lombok.Setter;
@@ -16,7 +15,6 @@
@ConfigurationProperties(prefix = "switcher")
public class SwitcherFeatures extends SwitcherContextBase implements SnapshotCallback {
- @SwitcherKey
public static final String SWITCHER_AC_ADM = "SWITCHER_AC_ADM";
private String relayCode;
@@ -25,6 +23,7 @@ public class SwitcherFeatures extends SwitcherContextBase implements SnapshotCal
@Override
protected void configureClient() {
super.truststore.setPath(FileUtil.getFilePathFromResource(truststore.getPath()));
+ super.registerSwitcherKeys(SWITCHER_AC_ADM);
super.configureClient();
scheduleSnapshotAutoUpdate(snapshot.getUpdateInterval(), this);
diff --git a/src/main/java/com/github/switcherapi/ac/controller/AdminController.java b/src/main/java/com/github/switcherapi/ac/controller/AdminController.java
index 81105c4..f305fe5 100644
--- a/src/main/java/com/github/switcherapi/ac/controller/AdminController.java
+++ b/src/main/java/com/github/switcherapi/ac/controller/AdminController.java
@@ -7,7 +7,7 @@
import com.github.switcherapi.ac.service.AdminService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirements;
-import jakarta.ws.rs.core.HttpHeaders;
+import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
diff --git a/src/main/java/com/github/switcherapi/ac/model/GitHubDetail.java b/src/main/java/com/github/switcherapi/ac/model/GitHubDetail.java
index 0fc7775..bd13bc9 100644
--- a/src/main/java/com/github/switcherapi/ac/model/GitHubDetail.java
+++ b/src/main/java/com/github/switcherapi/ac/model/GitHubDetail.java
@@ -2,6 +2,7 @@
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
+import com.google.gson.annotations.SerializedName;
@JsonIgnoreProperties(ignoreUnknown = true)
public record GitHubDetail(
@@ -9,6 +10,7 @@ public record GitHubDetail(
String name,
String login,
@JsonProperty("avatar_url")
+ @SerializedName("avatar_url")
String avatarUrl
) {
}
diff --git a/src/main/java/com/github/switcherapi/ac/service/facades/GitHubFacade.java b/src/main/java/com/github/switcherapi/ac/service/facades/GitHubFacade.java
index 67fdba4..51fdc5a 100644
--- a/src/main/java/com/github/switcherapi/ac/service/facades/GitHubFacade.java
+++ b/src/main/java/com/github/switcherapi/ac/service/facades/GitHubFacade.java
@@ -1,16 +1,19 @@
package com.github.switcherapi.ac.service.facades;
import com.github.switcherapi.ac.model.GitHubDetail;
-import jakarta.ws.rs.client.Client;
-import jakarta.ws.rs.client.ClientBuilder;
-import jakarta.ws.rs.client.WebTarget;
-import jakarta.ws.rs.core.HttpHeaders;
-import jakarta.ws.rs.core.MediaType;
+import com.github.switcherapi.client.exception.SwitcherRemoteException;
+import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
+import java.net.URI;
+import java.net.http.HttpClient;
+import java.net.http.HttpRequest;
+import java.net.http.HttpResponse;
import java.util.List;
import java.util.Map;
@@ -23,8 +26,8 @@ public class GitHubFacade {
public static final String INVALID_ACCOUNT = "Invalid GitHub account";
private static final String ACCESS_TOKEN = "access_token";
-
- private static final String HEADER_ACCEPT = "accept";
+
+ private final Gson gson = new Gson();
private final String clientId;
@@ -34,7 +37,7 @@ public class GitHubFacade {
private final String gitUrlDetail;
- private final Client client;
+ private final HttpClient httpClient;
public GitHubFacade(
@Value("${service.github.clientid}") String clientId,
@@ -45,48 +48,60 @@ public GitHubFacade(
this.oauthSecret = oauthSecret;
this.gitUrlAccess = gitUrlAccess;
this.gitUrlDetail = gitUrlDetail;
- this.client = ClientBuilder.newClient();
+ this.httpClient = HttpClient.newHttpClient();
}
public String getToken(String code) {
var codeSanitized = sanitize(code, List.of(trim(), alphaNumeric()));
- final WebTarget myResource = client.target(
- String.format(gitUrlAccess, clientId, oauthSecret, codeSanitized));
+ try {
+ final URI uri = new URI(String.format(gitUrlAccess, clientId, oauthSecret, codeSanitized));
- try (var response = myResource
- .request(MediaType.APPLICATION_JSON)
- .header(HEADER_ACCEPT, MediaType.APPLICATION_JSON)
- .post(null)) {
+ final HttpResponse response = httpClient.send(HttpRequest.newBuilder()
+ .uri(uri)
+ .header(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON.toString())
+ .POST(HttpRequest.BodyPublishers.noBody())
+ .build(), HttpResponse.BodyHandlers.ofString());
- if (response.getStatus() == 200) {
- final var responseEntity = response.readEntity(Map.class);
- if (responseEntity.containsKey(ACCESS_TOKEN)) {
- return responseEntity.get(ACCESS_TOKEN).toString();
- }
+ if (response.statusCode() != 200 || !response.body().contains(ACCESS_TOKEN)) {
+ log.error("Failed to get token from GitHub");
+ return StringUtils.EMPTY;
}
- log.error("Failed to get token from GitHub");
- return StringUtils.EMPTY;
+ final var responseEntity = gson.fromJson(response.body(), Map.class);
+ return responseEntity.get(ACCESS_TOKEN).toString();
+ } catch (Exception e) {
+ return exceptionHandler(e, gitUrlAccess);
}
}
public GitHubDetail getGitHubDetail(String token) {
- final WebTarget myResource = client.target(gitUrlDetail);
-
- try (var response = myResource
- .request(MediaType.APPLICATION_JSON)
- .header(HEADER_ACCEPT, MediaType.APPLICATION_JSON)
- .header(HttpHeaders.AUTHORIZATION, String.format("token %s", token))
- .get()) {
-
- if (response.getStatus() == 200) {
- return response.readEntity(GitHubDetail.class);
+ try {
+ final URI uri = new URI(gitUrlDetail);
+
+ final HttpResponse response = httpClient.send(HttpRequest.newBuilder()
+ .uri(uri)
+ .headers(
+ HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON.toString(),
+ HttpHeaders.AUTHORIZATION, String.format("token %s", token))
+ .GET().build(), HttpResponse.BodyHandlers.ofString());
+
+ if (response.statusCode() != 200) {
+ log.error("Failed to get GitHub detail");
+ return null;
}
- log.error("Failed to get GitHub detail");
- return null;
+ return gson.fromJson(response.body(), GitHubDetail.class);
+ } catch (Exception e) {
+ return exceptionHandler(e, gitUrlDetail);
+ }
+ }
+
+ private T exceptionHandler(Exception e, String url) {
+ if (e instanceof InterruptedException) {
+ Thread.currentThread().interrupt();
}
+ throw new SwitcherRemoteException(url, e);
}
}
diff --git a/src/test/java/com/github/switcherapi/ac/controller/AdminAccountControllerTests.java b/src/test/java/com/github/switcherapi/ac/controller/AdminAccountControllerTests.java
index e962e9f..74a0c8e 100644
--- a/src/test/java/com/github/switcherapi/ac/controller/AdminAccountControllerTests.java
+++ b/src/test/java/com/github/switcherapi/ac/controller/AdminAccountControllerTests.java
@@ -9,7 +9,6 @@
import com.github.switcherapi.ac.service.AdminService;
import com.github.switcherapi.ac.service.JwtTokenService;
import com.github.switcherapi.ac.service.PlanService;
-import jakarta.ws.rs.core.HttpHeaders;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@@ -17,6 +16,7 @@
import org.springframework.boot.test.autoconfigure.data.mongo.AutoConfigureDataMongo;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
diff --git a/src/test/java/com/github/switcherapi/ac/controller/AdminAuthControllerTests.java b/src/test/java/com/github/switcherapi/ac/controller/AdminAuthControllerTests.java
index 64b2769..50a3fc0 100644
--- a/src/test/java/com/github/switcherapi/ac/controller/AdminAuthControllerTests.java
+++ b/src/test/java/com/github/switcherapi/ac/controller/AdminAuthControllerTests.java
@@ -6,7 +6,6 @@
import com.github.switcherapi.ac.service.AdminService;
import com.github.switcherapi.ac.service.JwtTokenService;
import com.github.switcherapi.client.test.SwitcherTest;
-import jakarta.ws.rs.core.HttpHeaders;
import org.apache.commons.lang3.tuple.Pair;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@@ -14,6 +13,7 @@
import org.springframework.boot.test.autoconfigure.data.mongo.AutoConfigureDataMongo;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
diff --git a/src/test/java/com/github/switcherapi/ac/controller/AdminGitHubAuthControllerTests.java b/src/test/java/com/github/switcherapi/ac/controller/AdminGitHubAuthControllerTests.java
index 5dc5cc9..57e8054 100644
--- a/src/test/java/com/github/switcherapi/ac/controller/AdminGitHubAuthControllerTests.java
+++ b/src/test/java/com/github/switcherapi/ac/controller/AdminGitHubAuthControllerTests.java
@@ -7,7 +7,6 @@
import com.github.switcherapi.ac.model.dto.GitHubAuthDTO;
import com.github.switcherapi.ac.service.facades.GitHubFacade;
import com.github.switcherapi.client.test.SwitcherTest;
-import jakarta.ws.rs.core.MediaType;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
import org.junit.jupiter.api.AfterAll;
@@ -20,6 +19,7 @@
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
+import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import java.io.IOException;
@@ -157,7 +157,7 @@ void shouldNotLoginWithGitHub_invalidUrl() throws Exception {
private void givenGitHubToken() {
mockBackend.enqueue(new MockResponse()
- .setBody("{ \"access_token\": \"123\" }")
+ .setBody("{\"access_token\":\"123\",\"token_type\":\"bearer\",\"scope\":\"\"}")
.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/AdminPlanControllerErrorTests.java
index 6eb7f22..b6b6aae 100644
--- a/src/test/java/com/github/switcherapi/ac/controller/AdminPlanControllerErrorTests.java
+++ b/src/test/java/com/github/switcherapi/ac/controller/AdminPlanControllerErrorTests.java
@@ -3,7 +3,6 @@
import com.github.switcherapi.ac.model.domain.Plan;
import com.github.switcherapi.ac.service.PlanService;
import com.google.gson.Gson;
-import jakarta.ws.rs.core.HttpHeaders;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
@@ -12,6 +11,7 @@
import org.springframework.boot.test.autoconfigure.data.mongo.AutoConfigureDataMongo;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
diff --git a/src/test/java/com/github/switcherapi/ac/controller/ApiResourcesTests.java b/src/test/java/com/github/switcherapi/ac/controller/ApiResourcesTests.java
index f750a9a..1473bf9 100644
--- a/src/test/java/com/github/switcherapi/ac/controller/ApiResourcesTests.java
+++ b/src/test/java/com/github/switcherapi/ac/controller/ApiResourcesTests.java
@@ -3,13 +3,13 @@
import com.github.switcherapi.ac.model.domain.Admin;
import com.github.switcherapi.ac.service.AdminService;
import com.github.switcherapi.ac.service.JwtTokenService;
-import jakarta.ws.rs.core.HttpHeaders;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
diff --git a/src/test/java/com/github/switcherapi/ac/controller/PlanControllerTests.java b/src/test/java/com/github/switcherapi/ac/controller/PlanControllerTests.java
index dfea9ae..1041c0d 100644
--- a/src/test/java/com/github/switcherapi/ac/controller/PlanControllerTests.java
+++ b/src/test/java/com/github/switcherapi/ac/controller/PlanControllerTests.java
@@ -7,7 +7,6 @@
import com.github.switcherapi.ac.service.AdminService;
import com.github.switcherapi.ac.service.JwtTokenService;
import com.github.switcherapi.ac.service.PlanService;
-import jakarta.ws.rs.core.HttpHeaders;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@@ -15,6 +14,7 @@
import org.springframework.boot.test.autoconfigure.data.mongo.AutoConfigureDataMongo;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.server.ResponseStatusException;
diff --git a/src/test/java/com/github/switcherapi/ac/controller/SwitcherRelayControllerErrorTests.java b/src/test/java/com/github/switcherapi/ac/controller/SwitcherRelayControllerErrorTests.java
index d14bc27..1de9606 100644
--- a/src/test/java/com/github/switcherapi/ac/controller/SwitcherRelayControllerErrorTests.java
+++ b/src/test/java/com/github/switcherapi/ac/controller/SwitcherRelayControllerErrorTests.java
@@ -6,7 +6,6 @@
import com.github.switcherapi.ac.service.ValidatorBasicService;
import com.github.switcherapi.ac.service.validator.ValidatorBuilderService;
import com.google.gson.Gson;
-import jakarta.ws.rs.core.HttpHeaders;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
@@ -15,6 +14,7 @@
import org.springframework.boot.test.autoconfigure.data.mongo.AutoConfigureDataMongo;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
diff --git a/src/test/java/com/github/switcherapi/ac/controller/SwitcherRelayControllerTests.java b/src/test/java/com/github/switcherapi/ac/controller/SwitcherRelayControllerTests.java
index 5472f2f..56e7cd8 100644
--- a/src/test/java/com/github/switcherapi/ac/controller/SwitcherRelayControllerTests.java
+++ b/src/test/java/com/github/switcherapi/ac/controller/SwitcherRelayControllerTests.java
@@ -7,13 +7,13 @@
import com.github.switcherapi.ac.model.dto.ResponseRelayDTO;
import com.github.switcherapi.ac.service.AccountService;
import com.github.switcherapi.ac.service.PlanService;
-import jakarta.ws.rs.core.HttpHeaders;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.data.mongo.AutoConfigureDataMongo;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import java.util.List;
diff --git a/src/test/java/com/github/switcherapi/ac/controller/fixture/ControllerTestUtils.java b/src/test/java/com/github/switcherapi/ac/controller/fixture/ControllerTestUtils.java
index 9838e87..f27c0ea 100644
--- a/src/test/java/com/github/switcherapi/ac/controller/fixture/ControllerTestUtils.java
+++ b/src/test/java/com/github/switcherapi/ac/controller/fixture/ControllerTestUtils.java
@@ -10,8 +10,8 @@
import com.github.switcherapi.ac.model.dto.ResponseRelayDTO;
import com.github.switcherapi.ac.service.AccountService;
import com.google.gson.Gson;
-import jakarta.ws.rs.core.HttpHeaders;
import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
diff --git a/src/test/java/com/github/switcherapi/ac/service/GitHubServiceTest.java b/src/test/java/com/github/switcherapi/ac/service/GitHubServiceTest.java
new file mode 100644
index 0000000..8114c83
--- /dev/null
+++ b/src/test/java/com/github/switcherapi/ac/service/GitHubServiceTest.java
@@ -0,0 +1,120 @@
+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.service.facades.GitHubFacade;
+import com.github.switcherapi.client.exception.SwitcherRemoteException;
+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.springframework.http.MediaType;
+import org.springframework.web.server.ResponseStatusException;
+
+import java.io.IOException;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+@Slf4j
+public class GitHubServiceTest {
+
+ private final ObjectMapper mapper = new ObjectMapper();
+
+ public static MockWebServer mockBackend;
+
+ 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());
+ gitHubService = new GitHubService(new GitHubFacade("clientId", "oauthSecret", baseUrl, baseUrl));
+ }
+
+ @Test
+ void shouldGetToken() {
+ givenGitHubToken();
+ var token = gitHubService.getToken("code");
+ assertEquals("123", token);
+ }
+
+ @Test
+ void shouldNotGetToken_whenGitHubTokenIsInvalid() {
+ givenGitHubTokenInvalid();
+ var ex = assertThrows(ResponseStatusException.class, () -> gitHubService.getToken("code"));
+ assertEquals(401, ex.getStatusCode().value());
+ }
+
+ @Test
+ void shouldNotGetToken_whenURIIsInvalid() {
+ var failGitHubService = new GitHubService(
+ new GitHubFacade("clientId", "oauthSecret", "invalid", "invalid"));
+
+ var ex = assertThrows(SwitcherRemoteException.class, () -> failGitHubService.getToken("code"));
+ assertEquals("Something went wrong: It was not possible to reach the Switcher-API on this endpoint: invalid", ex.getMessage());
+ }
+
+ @Test
+ void shouldGetGitHubDetail() {
+ givenGitHubDetails();
+ var gitHubDetail = gitHubService.getGitHubDetail("123");
+ assertEquals("UserName", gitHubDetail.name());
+ assertEquals("login", gitHubDetail.login());
+ assertEquals("http://avatar.com", gitHubDetail.avatarUrl());
+ assertEquals("123", gitHubDetail.id());
+ }
+
+ @Test
+ void shouldNotGetGitHubDetail_whenGitHubTokenIsInvalid() {
+ givenGitHubTokenInvalid();
+ var ex = assertThrows(ResponseStatusException.class, () -> gitHubService.getGitHubDetail("code"));
+ assertEquals(401, ex.getStatusCode().value());
+ }
+
+ @Test
+ void shouldNotGetGitHubDetail_whenURIIsInvalid() {
+ var failGitHubService = new GitHubService(
+ new GitHubFacade("clientId", "oauthSecret", "invalid", "invalid"));
+
+ var ex = assertThrows(SwitcherRemoteException.class, () -> failGitHubService.getGitHubDetail("code"));
+ assertEquals("Something went wrong: It was not possible to reach the Switcher-API on this endpoint: invalid", 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));
+ }
+
+}