2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.pdp.xacml.application.common.std;
23 import static org.junit.Assert.assertNotNull;
24 import static org.junit.Assert.assertTrue;
25 import static org.mockito.Mockito.when;
27 import com.att.research.xacml.api.DataTypeException;
28 import com.att.research.xacml.api.Request;
29 import com.att.research.xacml.api.RequestAttributes;
30 import com.att.research.xacml.api.XACML3;
31 import com.att.research.xacml.std.IdentifierImpl;
32 import java.util.Arrays;
33 import java.util.Iterator;
35 import java.util.TreeMap;
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.mockito.Mock;
39 import org.mockito.MockitoAnnotations;
40 import org.onap.policy.models.decisions.concepts.DecisionRequest;
41 import org.onap.policy.pdp.xacml.application.common.ToscaDictionary;
43 public class StdMatchablePolicyRequestTest {
44 private static final String ACTION = "my-action";
45 private static final String ONAP_NAME = "my-name";
46 private static final String ONAP_INSTANCE = "my-instance";
47 private static final String ONAP_COMPONENT = "my-component";
48 private static final String RESOURCE1 = "my-scope";
49 private static final String RESOURCE2 = "my-service";
50 private static final String RESOURCE3 = "my-geography1";
51 private static final String RESOURCE4 = "my-geography2";
54 private DecisionRequest decreq;
56 private Map<String, Object> resources;
58 private Request stdreq;
61 * Initializes objects.
65 MockitoAnnotations.initMocks(this);
67 resources = new TreeMap<>();
69 when(decreq.getResource()).thenReturn(resources);
70 when(decreq.getAction()).thenReturn(ACTION);
71 when(decreq.getOnapComponent()).thenReturn(ONAP_COMPONENT);
72 when(decreq.getOnapInstance()).thenReturn(ONAP_INSTANCE);
73 when(decreq.getOnapName()).thenReturn(ONAP_NAME);
77 public void testCreateInstance() throws IllegalAccessException, DataTypeException {
78 resources.put("resource1", RESOURCE1);
79 resources.put("resource2", RESOURCE2);
80 resources.put("resource3", Arrays.asList(RESOURCE3, RESOURCE4));
82 stdreq = StdMatchablePolicyRequest.createInstance(decreq);
84 assertNotNull(stdreq);
86 assertTrue(stdreq.getRequestAttributes(XACML3.ID_ATTRIBUTE_CATEGORY_ACTION).hasNext());
87 assertTrue(stdreq.getRequestAttributes(XACML3.ID_SUBJECT_CATEGORY_ACCESS_SUBJECT).hasNext());
90 Iterator<RequestAttributes> iterResources = stdreq.getRequestAttributes(XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE);
91 assertTrue(iterResources.hasNext());
92 while (iterResources.hasNext()) {
93 RequestAttributes attrs = iterResources.next();
94 assertTrue(attrs.hasAttributes(new IdentifierImpl(ToscaDictionary.ID_RESOURCE_MATCHABLE + "resource1")));
101 public void testCreateInstance_StringValues() {
102 resources.put(StdMatchablePolicyRequest.POLICY_SCOPE_KEY, POLICY_SCOPE);
103 resources.put(StdMatchablePolicyRequest.POLICY_SCOPE_KEY + "-x", "unused value");
104 resources.put(StdMatchablePolicyRequest.POLICY_TYPE_KEY, POLICY_TYPE);
106 stdreq = StdMatchablePolicyRequest.createInstance(decreq);
108 Collection<String> res = stdreq.getPolicyScopes();
109 assertFalse(res.isEmpty());
110 assertEquals(POLICY_SCOPE, res.iterator().next());
112 res = stdreq.getPolicyTypes();
113 assertFalse(res.isEmpty());
114 assertEquals(POLICY_TYPE, res.iterator().next());
118 public void testCreateInstance_Collections() {
119 resources.put(StdMatchablePolicyRequest.POLICY_SCOPE_KEY, Collections.singleton(POLICY_SCOPE));
120 resources.put(StdMatchablePolicyRequest.POLICY_TYPE_KEY, Collections.singleton(POLICY_TYPE));
122 stdreq = StdMatchablePolicyRequest.createInstance(decreq);
124 Collection<String> res = stdreq.getPolicyScopes();
125 assertFalse(res.isEmpty());
126 assertEquals(POLICY_SCOPE, res.iterator().next());
128 res = stdreq.getPolicyTypes();
129 assertFalse(res.isEmpty());
130 assertEquals(POLICY_TYPE, res.iterator().next());