From b314f86d881d94116125f090994b485915b55eb4 Mon Sep 17 00:00:00 2001 From: "georgi.michev" <georgi.michev@vereign.com> Date: Tue, 7 Jun 2022 22:29:00 +0300 Subject: [PATCH] refactor policy Signed-off-by: georgi.michev <georgi.michev@vereign.com> --- build.gradle | 1 + devRun.sh | 15 +--- src/main/resources/REST/json/Policy.json | 5 ++ .../test/rest/RestGeneralStepDefinitions.java | 40 ++------- .../tsa/policy/PolicyStepDefinitions.java | 82 +++++++++++++++++++ .../{ => tsa}/policy/api/POST.feature | 0 .../policy/api/evaluatePolicySync.feature | 19 +++-- 7 files changed, 110 insertions(+), 52 deletions(-) create mode 100644 src/main/resources/REST/json/Policy.json create mode 100644 src/test/java/api/test/rest/tsa/policy/PolicyStepDefinitions.java rename src/test/resources/features/{ => tsa}/policy/api/POST.feature (100%) rename src/test/resources/features/{ => tsa}/policy/api/evaluatePolicySync.feature (67%) diff --git a/build.gradle b/build.gradle index 3c95fb52..64944b04 100644 --- a/build.gradle +++ b/build.gradle @@ -85,4 +85,5 @@ dependencies { implementation group: 'net.sourceforge.tess4j', name: 'tess4j', version: '4.4.1' implementation 'io.github.prashant-ramcharan:courgette-jvm:3.+' + implementation 'org.mongodb:mongodb-driver-sync:4.0.5' } \ No newline at end of file diff --git a/devRun.sh b/devRun.sh index 20d7bdca..90507307 100755 --- a/devRun.sh +++ b/devRun.sh @@ -1,21 +1,10 @@ #!/bin/bash # This is the run command -# ./gradlew regressionSuite \ - # -PbaseUrl=https://batch-service.rse-test.k8s.vereign.com \ - # -PoutlookManifest=https://azure-dev.vrgnservices.com/outlookaddin/manifest.xml \ - # -PprojectLocation=/home/michev/IdeaProjects/ \ - # -Dcourgette.threads=10 \ - # -Dcourgette.runLevel=Scenario \ - # -Dcourgette.rerunFailedScenarios=false \ - # -Dcourgette.rerunAttempts=1 \ - # -Dcucumber.tags="@batch, not @wip" \ - # -PdeviceType='PC' - ./gradlew regressionSuite \ - -PbaseUrl=https://TestEnv \ + -PbaseUrl="http://localhost:8081" \ -Dcucumber.tags="@rest, ~@wip" \ -Dcourgette.threads=10 \ -Dcourgette.runLevel=Scenario \ -Dcourgette.rerunFailedScenarios=false \ - -Dcourgette.rerunAttempts=1 + -Dcourgette.rerunAttempts=1 \ No newline at end of file diff --git a/src/main/resources/REST/json/Policy.json b/src/main/resources/REST/json/Policy.json new file mode 100644 index 00000000..676f9090 --- /dev/null +++ b/src/main/resources/REST/json/Policy.json @@ -0,0 +1,5 @@ +{ + "successful_evaluate": { + "message": "hello world" + } +} \ No newline at end of file diff --git a/src/test/java/api/test/rest/RestGeneralStepDefinitions.java b/src/test/java/api/test/rest/RestGeneralStepDefinitions.java index a5bc5068..8db36496 100644 --- a/src/test/java/api/test/rest/RestGeneralStepDefinitions.java +++ b/src/test/java/api/test/rest/RestGeneralStepDefinitions.java @@ -28,25 +28,28 @@ import core.*; import cucumber.api.java.en.And; import cucumber.api.java.en.Given; import cucumber.api.java.en.Then; -import cucumber.api.java.en.When; import exceptions.RAFException; import org.apache.commons.lang3.RandomStringUtils; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; + +import java.net.UnknownHostException; import java.security.KeyPair; import java.util.HashMap; import java.util.Map; -import static org.junit.Assert.assertEquals; + +import static org.junit.Assert.*; import static utils.RsaGenerator.encrypt; import static utils.RsaGenerator.generateKeyPair; + public class RestGeneralStepDefinitions extends BaseStepDefinitions { private static final Logger logger = LogManager.getLogger(RestGeneralStepDefinitions.class.getSimpleName()); RestSessionContainer restSessionContainer; Request currentRequest; - public RestGeneralStepDefinitions(RestSessionContainer restSessionContainer, Request currentRequest, DataContainer dataContainer) { + public RestGeneralStepDefinitions(RestSessionContainer restSessionContainer, Request currentRequest, DataContainer dataContainer) throws UnknownHostException { super(dataContainer); this.restSessionContainer = restSessionContainer; this.currentRequest = currentRequest; @@ -187,33 +190,4 @@ public class RestGeneralStepDefinitions extends BaseStepDefinitions { String cipherText = encrypt(message, pair.getPublic()); currentRequest.getHeaders().put("publicKey", cipherText); } - - @When("I upload the policy to repository") - public void iUploadThePolicyToRepository() { - - } - - @And("set the policy to productive") - public void setThePolicyToProductive() { - - } - - @And("the policy successfully uploaded to the system") - public void thePolicySuccessfullyUploadedToTheSystem() { - - } - - @When("I execute the policy") - public void iExecuteThePolicy() { - - } - - @Then("I get successful response") - public void iGetSuccessfulResponse() { - - } - - @And("response contains the result of execution of the policy") - public void responseContainsTheResultOfExecutionOfThePolicy() { - } -} \ No newline at end of file +} diff --git a/src/test/java/api/test/rest/tsa/policy/PolicyStepDefinitions.java b/src/test/java/api/test/rest/tsa/policy/PolicyStepDefinitions.java new file mode 100644 index 00000000..8846036e --- /dev/null +++ b/src/test/java/api/test/rest/tsa/policy/PolicyStepDefinitions.java @@ -0,0 +1,82 @@ +/* +Copyright (c) 2018 Vereign AG [https://www.vereign.com] + +This is free software: you can redistribute it and/or modify +it under the terms of the GNU Affero General Public License as +published by the Free Software Foundation, either version 3 of the +License, or (at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Affero General Public License for more details. + +You should have received a copy of the GNU Affero General Public License +along with this program. If not, see <http://www.gnu.org/licenses/>. +*/ + +package api.test.rest.tsa.policy; + +import api.test.core.BaseStepDefinitions; +import api.test.rest.RestGeneralStepDefinitions; +import api.test.rest.RestSessionContainer; +import com.mongodb.client.MongoClient; +import com.mongodb.client.MongoClients; +import com.mongodb.client.MongoCollection; +import com.mongodb.client.MongoDatabase; +import core.DataContainer; +import core.Request; +import core.Response; +import core.RestClient; +import cucumber.api.java.en.And; +import cucumber.api.java.en.When; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.bson.Document; +import org.bson.types.ObjectId; + +public class PolicyStepDefinitions extends BaseStepDefinitions { + + private static final Logger logger = LogManager.getLogger(RestGeneralStepDefinitions.class.getSimpleName()); + RestSessionContainer restSessionContainer; + Request currentRequest; + + public PolicyStepDefinitions(RestSessionContainer restSessionContainer, Request currentRequest, DataContainer dataContainer) { + super(dataContainer); + this.restSessionContainer = restSessionContainer; + 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_3.0.rego") + .append("name", "test") + .append("group", "testable") + .append("version", "1.0") + .append("locked", false) + .append("rego", "package test.testable allow {1 == 1} taskID := \"0001234567abcdef\""); + policies.insertOne(test); + } + + @And("set the policy to productive") + public void setThePolicyToProductive() throws Throwable { + + } + + @And("the policy successfully uploaded to the system") + public void thePolicySuccessfullyUploadedToTheSystem() throws Throwable { + + } + + @And("^I execute the policy \\{(.*)\\}$") + public void iExecuteThePolicy(String path) throws Throwable { + currentRequest.setPath(path); + Response response = RestClient.post(currentRequest); + addRequest(currentRequest); + addResponse(response); + } +} \ No newline at end of file diff --git a/src/test/resources/features/policy/api/POST.feature b/src/test/resources/features/tsa/policy/api/POST.feature similarity index 100% rename from src/test/resources/features/policy/api/POST.feature rename to src/test/resources/features/tsa/policy/api/POST.feature diff --git a/src/test/resources/features/policy/api/evaluatePolicySync.feature b/src/test/resources/features/tsa/policy/api/evaluatePolicySync.feature similarity index 67% rename from src/test/resources/features/policy/api/evaluatePolicySync.feature rename to src/test/resources/features/tsa/policy/api/evaluatePolicySync.feature index b617de4c..d10f9737 100644 --- a/src/test/resources/features/policy/api/evaluatePolicySync.feature +++ b/src/test/resources/features/tsa/policy/api/evaluatePolicySync.feature @@ -13,10 +13,10 @@ #You should have received a copy of the GNU Affero General Public License #along with this program. If not, see <http://www.gnu.org/licenses/>. -#http://localhost/policy +#http://localhost:8081/policy #Author: Georgi Michev georgi.michev@vereign.com -@rest @all +@rest @all @tsa @sync Feature: As user I want to evaluate the policy So I am able to execute it in the future @@ -24,13 +24,20 @@ Feature: As user Background: Given we are testing the TSA Policy Api - Scenario: Execute the policy + @policy + Scenario: TSA - Evaluate policy synchronously - Positive When I upload the policy to repository And set the policy to productive And the policy successfully uploaded to the system - When I execute the policy - Then I get successful response - And response contains the result of execution of the policy + # When I execute the policy + When I load the REST request {Policy.json} with profile {successful_evaluate} + And I execute the policy {/example/example/1.0/evaluation} + # Then I get successful response + Then the status code should be {200} + # And response contains the result of execution of the policy + And the field {allow} has the value {true} + And the field {taskID} is present and not empty + And I clear the request body #Acceptance criteria: #- HTTP endpoint to evaluate the policy -- GitLab