From 4048fdbee1191bb6763cc11116ff3066238e7e28 Mon Sep 17 00:00:00 2001 From: "georgi.michev" <georgi.michev@vereign.com> Date: Fri, 1 Jul 2022 09:57:58 +0300 Subject: [PATCH] refactor step definitions Signed-off-by: georgi.michev <georgi.michev@vereign.com> --- src/main/resources/REST/json/Policy.json | 10 ++--- .../rest/tsa/cache/CacheStepDefinitions.java | 41 +++++++++++++++++-- .../tsa/policy/PolicyStepDefinitions.java | 28 +++++++++---- .../rest/tsa/task/TaskStepDefinitions.java | 33 +++++++++++++++ .../features/tsa/cache/v1/cache/GET.feature | 16 ++++---- .../features/tsa/cache/v1/cache/POST.feature | 13 +++--- .../{name}/{version}/evaluation/POST.feature | 25 ++++++----- .../{name}/{version}/lock/DELETE.feature | 10 +++-- .../{name}/{version}/lock/POST.feature | 18 ++++---- .../tsa/task/v1/task/{taskName}/GET.feature | 7 ++-- .../tsa/task/v1/task/{taskName}/POST.feature | 11 +++-- 11 files changed, 144 insertions(+), 68 deletions(-) diff --git a/src/main/resources/REST/json/Policy.json b/src/main/resources/REST/json/Policy.json index d7a8debb..e1f37e85 100644 --- a/src/main/resources/REST/json/Policy.json +++ b/src/main/resources/REST/json/Policy.json @@ -1,17 +1,17 @@ { - "successful_evaluate": { + "successful_message": { "message": "hello world" }, - "unsuccessful_evaluate": { + "incorrect_message": { "message": "wrong value!" }, - "didResolve_indy_evaluate": { + "did_indy": { "did":"did:indy:idunion:BDrEcHc8Tb4Lb2VyQZWEDE" }, - "didResolve_evaluate": { + "did_key": { "did":"did:key:z6Mkfriq1MqLBoPWecGoDLjguo1sB9brj6wT3qZ5BxkKpuP6" }, - "didResolve_missing_method": { + "did_missing_method": { "did":"did:idunion:BDrEcHc8Tb4Lb2VyQZWEDE" } } \ No newline at end of file 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 7f854a01..546371ec 100644 --- a/src/test/java/api/test/rest/tsa/cache/CacheStepDefinitions.java +++ b/src/test/java/api/test/rest/tsa/cache/CacheStepDefinitions.java @@ -3,11 +3,10 @@ package api.test.rest.tsa.cache; import api.test.core.BaseStepDefinitions; import api.test.rest.RestGeneralStepDefinitions; import api.test.rest.RestSessionContainer; -import core.DataContainer; -import core.JsonUtils; -import core.Request; -import core.RestClient; +import core.*; +import cucumber.api.java.en.And; import cucumber.api.java.en.Given; +import exceptions.RAFException; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -22,6 +21,26 @@ public class CacheStepDefinitions extends BaseStepDefinitions { this.currentRequest = currentRequest; } + @Given("^I load the body of my Cache 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 load value \\{(.*)\\} into current request HEADER \\{(.*)\\}$") + public void iLoadValueFromTheResponseIntoCurrentRequestHEADER(String value, String headerName) throws Throwable { + currentRequest.getHeaders().put(headerName, value); + } + @Given("we are testing the TSA Cache Api") public void weAreTestingTheTSACacheApi() { RestClient.setDefaultEncoding("UTF8"); @@ -31,4 +50,18 @@ public class CacheStepDefinitions extends BaseStepDefinitions { currentRequest.getHeaders().put("X-Client-UserAgent", "test framework"); currentRequest.setContentType("application/json"); } + @And("^I send the Cache (POST|GET) request$") + public void iSendTheCachePOSTRequest(String method) { + currentRequest.setPath("/v1/cache"); + + if (method.equals("POST")) { + Response response = RestClient.post(currentRequest); + addRequest(currentRequest); + addResponse(response); + } else if (method.equals("GET")) { + Response response = RestClient.get(currentRequest); + addRequest(currentRequest); + addResponse(response); + } + } } 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 13aa8332..cfee7ca1 100644 --- a/src/test/java/api/test/rest/tsa/policy/PolicyStepDefinitions.java +++ b/src/test/java/api/test/rest/tsa/policy/PolicyStepDefinitions.java @@ -20,12 +20,11 @@ package api.test.rest.tsa.policy; import api.test.core.BaseStepDefinitions; import api.test.rest.RestGeneralStepDefinitions; import api.test.rest.RestSessionContainer; -import core.DataContainer; -import core.Request; -import core.Response; -import core.RestClient; +import core.*; import cucumber.api.java.en.And; +import cucumber.api.java.en.Given; import cucumber.api.java.en.When; +import exceptions.RAFException; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -41,9 +40,24 @@ 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 \\{(.*)\\}$") public void iExecuteThePolicy(String path) throws Throwable { - currentRequest.setPath(path); + currentRequest.setPath("/policy/example/" + path + "/1.0/evaluation"); Response response = RestClient.post(currentRequest); addRequest(currentRequest); addResponse(response); @@ -51,7 +65,7 @@ public class PolicyStepDefinitions extends BaseStepDefinitions { @When("^I lock the policy \\{(.*)\\}$") public void iLockThePolicy(String path) throws Throwable { - currentRequest.setPath(path); + currentRequest.setPath("/policy/example/" + path + "/1.0/lock"); Response response = RestClient.post(currentRequest); addRequest(currentRequest); addResponse(response); @@ -59,7 +73,7 @@ public class PolicyStepDefinitions extends BaseStepDefinitions { @When("^I unlock the policy \\{(.*)\\}$") public void iUnlockThePolicy(String path) throws Throwable { - currentRequest.setPath(path); + currentRequest.setPath("/policy/example/" + path + "/1.0/lock"); Response response = RestClient.delete(currentRequest); addRequest(currentRequest); addResponse(response); 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 9cf0d8f4..13373364 100644 --- a/src/test/java/api/test/rest/tsa/task/TaskStepDefinitions.java +++ b/src/test/java/api/test/rest/tsa/task/TaskStepDefinitions.java @@ -6,6 +6,7 @@ import api.test.rest.RestSessionContainer; import core.*; import cucumber.api.java.en.And; import cucumber.api.java.en.Given; +import exceptions.RAFException; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -38,4 +39,36 @@ public class TaskStepDefinitions extends BaseStepDefinitions{ addRequest(currentRequest); addResponse(response); } + + @Given("^I load the body of my Task 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 Task \\{(.*)\\}$") + public void iExecuteThePolicy(String path) throws Throwable { + currentRequest.setPath("/v1/task/" + path); + Response response = RestClient.post(currentRequest); + addRequest(currentRequest); + addResponse(response); + } + + @And("^I get the Task result with key \\{(.*)\\}$") + public void iGetTheTaskResultWithKey(String suffix) { + currentRequest.setPath("v1/taskResult/"+ getDataContainer().getObject(suffix)); + + Response response = RestClient.get(currentRequest); + addRequest(currentRequest); + addResponse(response); + } } diff --git a/src/test/resources/features/tsa/cache/v1/cache/GET.feature b/src/test/resources/features/tsa/cache/v1/cache/GET.feature index cbc8a8c3..73840699 100644 --- a/src/test/resources/features/tsa/cache/v1/cache/GET.feature +++ b/src/test/resources/features/tsa/cache/v1/cache/GET.feature @@ -31,28 +31,26 @@ Feature: API -TSA - Cache - v1/cache GET Given we are testing the TSA Cache Api Scenario: TSA - Working with Cache - Positive - Given I load the REST request {Cache.json} with profile {successful_set} - And I store key {key_positive} with value {test} in the data container - And I load object with key {key_positive} from DataContainer into currentRequest HEADER {x-cache-key} - And I send the current request as POST to endpoint {/v1/cache} + When I load the body of my Cache request with {successful_set} from {Cache.json} + And I load value {test} into current request HEADER {x-cache-key} + And I send the Cache POST request And the status code should be {201} Then I clear the request body - And I send the current request as GET to endpoint {/v1/cache} + And I send the Cache GET request Then the status code should be {200} And the field {msg} has the value {successful setting the cache} @negative Scenario: TSA - Access non existing Cache - Negative - And I store key {key_negative} with value {NEGATIVE} in the data container - And I load object with key {key_negative} from DataContainer into currentRequest HEADER {x-cache-key} - Given I send the current request as GET to endpoint {/v1/cache} + Given I load value {NEGATIVE} into current request HEADER {x-cache-key} + And I send the Cache GET request Then the status code should be {404} And the response is valid according to the {Cache_negative_schema.json} REST schema And the field {message} has the value {key not found in cache} @negative Scenario: TSA - Access Cache without header x-cache-key - Negative - Given I send the current request as GET to endpoint {/v1/cache} + Given I send the Cache GET request Then the status code should be {400} And the response is valid according to the {Cache_negative_schema.json} REST schema And the field {message} has the value {"x-cache-key" is missing from header} diff --git a/src/test/resources/features/tsa/cache/v1/cache/POST.feature b/src/test/resources/features/tsa/cache/v1/cache/POST.feature index f3758f53..a828b046 100644 --- a/src/test/resources/features/tsa/cache/v1/cache/POST.feature +++ b/src/test/resources/features/tsa/cache/v1/cache/POST.feature @@ -31,23 +31,22 @@ Feature: API -TSA - Cache - v1/cache POST Given we are testing the TSA Cache Api Scenario: TSA - Setting Cache - Positive - Given I load the REST request {Cache.json} with profile {successful_set} - And I store key {key_positive} with value {test} in the data container - And I load object with key {key_positive} from DataContainer into currentRequest HEADER {x-cache-key} - And I send the current request as POST to endpoint {/v1/cache} + When I load the body of my Cache request with {successful_set} from {Cache.json} + And I load value {test} into current request HEADER {x-cache-key} + And I send the Cache POST request And the status code should be {201} @negative Scenario: TSA - Setting Cache with missing header - x-cache-key - Negative - Given I load the REST request {Cache.json} with profile {missing_body} - And I send the current request as POST to endpoint {/v1/cache} + When I load the body of my Cache request with {missing_body} from {Cache.json} + And I send the Cache POST request Then the status code should be {400} And the response is valid according to the {Cache_negative_schema.json} REST schema And the field {message} has the value {"x-cache-key" is missing from header} @negative Scenario: TSA - Setting Cache with missing body - Negative - And I send the current request as POST to endpoint {/v1/cache} + When I send the Cache POST request Then the status code should be {400} And the response is valid according to the {Cache_negative_schema.json} REST schema And the field {message} has the value {missing required payload} 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 2e6ce350..80301a28 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 @@ -31,47 +31,46 @@ Feature: API -TSA - Policy - :group/:name/:version/evaluation POST Given we are testing the TSA Policy Api Scenario: TSA - Evaluate policy synchronously - Positive - Given I load the REST request {Policy.json} with profile {successful_evaluate} - And I execute the policy {/policy/example/test/1.0/evaluation} + When I load the body of my Policy request with {successful_message} from {Policy.json} + And I execute the policy {test} 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} - @bug-policy-35 Scenario: TSA - DID resolution - Positive - Given I load the REST request {Policy.json} with profile {didResolve_evaluate} - And I execute the policy {/policy/example/resolve/1.0/evaluation} + When I load the body of my Policy request with {did_key} from {Policy.json} + And I execute the policy {resolve} 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} @negative Scenario: TSA - Evaluate policy with incorrect value - Negative - Given I load the REST request {Policy.json} with profile {unsuccessful_evaluate} - And I execute the policy {/policy/example/test/1.0/evaluation} + When I load the body of my Policy request with {incorrect_message} from {Policy.json} + And I execute the policy {test} 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 {/policy/example/test/1.0/evaluation} + Given I execute the policy {test} 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 missing policy - Negative - Then I load the REST request {Policy.json} with profile {successful_evaluate} - Given I execute the policy {/policy/testable/test0/1.0/evaluation} + When I load the body of my Policy request with {successful_message} from {Policy.json} + Given I execute the policy {missing} 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} - @negative @bug-policy-35 + @negative Scenario: TSA - DID resolution with incorrect did - Negative - Then I load the REST request {Policy.json} with profile {didResolve_missing_method} - And I execute the policy {/policy/example/resolve/1.0/evaluation} + When I load the body of my Policy request with {did_missing_method} from {Policy.json} + And I execute the policy {resolve} 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 98592b36..bcb817a2 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,12 +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 {/policy/example/test/1.0/lock} - And I unlock the policy {/policy/example/test/1.0/lock} + When I lock the policy {test} + And I unlock the policy {test} 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} + Then the status code should be {200} @negative Scenario: TSA - Unlock none existing policy - Negative - When I unlock the policy {/policy/example/non_existing/1.0/lock} + When I unlock the policy {non_existing} 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 5aea20e1..6a0efd90 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,20 +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 {/policy/example/test/1.0/lock} + When I lock the policy {test} Then the status code should be {200} - When I load the REST request {Policy.json} with profile {successful_evaluate} - And I execute the policy {/policy/example/test/1.0/evaluation} + Then I load the body of my Policy request with {successful_message} from {Policy.json} + And I execute the policy {test} Then the status code should be {403} - And I lock the policy {/policy/example/test/1.0/lock} - And the status code should be {403} - And the field {message} has the value {policy is already locked} - And I unlock the policy {/policy/example/test/1.0/lock} + And the field {message} has the value {error evaluating policy} + And I unlock the policy {test} @negative Scenario: TSA - Lock already locked policy - Negative - When I lock the policy {/policy/example/test/1.0/lock} - And I lock the policy {/policy/example/test/1.0/lock} + When I lock the policy {test} + And I lock the policy {test} And the status code should be {403} And the field {message} has the value {policy is already locked} - And I unlock the policy {/policy/example/test/1.0/lock} + And I unlock the policy {test} diff --git a/src/test/resources/features/tsa/task/v1/task/{taskName}/GET.feature b/src/test/resources/features/tsa/task/v1/task/{taskName}/GET.feature index 1ee88762..926774fa 100644 --- a/src/test/resources/features/tsa/task/v1/task/{taskName}/GET.feature +++ b/src/test/resources/features/tsa/task/v1/task/{taskName}/GET.feature @@ -30,17 +30,16 @@ Feature: API -TSA - Task - v1/task GET Background: Given we are testing the TSA Task Api - @bug-policy-35 Scenario: TSA - Executing Task with DID resolver and Evaluate the Cache - Positive - Given I load the REST request {Policy.json} with profile {didResolve_evaluate} - And I send the current request as POST to endpoint {/v1/task/didResolve} + When I load the body of my Task request with {did_key} from {Policy.json} + And I execute the Task {didResolve} Then the status code should be {200} And the response is valid according to the {Task_Execute_schema.json} REST schema # Test the Task Result Then I get the value of {taskID} from the last response and store it in the DataContainer with key {taskID} Then I clear the request body And I wait for {2000} mseconds - And I send the current request to endpoint {v1/taskResult} with container value{taskID} + And I get the Task result with key {taskID} Then the status code should be {200} And the response is valid according to the {Task_ExecuteDID_schema.json} REST schema And the field {data.didDocument.id} has the value {did:key:z6Mkfriq1MqLBoPWecGoDLjguo1sB9brj6wT3qZ5BxkKpuP6} diff --git a/src/test/resources/features/tsa/task/v1/task/{taskName}/POST.feature b/src/test/resources/features/tsa/task/v1/task/{taskName}/POST.feature index bd1a4e35..c96d97c5 100644 --- a/src/test/resources/features/tsa/task/v1/task/{taskName}/POST.feature +++ b/src/test/resources/features/tsa/task/v1/task/{taskName}/POST.feature @@ -30,23 +30,22 @@ Feature: API -TSA - Task - v1/task POST Background: Given we are testing the TSA Task Api - @bug-policy-35 Scenario: TSA - Executing Task with DID resolver - Positive - Then I load the REST request {Policy.json} with profile {didResolve_evaluate} - And I send the current request as POST to endpoint {/v1/task/didResolve} + When I load the body of my Task request with {did_key} from {Policy.json} + And I execute the Task {didResolve} Then the status code should be {200} And the response is valid according to the {Task_Execute_schema.json} REST schema Then I get the value of {taskID} from the last response and store it in the DataContainer with key {taskID} Then I clear the request body And I wait for {2000} mseconds - And I send the current request to endpoint {v1/taskResult} with container value{taskID} + And I get the Task result with key {taskID} Then the status code should be {200} And the response is valid according to the {Task_ExecuteDID_schema.json} REST schema And the field {data.didDocument.id} has the value {did:key:z6Mkfriq1MqLBoPWecGoDLjguo1sB9brj6wT3qZ5BxkKpuP6} @negative Scenario: TSA - Executing Task with non existing task template - Negative - Then I load the REST request {Policy.json} with profile {didResolve_evaluate} - And I send the current request as POST to endpoint {/v1/task/resolve} + When I load the body of my Task request with {did_key} from {Policy.json} + And I execute the Task {non_existing} Then the response is valid according to the {Policy_Evaluate_negative_schema.json} REST schema And the field {message} has the value {task template not found} -- GitLab