Update Policy committers in policy/xacml-pdp
[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, 2021 AT&T Intellectual Property. All rights reserved.
4  * Modifications Copyright (C) 2024 Nordix Foundation.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.pdp.xacml.application.common.std;
23
24 import static org.junit.jupiter.api.Assertions.assertNotNull;
25 import static org.junit.jupiter.api.Assertions.assertTrue;
26 import static org.mockito.Mockito.when;
27
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.jupiter.api.BeforeEach;
37 import org.junit.jupiter.api.Test;
38 import org.junit.jupiter.api.extension.ExtendWith;
39 import org.mockito.Mock;
40 import org.mockito.junit.jupiter.MockitoExtension;
41 import org.onap.policy.models.decisions.concepts.DecisionRequest;
42 import org.onap.policy.pdp.xacml.application.common.ToscaDictionary;
43 import org.onap.policy.pdp.xacml.application.common.XacmlApplicationException;
44
45 @ExtendWith(MockitoExtension.class)
46 class StdMatchablePolicyRequestTest {
47     private static final String ACTION = "my-action";
48     private static final String ONAP_NAME = "my-name";
49     private static final String ONAP_INSTANCE = "my-instance";
50     private static final String ONAP_COMPONENT = "my-component";
51     private static final String RESOURCE1 = "my-scope";
52     private static final String RESOURCE2 = "my-service";
53     private static final String RESOURCE3 = "my-geography1";
54     private static final String RESOURCE4 = "my-geography2";
55
56     @Mock
57     private DecisionRequest decreq;
58
59     private Map<String, Object> resources;
60
61     /**
62      * Initializes objects.
63      */
64     @BeforeEach
65     void setUp() {
66         resources = new TreeMap<>();
67
68         when(decreq.getResource()).thenReturn(resources);
69         when(decreq.getAction()).thenReturn(ACTION);
70         when(decreq.getOnapComponent()).thenReturn(ONAP_COMPONENT);
71         when(decreq.getOnapInstance()).thenReturn(ONAP_INSTANCE);
72         when(decreq.getOnapName()).thenReturn(ONAP_NAME);
73     }
74
75     @Test
76     void testCreateInstance() throws XacmlApplicationException {
77         resources.put("resource1", RESOURCE1);
78         resources.put("resource2", RESOURCE2);
79         resources.put("resource3", Arrays.asList(RESOURCE3, RESOURCE4));
80
81         Request stdreq = StdMatchablePolicyRequest.createInstance(decreq);
82
83         assertNotNull(stdreq);
84
85         assertTrue(stdreq.getRequestAttributes(XACML3.ID_ATTRIBUTE_CATEGORY_ACTION).hasNext());
86         assertTrue(stdreq.getRequestAttributes(XACML3.ID_SUBJECT_CATEGORY_ACCESS_SUBJECT).hasNext());
87
88
89         Iterator<RequestAttributes> iterResources = stdreq.getRequestAttributes(XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE);
90         assertTrue(iterResources.hasNext());
91         while (iterResources.hasNext()) {
92             RequestAttributes attrs = iterResources.next();
93             assertTrue(attrs.hasAttributes(new IdentifierImpl(ToscaDictionary.ID_RESOURCE_MATCHABLE + "resource1")));
94         }
95
96     }
97
98 }