2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Modifications Copyright (C) 2019 Samsung
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
22 package org.onap.policy.pdp.rest.api.services;
24 import java.util.Collection;
25 import java.util.HashMap;
27 import java.util.UUID;
28 import java.util.Map.Entry;
29 import javax.json.Json;
30 import javax.json.JsonArrayBuilder;
31 import javax.json.JsonObject;
32 import javax.json.JsonObjectBuilder;
34 import org.junit.runner.RunWith;
35 import static org.junit.Assert.*;
36 import org.onap.policy.api.DecisionRequestParameters;
37 import org.onap.policy.api.PolicyDecisionException;
38 import org.onap.policy.pdp.rest.api.models.PDPResponse;
39 import org.onap.policy.pdp.rest.config.PDPRestConfig;
40 import org.springframework.test.context.ContextConfiguration;
41 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
42 import org.springframework.test.context.web.WebAppConfiguration;
44 @RunWith(SpringJUnit4ClassRunner.class)
45 @ContextConfiguration(classes = {PDPRestConfig.class})
47 public class PDPServicesTest {
49 * Run the PDPServices() constructor test.
51 * @generatedBy CodePro at 7/20/17 9:26 AM
54 public void testPDPServices_1() throws Exception {
55 PDPServices result = new PDPServices();
56 assertNotNull(result);
57 // add additional test code here
61 * Run the Collection<PDPResponse> generateRequest(String,UUID,boolean,boolean) method test.
65 * @generatedBy CodePro at 7/20/17 9:26 AM
68 public void testGenerateRequest_1() throws Exception {
69 DecisionRequestParameters pep = new DecisionRequestParameters();
70 Map<String, String> eventAttributes = new HashMap<>();
71 eventAttributes.put("TEST", "test");
72 pep.setOnapName("te123");
73 pep.setDecisionAttributes(eventAttributes);
74 PDPServices fixture = new PDPServices();
77 String jsonString = getModel(pep).toString();
78 UUID requestID = UUID.randomUUID();
80 Collection<PDPResponse> result = fixture.generateRequest(jsonString, requestID, false, true);
82 // add additional test code here
83 // An unexpected exception was thrown in user code while executing this test:
84 // java.lang.NoClassDefFoundError: Could not initialize class
85 // org.onap.policy.pdp.rest.api.services.PDPServices
86 assertNotNull(result);
90 private JsonObject getModel(DecisionRequestParameters pep) throws PolicyDecisionException {
91 JsonArrayBuilder resourceArray = Json.createArrayBuilder();
93 Map<String, String> decisionAttributes = pep.getDecisionAttributes();
94 for (Entry<String, String> key : decisionAttributes.entrySet()) {
95 JsonObjectBuilder resourceBuilder = Json.createObjectBuilder();
96 if (key.getValue().matches("[0-9]+")) {
98 if ((key.getKey().equals("ErrorCode")) || (key.getKey().equals("WorkStep"))) {
100 resourceBuilder.add("Value", key.getValue());
104 int val = Integer.parseInt(key.getValue());
105 resourceBuilder.add("Value", val);
110 resourceBuilder.add("Value", key.getValue());
112 resourceBuilder.add("AttributeId", key.getKey());
113 resourceArray.add(resourceBuilder);
115 return Json.createObjectBuilder().add("Request", Json.createObjectBuilder().add("AccessSubject",
116 Json.createObjectBuilder().add("Attribute",
117 Json.createObjectBuilder().add("Value", pep.getOnapName()).add("AttributeId", "ONAPName")))
118 .add("Resource", Json.createObjectBuilder().add("Attribute", resourceArray))
119 .add("Action", Json.createObjectBuilder().add("Attribute", Json.createObjectBuilder()
120 .add("Value", "DECIDE").add("AttributeId", "urn:oasis:names:tc:xacml:1.0:action:action-id"))))
125 * Run the Collection<PDPResponse> generateRequest(String,UUID,boolean,boolean) method test.
129 * @generatedBy CodePro at 7/20/17 9:26 AM
131 @Test(expected = org.onap.policy.api.PolicyException.class)
132 public void testGenerateRequest_2() throws Exception {
133 PDPServices fixture = new PDPServices();
134 fixture.generateRequest("", UUID.randomUUID(), true, true);
135 String jsonString = "";
136 UUID requestID = UUID.randomUUID();
137 boolean unique = true;
138 boolean decide = true;
140 Collection<PDPResponse> result = fixture.generateRequest(jsonString, requestID, unique, decide);
142 // add additional test code here
143 assertNotNull(result);
147 * Perform pre-test initialization.
149 * @throws Exception if the initialization fails for some reason
151 * @generatedBy CodePro at 7/20/17 9:26 AM
154 public void setUp() throws Exception {
155 // add additional set up code here
159 * Perform post-test clean-up.
161 * @throws Exception if the clean-up fails for some reason
163 * @generatedBy CodePro at 7/20/17 9:26 AM
166 public void tearDown() throws Exception {
167 // Add additional tear down code here
173 * @param args the command line arguments
175 * @generatedBy CodePro at 7/20/17 9:26 AM
177 public static void main(String[] args) {
178 new org.junit.runner.JUnitCore().run(PDPServicesTest.class);