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