2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
4 * Modifications Copyright (C) 2024 Nordix Foundation.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.pdp.xacml.application.common.std;
24 import static org.assertj.core.api.Assertions.assertThatCode;
25 import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
26 import static org.junit.jupiter.api.Assertions.assertEquals;
27 import static org.junit.jupiter.api.Assertions.assertNull;
28 import static org.junit.jupiter.api.Assertions.assertSame;
29 import static org.junit.jupiter.api.Assertions.assertTrue;
30 import static org.mockito.Mockito.lenient;
31 import static org.mockito.Mockito.mock;
32 import static org.mockito.Mockito.when;
34 import com.att.research.xacml.api.Attribute;
35 import com.att.research.xacml.api.AttributeValue;
36 import com.att.research.xacml.api.DataTypeException;
37 import com.att.research.xacml.api.Identifier;
38 import com.att.research.xacml.api.Status;
39 import com.att.research.xacml.api.XACML3;
40 import com.att.research.xacml.api.pip.PIPException;
41 import com.att.research.xacml.api.pip.PIPFinder;
42 import com.att.research.xacml.api.pip.PIPRequest;
43 import com.att.research.xacml.api.pip.PIPResponse;
44 import com.att.research.xacml.std.datatypes.DataTypes;
45 import com.att.research.xacml.std.pip.StdMutablePIPResponse;
46 import java.math.BigInteger;
47 import java.util.Collection;
48 import java.util.Collections;
49 import java.util.Iterator;
50 import java.util.Properties;
51 import org.junit.jupiter.api.BeforeEach;
52 import org.junit.jupiter.api.Test;
53 import org.junit.jupiter.api.extension.ExtendWith;
54 import org.mockito.Mock;
55 import org.mockito.junit.jupiter.MockitoExtension;
56 import org.onap.policy.pdp.xacml.application.common.ToscaDictionary;
58 @ExtendWith(MockitoExtension.class)
59 class StdOnapPipTest {
60 private static final String EXPECTED_EXCEPTION = "expected exception";
61 private static final String MY_ID = "my-id";
62 private static final String ISSUER = "my-issuer";
63 private static final String STRING_VALUE = "my-value";
65 private static final int INT_VALUE = 100;
66 private static final long LONG_VALUE = 200L;
68 private static final Identifier CATEGORY = XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE;
69 private static final Identifier ATTRIBUTE_ID = ToscaDictionary.ID_RESOURCE_GUARD_ACTOR;
72 private PIPRequest request;
75 private PIPFinder finder;
77 private StdMutablePIPResponse resp;
79 private StdOnapPip pip;
82 * Initializes objects, including the PIP.
84 * @throws PIPException if an error occurs
87 public void setUp() throws PIPException {
88 resp = new StdMutablePIPResponse();
90 lenient().when(request.getIssuer()).thenReturn(ISSUER);
91 lenient().when(request.getAttributeId()).thenReturn(ATTRIBUTE_ID);
95 lenient().when(finder.getMatchingAttributes(request, pip)).thenReturn(resp);
99 void testAttributesProvided() {
100 assertTrue(pip.attributesProvided().isEmpty());
104 void testConfigureStringProperties() throws PIPException {
105 Properties props = new Properties();
106 pip.configure(MY_ID, props);
108 assertEquals(MY_ID, pip.getName());
109 assertSame(props, pip.properties);
113 void testGetAttributePipFinderPipRequest_NullResponse() {
114 assertNull(pip.getAttribute(finder, request));
118 void testGetAttributePipFinderPipRequest() {
119 pip.addStringAttribute(resp, CATEGORY, CATEGORY, STRING_VALUE, request);
121 assertEquals(STRING_VALUE, pip.getAttribute(finder, request));
125 void testGetAttributePipRequestPipFinder_NoStatus() {
126 resp.setStatus(null);
127 pip.addStringAttribute(resp, CATEGORY, CATEGORY, STRING_VALUE, request);
129 assertSame(resp, pip.getAttribute(request, finder));
133 void testGetAttributePipRequestPipFinder_StatusNotOk() {
134 Status status = mock(Status.class);
135 when(status.isOk()).thenReturn(false);
136 resp.setStatus(status);
138 pip.addStringAttribute(resp, CATEGORY, CATEGORY, STRING_VALUE, request);
140 assertNull(pip.getAttribute(request, finder));
144 void testGetAttributePipRequestPipFinder_StatusOk() {
145 Status status = mock(Status.class);
146 when(status.isOk()).thenReturn(true);
147 resp.setStatus(status);
149 pip.addStringAttribute(resp, CATEGORY, CATEGORY, STRING_VALUE, request);
151 assertSame(resp, pip.getAttribute(request, finder));
155 void testGetAttributePipRequestPipFinder_NoAttributes() {
156 assertNull(pip.getAttribute(request, finder));
160 void testGetAttributePipRequestPipFinder_Ex() throws PIPException {
161 when(finder.getMatchingAttributes(request, pip)).thenThrow(new PIPException(EXPECTED_EXCEPTION));
163 pip.addStringAttribute(resp, CATEGORY, CATEGORY, STRING_VALUE, request);
165 assertNull(pip.getAttribute(request, finder));
169 void testFindFirstAttributeValue_NoAttributes() {
170 assertNull(pip.findFirstAttributeValue(resp));
174 void testFindFirstAttributeValue_NullAttributeValue() {
175 pip.addIntegerAttribute(resp, CATEGORY, ATTRIBUTE_ID, INT_VALUE, request);
177 assertNull(pip.findFirstAttributeValue(resp));
181 void testFindFirstAttributeValue_NullValues() {
182 pip.addStringAttribute(resp, CATEGORY, ATTRIBUTE_ID, null, request);
183 pip.addStringAttribute(resp, CATEGORY, ATTRIBUTE_ID, STRING_VALUE, request);
184 pip.addStringAttribute(resp, CATEGORY, ATTRIBUTE_ID, null, request);
186 assertEquals(STRING_VALUE, pip.findFirstAttributeValue(resp));
190 void testAddIntegerAttribute() {
191 pip.addIntegerAttribute(resp, CATEGORY, ATTRIBUTE_ID, INT_VALUE, request);
192 assertEquals(1, resp.getAttributes().size());
194 Attribute attr = resp.getAttributes().iterator().next();
195 assertEquals(ISSUER, attr.getIssuer());
196 assertEquals(CATEGORY, attr.getCategory());
197 assertEquals(ATTRIBUTE_ID, attr.getAttributeId());
199 Iterator<AttributeValue<BigInteger>> attrValues = attr.findValues(DataTypes.DT_INTEGER);
200 assertTrue(attrValues.hasNext());
201 assertEquals(INT_VALUE, attrValues.next().getValue().intValue());
205 void testAddIntegerAttribute_Ex() {
208 protected AttributeValue<BigInteger> makeInteger(int value) throws DataTypeException {
209 throw new RuntimeException(EXPECTED_EXCEPTION);
212 pip.addIntegerAttribute(resp, CATEGORY, ATTRIBUTE_ID, INT_VALUE, request);
213 assertEquals(0, resp.getAttributes().size());
217 void testAddIntegerAttribute_Null() {
220 protected AttributeValue<BigInteger> makeInteger(int value) throws DataTypeException {
224 pip.addIntegerAttribute(resp, CATEGORY, ATTRIBUTE_ID, INT_VALUE, request);
225 assertEquals(0, resp.getAttributes().size());
229 void testAddLongAttribute() {
230 pip.addLongAttribute(resp, CATEGORY, ATTRIBUTE_ID, LONG_VALUE, request);
231 assertEquals(1, resp.getAttributes().size());
233 Attribute attr = resp.getAttributes().iterator().next();
234 assertEquals(ISSUER, attr.getIssuer());
235 assertEquals(CATEGORY, attr.getCategory());
236 assertEquals(ATTRIBUTE_ID, attr.getAttributeId());
238 Iterator<AttributeValue<BigInteger>> attrValues = attr.findValues(DataTypes.DT_INTEGER);
239 assertTrue(attrValues.hasNext());
240 assertEquals(LONG_VALUE, attrValues.next().getValue().longValue());
244 void testAddLongAttribute_Ex() {
247 protected AttributeValue<BigInteger> makeLong(long value) throws DataTypeException {
248 throw new RuntimeException(EXPECTED_EXCEPTION);
251 pip.addLongAttribute(resp, CATEGORY, ATTRIBUTE_ID, LONG_VALUE, request);
252 assertEquals(0, resp.getAttributes().size());
256 void testAddLongAttribute_NullAttrValue() {
259 protected AttributeValue<BigInteger> makeLong(long value) throws DataTypeException {
263 pip.addLongAttribute(resp, CATEGORY, ATTRIBUTE_ID, LONG_VALUE, request);
264 assertEquals(0, resp.getAttributes().size());
268 void testAddStringAttribute() {
269 pip.addStringAttribute(resp, CATEGORY, ATTRIBUTE_ID, STRING_VALUE, request);
270 assertEquals(1, resp.getAttributes().size());
272 Attribute attr = resp.getAttributes().iterator().next();
273 assertEquals(ISSUER, attr.getIssuer());
274 assertEquals(CATEGORY, attr.getCategory());
275 assertEquals(ATTRIBUTE_ID, attr.getAttributeId());
277 Iterator<AttributeValue<String>> attrValues = attr.findValues(DataTypes.DT_STRING);
278 assertTrue(attrValues.hasNext());
279 assertEquals(STRING_VALUE, attrValues.next().getValue());
283 void testAddStringAttribute_Ex() {
286 protected AttributeValue<String> makeString(String value) throws DataTypeException {
287 throw new RuntimeException(EXPECTED_EXCEPTION);
290 pip.addStringAttribute(resp, CATEGORY, ATTRIBUTE_ID, STRING_VALUE, request);
291 assertEquals(0, resp.getAttributes().size());
295 void testAddStringAttribute_NullAttrValue() {
298 protected AttributeValue<String> makeString(String value) throws DataTypeException {
302 pip.addStringAttribute(resp, CATEGORY, ATTRIBUTE_ID, STRING_VALUE, request);
303 assertEquals(0, resp.getAttributes().size());
307 void testShutdown() {
308 assertThatCode(() -> pip.shutdown()).doesNotThrowAnyException();
309 assertThatExceptionOfType(PIPException.class).isThrownBy(() -> pip.configure("foo", new Properties()))
310 .withMessageContaining("Engine is shutdown");
313 private static class MyPip extends StdOnapPip {
316 public Collection<PIPRequest> attributesRequired() {
317 return Collections.emptyList();
321 public PIPResponse getAttributes(PIPRequest pipRequest, PIPFinder pipFinder) {