Fix sonars from dependency upgrade
[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  * ================================================================================
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.Request;
28 import com.att.research.xacml.api.RequestAttributes;
29 import com.att.research.xacml.api.XACML3;
30 import com.att.research.xacml.std.IdentifierImpl;
31 import java.util.Arrays;
32 import java.util.Iterator;
33 import java.util.Map;
34 import java.util.TreeMap;
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 import org.mockito.Mock;
39 import org.mockito.junit.MockitoJUnitRunner;
40 import org.onap.policy.models.decisions.concepts.DecisionRequest;
41 import org.onap.policy.pdp.xacml.application.common.ToscaDictionary;
42 import org.onap.policy.pdp.xacml.application.common.XacmlApplicationException;
43
44 @RunWith(MockitoJUnitRunner.class)
45 public class StdMatchablePolicyRequestTest {
46     private static final String ACTION = "my-action";
47     private static final String ONAP_NAME = "my-name";
48     private static final String ONAP_INSTANCE = "my-instance";
49     private static final String ONAP_COMPONENT = "my-component";
50     private static final String RESOURCE1 = "my-scope";
51     private static final String RESOURCE2 = "my-service";
52     private static final String RESOURCE3 = "my-geography1";
53     private static final String RESOURCE4 = "my-geography2";
54
55     @Mock
56     private DecisionRequest decreq;
57
58     private Map<String, Object> resources;
59
60     private Request stdreq;
61
62     /**
63      * Initializes objects.
64      */
65     @Before
66     public void setUp() {
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 XacmlApplicationException {
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 }