4bacaf3c32709f33e0f429654eaecca87ed4f639
[policy/xacml-pdp.git] /
1 /*-
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.pdp.xacml.application.common.std;
23
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;
33
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;
57
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";
64
65     private static final int INT_VALUE = 100;
66     private static final long LONG_VALUE = 200L;
67
68     private static final Identifier CATEGORY = XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE;
69     private static final Identifier ATTRIBUTE_ID = ToscaDictionary.ID_RESOURCE_GUARD_ACTOR;
70
71     @Mock
72     private PIPRequest request;
73
74     @Mock
75     private PIPFinder finder;
76
77     private StdMutablePIPResponse resp;
78
79     private StdOnapPip pip;
80
81     /**
82      * Initializes objects, including the PIP.
83      *
84      * @throws PIPException if an error occurs
85      */
86     @BeforeEach
87     public void setUp() throws PIPException {
88         resp = new StdMutablePIPResponse();
89
90         lenient().when(request.getIssuer()).thenReturn(ISSUER);
91         lenient().when(request.getAttributeId()).thenReturn(ATTRIBUTE_ID);
92
93         pip = new MyPip();
94
95         lenient().when(finder.getMatchingAttributes(request, pip)).thenReturn(resp);
96     }
97
98     @Test
99     void testAttributesProvided() {
100         assertTrue(pip.attributesProvided().isEmpty());
101     }
102
103     @Test
104     void testConfigureStringProperties() throws PIPException {
105         Properties props = new Properties();
106         pip.configure(MY_ID, props);
107
108         assertEquals(MY_ID, pip.getName());
109         assertSame(props, pip.properties);
110     }
111
112     @Test
113     void testGetAttributePipFinderPipRequest_NullResponse() {
114         assertNull(pip.getAttribute(finder, request));
115     }
116
117     @Test
118     void testGetAttributePipFinderPipRequest() {
119         pip.addStringAttribute(resp, CATEGORY, CATEGORY, STRING_VALUE, request);
120
121         assertEquals(STRING_VALUE, pip.getAttribute(finder, request));
122     }
123
124     @Test
125     void testGetAttributePipRequestPipFinder_NoStatus() {
126         resp.setStatus(null);
127         pip.addStringAttribute(resp, CATEGORY, CATEGORY, STRING_VALUE, request);
128
129         assertSame(resp, pip.getAttribute(request, finder));
130     }
131
132     @Test
133     void testGetAttributePipRequestPipFinder_StatusNotOk() {
134         Status status = mock(Status.class);
135         when(status.isOk()).thenReturn(false);
136         resp.setStatus(status);
137
138         pip.addStringAttribute(resp, CATEGORY, CATEGORY, STRING_VALUE, request);
139
140         assertNull(pip.getAttribute(request, finder));
141     }
142
143     @Test
144     void testGetAttributePipRequestPipFinder_StatusOk() {
145         Status status = mock(Status.class);
146         when(status.isOk()).thenReturn(true);
147         resp.setStatus(status);
148
149         pip.addStringAttribute(resp, CATEGORY, CATEGORY, STRING_VALUE, request);
150
151         assertSame(resp, pip.getAttribute(request, finder));
152     }
153
154     @Test
155     void testGetAttributePipRequestPipFinder_NoAttributes() {
156         assertNull(pip.getAttribute(request, finder));
157     }
158
159     @Test
160     void testGetAttributePipRequestPipFinder_Ex() throws PIPException {
161         when(finder.getMatchingAttributes(request, pip)).thenThrow(new PIPException(EXPECTED_EXCEPTION));
162
163         pip.addStringAttribute(resp, CATEGORY, CATEGORY, STRING_VALUE, request);
164
165         assertNull(pip.getAttribute(request, finder));
166     }
167
168     @Test
169     void testFindFirstAttributeValue_NoAttributes() {
170         assertNull(pip.findFirstAttributeValue(resp));
171     }
172
173     @Test
174     void testFindFirstAttributeValue_NullAttributeValue() {
175         pip.addIntegerAttribute(resp, CATEGORY, ATTRIBUTE_ID, INT_VALUE, request);
176
177         assertNull(pip.findFirstAttributeValue(resp));
178     }
179
180     @Test
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);
185
186         assertEquals(STRING_VALUE, pip.findFirstAttributeValue(resp));
187     }
188
189     @Test
190     void testAddIntegerAttribute() {
191         pip.addIntegerAttribute(resp, CATEGORY, ATTRIBUTE_ID, INT_VALUE, request);
192         assertEquals(1, resp.getAttributes().size());
193
194         Attribute attr = resp.getAttributes().iterator().next();
195         assertEquals(ISSUER, attr.getIssuer());
196         assertEquals(CATEGORY, attr.getCategory());
197         assertEquals(ATTRIBUTE_ID, attr.getAttributeId());
198
199         Iterator<AttributeValue<BigInteger>> attrValues = attr.findValues(DataTypes.DT_INTEGER);
200         assertTrue(attrValues.hasNext());
201         assertEquals(INT_VALUE, attrValues.next().getValue().intValue());
202     }
203
204     @Test
205     void testAddIntegerAttribute_Ex() {
206         pip = new MyPip() {
207             @Override
208             protected AttributeValue<BigInteger> makeInteger(int value) throws DataTypeException {
209                 throw new RuntimeException(EXPECTED_EXCEPTION);
210             }
211         };
212         pip.addIntegerAttribute(resp, CATEGORY, ATTRIBUTE_ID, INT_VALUE, request);
213         assertEquals(0, resp.getAttributes().size());
214     }
215
216     @Test
217     void testAddIntegerAttribute_Null() {
218         pip = new MyPip() {
219             @Override
220             protected AttributeValue<BigInteger> makeInteger(int value) throws DataTypeException {
221                 return null;
222             }
223         };
224         pip.addIntegerAttribute(resp, CATEGORY, ATTRIBUTE_ID, INT_VALUE, request);
225         assertEquals(0, resp.getAttributes().size());
226     }
227
228     @Test
229     void testAddLongAttribute() {
230         pip.addLongAttribute(resp, CATEGORY, ATTRIBUTE_ID, LONG_VALUE, request);
231         assertEquals(1, resp.getAttributes().size());
232
233         Attribute attr = resp.getAttributes().iterator().next();
234         assertEquals(ISSUER, attr.getIssuer());
235         assertEquals(CATEGORY, attr.getCategory());
236         assertEquals(ATTRIBUTE_ID, attr.getAttributeId());
237
238         Iterator<AttributeValue<BigInteger>> attrValues = attr.findValues(DataTypes.DT_INTEGER);
239         assertTrue(attrValues.hasNext());
240         assertEquals(LONG_VALUE, attrValues.next().getValue().longValue());
241     }
242
243     @Test
244     void testAddLongAttribute_Ex() {
245         pip = new MyPip() {
246             @Override
247             protected AttributeValue<BigInteger> makeLong(long value) throws DataTypeException {
248                 throw new RuntimeException(EXPECTED_EXCEPTION);
249             }
250         };
251         pip.addLongAttribute(resp, CATEGORY, ATTRIBUTE_ID, LONG_VALUE, request);
252         assertEquals(0, resp.getAttributes().size());
253     }
254
255     @Test
256     void testAddLongAttribute_NullAttrValue() {
257         pip = new MyPip() {
258             @Override
259             protected AttributeValue<BigInteger> makeLong(long value) throws DataTypeException {
260                 return null;
261             }
262         };
263         pip.addLongAttribute(resp, CATEGORY, ATTRIBUTE_ID, LONG_VALUE, request);
264         assertEquals(0, resp.getAttributes().size());
265     }
266
267     @Test
268     void testAddStringAttribute() {
269         pip.addStringAttribute(resp, CATEGORY, ATTRIBUTE_ID, STRING_VALUE, request);
270         assertEquals(1, resp.getAttributes().size());
271
272         Attribute attr = resp.getAttributes().iterator().next();
273         assertEquals(ISSUER, attr.getIssuer());
274         assertEquals(CATEGORY, attr.getCategory());
275         assertEquals(ATTRIBUTE_ID, attr.getAttributeId());
276
277         Iterator<AttributeValue<String>> attrValues = attr.findValues(DataTypes.DT_STRING);
278         assertTrue(attrValues.hasNext());
279         assertEquals(STRING_VALUE, attrValues.next().getValue());
280     }
281
282     @Test
283     void testAddStringAttribute_Ex() {
284         pip = new MyPip() {
285             @Override
286             protected AttributeValue<String> makeString(String value) throws DataTypeException {
287                 throw new RuntimeException(EXPECTED_EXCEPTION);
288             }
289         };
290         pip.addStringAttribute(resp, CATEGORY, ATTRIBUTE_ID, STRING_VALUE, request);
291         assertEquals(0, resp.getAttributes().size());
292     }
293
294     @Test
295     void testAddStringAttribute_NullAttrValue() {
296         pip = new MyPip() {
297             @Override
298             protected AttributeValue<String> makeString(String value) throws DataTypeException {
299                 return null;
300             }
301         };
302         pip.addStringAttribute(resp, CATEGORY, ATTRIBUTE_ID, STRING_VALUE, request);
303         assertEquals(0, resp.getAttributes().size());
304     }
305
306     @Test
307     void testShutdown() {
308         assertThatCode(() -> pip.shutdown()).doesNotThrowAnyException();
309         assertThatExceptionOfType(PIPException.class).isThrownBy(() -> pip.configure("foo", new Properties()))
310             .withMessageContaining("Engine is shutdown");
311     }
312
313     private static class MyPip extends StdOnapPip {
314
315         @Override
316         public Collection<PIPRequest> attributesRequired() {
317             return Collections.emptyList();
318         }
319
320         @Override
321         public PIPResponse getAttributes(PIPRequest pipRequest, PIPFinder pipFinder) {
322             return null;
323         }
324     }
325 }