Skip to content

Commit

Permalink
Tests for PO 559 591 (#527)
Browse files Browse the repository at this point in the history
* Adding methods and steps for creating test data and asserting the responses

* adding default test data

* addming mechanism to clean up data after test, even if test fails

* adding tests

* tidying up

* correcting uri

* checkstyle
  • Loading branch information
CadeFaulkner authored Sep 12, 2024
1 parent 3d41ae1 commit 0fb7b51
Show file tree
Hide file tree
Showing 12 changed files with 311 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ public class Constants {

public static final String ENFORCERS_REF_DATA_URI = "/enforcers/ref-data/Aldridge";
public static final String MAJOR_CREDITORS_URI = "/major-creditors/";
public static final String DRAFT_ACCOUNT_URI = "/draft-accounts";
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ private static String fetchToken(String user) {
return then().extract().body().jsonPath().getString("accessToken");
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ private static void updateAppMode(JSONObject requestBody) {
.contentType("application/json")
.body(requestBody.toString())
.when()
.put(getTestUrl() + "/api/testing-support/app-mode");
.put(getTestUrl() + "/testing-support/app-mode");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package uk.gov.hmcts.opal.steps.draftaccount;

import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
import net.serenitybdd.rest.SerenityRest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import uk.gov.hmcts.opal.steps.BaseStepDef;
import uk.gov.hmcts.opal.utils.DraftAccountUtils;

import java.util.ArrayList;

import static uk.gov.hmcts.opal.config.Constants.DRAFT_ACCOUNT_URI;
import static uk.gov.hmcts.opal.steps.BearerTokenStepDef.getToken;

public class DraftAccountDeleteSteps extends BaseStepDef {
static Logger log = LoggerFactory.getLogger(DraftAccountDeleteSteps.class.getName());

@When("I delete the draft account {string}")
public static void deleteDraftAccount(String draftAccountId) {
SerenityRest
.given()
.header("Authorization", "Bearer " + getToken())
.accept("*/*")
.contentType("application/json")
.when()
.delete(getTestUrl() + DRAFT_ACCOUNT_URI + "/" + draftAccountId + "?ignoreMissing=true");
}

@Then("I delete the created draft accounts")
public static void deleteAllCreatedDraftAccounts() {
ArrayList<String> accounts = DraftAccountUtils.getAllDraftAccountIds();
for (String account : accounts) {
log.info("Deleting draft account: {}", account);
deleteDraftAccount(account);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package uk.gov.hmcts.opal.steps.draftaccount;

import io.cucumber.datatable.DataTable;
import io.cucumber.java.en.When;
import net.serenitybdd.rest.SerenityRest;
import uk.gov.hmcts.opal.steps.BaseStepDef;
import uk.gov.hmcts.opal.utils.DraftAccountUtils;

import java.util.Map;

import static net.serenitybdd.rest.SerenityRest.then;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static uk.gov.hmcts.opal.config.Constants.DRAFT_ACCOUNT_URI;
import static uk.gov.hmcts.opal.steps.BearerTokenStepDef.getToken;

public class DraftAccountGetSteps extends BaseStepDef {
@When("I get the draft account {string}")
public void getDraftAccount(String draftAccountId) {
SerenityRest
.given()
.header("Authorization", "Bearer " + getToken())
.accept("*/*")
.contentType("application/json")
.when()
.get(getTestUrl() + DRAFT_ACCOUNT_URI + "/" + draftAccountId);
}

@When("I get the single created draft account and the response contains")
public void getSingleDraftAccount(DataTable data) {
assertEquals(
1,
DraftAccountUtils.getAllDraftAccountIds().size(),
"There should be only one draft account but found multiple: " + DraftAccountUtils.getAllDraftAccountIds()
);
String draftAccountId = DraftAccountUtils.getAllDraftAccountIds().getFirst();
SerenityRest
.given()
.header("Authorization", "Bearer " + getToken())
.accept("*/*")
.contentType("application/json")
.when()
.get(getTestUrl() + DRAFT_ACCOUNT_URI + "/" + draftAccountId);

Map<String, String> expectedData = data.asMap(String.class, String.class);

for (String key : expectedData.keySet()) {
String apiResponseValue = then().extract().body().jsonPath().getString(key);
assertEquals(expectedData.get(key), apiResponseValue, "Values are not equal : ");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package uk.gov.hmcts.opal.steps.draftaccount;

import io.cucumber.datatable.DataTable;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
import net.serenitybdd.rest.SerenityRest;
import org.json.JSONException;
import org.json.JSONObject;
import uk.gov.hmcts.opal.steps.BaseStepDef;
import uk.gov.hmcts.opal.utils.DraftAccountUtils;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Map;

import static net.serenitybdd.rest.SerenityRest.then;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static uk.gov.hmcts.opal.config.Constants.DRAFT_ACCOUNT_URI;
import static uk.gov.hmcts.opal.steps.BearerTokenStepDef.getToken;

public class DraftAccountPostSteps extends BaseStepDef {
@When("I create a draft account with the following details")
public void postDraftAccount(DataTable accountData) throws JSONException, IOException {
Map<String, String> dataToPost = accountData.asMap(String.class, String.class);
JSONObject postBody = new JSONObject();

postBody.put(
"business_unit_id",
dataToPost.get("business_unit_id") != null ? dataToPost.get("business_unit_id") : ""
);
postBody.put("submitted_by", dataToPost.get("submitted_by") != null ? dataToPost.get("submitted_by") : "");
postBody.put("account_type", dataToPost.get("account_type") != null ? dataToPost.get("account_type") : "");
postBody.put(
"account_status",
dataToPost.get("account_status") != null ? dataToPost.get("account_status") : ""
);


String accountFilePath = "build/resources/functionalTest/features/opalMode/manualAccountCreation/"
+ dataToPost.get(
"account");
String account = new String(Files.readAllBytes(Paths.get(accountFilePath)));
JSONObject accountObject = new JSONObject(account);

JSONObject timelineObject;
if (dataToPost.get("timeline_data") != null) {
String timelineFilePath = "build/resources/functionalTest/features/opalMode/manualAccountCreation/"
+ dataToPost.get(
"account");
String timeline = new String(Files.readAllBytes(Paths.get(timelineFilePath)));
timelineObject = new JSONObject(timeline);
} else {
String timelineFilePath = "build/resources/functionalTest/features/opalMode/manualAccountCreation"
+ "/draftAccounts/timelineJson/default.json";
String timeline = new String(Files.readAllBytes(Paths.get(timelineFilePath)));
timelineObject = new JSONObject(timeline);
}
postBody.put("account", accountObject);
postBody.put("timeline_data", timelineObject);

SerenityRest
.given()
.header("Authorization", "Bearer " + getToken())
.accept("*/*")
.contentType("application/json")
.body(postBody.toString())
.when()
.post(getTestUrl() + DRAFT_ACCOUNT_URI);
}

@Then("I store the created draft account ID")
public void storeDraftAccountId() {
String draftAccountId = then().extract().body().jsonPath().getString("draft_account_id");
DraftAccountUtils.addDraftAccountId(draftAccountId);
}

@Then("The draft account response contains the following data")
public void draftAccountResponseContains(DataTable data) {
Map<String, String> expectedData = data.asMap(String.class, String.class);

for (String key : expectedData.keySet()) {
String apiResponseValue = then().extract().body().jsonPath().getString(key);
assertEquals(expectedData.get(key), apiResponseValue, "Values are not equal : ");
}
}

@Then("The draft account response returns 201")
public void draftAccountResponseCreated() {
then().assertThat()
.statusCode(201);
}

@Then("The draft account response returns 400")
public void draftAccountResponseBadRequest() {
then().assertThat()
.statusCode(400);
}

@Then("The draft account response returns 500")
public void draftAccountResponseInternalServerError() {
then().assertThat()
.statusCode(500);
}
}
14 changes: 14 additions & 0 deletions src/functionalTest/java/uk/gov/hmcts/opal/utils/DataCleanUp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package uk.gov.hmcts.opal.utils;

import io.cucumber.java.After;
import uk.gov.hmcts.opal.steps.BaseStepDef;
import uk.gov.hmcts.opal.steps.draftaccount.DraftAccountDeleteSteps;

public class DataCleanUp extends BaseStepDef {

@After("@cleanUpData")
public void cleanUpData() {
DraftAccountDeleteSteps.deleteAllCreatedDraftAccounts();
DraftAccountUtils.clearDraftAccountIds();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package uk.gov.hmcts.opal.utils;

import java.util.ArrayList;

public class DraftAccountUtils {
private static final ThreadLocal<ArrayList<String>> draftAccountId = ThreadLocal.withInitial(ArrayList::new);

public static void addDraftAccountId(String id) {
draftAccountId.get().add(id);
}

public static ArrayList<String> getAllDraftAccountIds() {
return draftAccountId.get();
}

public static void clearDraftAccountIds() {
draftAccountId.remove();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
@Opal
Feature: PO-559 get draft account

@PO-559 @cleanUpData
Scenario: Get draft account - happy path
Given I am testing as the "opal-test@hmcts.net" user
When I create a draft account with the following details
| business_unit_id | 73 |
| account | draftAccounts/accountJson/adultAccount.json |
| account_type | Fine |
| account_status | |
| submitted_by | |
| timeline_data | |
Then The draft account response returns 201
And I store the created draft account ID

Then I get the single created draft account and the response contains
| business_unit_id | 73 |
| account_type | Fine |
| account_status | Submitted |
| account_snapshot.DefendantName | LNAME, FNAME |
| account_snapshot.DateOfBirth | 01/01/2000 |
| account_snapshot.AccountType | Fine |
| account_snapshot.SubmittedBy | opal-test@HMCTS.NET |
| account_snapshot.BusinessUnitName | MBEC London |

Then I delete the created draft accounts


Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
@Opal
Feature: PO-591 create draft account

@PO-591 @cleanUpData
Scenario: Create draft account - happy path
Given I am testing as the "opal-test@hmcts.net" user
When I create a draft account with the following details
| business_unit_id | 73 |
| account | draftAccounts/accountJson/adultAccount.json |
| account_type | Fine |
| account_status | |
| submitted_by | |
| timeline_data | |
Then The draft account response returns 201
And I store the created draft account ID

And The draft account response contains the following data
| business_unit_id | 73 |
| account_type | Fine |
| account_status | Submitted |
| account_snapshot.DefendantName | LNAME, FNAME |
| account_snapshot.DateOfBirth | 01/01/2000 |
| account_snapshot.AccountType | Fine |
| account_snapshot.SubmittedBy | opal-test@HMCTS.NET |
| account_snapshot.BusinessUnitName | MBEC London |

Then I delete the created draft accounts


Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"accountCreateRequest": {
"Defendant": {
"Surname": "LNAME",
"Forenames": "FNAME",
"DOB": "01/01/2000"
},
"Account": {
"AccountType": "Fine"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"timeline": [
{
"username": "",
"status": "",
"status_date": "",
"reason_text": ""
}
]
}

0 comments on commit 0fb7b51

Please sign in to comment.