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
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.assertj.core.api.Assertions.assertThatCode;
24 import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNull;
27 import static org.junit.Assert.assertSame;
28 import static org.junit.Assert.assertTrue;
29 import static org.mockito.Mockito.mock;
30 import static org.mockito.Mockito.when;
32 import com.att.research.xacml.api.Attribute;
33 import com.att.research.xacml.api.AttributeValue;
34 import com.att.research.xacml.api.DataTypeException;
35 import com.att.research.xacml.api.Identifier;
36 import com.att.research.xacml.api.Status;
37 import com.att.research.xacml.api.XACML3;
38 import com.att.research.xacml.api.pip.PIPException;
39 import com.att.research.xacml.api.pip.PIPFinder;
40 import com.att.research.xacml.api.pip.PIPRequest;
41 import com.att.research.xacml.api.pip.PIPResponse;
42 import com.att.research.xacml.std.datatypes.DataTypes;
43 import com.att.research.xacml.std.pip.StdMutablePIPResponse;
44 import java.math.BigInteger;
45 import java.util.Collection;
46 import java.util.Collections;
47 import java.util.Iterator;
48 import java.util.Properties;
49 import org.junit.Before;
50 import org.junit.Test;
51 import org.junit.runner.RunWith;
52 import org.mockito.Mock;
53 import org.mockito.junit.MockitoJUnitRunner;
54 import org.onap.policy.pdp.xacml.application.common.ToscaDictionary;
56 @RunWith(MockitoJUnitRunner.class)
57 public class StdOnapPipTest {
58 private static final String EXPECTED_EXCEPTION = "expected exception";
59 private static final String MY_ID = "my-id";
60 private static final String ISSUER = "my-issuer";
61 private static final String STRING_VALUE = "my-value";
63 private static final int INT_VALUE = 100;
64 private static final long LONG_VALUE = 200L;
66 private static final Identifier CATEGORY = XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE;
67 private static final Identifier ATTRIBUTE_ID = ToscaDictionary.ID_RESOURCE_GUARD_ACTOR;
70 private PIPRequest request;
73 private PIPFinder finder;
75 private StdMutablePIPResponse resp;
77 private StdOnapPip pip;
80 * Initializes objects, including the PIP.
82 * @throws PIPException if an error occurs
85 public void setUp() throws PIPException {
86 resp = new StdMutablePIPResponse();
88 when(request.getIssuer()).thenReturn(ISSUER);
89 when(request.getAttributeId()).thenReturn(ATTRIBUTE_ID);
93 when(finder.getMatchingAttributes(request, pip)).thenReturn(resp);
97 public void testAttributesProvided() {
98 assertTrue(pip.attributesProvided().isEmpty());
102 public void testConfigureStringProperties() throws PIPException {
103 Properties props = new Properties();
104 pip.configure(MY_ID, props);
106 assertEquals(MY_ID, pip.getName());
107 assertSame(props, pip.properties);
111 public void testGetAttributePipFinderPipRequest_NullResponse() {
112 assertNull(pip.getAttribute(finder, request));
116 public void testGetAttributePipFinderPipRequest() {
117 pip.addStringAttribute(resp, CATEGORY, CATEGORY, STRING_VALUE, request);
119 assertEquals(STRING_VALUE, pip.getAttribute(finder, request));
123 public void testGetAttributePipRequestPipFinder_NoStatus() {
124 resp.setStatus(null);
125 pip.addStringAttribute(resp, CATEGORY, CATEGORY, STRING_VALUE, request);
127 assertSame(resp, pip.getAttribute(request, finder));
131 public void testGetAttributePipRequestPipFinder_StatusNotOk() {
132 Status status = mock(Status.class);
133 when(status.isOk()).thenReturn(false);
134 resp.setStatus(status);
136 pip.addStringAttribute(resp, CATEGORY, CATEGORY, STRING_VALUE, request);
138 assertNull(pip.getAttribute(request, finder));
142 public void testGetAttributePipRequestPipFinder_StatusOk() {
143 Status status = mock(Status.class);
144 when(status.isOk()).thenReturn(true);
145 resp.setStatus(status);
147 pip.addStringAttribute(resp, CATEGORY, CATEGORY, STRING_VALUE, request);
149 assertSame(resp, pip.getAttribute(request, finder));
153 public void testGetAttributePipRequestPipFinder_NoAttributes() {
154 assertNull(pip.getAttribute(request, finder));
158 public void testGetAttributePipRequestPipFinder_Ex() throws PIPException {
159 when(finder.getMatchingAttributes(request, pip)).thenThrow(new PIPException(EXPECTED_EXCEPTION));
161 pip.addStringAttribute(resp, CATEGORY, CATEGORY, STRING_VALUE, request);
163 assertNull(pip.getAttribute(request, finder));
167 public void testFindFirstAttributeValue_NoAttributes() {
168 assertNull(pip.findFirstAttributeValue(resp));
172 public void testFindFirstAttributeValue_NullAttributeValue() {
173 pip.addIntegerAttribute(resp, CATEGORY, ATTRIBUTE_ID, INT_VALUE, request);
175 assertNull(pip.findFirstAttributeValue(resp));
179 public void testFindFirstAttributeValue_NullValues() {
180 pip.addStringAttribute(resp, CATEGORY, ATTRIBUTE_ID, null, request);
181 pip.addStringAttribute(resp, CATEGORY, ATTRIBUTE_ID, STRING_VALUE, request);
182 pip.addStringAttribute(resp, CATEGORY, ATTRIBUTE_ID, null, request);
184 assertEquals(STRING_VALUE, pip.findFirstAttributeValue(resp));
188 public void testAddIntegerAttribute() {
189 pip.addIntegerAttribute(resp, CATEGORY, ATTRIBUTE_ID, INT_VALUE, request);
190 assertEquals(1, resp.getAttributes().size());
192 Attribute attr = resp.getAttributes().iterator().next();
193 assertEquals(ISSUER, attr.getIssuer());
194 assertEquals(CATEGORY, attr.getCategory());
195 assertEquals(ATTRIBUTE_ID, attr.getAttributeId());
197 Iterator<AttributeValue<BigInteger>> attrValues = attr.findValues(DataTypes.DT_INTEGER);
198 assertTrue(attrValues.hasNext());
199 assertEquals(INT_VALUE, attrValues.next().getValue().intValue());
203 public void testAddIntegerAttribute_Ex() {
206 protected AttributeValue<BigInteger> makeInteger(int value) throws DataTypeException {
207 throw new RuntimeException(EXPECTED_EXCEPTION);
210 pip.addIntegerAttribute(resp, CATEGORY, ATTRIBUTE_ID, INT_VALUE, request);
211 assertEquals(0, resp.getAttributes().size());
215 public void testAddIntegerAttribute_Null() {
218 protected AttributeValue<BigInteger> makeInteger(int value) throws DataTypeException {
222 pip.addIntegerAttribute(resp, CATEGORY, ATTRIBUTE_ID, INT_VALUE, request);
223 assertEquals(0, resp.getAttributes().size());
227 public void testAddLongAttribute() {
228 pip.addLongAttribute(resp, CATEGORY, ATTRIBUTE_ID, LONG_VALUE, request);
229 assertEquals(1, resp.getAttributes().size());
231 Attribute attr = resp.getAttributes().iterator().next();
232 assertEquals(ISSUER, attr.getIssuer());
233 assertEquals(CATEGORY, attr.getCategory());
234 assertEquals(ATTRIBUTE_ID, attr.getAttributeId());
236 Iterator<AttributeValue<BigInteger>> attrValues = attr.findValues(DataTypes.DT_INTEGER);
237 assertTrue(attrValues.hasNext());
238 assertEquals(LONG_VALUE, attrValues.next().getValue().longValue());
242 public void testAddLongAttribute_Ex() {
245 protected AttributeValue<BigInteger> makeLong(long value) throws DataTypeException {
246 throw new RuntimeException(EXPECTED_EXCEPTION);
249 pip.addLongAttribute(resp, CATEGORY, ATTRIBUTE_ID, LONG_VALUE, request);
250 assertEquals(0, resp.getAttributes().size());
254 public void testAddLongAttribute_NullAttrValue() {
257 protected AttributeValue<BigInteger> makeLong(long value) throws DataTypeException {
261 pip.addLongAttribute(resp, CATEGORY, ATTRIBUTE_ID, LONG_VALUE, request);
262 assertEquals(0, resp.getAttributes().size());
266 public void testAddStringAttribute() {
267 pip.addStringAttribute(resp, CATEGORY, ATTRIBUTE_ID, STRING_VALUE, request);
268 assertEquals(1, resp.getAttributes().size());
270 Attribute attr = resp.getAttributes().iterator().next();
271 assertEquals(ISSUER, attr.getIssuer());
272 assertEquals(CATEGORY, attr.getCategory());
273 assertEquals(ATTRIBUTE_ID, attr.getAttributeId());
275 Iterator<AttributeValue<String>> attrValues = attr.findValues(DataTypes.DT_STRING);
276 assertTrue(attrValues.hasNext());
277 assertEquals(STRING_VALUE, attrValues.next().getValue());
281 public void testAddStringAttribute_Ex() {
284 protected AttributeValue<String> makeString(String value) throws DataTypeException {
285 throw new RuntimeException(EXPECTED_EXCEPTION);
288 pip.addStringAttribute(resp, CATEGORY, ATTRIBUTE_ID, STRING_VALUE, request);
289 assertEquals(0, resp.getAttributes().size());
293 public void testAddStringAttribute_NullAttrValue() {
296 protected AttributeValue<String> makeString(String value) throws DataTypeException {
300 pip.addStringAttribute(resp, CATEGORY, ATTRIBUTE_ID, STRING_VALUE, request);
301 assertEquals(0, resp.getAttributes().size());
305 public void testShutdown() {
306 assertThatCode(() -> pip.shutdown()).doesNotThrowAnyException();
307 assertThatExceptionOfType(PIPException.class).isThrownBy(() -> pip.configure("foo", new Properties()))
308 .withMessageContaining("Engine is shutdown");
311 private class MyPip extends StdOnapPip {
314 public Collection<PIPRequest> attributesRequired() {
315 return Collections.emptyList();
319 public PIPResponse getAttributes(PIPRequest pipRequest, PIPFinder pipFinder) throws PIPException {