xcaml-pdp sonar issue fixes
[policy/xacml-pdp.git] / applications / monitoring / src / test / java / cucumber / Stepdefs.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package cucumber;
24
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;
29
30 import io.cucumber.java.en.Given;
31 import io.cucumber.java.en.Then;
32 import io.cucumber.java.en.When;
33
34 import org.junit.ClassRule;
35 import org.junit.rules.TemporaryFolder;
36
37 public class Stepdefs {
38
39     /*
40
41     private static final Logger logger = LoggerFactory.getLogger(Stepdefs.class);
42
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;
49
50     /**
51      * Temporary folder where we will store newly created policies.
52      */
53     @ClassRule
54     public TemporaryFolder policyFolder = new TemporaryFolder();
55
56     /**
57      * This is a simple annotation class to simulate
58      * requests coming in.
59      */
60     @XACMLRequest(ReturnPolicyIdList = true)
61     public class MyXacmlRequest {
62
63         @XACMLSubject(includeInResults = true)
64         String onapName = "DCAE";
65
66         @XACMLResource(includeInResults = true)
67         String resource = "onap.policies.Monitoring";
68
69         @XACMLAction()
70         String action = "configure";
71     }
72
73     /**
74      * Initialization.
75      */
76     @Given("Initialization")
77     public void initialization() {
78         /*
79         //
80         // Everything initializes upon startup
81         //
82         assertThatCode(() -> {
83             //
84             // Assume XACML REST Controller loads PDP engine
85             //
86             onapPdpEngine = new OnapXacmlPdpEngine();
87             //
88             // Come up with defaults
89             //
90             File path = Paths.get("src/test/resources").toFile();
91             /*
92         //    try (InputStream is = new FileInputStream("src/test/resources/xacml.properties")) {
93       //          properties = new Properties();
94     //            properties.load(is);
95   //              onapPdpEngine.initializeEngine(properties);
96 //            }
97             onapPdpEngine.initialize(path.toPath());
98             //
99             // Store the properties in new path
100             //
101             // JUNIT IS CRASHING - THE TEMP FOLDER NOT CREATED -->
102             //pathProperties = policyFolder.newFile("xacml.properties");
103             //
104             // Store the root policies
105             //
106             for (String rootPolicyId : XACMLProperties.getRootPolicyIDs(properties)) {
107                 logger.debug("Root policy id: " + rootPolicyId);
108             }
109
110         }).doesNotThrowAnyException();
111         */
112     }
113
114     /**
115      * Initialization.
116      */
117     @When("Decision Requested")
118     public void decision_Requested() {
119         /*
120         //
121         // Simulate a request coming in from Xacml REST server
122         //
123         assertThatCode(() -> {
124             request = RequestParser.parseRequest(new MyXacmlRequest());
125         }).doesNotThrowAnyException();
126         */
127     }
128
129     /**
130      * Initialization.
131      */
132     @Then("Decision Permit {int} Obligations")
133     public void decision_Permit_Obligations(Integer int1) {
134         /*
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);
140         }
141         */
142     }
143
144     /**
145      * Initialization.
146      */
147     @When("The application gets new Tosca Policy")
148     public void the_application_gets_new_Tosca_Policy() {
149         /*
150         //
151         // The Xacml PDP REST controller Would receive this from the PAP
152         //
153         // And then parse it looking for Policy Types
154         //
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);
159                 //
160                 // Do we test iterating and determining if supported?
161                 //
162
163             }
164         }).doesNotThrowAnyException();
165         */
166     }
167
168     /**
169      * Initialization.
170      */
171     @Then("Load Policy")
172     public void load_Policy() {
173         /*
174         assertThatCode(() -> {
175             //
176             // Load the policies
177             //
178             List<PolicyType> convertedPolicies = onapPdpEngine.convertPolicies(tcaPolicy);
179             //
180             // Store these in temporary folder
181             //
182             int id = 1;
183             List<Path> newReferencedPolicies = new ArrayList<>();
184             for (PolicyType convertedPolicy : convertedPolicies) {
185                 //
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.
188                 //
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);
192                 //
193                 // Add it to our list
194                 //
195                 newReferencedPolicies.add(path);
196             }
197             //
198             // Now updated the properties
199             //
200             Path[] args = new Path[newReferencedPolicies.size()];
201             newReferencedPolicies.toArray(args);
202             XACMLProperties.setXacmlReferencedProperties(properties, args);
203             //
204             // Reload the PDP engine
205             //
206             onapPdpEngine.initializeEngine(properties);
207         }).doesNotThrowAnyException();
208         */
209     }
210
211     /**
212      * Initialization.
213      */
214     @Then("Save Configuration")
215     public void save_Configuration() {
216         /*
217         assertThatCode(() -> {
218             //
219             // Save the configuration
220             //
221             onapPdpEngine.storeXacmlProperties(pathProperties.getAbsolutePath());
222         }).doesNotThrowAnyException();
223         */
224     }
225 }