Reformat ONAP-PDP-REST test cases
[policy/engine.git] / ONAP-PDP-REST / src / test / java / org / onap / policy / pdp / rest / api / services / PDPServicesTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-PDP-REST
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
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
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=========================================================
21  */
22 package org.onap.policy.pdp.rest.api.services;
23
24 import java.util.Collection;
25 import java.util.HashMap;
26 import java.util.Map;
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;
33 import org.junit.*;
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;
43
44 @RunWith(SpringJUnit4ClassRunner.class)
45 @ContextConfiguration(classes = {PDPRestConfig.class})
46 @WebAppConfiguration
47 public class PDPServicesTest {
48     /**
49      * Run the PDPServices() constructor test.
50      *
51      * @generatedBy CodePro at 7/20/17 9:26 AM
52      */
53     @Test
54     public void testPDPServices_1() throws Exception {
55         PDPServices result = new PDPServices();
56         assertNotNull(result);
57         // add additional test code here
58     }
59
60     /**
61      * Run the Collection<PDPResponse> generateRequest(String,UUID,boolean,boolean) method test.
62      *
63      * @throws Exception
64      *
65      * @generatedBy CodePro at 7/20/17 9:26 AM
66      */
67     @Test
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();
75
76         // Failure Tests.
77         String jsonString = getModel(pep).toString();
78         UUID requestID = UUID.randomUUID();
79
80         Collection<PDPResponse> result = fixture.generateRequest(jsonString, requestID, false, true);
81
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);
87
88     }
89
90     private JsonObject getModel(DecisionRequestParameters pep) throws PolicyDecisionException {
91         JsonArrayBuilder resourceArray = Json.createArrayBuilder();
92
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]+")) {
97
98                 if ((key.getKey().equals("ErrorCode")) || (key.getKey().equals("WorkStep"))) {
99
100                     resourceBuilder.add("Value", key.getValue());
101
102                 } else {
103
104                     int val = Integer.parseInt(key.getValue());
105                     resourceBuilder.add("Value", val);
106
107                 }
108
109             } else {
110                 resourceBuilder.add("Value", key.getValue());
111             }
112             resourceBuilder.add("AttributeId", key.getKey());
113             resourceArray.add(resourceBuilder);
114         }
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"))))
121                 .build();
122     }
123
124     /**
125      * Run the Collection<PDPResponse> generateRequest(String,UUID,boolean,boolean) method test.
126      *
127      * @throws Exception
128      *
129      * @generatedBy CodePro at 7/20/17 9:26 AM
130      */
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;
139
140         Collection<PDPResponse> result = fixture.generateRequest(jsonString, requestID, unique, decide);
141
142         // add additional test code here
143         assertNotNull(result);
144     }
145
146     /**
147      * Perform pre-test initialization.
148      *
149      * @throws Exception if the initialization fails for some reason
150      *
151      * @generatedBy CodePro at 7/20/17 9:26 AM
152      */
153     @Before
154     public void setUp() throws Exception {
155         // add additional set up code here
156     }
157
158     /**
159      * Perform post-test clean-up.
160      *
161      * @throws Exception if the clean-up fails for some reason
162      *
163      * @generatedBy CodePro at 7/20/17 9:26 AM
164      */
165     @After
166     public void tearDown() throws Exception {
167         // Add additional tear down code here
168     }
169
170     /**
171      * Launch the test.
172      *
173      * @param args the command line arguments
174      *
175      * @generatedBy CodePro at 7/20/17 9:26 AM
176      */
177     public static void main(String[] args) {
178         new org.junit.runner.JUnitCore().run(PDPServicesTest.class);
179     }
180 }