Skip to content
Snippets Groups Projects
Unverified Commit 70b5b75a authored by Georgi Michev's avatar Georgi Michev
Browse files

manage mongo documents

parent 39c42c85
Branches
Tags
1 merge request!15remove test results, initiate evaluate policy sync
...@@ -79,7 +79,6 @@ public class RestGeneralStepDefinitions extends BaseStepDefinitions { ...@@ -79,7 +79,6 @@ public class RestGeneralStepDefinitions extends BaseStepDefinitions {
profileName) profileName)
, RestGeneralStepDefinitions.class); , RestGeneralStepDefinitions.class);
} }
} }
@Then("^the response is valid according to the \\{(.*?)\\} REST schema$") @Then("^the response is valid according to the \\{(.*?)\\} REST schema$")
......
...@@ -20,10 +20,12 @@ package api.test.rest.tsa.policy; ...@@ -20,10 +20,12 @@ package api.test.rest.tsa.policy;
import api.test.core.BaseStepDefinitions; import api.test.core.BaseStepDefinitions;
import api.test.rest.RestGeneralStepDefinitions; import api.test.rest.RestGeneralStepDefinitions;
import api.test.rest.RestSessionContainer; import api.test.rest.RestSessionContainer;
import com.mongodb.MongoException;
import com.mongodb.client.MongoClient; import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients; import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase; import com.mongodb.client.MongoDatabase;
import com.mongodb.client.result.DeleteResult;
import core.DataContainer; import core.DataContainer;
import core.Request; import core.Request;
import core.Response; import core.Response;
...@@ -36,9 +38,11 @@ import cucumber.api.java8.Th; ...@@ -36,9 +38,11 @@ import cucumber.api.java8.Th;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import org.bson.Document; import org.bson.Document;
import org.bson.conversions.Bson;
import org.bson.types.ObjectId; import org.bson.types.ObjectId;
import static api.test.core.GeneralStepDefinitions.the_field_has_the_value_; import static api.test.core.GeneralStepDefinitions.the_field_has_the_value_;
import static com.mongodb.client.model.Filters.eq;
public class PolicyStepDefinitions extends BaseStepDefinitions { public class PolicyStepDefinitions extends BaseStepDefinitions {
...@@ -52,19 +56,19 @@ public class PolicyStepDefinitions extends BaseStepDefinitions { ...@@ -52,19 +56,19 @@ public class PolicyStepDefinitions extends BaseStepDefinitions {
this.currentRequest = currentRequest; this.currentRequest = currentRequest;
} }
@When("I upload the policy to repository") @When("^I upload a policy \\{(.*)\\} to repository$")
public void iUploadThePolicyToRepository() throws Throwable { public void iUploadThePolicyToRepository(String policy) throws Throwable {
// MongoClient client = MongoClients.create("mongodb://root:root@localhost:27017"); MongoClient client = MongoClients.create("mongodb://root:root@localhost:27017");
// MongoDatabase database = client.getDatabase("policy"); MongoDatabase database = client.getDatabase("policy");
// MongoCollection<Document> policies = database.getCollection("policies"); MongoCollection<Document> policies = database.getCollection("policies");
// Document test = new Document("_id", new ObjectId()) Document test = new Document("_id", new ObjectId())
// .append("filename", "example_4.0.rego") .append("filename", "example_2.0.rego")
// .append("name", "test") .append("name", policy)
// .append("group", "testable") .append("group", "testable")
// .append("version", "4.0") .append("version", "2.0")
// .append("locked", false) .append("locked", false)
// .append("rego", "package testable.test\n\t\t\n allow {\n 1 == 1\n }\n\n taskID := \"0123456789abcdef\"\n "); .append("rego", "package testable."+ policy +"\n\t\t\n allow {\n 1 == 1\n }\n\n taskID := \"0123456789abcdef\"\n ");
// policies.insertOne(test); policies.insertOne(test);
} }
@And("set the policy to productive") @And("set the policy to productive")
...@@ -102,4 +106,18 @@ public class PolicyStepDefinitions extends BaseStepDefinitions { ...@@ -102,4 +106,18 @@ public class PolicyStepDefinitions extends BaseStepDefinitions {
addRequest(currentRequest); addRequest(currentRequest);
addResponse(response); 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
...@@ -23,15 +23,19 @@ Feature: As user ...@@ -23,15 +23,19 @@ Feature: As user
Background: Background:
Given we are testing the TSA Policy Api 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 Scenario: TSA - Unlock policy - Positive
When I lock the policy {/testable/test/3.0/lock} Given I upload a policy {test4} to repository
And I unlock the policy {/testable/test/3.0/lock} 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 the status code should be {200}
And delete policy {test4} from repository
Scenario: TSA - Unlock already unlocked policy - Negative Scenario: TSA - Unlock already unlocked policy - Negative
When I unlock the policy {/testable/test/4.0/lock} Given I upload a policy {test5} to repository
And I unlock the policy {/testable/test/4.0/lock} 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 status code should be {403}
And the field {message} has the value {policy is unlocked} And the field {message} has the value {policy is unlocked}
And delete policy {test5} from repository
...@@ -28,36 +28,39 @@ Feature: As user ...@@ -28,36 +28,39 @@ Feature: As user
Background: Background:
Given we are testing the TSA Policy Api 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 Scenario: TSA - Evaluate policy synchronously - Positive
Given I upload a policy {test1} to repository
And set the policy to productive And set the policy to productive
And the policy successfully uploaded to the system And the policy successfully uploaded to the system
Then I load the REST request {Policy.json} with profile {successful_evaluate} 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} Then the status code should be {200}
And the field {allow} has the value {true} And the field {allow} has the value {true}
And the field {taskID} is present and not empty And the field {taskID} is present and not empty
And I clear the request body And delete policy {test1} from repository
@lock @lock
Scenario: TSA - Lock policy - Positive 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} Then the status code should be {200}
When I load the REST request {Policy.json} with profile {successful_evaluate} 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} 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 status code should be {403}
And the field {message} has the value {policy is already locked} And the field {message} has the value {policy is already locked}
And I unlock the policy {/testable/test/1.0/lock} And I unlock the policy {/testable/test2/2.0/lock}
And I clear the request body And delete policy {test2} from repository
@lock @lock
Scenario: TSA - Lock already locked policy - Negative Scenario: TSA - Lock already locked policy - Negative
When I lock the policy {/testable/test/2.0/lock} Given I upload a policy {test3} to repository
And I lock the policy {/testable/test/2.0/lock} 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 status code should be {403}
And the field {message} has the value {policy is already locked} And the field {message} has the value {policy is already locked}
And I unlock the policy {/testable/test/2.0/lock} And I unlock the policy {/testable/test3/2.0/lock}
And I clear the request body And delete policy {test3} from repository
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment