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