[POLICY-122] Policy GUI Fixes
[policy/engine.git] / ECOMP-PDP-REST / src / test / java / org / openecomp / policy / pdp / rest / api / services / PDPServicesTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ECOMP-PDP-REST
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
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  * ============LICENSE_END=========================================================
19  */
20 package org.openecomp.policy.pdp.rest.api.services;
21
22 import java.util.Collection;
23 import java.util.HashMap;
24 import java.util.Map;
25 import java.util.UUID;
26 import java.util.Map.Entry;
27
28 import javax.json.Json;
29 import javax.json.JsonArrayBuilder;
30 import javax.json.JsonObject;
31 import javax.json.JsonObjectBuilder;
32
33 import org.junit.*;
34 import org.junit.runner.RunWith;
35
36 import static org.junit.Assert.*;
37
38 import org.openecomp.policy.api.DecisionRequestParameters;
39 import org.openecomp.policy.api.PolicyDecisionException;
40 import org.openecomp.policy.pdp.rest.api.models.PDPResponse;
41 import org.openecomp.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;
45
46 @RunWith(SpringJUnit4ClassRunner.class)
47 @ContextConfiguration(classes = {PDPRestConfig.class})
48 @WebAppConfiguration
49 public class PDPServicesTest {
50         /**
51          * Run the PDPServices() constructor test.
52          *
53          * @generatedBy CodePro at 7/20/17 9:26 AM
54          */
55         @Test
56         public void testPDPServices_1()
57                 throws Exception {
58                 PDPServices result = new PDPServices();
59                 assertNotNull(result);
60                 // add additional test code here
61         }
62
63         /**
64          * Run the Collection<PDPResponse> generateRequest(String,UUID,boolean,boolean) method test.
65          *
66          * @throws Exception
67          *
68          * @generatedBy CodePro at 7/20/17 9:26 AM
69          */
70         @Test
71         public void testGenerateRequest_1()
72                 throws Exception {
73                 DecisionRequestParameters pep = new DecisionRequestParameters();
74                 Map<String,String> eventAttributes = new HashMap<>();
75                 eventAttributes.put("TEST", "test");
76                 pep.setECOMPComponentName("te123");
77                 pep.setDecisionAttributes(eventAttributes);
78                 PDPServices fixture = new PDPServices();
79
80                 //Failure Tests. 
81                 String jsonString = getModel(pep).toString();
82                 UUID requestID = UUID.randomUUID();
83
84                 Collection<PDPResponse> result = fixture.generateRequest(jsonString, requestID, false, true);
85
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.openecomp.policy.pdp.rest.api.services.PDPServices
89                 assertNotNull(result);
90         
91         }
92
93     private JsonObject getModel(DecisionRequestParameters pep) throws PolicyDecisionException{
94         JsonArrayBuilder resourceArray = Json.createArrayBuilder();
95         
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]+")) {
100                 
101                 if ((key.getKey().equals("ErrorCode")) || (key.getKey().equals("WorkStep"))) {
102                     
103                         resourceBuilder.add("Value", key.getValue());
104
105                 } else {
106                         
107                     int val = Integer.parseInt(key.getValue());
108                     resourceBuilder.add("Value", val);
109                     
110                 }
111                 
112             } else {
113                 resourceBuilder.add("Value", key.getValue());
114             }
115             resourceBuilder.add("AttributeId", key.getKey());
116             resourceArray.add(resourceBuilder);
117         }
118         return Json.createObjectBuilder()
119                 .add("Request", Json.createObjectBuilder()
120                                 .add("AccessSubject", Json.createObjectBuilder()
121                                                 .add("Attribute", Json.createObjectBuilder()
122                                                                 .add("Value", pep.getECOMPComponentName())
123                                                                 .add("AttributeId", "ECOMPName")))
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"))))
130                 .build();
131     }
132     
133         /**
134          * Run the Collection<PDPResponse> generateRequest(String,UUID,boolean,boolean) method test.
135          *
136          * @throws Exception
137          *
138          * @generatedBy CodePro at 7/20/17 9:26 AM
139          */
140         @Test(expected = org.openecomp.policy.api.PolicyException.class)
141         public void testGenerateRequest_2()
142                 throws Exception {
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;
149
150                 Collection<PDPResponse> result = fixture.generateRequest(jsonString, requestID, unique, decide);
151
152                 // add additional test code here
153                 assertNotNull(result);
154         }
155
156         /**
157          * Perform pre-test initialization.
158          *
159          * @throws Exception
160          *         if the initialization fails for some reason
161          *
162          * @generatedBy CodePro at 7/20/17 9:26 AM
163          */
164         @Before
165         public void setUp()
166                 throws Exception {
167                 // add additional set up code here
168         }
169
170         /**
171          * Perform post-test clean-up.
172          *
173          * @throws Exception
174          *         if the clean-up fails for some reason
175          *
176          * @generatedBy CodePro at 7/20/17 9:26 AM
177          */
178         @After
179         public void tearDown()
180                 throws Exception {
181                 // Add additional tear down code here
182         }
183
184         /**
185          * Launch the test.
186          *
187          * @param args the command line arguments
188          *
189          * @generatedBy CodePro at 7/20/17 9:26 AM
190          */
191         public static void main(String[] args) {
192                 new org.junit.runner.JUnitCore().run(PDPServicesTest.class);
193         }
194 }