From f112e4e4d486d98aa79be5864384f420ccdb9228 Mon Sep 17 00:00:00 2001 From: Sabah u Din Irfan Date: Wed, 17 Jul 2024 18:53:17 +0100 Subject: [PATCH] Debug pipelines (#458) * Debug pipelines * Debug pipelines * Test pipelines * Test pipelines --- .../hmcts/opal/steps/BearerTokenStepDef.java | 46 ++++++++----------- .../hmcts/opal/steps/ModeSwitchStepDef.java | 19 +++++--- 2 files changed, 30 insertions(+), 35 deletions(-) diff --git a/src/functionalTest/java/uk/gov/hmcts/opal/steps/BearerTokenStepDef.java b/src/functionalTest/java/uk/gov/hmcts/opal/steps/BearerTokenStepDef.java index 48d650779..3f637978c 100644 --- a/src/functionalTest/java/uk/gov/hmcts/opal/steps/BearerTokenStepDef.java +++ b/src/functionalTest/java/uk/gov/hmcts/opal/steps/BearerTokenStepDef.java @@ -9,18 +9,27 @@ import static net.serenitybdd.rest.SerenityRest.then; - public class BearerTokenStepDef extends BaseStepDef { - protected static String TOKEN; - protected static ThreadLocal ALT_TOKEN = new ThreadLocal<>(); + private static final String DEFAULT_USER = "opal-test@hmcts.net"; + private static final ThreadLocal TOKEN = new ThreadLocal<>(); + private static final ThreadLocal ALT_TOKEN = new ThreadLocal<>(); private static final ConcurrentHashMap 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) @@ -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(); } } diff --git a/src/functionalTest/java/uk/gov/hmcts/opal/steps/ModeSwitchStepDef.java b/src/functionalTest/java/uk/gov/hmcts/opal/steps/ModeSwitchStepDef.java index 339bb4709..495da2565 100644 --- a/src/functionalTest/java/uk/gov/hmcts/opal/steps/ModeSwitchStepDef.java +++ b/src/functionalTest/java/uk/gov/hmcts/opal/steps/ModeSwitchStepDef.java @@ -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); } } @@ -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"); } }