2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
25 import com.att.research.xacml.std.annotations.XACMLAction;
26 import com.att.research.xacml.std.annotations.XACMLRequest;
27 import com.att.research.xacml.std.annotations.XACMLResource;
28 import com.att.research.xacml.std.annotations.XACMLSubject;
30 import cucumber.api.java.en.Given;
31 import cucumber.api.java.en.Then;
32 import cucumber.api.java.en.When;
34 import org.junit.ClassRule;
35 import org.junit.rules.TemporaryFolder;
37 public class Stepdefs {
41 private static final Logger logger = LoggerFactory.getLogger(Stepdefs.class);
43 public static OnapXacmlPdpEngine onapPdpEngine;
44 public static Properties properties;
45 public static Map<String, Object> tcaPolicy;
46 public static Request request;
47 public static File pathProperties;
48 public static File pathRootPolicy;
51 * Temporary folder where we will store newly created policies.
54 public TemporaryFolder policyFolder = new TemporaryFolder();
57 * This is a simple annotation class to simulate
60 @XACMLRequest(ReturnPolicyIdList = true)
61 public class MyXacmlRequest {
63 @XACMLSubject(includeInResults = true)
64 String onapName = "DCAE";
66 @XACMLResource(includeInResults = true)
67 String resource = "onap.policies.Monitoring";
70 String action = "configure";
76 @Given("Initialization")
77 public void initialization() {
80 // Everything initializes upon startup
82 assertThatCode(() -> {
84 // Assume XACML REST Controller loads PDP engine
86 onapPdpEngine = new OnapXacmlPdpEngine();
88 // Come up with defaults
90 File path = Paths.get("src/test/resources").toFile();
92 // try (InputStream is = new FileInputStream("src/test/resources/xacml.properties")) {
93 // properties = new Properties();
94 // properties.load(is);
95 // onapPdpEngine.initializeEngine(properties);
97 onapPdpEngine.initialize(path.toPath());
99 // Store the properties in new path
101 // JUNIT IS CRASHING - THE TEMP FOLDER NOT CREATED -->
102 //pathProperties = policyFolder.newFile("xacml.properties");
104 // Store the root policies
106 for (String rootPolicyId : XACMLProperties.getRootPolicyIDs(properties)) {
107 logger.debug("Root policy id: " + rootPolicyId);
110 }).doesNotThrowAnyException();
117 @When("Decision Requested")
118 public void decision_Requested() {
121 // Simulate a request coming in from Xacml REST server
123 assertThatCode(() -> {
124 request = RequestParser.parseRequest(new MyXacmlRequest());
125 }).doesNotThrowAnyException();
132 @Then("Decision Permit {int} Obligations")
133 public void decision_Permit_Obligations(Integer int1) {
135 Response response = onapPdpEngine.decision(request);
136 for (Result result : response.getResults()) {
137 logger.debug(result.getDecision().toString());
138 assertEquals(Decision.PERMIT, result.getDecision());
139 assertThat(result.getObligations().size()).isEqualTo(int1);
147 @When("The application gets new Tosca Policy")
148 public void the_application_gets_new_Tosca_Policy() {
151 // The Xacml PDP REST controller Would receive this from the PAP
153 // And then parse it looking for Policy Types
155 assertThatCode(() -> {
156 try (InputStream is = new FileInputStream("src/test/resources/vDNS.policy.input.yaml")) {
157 Yaml yaml = new Yaml();
158 tcaPolicy = yaml.load(is);
160 // Do we test iterating and determining if supported?
164 }).doesNotThrowAnyException();
172 public void load_Policy() {
174 assertThatCode(() -> {
178 List<PolicyType> convertedPolicies = onapPdpEngine.convertPolicies(tcaPolicy);
180 // Store these in temporary folder
183 List<Path> newReferencedPolicies = new ArrayList<>();
184 for (PolicyType convertedPolicy : convertedPolicies) {
186 // I don't think we should use the policy id as the filename - there could
187 // possibly be duplicates. eg. Not guaranteed to be unique.
189 File file = policyFolder.newFile("policy." + id + convertedPolicy.getPolicyId() + ".xml");
190 logger.info("Creating Policy {}", file.getAbsolutePath());
191 Path path = XACMLPolicyWriter.writePolicyFile(file.toPath(), convertedPolicy);
193 // Add it to our list
195 newReferencedPolicies.add(path);
198 // Now updated the properties
200 Path[] args = new Path[newReferencedPolicies.size()];
201 newReferencedPolicies.toArray(args);
202 XACMLProperties.setXacmlReferencedProperties(properties, args);
204 // Reload the PDP engine
206 onapPdpEngine.initializeEngine(properties);
207 }).doesNotThrowAnyException();
214 @Then("Save Configuration")
215 public void save_Configuration() {
217 assertThatCode(() -> {
219 // Save the configuration
221 onapPdpEngine.storeXacmlProperties(pathProperties.getAbsolutePath());
222 }).doesNotThrowAnyException();