2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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.
18 * ============LICENSE_END=========================================================
20 package org.onap.policy.pdp.rest.api.services;
22 import java.util.Collection;
23 import java.util.HashMap;
25 import java.util.UUID;
26 import java.util.Map.Entry;
28 import javax.json.Json;
29 import javax.json.JsonArrayBuilder;
30 import javax.json.JsonObject;
31 import javax.json.JsonObjectBuilder;
34 import org.junit.runner.RunWith;
36 import static org.junit.Assert.*;
38 import org.onap.policy.api.DecisionRequestParameters;
39 import org.onap.policy.api.PolicyDecisionException;
40 import org.onap.policy.pdp.rest.api.models.PDPResponse;
41 import org.onap.policy.pdp.rest.config.PDPRestConfig;
42 import org.springframework.test.context.ContextConfiguration;
43 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
44 import org.springframework.test.context.web.WebAppConfiguration;
46 @RunWith(SpringJUnit4ClassRunner.class)
47 @ContextConfiguration(classes = {PDPRestConfig.class})
49 public class PDPServicesTest {
51 * Run the PDPServices() constructor test.
53 * @generatedBy CodePro at 7/20/17 9:26 AM
56 public void testPDPServices_1()
58 PDPServices result = new PDPServices();
59 assertNotNull(result);
60 // add additional test code here
64 * Run the Collection<PDPResponse> generateRequest(String,UUID,boolean,boolean) method test.
68 * @generatedBy CodePro at 7/20/17 9:26 AM
71 public void testGenerateRequest_1()
73 DecisionRequestParameters pep = new DecisionRequestParameters();
74 Map<String,String> eventAttributes = new HashMap<>();
75 eventAttributes.put("TEST", "test");
76 pep.setOnapName("te123");
77 pep.setDecisionAttributes(eventAttributes);
78 PDPServices fixture = new PDPServices();
81 String jsonString = getModel(pep).toString();
82 UUID requestID = UUID.randomUUID();
84 Collection<PDPResponse> result = fixture.generateRequest(jsonString, requestID, false, true);
86 // add additional test code here
87 // An unexpected exception was thrown in user code while executing this test:
88 // java.lang.NoClassDefFoundError: Could not initialize class org.onap.policy.pdp.rest.api.services.PDPServices
89 assertNotNull(result);
93 private JsonObject getModel(DecisionRequestParameters pep) throws PolicyDecisionException{
94 JsonArrayBuilder resourceArray = Json.createArrayBuilder();
96 Map<String, String> decisionAttributes = pep.getDecisionAttributes();
97 for (Entry<String,String> key : decisionAttributes.entrySet()) {
98 JsonObjectBuilder resourceBuilder = Json.createObjectBuilder();
99 if (key.getValue().matches("[0-9]+")) {
101 if ((key.getKey().equals("ErrorCode")) || (key.getKey().equals("WorkStep"))) {
103 resourceBuilder.add("Value", key.getValue());
107 int val = Integer.parseInt(key.getValue());
108 resourceBuilder.add("Value", val);
113 resourceBuilder.add("Value", key.getValue());
115 resourceBuilder.add("AttributeId", key.getKey());
116 resourceArray.add(resourceBuilder);
118 return Json.createObjectBuilder()
119 .add("Request", Json.createObjectBuilder()
120 .add("AccessSubject", Json.createObjectBuilder()
121 .add("Attribute", Json.createObjectBuilder()
122 .add("Value", pep.getOnapName())
123 .add("AttributeId", "ONAPName")))
124 .add("Resource", Json.createObjectBuilder()
125 .add("Attribute", resourceArray))
126 .add("Action", Json.createObjectBuilder()
127 .add("Attribute", Json.createObjectBuilder()
128 .add("Value", "DECIDE")
129 .add("AttributeId", "urn:oasis:names:tc:xacml:1.0:action:action-id"))))
134 * Run the Collection<PDPResponse> generateRequest(String,UUID,boolean,boolean) method test.
138 * @generatedBy CodePro at 7/20/17 9:26 AM
140 @Test(expected = org.onap.policy.api.PolicyException.class)
141 public void testGenerateRequest_2()
143 PDPServices fixture = new PDPServices();
144 fixture.generateRequest("", UUID.randomUUID(), true, true);
145 String jsonString = "";
146 UUID requestID = UUID.randomUUID();
147 boolean unique = true;
148 boolean decide = true;
150 Collection<PDPResponse> result = fixture.generateRequest(jsonString, requestID, unique, decide);
152 // add additional test code here
153 assertNotNull(result);
157 * Perform pre-test initialization.
160 * if the initialization fails for some reason
162 * @generatedBy CodePro at 7/20/17 9:26 AM
167 // add additional set up code here
171 * Perform post-test clean-up.
174 * if the clean-up fails for some reason
176 * @generatedBy CodePro at 7/20/17 9:26 AM
179 public void tearDown()
181 // Add additional tear down code here
187 * @param args the command line arguments
189 * @generatedBy CodePro at 7/20/17 9:26 AM
191 public static void main(String[] args) {
192 new org.junit.runner.JUnitCore().run(PDPServicesTest.class);