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.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;
29 import java.util.Collection;
30 import java.util.Collections;
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;
39 public class StdCombinedPolicyRequestTest {
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_ID = "my-policy";
45 private static final String POLICY_TYPE = "my-type";
48 private DecisionRequest decreq;
50 private Map<String, Object> resources;
52 private StdCombinedPolicyRequest stdreq;
55 * Initializes objects.
59 MockitoAnnotations.initMocks(this);
61 resources = new TreeMap<>();
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);
71 public void testCreateInstance() {
72 resources.put(StdCombinedPolicyRequest.POLICY_ID_KEY, 100);
73 resources.put(StdCombinedPolicyRequest.POLICY_TYPE_KEY, 101);
75 stdreq = StdCombinedPolicyRequest.createInstance(decreq);
77 assertNotNull(stdreq);
79 assertEquals(ACTION, stdreq.getAction());
80 assertEquals(ONAP_COMPONENT, stdreq.getOnapComponent());
81 assertEquals(ONAP_INSTANCE, stdreq.getOnapInstance());
82 assertEquals(ONAP_NAME, stdreq.getOnapName());
84 assertTrue(stdreq.getResource().isEmpty());
85 assertTrue(stdreq.getResourcePolicyType().isEmpty());
89 public void testCreateInstance_StringValues() {
90 resources.put(StdCombinedPolicyRequest.POLICY_ID_KEY, POLICY_ID);
91 resources.put(StdCombinedPolicyRequest.POLICY_ID_KEY + "-x", "unused value");
92 resources.put(StdCombinedPolicyRequest.POLICY_TYPE_KEY, POLICY_TYPE);
94 stdreq = StdCombinedPolicyRequest.createInstance(decreq);
96 Collection<String> res = stdreq.getResource();
97 assertFalse(res.isEmpty());
98 assertEquals(POLICY_ID, res.iterator().next());
100 res = stdreq.getResourcePolicyType();
101 assertFalse(res.isEmpty());
102 assertEquals(POLICY_TYPE, res.iterator().next());
106 public void testCreateInstance_Collections() {
107 resources.put(StdCombinedPolicyRequest.POLICY_ID_KEY, Collections.singleton(POLICY_ID));
108 resources.put(StdCombinedPolicyRequest.POLICY_TYPE_KEY, Collections.singleton(POLICY_TYPE));
110 stdreq = StdCombinedPolicyRequest.createInstance(decreq);
112 Collection<String> res = stdreq.getResource();
113 assertFalse(res.isEmpty());
114 assertEquals(POLICY_ID, res.iterator().next());
116 res = stdreq.getResourcePolicyType();
117 assertFalse(res.isEmpty());
118 assertEquals(POLICY_TYPE, res.iterator().next());