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.assertNull;
25 import static org.junit.Assert.assertSame;
26 import static org.junit.Assert.assertTrue;
27 import static org.mockito.Mockito.mock;
28 import static org.mockito.Mockito.when;
30 import com.att.research.xacml.api.Attribute;
31 import com.att.research.xacml.api.AttributeValue;
32 import com.att.research.xacml.api.DataTypeException;
33 import com.att.research.xacml.api.Identifier;
34 import com.att.research.xacml.api.Status;
35 import com.att.research.xacml.api.XACML3;
36 import com.att.research.xacml.api.pip.PIPException;
37 import com.att.research.xacml.api.pip.PIPFinder;
38 import com.att.research.xacml.api.pip.PIPRequest;
39 import com.att.research.xacml.api.pip.PIPResponse;
40 import com.att.research.xacml.std.datatypes.DataTypes;
41 import com.att.research.xacml.std.pip.StdMutablePIPResponse;
42 import java.math.BigInteger;
43 import java.util.Collection;
44 import java.util.Collections;
45 import java.util.Iterator;
46 import java.util.Properties;
47 import org.junit.Before;
48 import org.junit.Test;
49 import org.mockito.Mock;
50 import org.mockito.MockitoAnnotations;
51 import org.onap.policy.pdp.xacml.application.common.ToscaDictionary;
53 public class StdOnapPipTest {
54 private static final String EXPECTED_EXCEPTION = "expected exception";
55 private static final String MY_ID = "my-id";
56 private static final String ISSUER = "my-issuer";
57 private static final String STRING_VALUE = "my-value";
59 private static final int INT_VALUE = 100;
60 private static final long LONG_VALUE = 200L;
62 private static final Identifier CATEGORY = XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE;
63 private static final Identifier ATTRIBUTE_ID = ToscaDictionary.ID_RESOURCE_GUARD_ACTOR;
66 private PIPRequest request;
69 private PIPFinder finder;
71 private StdMutablePIPResponse resp;
73 private StdOnapPip pip;
76 * Initializes objects, including the PIP.
78 * @throws PIPException if an error occurs
81 public void setUp() throws PIPException {
82 MockitoAnnotations.initMocks(this);
84 resp = new StdMutablePIPResponse();
86 when(request.getIssuer()).thenReturn(ISSUER);
87 when(request.getAttributeId()).thenReturn(ATTRIBUTE_ID);
91 when(finder.getMatchingAttributes(request, pip)).thenReturn(resp);
95 public void testAttributesProvided() {
96 assertTrue(pip.attributesProvided().isEmpty());
100 public void testConfigureStringProperties() throws PIPException {
101 Properties props = new Properties();
102 pip.configure(MY_ID, props);
104 assertEquals(MY_ID, pip.getName());
105 assertSame(props, pip.properties);
109 public void testGetAttributePipFinderPipRequest_NullResponse() {
110 assertNull(pip.getAttribute(finder, request));
114 public void testGetAttributePipFinderPipRequest() {
115 pip.addStringAttribute(resp, CATEGORY, CATEGORY, STRING_VALUE, request);
117 assertEquals(STRING_VALUE, pip.getAttribute(finder, request));
121 public void testGetAttributePipRequestPipFinder_NoStatus() {
122 resp.setStatus(null);
123 pip.addStringAttribute(resp, CATEGORY, CATEGORY, STRING_VALUE, request);
125 assertSame(resp, pip.getAttribute(request, finder));
129 public void testGetAttributePipRequestPipFinder_StatusNotOk() {
130 Status status = mock(Status.class);
131 when(status.isOk()).thenReturn(false);
132 resp.setStatus(status);
134 pip.addStringAttribute(resp, CATEGORY, CATEGORY, STRING_VALUE, request);
136 assertNull(pip.getAttribute(request, finder));
140 public void testGetAttributePipRequestPipFinder_StatusOk() {
141 Status status = mock(Status.class);
142 when(status.isOk()).thenReturn(true);
143 resp.setStatus(status);
145 pip.addStringAttribute(resp, CATEGORY, CATEGORY, STRING_VALUE, request);
147 assertSame(resp, pip.getAttribute(request, finder));
151 public void testGetAttributePipRequestPipFinder_NoAttributes() {
152 assertNull(pip.getAttribute(request, finder));
156 public void testGetAttributePipRequestPipFinder_Ex() throws PIPException {
157 when(finder.getMatchingAttributes(request, pip)).thenThrow(new PIPException(EXPECTED_EXCEPTION));
159 pip.addStringAttribute(resp, CATEGORY, CATEGORY, STRING_VALUE, request);
161 assertNull(pip.getAttribute(request, finder));
165 public void testFindFirstAttributeValue_NoAttributes() {
166 assertNull(pip.findFirstAttributeValue(resp));
170 public void testFindFirstAttributeValue_NullAttributeValue() {
171 pip.addIntegerAttribute(resp, CATEGORY, ATTRIBUTE_ID, INT_VALUE, request);
173 assertNull(pip.findFirstAttributeValue(resp));
177 public void testFindFirstAttributeValue_NullValues() {
178 pip.addStringAttribute(resp, CATEGORY, ATTRIBUTE_ID, null, request);
179 pip.addStringAttribute(resp, CATEGORY, ATTRIBUTE_ID, STRING_VALUE, request);
180 pip.addStringAttribute(resp, CATEGORY, ATTRIBUTE_ID, null, request);
182 assertEquals(STRING_VALUE, pip.findFirstAttributeValue(resp));
186 public void testAddIntegerAttribute() {
187 pip.addIntegerAttribute(resp, CATEGORY, ATTRIBUTE_ID, INT_VALUE, request);
188 assertEquals(1, resp.getAttributes().size());
190 Attribute attr = resp.getAttributes().iterator().next();
191 assertEquals(ISSUER, attr.getIssuer());
192 assertEquals(CATEGORY, attr.getCategory());
193 assertEquals(ATTRIBUTE_ID, attr.getAttributeId());
195 Iterator<AttributeValue<BigInteger>> attrValues = attr.findValues(DataTypes.DT_INTEGER);
196 assertTrue(attrValues.hasNext());
197 assertEquals(INT_VALUE, attrValues.next().getValue().intValue());
201 public void testAddIntegerAttribute_Ex() {
204 protected AttributeValue<BigInteger> makeInteger(int value) throws DataTypeException {
205 throw new RuntimeException(EXPECTED_EXCEPTION);
208 pip.addIntegerAttribute(resp, CATEGORY, ATTRIBUTE_ID, INT_VALUE, request);
209 assertEquals(0, resp.getAttributes().size());
213 public void testAddIntegerAttribute_Null() {
216 protected AttributeValue<BigInteger> makeInteger(int value) throws DataTypeException {
220 pip.addIntegerAttribute(resp, CATEGORY, ATTRIBUTE_ID, INT_VALUE, request);
221 assertEquals(0, resp.getAttributes().size());
225 public void testAddLongAttribute() {
226 pip.addLongAttribute(resp, CATEGORY, ATTRIBUTE_ID, LONG_VALUE, request);
227 assertEquals(1, resp.getAttributes().size());
229 Attribute attr = resp.getAttributes().iterator().next();
230 assertEquals(ISSUER, attr.getIssuer());
231 assertEquals(CATEGORY, attr.getCategory());
232 assertEquals(ATTRIBUTE_ID, attr.getAttributeId());
234 Iterator<AttributeValue<BigInteger>> attrValues = attr.findValues(DataTypes.DT_INTEGER);
235 assertTrue(attrValues.hasNext());
236 assertEquals(LONG_VALUE, attrValues.next().getValue().longValue());
240 public void testAddLongAttribute_Ex() {
243 protected AttributeValue<BigInteger> makeLong(long value) throws DataTypeException {
244 throw new RuntimeException(EXPECTED_EXCEPTION);
247 pip.addLongAttribute(resp, CATEGORY, ATTRIBUTE_ID, LONG_VALUE, request);
248 assertEquals(0, resp.getAttributes().size());
252 public void testAddLongAttribute_NullAttrValue() {
255 protected AttributeValue<BigInteger> makeLong(long value) throws DataTypeException {
259 pip.addLongAttribute(resp, CATEGORY, ATTRIBUTE_ID, LONG_VALUE, request);
260 assertEquals(0, resp.getAttributes().size());
264 public void testAddStringAttribute() {
265 pip.addStringAttribute(resp, CATEGORY, ATTRIBUTE_ID, STRING_VALUE, request);
266 assertEquals(1, resp.getAttributes().size());
268 Attribute attr = resp.getAttributes().iterator().next();
269 assertEquals(ISSUER, attr.getIssuer());
270 assertEquals(CATEGORY, attr.getCategory());
271 assertEquals(ATTRIBUTE_ID, attr.getAttributeId());
273 Iterator<AttributeValue<String>> attrValues = attr.findValues(DataTypes.DT_STRING);
274 assertTrue(attrValues.hasNext());
275 assertEquals(STRING_VALUE, attrValues.next().getValue());
279 public void testAddStringAttribute_Ex() {
282 protected AttributeValue<String> makeString(String value) throws DataTypeException {
283 throw new RuntimeException(EXPECTED_EXCEPTION);
286 pip.addStringAttribute(resp, CATEGORY, ATTRIBUTE_ID, STRING_VALUE, request);
287 assertEquals(0, resp.getAttributes().size());
291 public void testAddStringAttribute_NullAttrValue() {
294 protected AttributeValue<String> makeString(String value) throws DataTypeException {
298 pip.addStringAttribute(resp, CATEGORY, ATTRIBUTE_ID, STRING_VALUE, request);
299 assertEquals(0, resp.getAttributes().size());
302 private class MyPip extends StdOnapPip {
305 public Collection<PIPRequest> attributesRequired() {
306 return Collections.emptyList();
310 public PIPResponse getAttributes(PIPRequest pipRequest, PIPFinder pipFinder) throws PIPException {