Skip to content

Commit

Permalink
Debug pipelines (#458)
Browse files Browse the repository at this point in the history
* Debug pipelines

* Debug pipelines

* Test  pipelines

* Test  pipelines
  • Loading branch information
sabahirfan committed Jul 17, 2024
1 parent 1bc6bc3 commit f112e4e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,27 @@

import static net.serenitybdd.rest.SerenityRest.then;


public class BearerTokenStepDef extends BaseStepDef {

protected static String TOKEN;
protected static ThreadLocal<String> ALT_TOKEN = new ThreadLocal<>();
private static final String DEFAULT_USER = "opal-test@hmcts.net";
private static final ThreadLocal<String> TOKEN = new ThreadLocal<>();
private static final ThreadLocal<String> ALT_TOKEN = new ThreadLocal<>();
private static final ConcurrentHashMap<String, String> tokenCache = new ConcurrentHashMap<>();

public String getAccessTokenForUser(String user) {
return tokenCache.computeIfAbsent(user, this::fetchAccessToken);
return tokenCache.computeIfAbsent(user, BearerTokenStepDef::fetchAccessToken);
}

private static String fetchAccessToken(String user) {
return fetchToken(user);
}

private String fetchAccessToken(String user) {
@BeforeAll
public static void setDefaultToken() {
TOKEN.set(tokenCache.computeIfAbsent(DEFAULT_USER, BearerTokenStepDef::fetchAccessToken));
}

private static String fetchToken(String user) {
SerenityRest.given()
.accept("*/*")
.header("X-User-Email", user)
Expand All @@ -32,38 +41,19 @@ private String fetchAccessToken(String user) {
return then().extract().body().jsonPath().getString("accessToken");
}

@BeforeAll
public static void setToken() {
SerenityRest.given()
.accept("*/*")
.contentType("application/json")
.when()
.get(getTestUrl() + "/api/testing-support/token/test-user");
then().assertThat().statusCode(200);
TOKEN = then().extract().body().jsonPath().getString("accessToken");
}

protected static String getToken() {
if (ALT_TOKEN.get() == null) {
return TOKEN;
} else {
return ALT_TOKEN.get();
}
return ALT_TOKEN.get() != null ? ALT_TOKEN.get() : TOKEN.get();
}

@When("I am testing as the {string} user")
public void setTokenWithUser(String user) {
String accessToken = getAccessTokenForUser(user);
ALT_TOKEN.set(accessToken);
}

public static void clearCurrentToken() {
ALT_TOKEN.remove();
ALT_TOKEN.set(getAccessTokenForUser(user));
}

@AfterAll
public static void clearCache() {
tokenCache.clear();
clearCurrentToken();
ALT_TOKEN.remove();
TOKEN.remove();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,15 @@ public static void beforeAll() throws JSONException {
JSONObject requestBody = new JSONObject();
requestBody.put("mode", "opal");


log.info("Test mode equals " + testMode + " setting to: opal");
SerenityRest.given().accept("*/*").contentType("application/json").body(requestBody.toString()).when().put(
getTestUrl() + "/api/testing-support/app-mode");
updateAppMode(requestBody);
}
if (Objects.equals(testMode, "legacy")) {
JSONObject requestBody = new JSONObject();
requestBody.put("mode", "legacy");

log.info("Test mode equals " + testMode + " setting to: legacy");
SerenityRest.given().accept("*/*").contentType("application/json").body(requestBody.toString()).when().put(
getTestUrl() + "/api/testing-support/app-mode");
updateAppMode(requestBody);
}
}

Expand All @@ -44,8 +41,16 @@ public static void afterAll() throws JSONException {
requestBody.put("mode", "opal");

log.info("All tests ran setting back to: opal");
SerenityRest.given().accept("*/*").contentType("application/json").body(requestBody.toString()).when().put(
getTestUrl() + "/api/testing-support/app-mode");
updateAppMode(requestBody);
}

private static void updateAppMode(JSONObject requestBody) {
SerenityRest.given()
.accept("*/*")
.contentType("application/json")
.body(requestBody.toString())
.when()
.put(getTestUrl() + "/api/testing-support/app-mode");
}

}

0 comments on commit f112e4e

Please sign in to comment.