Added Junits for ONAP PDP
[policy/engine.git] / ONAP-PDP / src / test / java / org / onap / policy / xacml / action / FindActionTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-PDP
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.onap.policy.xacml.action;
21
22 import static org.junit.Assert.assertTrue;
23 import static org.junit.Assert.fail;
24
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.onap.policy.xacml.custom.OnapFunctionDefinitionFactory;
28
29 import com.att.research.xacml.api.Decision;
30 import com.att.research.xacml.api.Request;
31 import com.att.research.xacml.api.XACML3;
32 import com.att.research.xacml.std.StdAttributeValue;
33 import com.att.research.xacml.std.StdMutableAdvice;
34 import com.att.research.xacml.std.StdMutableAttributeAssignment;
35 import com.att.research.xacml.std.StdMutableMissingAttributeDetail;
36 import com.att.research.xacml.std.StdMutableObligation;
37 import com.att.research.xacml.std.StdMutableResponse;
38 import com.att.research.xacml.std.StdMutableResult;
39 import com.att.research.xacml.std.StdMutableStatus;
40 import com.att.research.xacml.std.StdMutableStatusDetail;
41 import com.att.research.xacml.std.StdStatusCode;
42 import com.att.research.xacml.std.datatypes.DataTypes;
43 import com.att.research.xacml.std.json.JSONRequest;
44
45
46 public class FindActionTest {
47         
48         String xPathExampleFromSpec = "{ " +
49                         "\"Request\" : { " +
50                                 "\"Resource\" : { " +
51                                         "\"Attribute\": [ " +
52                                                 "{ " +
53                                                         "\"Id\" : \"urn:oasis:names:tc:xacml:3.0:content-selector\", " +
54                                             "\"DataType\" : \"xpathExpression\", " +
55                                             "\"Value\" : { " +
56                                                 "\"XPathCategory\" : \"urn:oasis:names:tc:xacml:3.0:attribute-category:resource\", " +
57                                                 "\"Namespaces\" : [{ " +
58                                                         "\"Namespace\" : \"urn:oasis:names:tc:xacml:3.0:core:schema:wd-17\" " +
59                                                         "}, " +
60                                                     "{ " +
61                                                         "\"Prefix\" : \"md\", " +
62                                                         "\"Namespace\" : \"urn:example:med:schemas:record\" " +
63                                                     "} " +
64                                                 "], " +
65                                                 "\"XPath\" : \"md:record/md:patient/md:patientDoB\" " +
66                                             "} " +
67                                         "} " +
68                                         "] " +
69                                 "} " +
70                         "} " +
71                 "} ";
72         
73         String jsonResponse;
74         
75         Request request;
76         
77         @Before
78         public void setUp() throws Exception {
79                 new OnapFunctionDefinitionFactory();
80                 request = JSONRequest.load(xPathExampleFromSpec);
81         }
82
83         @Test
84         public final void testRun() {
85                 FindAction action = new FindAction();
86                 // fully-loaded multiple response
87                 StdMutableResponse response = new StdMutableResponse();
88                 // create a Status object
89                 StdMutableStatus status = new StdMutableStatus(StdStatusCode.STATUS_CODE_MISSING_ATTRIBUTE);
90                 status.setStatusMessage("some status message");
91                 StdMutableStatusDetail statusDetailIn = new StdMutableStatusDetail();
92                 StdMutableMissingAttributeDetail mad = new StdMutableMissingAttributeDetail();
93                 mad.addAttributeValue(new StdAttributeValue<String>(DataTypes.DT_STRING.getId(), "PEPACTION"));
94                 mad.addAttributeValue(new StdAttributeValue<String>(DataTypes.DT_INTEGER.getId(), "PDPACTION"));
95                 mad.setAttributeId(XACML3.ID_ACTION_PURPOSE);
96                 mad.setCategory(XACML3.ID_ATTRIBUTE_CATEGORY_ACTION);
97                 mad.setDataTypeId(XACML3.ID_DATATYPE_STRING);
98                 mad.setIssuer("an Issuer");
99                 statusDetailIn.addMissingAttributeDetail(mad);
100                 status.setStatusDetail(statusDetailIn);
101                 // create a single result object
102                 StdMutableResult result = new StdMutableResult(status);
103                 // set the decision
104                 result.setDecision(Decision.INDETERMINATE);
105                 // put the Result into the Response
106                 response.add(result);
107                 // create a new Result with a different Decision
108                 status = new StdMutableStatus(StdStatusCode.STATUS_CODE_OK);
109                 result = new StdMutableResult(status);
110                 result.setDecision(Decision.DENY);
111
112                 StdMutableObligation obligation = new StdMutableObligation();
113                 obligation.setId(XACML3.ID_ACTION_IMPLIED_ACTION);
114                 obligation.addAttributeAssignment(new StdMutableAttributeAssignment(
115                                 XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE, 
116                                 XACML3.ID_SUBJECT, 
117                                 "obligation-issuer1", 
118                                 new StdAttributeValue<String>(DataTypes.DT_STRING.getId(), "Test")));
119                 result.addObligation(obligation);
120
121                 StdMutableAdvice advice = new StdMutableAdvice();
122                 advice.setId(XACML3.ID_ACTION_IMPLIED_ACTION);
123                 advice.addAttributeAssignment(new StdMutableAttributeAssignment(
124                                 XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE, 
125                                 XACML3.ID_SUBJECT, 
126                                 "advice-issuer1", 
127                                 new StdAttributeValue<String>(DataTypes.DT_STRING.getId(), "Test")));
128                 response.add(result);
129                 try {
130                         assertTrue(action.run(response, request) != null);
131                 } catch (Exception e) {
132                         fail("operation failed, e="+e);
133                 }
134         }
135
136 }