From f88990dd465725a5d6596b3ebd15393c1ffc5e8d Mon Sep 17 00:00:00 2001 From: "georgi.michev" <georgi.michev@vereign.com> Date: Thu, 7 Jul 2022 15:37:40 +0300 Subject: [PATCH] apply code review recommendations Signed-off-by: georgi.michev <georgi.michev@vereign.com> --- .../test/rest/RestGeneralStepDefinitions.java | 30 +++++++++ .../rest/tsa/cache/CacheStepDefinitions.java | 9 --- .../tsa/policy/PolicyStepDefinitions.java | 33 +++------- .../tsa/signer/SignerStepDefinitions.java | 10 --- .../rest/tsa/task/TaskStepDefinitions.java | 66 +++++++++++-------- .../{name}/{version}/evaluation/POST.feature | 12 ++-- .../{name}/{version}/lock/DELETE.feature | 10 +-- .../{name}/{version}/lock/POST.feature | 14 ++-- .../task/v1/task/taskListStatus/GET.feature | 6 +- .../tsa/task/v1/task/taskResult/GET.feature | 1 - 10 files changed, 99 insertions(+), 92 deletions(-) diff --git a/src/test/java/api/test/rest/RestGeneralStepDefinitions.java b/src/test/java/api/test/rest/RestGeneralStepDefinitions.java index 2abd57cf..961c5ee4 100644 --- a/src/test/java/api/test/rest/RestGeneralStepDefinitions.java +++ b/src/test/java/api/test/rest/RestGeneralStepDefinitions.java @@ -56,6 +56,16 @@ public class RestGeneralStepDefinitions extends BaseStepDefinitions { } + @Given("we are testing the TSA Cache Api") + public void weAreTestingTheTSACacheApi() { + RestClient.setDefaultEncoding("UTF8"); + RestClient.setBaseURI(JsonUtils.getTSACache()); + RestClient.appendDefaultContentCharsetToContentTypeIfUndefined(false); + currentRequest.clear(); + currentRequest.getHeaders().put("X-Client-UserAgent", "test framework"); + currentRequest.setContentType("application/json"); + } + @Given("^we are testing the TSA Policy Api") public void we_are_testing_the_Policy_api() throws Throwable { RestClient.setDefaultEncoding("UTF8"); @@ -66,6 +76,26 @@ public class RestGeneralStepDefinitions extends BaseStepDefinitions { currentRequest.setContentType("application/json"); } + @Given("we are testing the TSA Signer Api") + public void weAreTestingTheTSASignerApi() { + RestClient.setDefaultEncoding("UTF8"); + RestClient.setBaseURI(JsonUtils.getTSASigner()); + RestClient.appendDefaultContentCharsetToContentTypeIfUndefined(false); + currentRequest.clear(); + currentRequest.getHeaders().put("X-Client-UserAgent", "test framework"); + currentRequest.setContentType("application/json"); + } + + @Given("we are testing the TSA Task Api") + public void weAreTestingTheTSATaskApi() { + RestClient.setDefaultEncoding("UTF8"); + RestClient.setBaseURI(JsonUtils.getTSATask()); + RestClient.appendDefaultContentCharsetToContentTypeIfUndefined(false); + currentRequest.clear(); + currentRequest.getHeaders().put("X-Client-UserAgent", "test framework"); + currentRequest.setContentType("application/json"); + } + @Given("^I load the REST request \\{(.*)\\} with profile \\{(.*)\\}$") public void I_load_the_REST_request__with_profile_(String jsonName, String profileName) throws Throwable { logger.info("Loading REST json into current request body. Json file= [{}] , profile= [{}]", jsonName, profileName); diff --git a/src/test/java/api/test/rest/tsa/cache/CacheStepDefinitions.java b/src/test/java/api/test/rest/tsa/cache/CacheStepDefinitions.java index b92245e6..352cef90 100644 --- a/src/test/java/api/test/rest/tsa/cache/CacheStepDefinitions.java +++ b/src/test/java/api/test/rest/tsa/cache/CacheStepDefinitions.java @@ -20,15 +20,6 @@ public class CacheStepDefinitions extends BaseStepDefinitions { this.currentRequest = currentRequest; } - @Given("we are testing the TSA Cache Api") - public void weAreTestingTheTSACacheApi() { - RestClient.setDefaultEncoding("UTF8"); - RestClient.setBaseURI(JsonUtils.getTSACache()); - RestClient.appendDefaultContentCharsetToContentTypeIfUndefined(false); - currentRequest.clear(); - currentRequest.getHeaders().put("X-Client-UserAgent", "test framework"); - currentRequest.setContentType("application/json"); - } @And("^I send the Cache (POST|GET) request via TSA Cache API$") public void iSendTheCachePOSTRequest(String method) { currentRequest.setPath("/v1/cache"); diff --git a/src/test/java/api/test/rest/tsa/policy/PolicyStepDefinitions.java b/src/test/java/api/test/rest/tsa/policy/PolicyStepDefinitions.java index 7c924153..fc374d82 100644 --- a/src/test/java/api/test/rest/tsa/policy/PolicyStepDefinitions.java +++ b/src/test/java/api/test/rest/tsa/policy/PolicyStepDefinitions.java @@ -40,40 +40,25 @@ public class PolicyStepDefinitions extends BaseStepDefinitions { this.currentRequest = currentRequest; } - @Given("^I load the body of my Policy request with \\{(.*)\\} from \\{(.*)\\}$") - public void iLoadTheBodyOfMyPolicyRequestWithDataFromFile(String profileName, String jsonName) throws Throwable { - logger.info("Loading REST json into current request body. Json file= [{}] , profile= [{}]", jsonName, profileName); - String loadedProfileData = JsonUtils.getProfileFromJson("/REST/json/" + jsonName, profileName); - if (!loadedProfileData.equals("null")) { - currentRequest.setBody(loadedProfileData); - logger.info("SUCCESS - profile loaded."); - } else { - throw new RAFException(String.format("Profile loading FAILED. Value is \"null\". File= [%s] , profile= [%s]", - jsonName, - profileName) - , RestGeneralStepDefinitions.class); - } - } - - @And("^I execute the Policy \\{(.*)\\} via TSA Policy API$") - public void iExecuteThePolicyTSAPolicyAPI(String path) throws Throwable { - currentRequest.setPath("/policy/example/" + path + "/1.0/evaluation"); + @And("^I execute the Policy group \\{(.*)\\} name \\{(.*)\\} version \\{(.*)\\} via TSA Policy API$") + public void iExecuteThePolicyGroupNameVersionTSAPolicyAPI(String group, String name, String version) throws Throwable { + currentRequest.setPath("/policy/" + group + "/" + name + "/" + version + "/evaluation"); Response response = RestClient.post(currentRequest); addRequest(currentRequest); addResponse(response); } - @When("^I lock the policy \\{(.*)\\}$") - public void iLockThePolicy(String path) throws Throwable { - currentRequest.setPath("/policy/example/" + path + "/1.0/lock"); + @When("^I lock the Policy group \\{(.*)\\} name \\{(.*)\\} version \\{(.*)\\}$") + public void iLockThePolicyGroupNameVersion(String group, String name, String version) throws Throwable { + currentRequest.setPath("/policy/" + group + "/" + name + "/" + version + "/lock"); Response response = RestClient.post(currentRequest); addRequest(currentRequest); addResponse(response); } - @When("^I unlock the policy \\{(.*)\\}$") - public void iUnlockThePolicy(String path) throws Throwable { - currentRequest.setPath("/policy/example/" + path + "/1.0/lock"); + @When("^I unlock the policy group \\{(.*)\\} name \\{(.*)\\} version \\{(.*)\\}$") + public void iUnlockThePolicy(String group, String name, String version) throws Throwable { + currentRequest.setPath("/policy/" + group + "/" + name + "/" + version + "/lock"); Response response = RestClient.delete(currentRequest); addRequest(currentRequest); addResponse(response); diff --git a/src/test/java/api/test/rest/tsa/signer/SignerStepDefinitions.java b/src/test/java/api/test/rest/tsa/signer/SignerStepDefinitions.java index c3b24639..fb877127 100644 --- a/src/test/java/api/test/rest/tsa/signer/SignerStepDefinitions.java +++ b/src/test/java/api/test/rest/tsa/signer/SignerStepDefinitions.java @@ -20,16 +20,6 @@ public class SignerStepDefinitions extends BaseStepDefinitions { this.currentRequest = currentRequest; } - @Given("we are testing the TSA Signer Api") - public void weAreTestingTheTSASignerApi() { - RestClient.setDefaultEncoding("UTF8"); - RestClient.setBaseURI(JsonUtils.getTSASigner()); - RestClient.appendDefaultContentCharsetToContentTypeIfUndefined(false); - currentRequest.clear(); - currentRequest.getHeaders().put("X-Client-UserAgent", "test framework"); - currentRequest.setContentType("application/json"); - } - @When("I get all keys via TSA Signer API") public void iGetAllKeysViaTSASignerAPI() { currentRequest.setPath("/v1/keys"); diff --git a/src/test/java/api/test/rest/tsa/task/TaskStepDefinitions.java b/src/test/java/api/test/rest/tsa/task/TaskStepDefinitions.java index 9b612de5..6c5033e4 100644 --- a/src/test/java/api/test/rest/tsa/task/TaskStepDefinitions.java +++ b/src/test/java/api/test/rest/tsa/task/TaskStepDefinitions.java @@ -3,7 +3,10 @@ package api.test.rest.tsa.task; import api.test.core.BaseStepDefinitions; import api.test.rest.RestGeneralStepDefinitions; import api.test.rest.RestSessionContainer; +import com.jayway.jsonpath.Configuration; import com.jayway.jsonpath.JsonPath; +import com.jayway.jsonpath.Option; +import com.jayway.jsonpath.ReadContext; import core.*; import cucumber.api.java.en.And; import cucumber.api.java.en.Given; @@ -12,6 +15,9 @@ import org.apache.logging.log4j.Logger; import java.util.List; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + public class TaskStepDefinitions extends BaseStepDefinitions{ private static final Logger logger = LogManager.getLogger(RestGeneralStepDefinitions.class.getSimpleName()); RestSessionContainer restSessionContainer; @@ -23,16 +29,6 @@ public class TaskStepDefinitions extends BaseStepDefinitions{ this.currentRequest = currentRequest; } - @Given("we are testing the TSA Task Api") - public void weAreTestingTheTSATaskApi() { - RestClient.setDefaultEncoding("UTF8"); - RestClient.setBaseURI(JsonUtils.getTSATask()); - RestClient.appendDefaultContentCharsetToContentTypeIfUndefined(false); - currentRequest.clear(); - currentRequest.getHeaders().put("X-Client-UserAgent", "test framework"); - currentRequest.setContentType("application/json"); - } - @And("^I execute the Task \\{(.*)\\} via TSA Task API$") public void iExecuteTheTaskTSATaskAPI(String path) throws Throwable { currentRequest.setPath("/v1/task/" + path); @@ -68,19 +64,9 @@ public class TaskStepDefinitions extends BaseStepDefinitions{ } } - @And("^I get tasks IDs via TSA Task API$") - public void iGetTasksIdsTSATaskAPI() throws Throwable { - if (getLastResponse().getStatusCode() == 200) { - String responseBody = getLastResponse().getBody(); - - List<String> taskResult = JsonPath.read(responseBody, "$..tasks..id"); - restSessionContainer.setGroupTaskIDs(taskResult); - } - } - @And("^I get the Task result with key \\{(.*)\\}$") public void iGetTheTaskResultWithKey(String suffix) { - currentRequest.setPath("v1/"+ suffix); + currentRequest.setPath("v1/taskResult/"+ suffix); Response response = RestClient.get(currentRequest); addRequest(currentRequest); @@ -90,18 +76,46 @@ public class TaskStepDefinitions extends BaseStepDefinitions{ @And("^I get the current Task Result via TSA Task API$") public void iGetTheCurrentTaskResultTSATaskAPI() { String currentTask = restSessionContainer.getTaskID(); - iGetTheTaskResultWithKey("taskResult/"+ currentTask); + iGetTheTaskResultWithKey(currentTask); } @And("^I get the current taskList Status via TSA Task API$") public void iGetTheCurrentTaskListStatusTSATaskAPI() { String currentTask = restSessionContainer.getTaskListID(); - iGetTheTaskResultWithKey("taskListStatus/"+ currentTask); + currentRequest.setPath("v1/taskListStatus/"+ currentTask); + + Response response = RestClient.get(currentRequest); + addRequest(currentRequest); + addResponse(response); + + if (getLastResponse().getStatusCode() == 200) { + String responseBody = getLastResponse().getBody(); + + List<String> taskResult = JsonPath.read(responseBody, "$..tasks..id"); + restSessionContainer.setGroupTaskIDs(taskResult); + } } @And("^I get the result of Task \\{(\\d+)\\}$") public void iGetTheResultOfTask(int id) { - String currentTask = restSessionContainer.getTaskID(); - iGetTheTaskResultWithKey("taskResult/"+ restSessionContainer.getGroupTaskIDs().get(id)); + iGetTheTaskResultWithKey( restSessionContainer.getGroupTaskIDs().get(id)); + } + + @And("^the field \\{(.*?)\\} has one of the values$") + public static void theFieldHasOneOfTheValues(String jsonPath, List<String> expectedResultAsList) throws Throwable { + + Configuration conf = Configuration.builder().options(Option.SUPPRESS_EXCEPTIONS).build(); + ReadContext responseContext = JsonPath.using(conf).parse(getLastResponse().getBody()); + + Object parsedResult = responseContext.read(jsonPath); + String actualResultAsString = String.valueOf(parsedResult); + + for(String x: expectedResultAsList) { + if (x.equals(actualResultAsString)){ + return; + } + } + + assertEquals("Both of the expected values doesn't match actual: ", expectedResultAsList, actualResultAsString); } -} +} \ No newline at end of file diff --git a/src/test/resources/features/tsa/policy/policy/{group}/{name}/{version}/evaluation/POST.feature b/src/test/resources/features/tsa/policy/policy/{group}/{name}/{version}/evaluation/POST.feature index 91b9b41b..ae395f1a 100644 --- a/src/test/resources/features/tsa/policy/policy/{group}/{name}/{version}/evaluation/POST.feature +++ b/src/test/resources/features/tsa/policy/policy/{group}/{name}/{version}/evaluation/POST.feature @@ -32,14 +32,14 @@ Feature: API -TSA - Policy - :group/:name/:version/evaluation POST Scenario: TSA - Evaluate policy synchronously - Positive When I load the REST request {Policy.json} with profile {successful_message} - And I execute the Policy {test} via TSA Policy API + And I execute the Policy group {example} name {test} version {1.0} via TSA Policy API Then the status code should be {200} And the response is valid according to the {Policy_Evaluate_schema.json} REST schema And the field {allow} has the value {true} Scenario: TSA - DID resolution - Positive When I load the REST request {Policy.json} with profile {did_key} - And I execute the Policy {resolve} via TSA Policy API + And I execute the Policy group {example} name {resolve} version {1.0} via TSA Policy API Then the status code should be {200} And the response is valid according to the {Policy_EvaluateDID_schema.json} REST schema And the field {data.didDocument.id} has the value {did:key:z6Mkfriq1MqLBoPWecGoDLjguo1sB9brj6wT3qZ5BxkKpuP6} @@ -47,14 +47,14 @@ Feature: API -TSA - Policy - :group/:name/:version/evaluation POST @negative Scenario: TSA - Evaluate policy with incorrect value - Negative When I load the REST request {Policy.json} with profile {incorrect_message} - And I execute the Policy {test} via TSA Policy API + And I execute the Policy group {example} name {test} version {1.0} via TSA Policy API Then the status code should be {200} And the response is valid according to the {Policy_Evaluate_schema.json} REST schema And the field {allow} has the value {false} @negative Scenario: TSA - Evaluate policy with missing body- Negative - Given I execute the Policy {test} via TSA Policy API + And I execute the Policy group {example} name {test} version {1.0} via TSA Policy API Then the status code should be {200} And the response is valid according to the {Policy_Evaluate_schema.json} REST schema And the field {allow} has the value {false} @@ -62,7 +62,7 @@ Feature: API -TSA - Policy - :group/:name/:version/evaluation POST @negative Scenario: TSA - Evaluate missing policy - Negative When I load the REST request {Policy.json} with profile {successful_message} - Given I execute the Policy {missing} via TSA Policy API + And I execute the Policy group {example} name {missing} version {1.0} via TSA Policy API Then the status code should be {404} And the response is valid according to the {Policy_Evaluate_negative_schema.json} REST schema And the field {message} has the value {error evaluating policy} @@ -70,7 +70,7 @@ Feature: API -TSA - Policy - :group/:name/:version/evaluation POST @negative Scenario: TSA - DID resolution with incorrect did - Negative When I load the REST request {Policy.json} with profile {did_missing_method} - And I execute the Policy {resolve} via TSA Policy API + And I execute the Policy group {example} name {resolve} version {1.0} via TSA Policy API Then the status code should be {200} And the response is valid according to the {Policy_EvaluateDID_negative_schema.json} REST schema And the field {data.didResolutionMetadata.error} has the value {notFound} diff --git a/src/test/resources/features/tsa/policy/policy/{group}/{name}/{version}/lock/DELETE.feature b/src/test/resources/features/tsa/policy/policy/{group}/{name}/{version}/lock/DELETE.feature index a152ed77..e1a91103 100644 --- a/src/test/resources/features/tsa/policy/policy/{group}/{name}/{version}/lock/DELETE.feature +++ b/src/test/resources/features/tsa/policy/policy/{group}/{name}/{version}/lock/DELETE.feature @@ -26,16 +26,16 @@ Feature: API - TSA - Policy - :group/:name/:version/lock DELETE Given we are testing the TSA Policy Api Scenario: TSA - Unlock policy - Positive - When I lock the policy {test} - And I unlock the policy {test} + When I lock the Policy group {example} name {test} version {1.0} + And I unlock the policy group {example} name {test} version {1.0} And the status code should be {200} # Check if the policy can be executed - When I load the body of my Policy request with {successful_message} from {Policy.json} - And I execute the Policy {test} via TSA Policy API + When I load the REST request {Policy.json} with profile {successful_message} + And I execute the Policy group {example} name {test} version {1.0} via TSA Policy API Then the status code should be {200} @negative Scenario: TSA - Unlock none existing policy - Negative - When I unlock the policy {non_existing} + And I unlock the policy group {example} name {non_existing} version {1.0} And the status code should be {404} And the field {message} has the value {policy not found} diff --git a/src/test/resources/features/tsa/policy/policy/{group}/{name}/{version}/lock/POST.feature b/src/test/resources/features/tsa/policy/policy/{group}/{name}/{version}/lock/POST.feature index 19f22214..6b904c34 100644 --- a/src/test/resources/features/tsa/policy/policy/{group}/{name}/{version}/lock/POST.feature +++ b/src/test/resources/features/tsa/policy/policy/{group}/{name}/{version}/lock/POST.feature @@ -31,18 +31,18 @@ Feature: API - TSA -Policy - :group/:name/:version/lock POST Given we are testing the TSA Policy Api Scenario: TSA - Lock policy - Positive - When I lock the policy {test} + When I lock the Policy group {example} name {test} version {1.0} Then the status code should be {200} - Then I load the body of my Policy request with {successful_message} from {Policy.json} - And I execute the Policy {test} via TSA Policy API + When I load the REST request {Policy.json} with profile {successful_message} + And I execute the Policy group {example} name {test} version {1.0} via TSA Policy API Then the status code should be {403} And the field {message} has the value {error evaluating policy} - And I unlock the policy {test} + And I unlock the policy group {example} name {test} version {1.0} @negative Scenario: TSA - Lock already locked policy - Negative - When I lock the policy {test} - And I lock the policy {test} + When I lock the Policy group {example} name {test} version {1.0} + When I lock the Policy group {example} name {test} version {1.0} And the status code should be {403} And the field {message} has the value {policy is already locked} - And I unlock the policy {test} + And I unlock the policy group {example} name {test} version {1.0} diff --git a/src/test/resources/features/tsa/task/v1/task/taskListStatus/GET.feature b/src/test/resources/features/tsa/task/v1/task/taskListStatus/GET.feature index 1c058853..2db90670 100644 --- a/src/test/resources/features/tsa/task/v1/task/taskListStatus/GET.feature +++ b/src/test/resources/features/tsa/task/v1/task/taskListStatus/GET.feature @@ -33,7 +33,6 @@ Feature: API -TSA - Task - v1/taskList GET And I wait for {1000} mseconds Then I get the current taskList Status via TSA Task API Then the status code should be {200} - And I get tasks IDs via TSA Task API Then I get the current taskList Status via TSA Task API And the response is valid according to the {TaskList_Status_schema.json} REST schema And the field {status} has the value {done} @@ -47,7 +46,6 @@ Feature: API -TSA - Task - v1/taskList GET And I wait for {1000} mseconds Then I get the current taskList Status via TSA Task API Then the status code should be {207} - And I get tasks IDs via TSA Task API Then I get the current taskList Status via TSA Task API And the response is valid according to the {TaskList_Status_schema.json} REST schema And the field {status} has the value {failed} @@ -61,8 +59,8 @@ Feature: API -TSA - Task - v1/taskList GET And I wait for {1000} mseconds Then I get the current taskList Status via TSA Task API Then the status code should be {207} - And I get tasks IDs via TSA Task API Then I get the current taskList Status via TSA Task API And the response is valid according to the {TaskList_Status_schema.json} REST schema And the field {status} has the value {failed} - And the field {$..tasks..status} has the value {["failed","done","done"]} + And the field {$..tasks..status} has one of the values + | ["done","failed","done"] | ["failed","done","done"] | \ No newline at end of file diff --git a/src/test/resources/features/tsa/task/v1/task/taskResult/GET.feature b/src/test/resources/features/tsa/task/v1/task/taskResult/GET.feature index 1b16afc3..fc627ff1 100644 --- a/src/test/resources/features/tsa/task/v1/task/taskResult/GET.feature +++ b/src/test/resources/features/tsa/task/v1/task/taskResult/GET.feature @@ -47,7 +47,6 @@ Feature: API -TSA - Task - v1/taskList GET And I wait for {1000} mseconds Then I get the current taskList Status via TSA Task API And the status code should be {200} - Then I get tasks IDs via TSA Task API And I get the result of Task {0} And the status code should be {200} And I get the result of Task {1} -- GitLab