Integrate using Policy Type to find Matchable
[policy/xacml-pdp.git] / applications / common / src / test / java / org / onap / policy / pdp / xacml / application / common / std / StdMatchablePolicyRequestTest.java
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.pdp.xacml.application.common.std;
22
23 import static org.junit.Assert.assertNotNull;
24 import static org.junit.Assert.assertTrue;
25 import static org.mockito.Mockito.when;
26
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;
34 import java.util.Map;
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;
42
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";
52
53     @Mock
54     private DecisionRequest decreq;
55
56     private Map<String, Object> resources;
57
58     private Request stdreq;
59
60     /**
61      * Initializes objects.
62      */
63     @Before
64     public void setUp() {
65         MockitoAnnotations.initMocks(this);
66
67         resources = new TreeMap<>();
68
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);
74     }
75
76     @Test
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));
81
82         stdreq = StdMatchablePolicyRequest.createInstance(decreq);
83
84         assertNotNull(stdreq);
85
86         assertTrue(stdreq.getRequestAttributes(XACML3.ID_ATTRIBUTE_CATEGORY_ACTION).hasNext());
87         assertTrue(stdreq.getRequestAttributes(XACML3.ID_SUBJECT_CATEGORY_ACCESS_SUBJECT).hasNext());
88
89
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")));
95         }
96
97     }
98
99     /*
100     @Test
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);
105
106         stdreq = StdMatchablePolicyRequest.createInstance(decreq);
107
108         Collection<String> res = stdreq.getPolicyScopes();
109         assertFalse(res.isEmpty());
110         assertEquals(POLICY_SCOPE, res.iterator().next());
111
112         res = stdreq.getPolicyTypes();
113         assertFalse(res.isEmpty());
114         assertEquals(POLICY_TYPE, res.iterator().next());
115     }
116
117     @Test
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));
121
122         stdreq = StdMatchablePolicyRequest.createInstance(decreq);
123
124         Collection<String> res = stdreq.getPolicyScopes();
125         assertFalse(res.isEmpty());
126         assertEquals(POLICY_SCOPE, res.iterator().next());
127
128         res = stdreq.getPolicyTypes();
129         assertFalse(res.isEmpty());
130         assertEquals(POLICY_TYPE, res.iterator().next());
131     }
132 */
133
134 }