From 70b5b75a7ea858f63f2df062cdb4bc5ad0bb8223 Mon Sep 17 00:00:00 2001 From: "georgi.michev" <georgi.michev@vereign.com> Date: Wed, 8 Jun 2022 17:54:09 +0300 Subject: [PATCH] manage mongo documents Signed-off-by: georgi.michev <georgi.michev@vereign.com> --- .../test/rest/RestGeneralStepDefinitions.java | 1 - .../tsa/policy/PolicyStepDefinitions.java | 44 +++++++++++++------ .../features/tsa/policy/DELETE.feature | 14 +++--- .../features/tsa/policy/POST.feature | 27 +++++++----- 4 files changed, 55 insertions(+), 31 deletions(-) diff --git a/src/test/java/api/test/rest/RestGeneralStepDefinitions.java b/src/test/java/api/test/rest/RestGeneralStepDefinitions.java index 8db36496..ee4e6ada 100644 --- a/src/test/java/api/test/rest/RestGeneralStepDefinitions.java +++ b/src/test/java/api/test/rest/RestGeneralStepDefinitions.java @@ -79,7 +79,6 @@ public class RestGeneralStepDefinitions extends BaseStepDefinitions { profileName) , RestGeneralStepDefinitions.class); } - } @Then("^the response is valid according to the \\{(.*?)\\} REST schema$") 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 c2ab4532..ce0af623 100644 --- a/src/test/java/api/test/rest/tsa/policy/PolicyStepDefinitions.java +++ b/src/test/java/api/test/rest/tsa/policy/PolicyStepDefinitions.java @@ -20,10 +20,12 @@ package api.test.rest.tsa.policy; import api.test.core.BaseStepDefinitions; import api.test.rest.RestGeneralStepDefinitions; import api.test.rest.RestSessionContainer; +import com.mongodb.MongoException; import com.mongodb.client.MongoClient; import com.mongodb.client.MongoClients; import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoDatabase; +import com.mongodb.client.result.DeleteResult; import core.DataContainer; import core.Request; import core.Response; @@ -36,9 +38,11 @@ import cucumber.api.java8.Th; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.bson.Document; +import org.bson.conversions.Bson; import org.bson.types.ObjectId; import static api.test.core.GeneralStepDefinitions.the_field_has_the_value_; +import static com.mongodb.client.model.Filters.eq; public class PolicyStepDefinitions extends BaseStepDefinitions { @@ -52,19 +56,19 @@ public class PolicyStepDefinitions extends BaseStepDefinitions { this.currentRequest = currentRequest; } - @When("I upload the policy to repository") - public void iUploadThePolicyToRepository() throws Throwable { -// MongoClient client = MongoClients.create("mongodb://root:root@localhost:27017"); -// MongoDatabase database = client.getDatabase("policy"); -// MongoCollection<Document> policies = database.getCollection("policies"); -// Document test = new Document("_id", new ObjectId()) -// .append("filename", "example_4.0.rego") -// .append("name", "test") -// .append("group", "testable") -// .append("version", "4.0") -// .append("locked", false) -// .append("rego", "package testable.test\n\t\t\n allow {\n 1 == 1\n }\n\n taskID := \"0123456789abcdef\"\n "); -// policies.insertOne(test); + @When("^I upload a policy \\{(.*)\\} to repository$") + public void iUploadThePolicyToRepository(String policy) throws Throwable { + MongoClient client = MongoClients.create("mongodb://root:root@localhost:27017"); + MongoDatabase database = client.getDatabase("policy"); + MongoCollection<Document> policies = database.getCollection("policies"); + Document test = new Document("_id", new ObjectId()) + .append("filename", "example_2.0.rego") + .append("name", policy) + .append("group", "testable") + .append("version", "2.0") + .append("locked", false) + .append("rego", "package testable."+ policy +"\n\t\t\n allow {\n 1 == 1\n }\n\n taskID := \"0123456789abcdef\"\n "); + policies.insertOne(test); } @And("set the policy to productive") @@ -102,4 +106,18 @@ public class PolicyStepDefinitions extends BaseStepDefinitions { addRequest(currentRequest); addResponse(response); } + + @And("^delete policy \\{(.*)\\} from repository$") + public void deletePolicyTestFromRepository(String policy) { + MongoClient client = MongoClients.create("mongodb://root:root@localhost:27017"); + MongoDatabase database = client.getDatabase("policy"); + MongoCollection<Document> policies = database.getCollection("policies"); + Bson query = eq("name", policy); + try { + DeleteResult result = policies.deleteMany(query); + System.out.println("Deleted document count: " + result.getDeletedCount()); + } catch (MongoException me) { + System.err.println("Unable to delete due to an error: " + me); + } + } } \ No newline at end of file diff --git a/src/test/resources/features/tsa/policy/DELETE.feature b/src/test/resources/features/tsa/policy/DELETE.feature index 255cd41d..53af9ca3 100644 --- a/src/test/resources/features/tsa/policy/DELETE.feature +++ b/src/test/resources/features/tsa/policy/DELETE.feature @@ -23,15 +23,19 @@ Feature: As user Background: Given we are testing the TSA Policy Api - And I upload the policy to repository +# And I upload the policy to repository Scenario: TSA - Unlock policy - Positive - When I lock the policy {/testable/test/3.0/lock} - And I unlock the policy {/testable/test/3.0/lock} + Given I upload a policy {test4} to repository + When I lock the policy {/testable/test4/2.0/lock} + And I unlock the policy {/testable/test4/2.0/lock} And the status code should be {200} + And delete policy {test4} from repository Scenario: TSA - Unlock already unlocked policy - Negative - When I unlock the policy {/testable/test/4.0/lock} - And I unlock the policy {/testable/test/4.0/lock} + Given I upload a policy {test5} to repository + When I unlock the policy {/testable/test5/2.0/lock} + And I unlock the policy {/testable/test5/2.0/lock} And the status code should be {403} And the field {message} has the value {policy is unlocked} + And delete policy {test5} from repository diff --git a/src/test/resources/features/tsa/policy/POST.feature b/src/test/resources/features/tsa/policy/POST.feature index 0809c6c2..de8d42ca 100644 --- a/src/test/resources/features/tsa/policy/POST.feature +++ b/src/test/resources/features/tsa/policy/POST.feature @@ -28,36 +28,39 @@ Feature: As user Background: Given we are testing the TSA Policy Api - And I upload the policy to repository +# And I upload the policy to repository Scenario: TSA - Evaluate policy synchronously - Positive + Given I upload a policy {test1} to repository And set the policy to productive And the policy successfully uploaded to the system Then I load the REST request {Policy.json} with profile {successful_evaluate} - And I execute the policy {/example/example/1.0/evaluation} + And I execute the policy {/testable/test1/2.0/evaluation} Then the status code should be {200} And the field {allow} has the value {true} And the field {taskID} is present and not empty - And I clear the request body + And delete policy {test1} from repository @lock Scenario: TSA - Lock policy - Positive - When I lock the policy {/testable/test/1.0/lock} + Given I upload a policy {test2} to repository + When I lock the policy {/testable/test2/2.0/lock} Then the status code should be {200} When I load the REST request {Policy.json} with profile {successful_evaluate} - And I execute the policy {/testable/test/1.0/evaluation} + And I execute the policy {/testable/test2/2.0/evaluation} Then the status code should be {403} - And I lock the policy {/testable/test/1.0/lock} + And I lock the policy {/testable/test2/2.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 {/testable/test/1.0/lock} - And I clear the request body + And I unlock the policy {/testable/test2/2.0/lock} + And delete policy {test2} from repository @lock Scenario: TSA - Lock already locked policy - Negative - When I lock the policy {/testable/test/2.0/lock} - And I lock the policy {/testable/test/2.0/lock} + Given I upload a policy {test3} to repository + When I lock the policy {/testable/test3/2.0/lock} + And I lock the policy {/testable/test3/2.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 {/testable/test/2.0/lock} - And I clear the request body + And I unlock the policy {/testable/test3/2.0/lock} + And delete policy {test3} from repository -- GitLab