00c86f25a43d164a3e9b4b60e62ad8757188a941
[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.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertTrue;
27 import static org.mockito.Mockito.when;
28
29 import java.util.Collection;
30 import java.util.Collections;
31 import java.util.Map;
32 import java.util.TreeMap;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.mockito.Mock;
36 import org.mockito.MockitoAnnotations;
37 import org.onap.policy.models.decisions.concepts.DecisionRequest;
38
39 public class StdMatchablePolicyRequestTest {
40     private static final String ACTION = "my-action";
41     private static final String ONAP_NAME = "my-name";
42     private static final String ONAP_INSTANCE = "my-instance";
43     private static final String ONAP_COMPONENT = "my-component";
44     private static final String POLICY_SCOPE = "my-scope";
45     private static final String POLICY_TYPE = "my-type";
46
47     @Mock
48     private DecisionRequest decreq;
49
50     private Map<String, Object> resources;
51
52     private StdMatchablePolicyRequest stdreq;
53
54     /**
55      * Initializes objects.
56      */
57     @Before
58     public void setUp() {
59         MockitoAnnotations.initMocks(this);
60
61         resources = new TreeMap<>();
62
63         when(decreq.getResource()).thenReturn(resources);
64         when(decreq.getAction()).thenReturn(ACTION);
65         when(decreq.getOnapComponent()).thenReturn(ONAP_COMPONENT);
66         when(decreq.getOnapInstance()).thenReturn(ONAP_INSTANCE);
67         when(decreq.getOnapName()).thenReturn(ONAP_NAME);
68     }
69
70     @Test
71     public void testCreateInstance() {
72         resources.put(StdMatchablePolicyRequest.POLICY_SCOPE_KEY, 100);
73         resources.put(StdMatchablePolicyRequest.POLICY_TYPE_KEY, 101);
74
75         stdreq = StdMatchablePolicyRequest.createInstance(decreq);
76
77         assertNotNull(stdreq);
78
79         assertEquals(ACTION, stdreq.getAction());
80         assertEquals(ONAP_COMPONENT, stdreq.getOnapComponent());
81         assertEquals(ONAP_INSTANCE, stdreq.getOnapInstance());
82         assertEquals(ONAP_NAME, stdreq.getOnapName());
83
84         assertTrue(stdreq.getPolicyScopes().isEmpty());
85         assertTrue(stdreq.getPolicyTypes().isEmpty());
86     }
87
88     @Test
89     public void testCreateInstance_StringValues() {
90         resources.put(StdMatchablePolicyRequest.POLICY_SCOPE_KEY, POLICY_SCOPE);
91         resources.put(StdMatchablePolicyRequest.POLICY_SCOPE_KEY + "-x", "unused value");
92         resources.put(StdMatchablePolicyRequest.POLICY_TYPE_KEY, POLICY_TYPE);
93
94         stdreq = StdMatchablePolicyRequest.createInstance(decreq);
95
96         Collection<String> res = stdreq.getPolicyScopes();
97         assertFalse(res.isEmpty());
98         assertEquals(POLICY_SCOPE, res.iterator().next());
99
100         res = stdreq.getPolicyTypes();
101         assertFalse(res.isEmpty());
102         assertEquals(POLICY_TYPE, res.iterator().next());
103     }
104
105     @Test
106     public void testCreateInstance_Collections() {
107         resources.put(StdMatchablePolicyRequest.POLICY_SCOPE_KEY, Collections.singleton(POLICY_SCOPE));
108         resources.put(StdMatchablePolicyRequest.POLICY_TYPE_KEY, Collections.singleton(POLICY_TYPE));
109
110         stdreq = StdMatchablePolicyRequest.createInstance(decreq);
111
112         Collection<String> res = stdreq.getPolicyScopes();
113         assertFalse(res.isEmpty());
114         assertEquals(POLICY_SCOPE, res.iterator().next());
115
116         res = stdreq.getPolicyTypes();
117         assertFalse(res.isEmpty());
118         assertEquals(POLICY_TYPE, res.iterator().next());
119     }
120
121 }