aboutsummaryrefslogtreecommitdiffstats
path: root/ONAP-PDP/src/test/java/org/onap/policy/xacml/action/FindActionTest.java
blob: 2a36fa280218cd00be2d62a867a1e9c2ae159321 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*-
 * ============LICENSE_START=======================================================
 * ONAP-PDP
 * ================================================================================
 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */
package org.onap.policy.xacml.action;

import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.onap.policy.xacml.custom.OnapFunctionDefinitionFactory;
import com.att.research.xacml.api.Decision;
import com.att.research.xacml.api.Request;
import com.att.research.xacml.api.XACML3;
import com.att.research.xacml.std.IdentifierImpl;
import com.att.research.xacml.std.StdAttributeValue;
import com.att.research.xacml.std.StdMutableAdvice;
import com.att.research.xacml.std.StdMutableAttributeAssignment;
import com.att.research.xacml.std.StdMutableMissingAttributeDetail;
import com.att.research.xacml.std.StdMutableObligation;
import com.att.research.xacml.std.StdMutableResponse;
import com.att.research.xacml.std.StdMutableResult;
import com.att.research.xacml.std.StdMutableStatus;
import com.att.research.xacml.std.StdMutableStatusDetail;
import com.att.research.xacml.std.StdStatusCode;
import com.att.research.xacml.std.datatypes.DataTypes;
import com.att.research.xacml.std.json.JSONRequest;
import com.att.research.xacml.util.XACMLProperties;

import org.onap.policy.drools.http.server.HttpServletServer;
import org.onap.policy.drools.utils.NetworkUtil;

public class FindActionTest {
	

	String xPathExampleFromSpec = "{ " +
			"\"Request\" : { " +
				"\"Resource\" : { " +
					"\"Attribute\": [ " +
						"{ " +
						 	"\"Id\" : \"urn:oasis:names:tc:xacml:3.0:content-selector\", " +
						 	"\"DataType\" : \"xpathExpression\", " +
				            "\"Value\" : { " +
				                "\"XPathCategory\" : \"urn:oasis:names:tc:xacml:3.0:attribute-category:resource\", " +
				                "\"Namespaces\" : [{ " +
				                    	"\"Namespace\" : \"urn:oasis:names:tc:xacml:3.0:core:schema:wd-17\" " +
				                    	"}, " +
				                    "{ " +
				                    	"\"Prefix\" : \"md\", " +
				                    	"\"Namespace\" : \"urn:example:med:schemas:record\" " +
				                    "} " +
				                "], " +
				                "\"XPath\" : \"md:record/md:patient/md:patientDoB\" " +
				            "} " +
				        "} " +
					"] " +
				"} " +
			"} " +
		"} ";
	
	String jsonResponse;
	Request request;
	private static final int MOCK_SERVER_PORT = 6670;
	
	@BeforeClass
	public static void setUpServer() {
        try {
        	final HttpServletServer testServer = HttpServletServer.factory.build("dmaapSim",
        			"localhost", MOCK_SERVER_PORT, "/", false, true);
        	testServer.addServletClass("/*", DummyRest.class.getName());
        	testServer.waitedStart(2000);
        	if (!NetworkUtil.isTcpPortOpen("localhost", testServer.getPort(), 5, 10000L))
        		throw new IllegalStateException("cannot connect to port " + testServer.getPort());
        } catch (final Exception e) {
        	fail(e.getMessage());
        }
		
	}
	
    @AfterClass
    public static void tearDownSimulator() {
        HttpServletServer.factory.destroy();
    }
    
	@Before
	public void setUp() throws Exception {
		new OnapFunctionDefinitionFactory();
		request = JSONRequest.load(xPathExampleFromSpec);

		try {	
			XACMLProperties.reloadProperties();
			System.setProperty(XACMLProperties.XACML_PROPERTIES_NAME, "src/test/resources/xacml.pdp.properties");
			XACMLProperties.getProperties();
			
			assertTrue(true);
		} catch (Exception e) {
			fail();
			
		}
	}
	

	@Test
	public final void testRun() {
		FindAction action = new FindAction();
		// fully-loaded multiple response
		StdMutableResponse response = new StdMutableResponse();
		// create a Status object
		StdMutableStatus status = new StdMutableStatus(StdStatusCode.STATUS_CODE_MISSING_ATTRIBUTE);
		status.setStatusMessage("some status message");
		StdMutableStatusDetail statusDetailIn = new StdMutableStatusDetail();
		StdMutableMissingAttributeDetail mad = new StdMutableMissingAttributeDetail();
		mad.addAttributeValue(new StdAttributeValue<String>(DataTypes.DT_STRING.getId(), "PEPACTION"));
		mad.addAttributeValue(new StdAttributeValue<String>(DataTypes.DT_INTEGER.getId(), "PDPACTION"));
		mad.setAttributeId(XACML3.ID_ACTION_PURPOSE);
		mad.setCategory(XACML3.ID_ATTRIBUTE_CATEGORY_ACTION);
		mad.setDataTypeId(XACML3.ID_DATATYPE_STRING);
		mad.setIssuer("an Issuer");
		statusDetailIn.addMissingAttributeDetail(mad);
		status.setStatusDetail(statusDetailIn);
		// create a single result object
		StdMutableResult result = new StdMutableResult(status);
		// set the decision
		result.setDecision(Decision.INDETERMINATE);
		// put the Result into the Response
		response.add(result);
		// create a new Result with a different Decision
		status = new StdMutableStatus(StdStatusCode.STATUS_CODE_OK);
		result = new StdMutableResult(status);
		result.setDecision(Decision.DENY);

		StdMutableObligation obligation = new StdMutableObligation();
		obligation.setId(XACML3.ID_ACTION_IMPLIED_ACTION);
		obligation.addAttributeAssignment(new StdMutableAttributeAssignment(
				XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE, 
				XACML3.ID_SUBJECT, 
				"obligation-issuer1", 
				new StdAttributeValue<String>(DataTypes.DT_STRING.getId(), "Test")));
		result.addObligation(obligation);

		StdMutableAdvice advice = new StdMutableAdvice();
		advice.setId(XACML3.ID_ACTION_IMPLIED_ACTION);
		advice.addAttributeAssignment(new StdMutableAttributeAssignment(
				XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE, 
				XACML3.ID_SUBJECT, 
				"advice-issuer1", 
				new StdAttributeValue<String>(DataTypes.DT_STRING.getId(), "Test")));
		response.add(result);
		
			//	The logic below exercises the  callRest and takeAction methods in FindAction
			//	GET request
		status = new StdMutableStatus(StdStatusCode.STATUS_CODE_OK);
		result = new StdMutableResult(status);
		result.setDecision(Decision.PERMIT);
		
		obligation = new StdMutableObligation();
		obligation.setId(XACML3.ID_ACTION_IMPLIED_ACTION);
		obligation.addAttributeAssignment(new StdMutableAttributeAssignment(
				XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE, 
				new IdentifierImpl("performer"), 
				"obligation-issuer", 
				new StdAttributeValue<String>(DataTypes.DT_STRING.getId(), "PDPACTION")));
		
		obligation.addAttributeAssignment(new StdMutableAttributeAssignment(
				XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE, 
				new IdentifierImpl("URL"), 
				"obligation-issuer", 
				new StdAttributeValue<String>(DataTypes.DT_STRING.getId(), "http://localhost:" + MOCK_SERVER_PORT)));
		obligation.addAttributeAssignment(new StdMutableAttributeAssignment(
				XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE, 
				new IdentifierImpl("method"), 
				"obligation-issuer", 
				new StdAttributeValue<String>(DataTypes.DT_STRING.getId(), "GET")));
		obligation.addAttributeAssignment(new StdMutableAttributeAssignment(
				XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE, 
				new IdentifierImpl("headers"), 
				"obligation-issuer", 
				new StdAttributeValue<String>(DataTypes.DT_STRING.getId(), "foobar")));

		
		result.addObligation(obligation);
		response.add(result);
	
			//	POST request
		status = new StdMutableStatus(StdStatusCode.STATUS_CODE_OK);
		result = new StdMutableResult(status);
		result.setDecision(Decision.PERMIT);
		
		obligation = new StdMutableObligation();
		obligation.setId(XACML3.ID_ACTION_IMPLIED_ACTION);
		obligation.addAttributeAssignment(new StdMutableAttributeAssignment(
				XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE, 
				new IdentifierImpl("performer"), 
				"obligation-issuer", 
				new StdAttributeValue<String>(DataTypes.DT_STRING.getId(), "PDPACTION")));
		
		obligation.addAttributeAssignment(new StdMutableAttributeAssignment(
				XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE, 
				new IdentifierImpl("URL"), 
				"obligation-issuer", 
				new StdAttributeValue<String>(DataTypes.DT_STRING.getId(), "http://localhost:" + MOCK_SERVER_PORT)));
		obligation.addAttributeAssignment(new StdMutableAttributeAssignment(
				XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE, 
				new IdentifierImpl("method"), 
				"obligation-issuer", 
				new StdAttributeValue<String>(DataTypes.DT_STRING.getId(), "POST")));
		obligation.addAttributeAssignment(new StdMutableAttributeAssignment(
				XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE, 
				new IdentifierImpl("body"), 
				"obligation-issuer", 
				new StdAttributeValue<String>(DataTypes.DT_STRING.getId(), "http://localhost:" + MOCK_SERVER_PORT + "/foobar")));
		
		result.addObligation(obligation);
		response.add(result);
		
			//PUT request
		status = new StdMutableStatus(StdStatusCode.STATUS_CODE_OK);
		result = new StdMutableResult(status);
		result.setDecision(Decision.PERMIT);
		
		obligation = new StdMutableObligation();
		obligation.setId(XACML3.ID_ACTION_IMPLIED_ACTION);
		obligation.addAttributeAssignment(new StdMutableAttributeAssignment(
				XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE, 
				new IdentifierImpl("performer"), 
				"obligation-issuer", 
				new StdAttributeValue<String>(DataTypes.DT_STRING.getId(), "PDPACTION")));
		
		obligation.addAttributeAssignment(new StdMutableAttributeAssignment(
				XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE, 
				new IdentifierImpl("URL"), 
				"obligation-issuer", 
				new StdAttributeValue<String>(DataTypes.DT_STRING.getId(), "http://localhost:" + MOCK_SERVER_PORT)));
		obligation.addAttributeAssignment(new StdMutableAttributeAssignment(
				XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE, 
				new IdentifierImpl("method"), 
				"obligation-issuer", 
				new StdAttributeValue<String>(DataTypes.DT_STRING.getId(), "PUT")));
		obligation.addAttributeAssignment(new StdMutableAttributeAssignment(
				XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE, 
				new IdentifierImpl("body"), 
				"obligation-issuer", 
				new StdAttributeValue<String>(DataTypes.DT_STRING.getId(), "http://localhost:" + MOCK_SERVER_PORT + "/foobar")));
		
		result.addObligation(obligation);
		response.add(result);
		
		try {
			assertTrue(action.run(response, request) != null);
		} catch (Exception e) {
			fail("operation failed, e="+e);
		}
	}

}