Update css file name in conf.py
[policy/engine.git] / POLICY-SDK-APP / src / test / java / org / onap / policy / components / HumanPolicyComponentTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Engine
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.components;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertNull;
26 import static org.junit.Assert.fail;
27 import static org.mockito.Mockito.atLeast;
28 import static org.mockito.Mockito.doReturn;
29 import static org.mockito.Mockito.verify;
30 import static org.mockito.Mockito.when;
31
32 import com.att.research.xacml.api.XACML1;
33 import com.att.research.xacml.util.XACMLPolicyScanner.CallbackResult;
34 import java.io.File;
35 import java.io.IOException;
36 import java.util.ArrayList;
37 import java.util.Collections;
38 import java.util.List;
39 import javax.xml.bind.JAXBElement;
40 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AdviceExpressionType;
41 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AdviceExpressionsType;
42 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AllOfType;
43 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AnyOfType;
44 import oasis.names.tc.xacml._3_0.core.schema.wd_17.ApplyType;
45 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeAssignmentExpressionType;
46 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeDesignatorType;
47 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeSelectorType;
48 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeValueType;
49 import oasis.names.tc.xacml._3_0.core.schema.wd_17.ConditionType;
50 import oasis.names.tc.xacml._3_0.core.schema.wd_17.EffectType;
51 import oasis.names.tc.xacml._3_0.core.schema.wd_17.MatchType;
52 import oasis.names.tc.xacml._3_0.core.schema.wd_17.ObligationExpressionType;
53 import oasis.names.tc.xacml._3_0.core.schema.wd_17.ObligationExpressionsType;
54 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicySetType;
55 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
56 import oasis.names.tc.xacml._3_0.core.schema.wd_17.RuleType;
57 import oasis.names.tc.xacml._3_0.core.schema.wd_17.TargetType;
58 import oasis.names.tc.xacml._3_0.core.schema.wd_17.VariableReferenceType;
59 import org.junit.BeforeClass;
60 import org.junit.Test;
61 import org.mockito.Mockito;
62
63 public class HumanPolicyComponentTest {
64
65     private AttributeIdentifiers attrIds;
66     private HtmlProcessor processor;
67     private static File temp;
68     private static File tempAction;
69     private static File tempConfig;
70
71     @BeforeClass
72     public static void setup() throws IOException {
73         temp = File.createTempFile("tmpFile", ".tmp");
74         tempAction = File.createTempFile("Action_test", ".tmp");
75         tempConfig = File.createTempFile("Config_test", ".tmp");
76         temp.deleteOnExit();
77         tempAction.deleteOnExit();
78         tempConfig.deleteOnExit();
79     }
80
81     @Test
82     public void testAttributeIdentifiers() {
83         String testCategory = "testCategory";
84         String testID = "testId";
85         String testType = "testType";
86         String newTestType = "testNewType";
87
88         attrIds = new AttributeIdentifiers(testCategory, testType, testID);
89         assertEquals(testCategory, attrIds.category);
90         assertEquals(testID, attrIds.id);
91         assertEquals(testType, attrIds.getType());
92
93         attrIds.setType(newTestType);
94         assertEquals(newTestType, attrIds.getType());
95     }
96
97     @SuppressWarnings("unchecked")
98     @Test
99     public void testHumanPolicyComponentException() {
100         JAXBElement<PolicySetType> mockRoot = Mockito.mock(JAXBElement.class);
101         when(mockRoot.getValue()).thenReturn(null);
102         assertNull(HumanPolicyComponent.DescribePolicy(temp));
103     }
104
105     @Test(expected = IllegalArgumentException.class)
106     public void testHtmlProcessorNull() throws IOException {
107         processor = new HtmlProcessor(null, null);
108     }
109
110     @Test(expected = IllegalArgumentException.class)
111     public void testHtmlProcessor() throws IOException {
112         File tempFile = File.createTempFile("testFile", ".tmp");
113         tempFile.delete();
114         processor = new HtmlProcessor(tempFile, null);
115     }
116
117     @Test(expected = IllegalArgumentException.class)
118     public void testHtmlProcessorInvalidObject() throws IOException {
119         processor = new HtmlProcessor(temp, null);
120     }
121
122     @Test
123     public void testHtmlProcessorConfigPolicySetType() {
124         PolicySetType mockPolicySetType = Mockito.mock(PolicySetType.class);
125         processor = new HtmlProcessor(tempConfig, mockPolicySetType);
126         processor.onFinishScan(mockPolicySetType);
127         verify(mockPolicySetType).getVersion();
128     }
129
130     @Test
131     public void testHtmlProcessorActionPolicySetType() {
132         PolicySetType mockPolicySetType = Mockito.mock(PolicySetType.class);
133         processor = new HtmlProcessor(tempAction, mockPolicySetType);
134         processor.onFinishScan(mockPolicySetType);
135         verify(mockPolicySetType).getVersion();
136     }
137
138     @Test
139     public void testHtmlProcessorConfigPolicyType() {
140         PolicyType mockPolicyType = Mockito.mock(PolicyType.class);
141         processor = new HtmlProcessor(tempConfig, mockPolicyType);
142         verify(mockPolicyType).getVersion();
143     }
144
145     @Test
146     public void testHtmlProcessorActionPolicyType() {
147         PolicyType mockPolicyType = Mockito.mock(PolicyType.class);
148         processor = new HtmlProcessor(tempAction, mockPolicyType);
149         assertNotNull(processor.getAttributeIdentifiersMap());
150         verify(mockPolicyType).getVersion();
151     }
152
153     @Test
154     public void testHtmlProcessorOnPreVisitPolicySet() {
155         PolicySetType mockPolicySetType = Mockito.mock(PolicySetType.class);
156         PolicySetType mockPolicyParent = Mockito.mock(PolicySetType.class);
157
158         processor = new HtmlProcessor(temp, mockPolicySetType);
159
160         when(mockPolicySetType.getPolicySetOrPolicyOrPolicySetIdReference()).thenReturn(Collections.emptyList());
161         when(mockPolicySetType.getDescription()).thenReturn(null);
162
163         CallbackResult preResult = processor.onPreVisitPolicySet(mockPolicyParent, mockPolicySetType);
164         assertEquals("CONTINUE", preResult.name());
165         verify(mockPolicySetType, atLeast(1)).getPolicySetOrPolicyOrPolicySetIdReference();
166         verify(mockPolicySetType, atLeast(1)).getDescription();
167     }
168
169     @Test
170     public void testHtmlProcessorOnPreVisitPolicySetNullParent() {
171         PolicySetType mockPolicySetType = Mockito.mock(PolicySetType.class);
172         PolicySetType mockPolicyParent = null;
173         JAXBElement<?> mockElement = Mockito.mock(JAXBElement.class);
174
175         List<JAXBElement<?>> testList = new ArrayList<JAXBElement<?>>();
176         testList.add(mockElement);
177
178         processor = new HtmlProcessor(temp, mockPolicySetType);
179
180         when(mockPolicySetType.getPolicySetOrPolicyOrPolicySetIdReference()).thenReturn(testList);
181         when(mockPolicySetType.getDescription()).thenReturn("");
182
183         CallbackResult preResult = processor.onPreVisitPolicySet(mockPolicyParent, mockPolicySetType);
184         assertEquals("CONTINUE", preResult.name());
185         verify(mockPolicySetType, atLeast(1)).getPolicySetOrPolicyOrPolicySetIdReference();
186         verify(mockPolicySetType, atLeast(1)).getDescription();
187     }
188
189     @Test
190     public void testHtmlProcessorOnPostVisitPolicySet() {
191         PolicySetType mockPolicySetType = Mockito.mock(PolicySetType.class);
192         PolicySetType mockPolicyParent = Mockito.mock(PolicySetType.class);
193
194         processor = new HtmlProcessor(temp, mockPolicySetType);
195
196         when(mockPolicySetType.getPolicySetOrPolicyOrPolicySetIdReference()).thenReturn(Collections.emptyList());
197         when(mockPolicySetType.getDescription()).thenReturn(null);
198
199         CallbackResult postResult = processor.onPostVisitPolicySet(mockPolicyParent, mockPolicySetType);
200         assertEquals("CONTINUE", postResult.name());
201         verify(mockPolicySetType, atLeast(1)).getPolicySetOrPolicyOrPolicySetIdReference();
202         verify(mockPolicySetType, atLeast(1)).getDescription();
203     }
204
205     @Test
206     public void testHtmlProcessorOnPostVisitPolicySetNullParent() {
207         PolicySetType mockPolicySetType = Mockito.mock(PolicySetType.class);
208         PolicySetType mockPolicyParent = null;
209         JAXBElement<?> mockElement = Mockito.mock(JAXBElement.class);
210
211         List<JAXBElement<?>> testList = new ArrayList<JAXBElement<?>>();
212         testList.add(mockElement);
213
214         processor = new HtmlProcessor(temp, mockPolicySetType);
215
216         when(mockPolicySetType.getPolicySetOrPolicyOrPolicySetIdReference()).thenReturn(testList);
217         when(mockPolicySetType.getDescription()).thenReturn("");
218
219         CallbackResult postResult = processor.onPostVisitPolicySet(mockPolicyParent, mockPolicySetType);
220         assertEquals("CONTINUE", postResult.name());
221         verify(mockPolicySetType, atLeast(1)).getPolicySetOrPolicyOrPolicySetIdReference();
222         verify(mockPolicySetType, atLeast(1)).getDescription();
223     }
224
225     @Test
226     public void testHtmlProcessorOnPreVisitPolicy() {
227         PolicySetType mockPolicySetType = Mockito.mock(PolicySetType.class);
228         PolicyType mockPolicyType = Mockito.mock(PolicyType.class);
229         List<Object> testList = new ArrayList<Object>();
230         processor = new HtmlProcessor(temp, mockPolicySetType);
231
232         when(mockPolicyType.getCombinerParametersOrRuleCombinerParametersOrVariableDefinition()).thenReturn(testList);
233         when(mockPolicySetType.getDescription()).thenReturn(null);
234
235         CallbackResult preResult = processor.onPreVisitPolicy(mockPolicySetType, mockPolicyType);
236         assertEquals("CONTINUE", preResult.name());
237         verify(mockPolicyType, atLeast(1)).getCombinerParametersOrRuleCombinerParametersOrVariableDefinition();
238         verify(mockPolicyType, atLeast(1)).getDescription();
239     }
240
241     @Test
242     public void testHtmlProcessorOnPreVisitPolicyNullParent() {
243         PolicyType mockPolicyType = Mockito.mock(PolicyType.class);
244         PolicySetType mockPolicyParent = null;
245         List<Object> testList = new ArrayList<Object>();
246         testList.add(new Object());
247         processor = new HtmlProcessor(temp, mockPolicyType);
248
249         when(mockPolicyType.getCombinerParametersOrRuleCombinerParametersOrVariableDefinition()).thenReturn(testList);
250         when(mockPolicyType.getDescription()).thenReturn("");
251
252         CallbackResult preResult = processor.onPreVisitPolicy(mockPolicyParent, mockPolicyType);
253         assertEquals("CONTINUE", preResult.name());
254         verify(mockPolicyType, atLeast(1)).getCombinerParametersOrRuleCombinerParametersOrVariableDefinition();
255         verify(mockPolicyType, atLeast(1)).getDescription();
256
257     }
258
259     @Test
260     public void testHtmlProcessorOnPostVisitPolicy() {
261         PolicySetType mockPolicySetType = Mockito.mock(PolicySetType.class);
262         PolicyType mockPolicyType = Mockito.mock(PolicyType.class);
263         List<Object> testList = new ArrayList<Object>();
264         processor = new HtmlProcessor(temp, mockPolicySetType);
265
266         when(mockPolicyType.getCombinerParametersOrRuleCombinerParametersOrVariableDefinition()).thenReturn(testList);
267
268         CallbackResult postResult = processor.onPostVisitPolicy(mockPolicySetType, mockPolicyType);
269         assertEquals("CONTINUE", postResult.name());
270         verify(mockPolicyType, atLeast(1)).getCombinerParametersOrRuleCombinerParametersOrVariableDefinition();
271     }
272
273     @Test
274     public void testHtmlProcessorOnPostVisitPolicyNullParent() {
275         PolicyType mockPolicyType = Mockito.mock(PolicyType.class);
276         PolicySetType mockPolicyParent = null;
277         List<Object> testList = new ArrayList<Object>();
278         testList.add(new Object());
279         processor = new HtmlProcessor(temp, mockPolicyType);
280
281         when(mockPolicyType.getCombinerParametersOrRuleCombinerParametersOrVariableDefinition()).thenReturn(testList);
282
283         CallbackResult postResult = processor.onPostVisitPolicy(mockPolicyParent, mockPolicyType);
284         assertEquals("CONTINUE", postResult.name());
285         verify(mockPolicyType, atLeast(1)).getCombinerParametersOrRuleCombinerParametersOrVariableDefinition();
286     }
287
288     @Test
289     public void testHtmlProcessorPolicy() {
290         PolicySetType mockPolicySetType = Mockito.mock(PolicySetType.class);
291         PolicyType mockPolicyType = Mockito.mock(PolicyType.class);
292         processor = new HtmlProcessor(temp, mockPolicySetType);
293
294         when(mockPolicyType.getRuleCombiningAlgId()).thenReturn(null);
295         when(mockPolicyType.getPolicyId()).thenReturn(null);
296         when(mockPolicyType.getVersion()).thenReturn(null);
297         when(mockPolicyType.getTarget()).thenReturn(null);
298
299         processor.policy(mockPolicyType);
300         verify(mockPolicyType).getRuleCombiningAlgId();
301         verify(mockPolicyType).getPolicyId();
302         verify(mockPolicyType).getVersion();
303         verify(mockPolicyType).getTarget();
304     }
305
306     @Test
307     public void testHtmlProcessorPolicyListEmpty() {
308         PolicySetType mockPolicySetType = Mockito.mock(PolicySetType.class);
309         PolicyType mockPolicyType = Mockito.mock(PolicyType.class);
310         TargetType mockTargetType = Mockito.mock(TargetType.class);
311         List<AnyOfType> anyOfList = new ArrayList<AnyOfType>();
312         processor = new HtmlProcessor(temp, mockPolicySetType);
313
314         when(mockPolicyType.getRuleCombiningAlgId()).thenReturn(null);
315         when(mockPolicyType.getPolicyId()).thenReturn(null);
316         when(mockPolicyType.getVersion()).thenReturn(null);
317         when(mockPolicyType.getTarget()).thenReturn(mockTargetType);
318         when(mockTargetType.getAnyOf()).thenReturn(anyOfList);
319
320         processor.policy(mockPolicyType);
321
322         verify(mockPolicyType, atLeast(1)).getRuleCombiningAlgId();
323         verify(mockPolicyType, atLeast(1)).getPolicyId();
324         verify(mockPolicyType, atLeast(1)).getVersion();
325         verify(mockPolicyType, atLeast(1)).getTarget();
326         verify(mockTargetType, atLeast(1)).getAnyOf();
327     }
328
329     @Test
330     public void testHtmlProcessorPolicyListNotEmpty() {
331         PolicySetType mockPolicySetType = Mockito.mock(PolicySetType.class);
332         PolicyType mockPolicyType = Mockito.mock(PolicyType.class);
333         TargetType mockTargetType = Mockito.mock(TargetType.class);
334         List<AnyOfType> anyOfList = new ArrayList<AnyOfType>();
335         anyOfList.add(new AnyOfType());
336         processor = new HtmlProcessor(temp, mockPolicySetType);
337
338         when(mockPolicyType.getRuleCombiningAlgId()).thenReturn(null);
339         when(mockPolicyType.getPolicyId()).thenReturn(null);
340         when(mockPolicyType.getVersion()).thenReturn(null);
341         when(mockPolicyType.getTarget()).thenReturn(mockTargetType);
342         when(mockTargetType.getAnyOf()).thenReturn(anyOfList);
343
344         processor.policy(mockPolicyType);
345         verify(mockPolicyType, atLeast(1)).getRuleCombiningAlgId();
346         verify(mockPolicyType, atLeast(1)).getPolicyId();
347         verify(mockPolicyType, atLeast(1)).getVersion();
348         verify(mockPolicyType, atLeast(1)).getTarget();
349         verify(mockTargetType, atLeast(1)).getAnyOf();
350     }
351
352     @Test
353     public void testHtmlProcessorPolicyNull() {
354         PolicySetType mockPolicySetType = Mockito.mock(PolicySetType.class);
355         PolicyType mockPolicyType = Mockito.mock(PolicyType.class);
356         TargetType mockTargetType = Mockito.mock(TargetType.class);
357         processor = new HtmlProcessor(temp, mockPolicySetType);
358
359         when(mockPolicyType.getRuleCombiningAlgId()).thenReturn(null);
360         when(mockPolicyType.getPolicyId()).thenReturn(null);
361         when(mockPolicyType.getVersion()).thenReturn(null);
362         when(mockPolicyType.getTarget()).thenReturn(mockTargetType);
363         when(mockPolicyType.getCombinerParametersOrRuleCombinerParametersOrVariableDefinition()).thenReturn(null);
364         when(mockTargetType.getAnyOf()).thenReturn(null);
365
366         processor.policy(mockPolicyType);
367         verify(mockPolicyType, atLeast(1)).getRuleCombiningAlgId();
368         verify(mockPolicyType, atLeast(1)).getPolicyId();
369         verify(mockPolicyType, atLeast(1)).getVersion();
370         verify(mockPolicyType, atLeast(1)).getTarget();
371         verify(mockPolicyType, atLeast(1)).getCombinerParametersOrRuleCombinerParametersOrVariableDefinition();
372         verify(mockTargetType, atLeast(1)).getAnyOf();
373     }
374
375     @Test
376     public void testHtmlProcessorPolicySet() {
377         PolicySetType mockPolicySetType = Mockito.mock(PolicySetType.class);
378         processor = new HtmlProcessor(temp, mockPolicySetType);
379
380         when(mockPolicySetType.getPolicyCombiningAlgId()).thenReturn("");
381         when(mockPolicySetType.getPolicySetId()).thenReturn("");
382         when(mockPolicySetType.getVersion()).thenReturn("");
383
384         processor.policySet(mockPolicySetType, "");
385         verify(mockPolicySetType, atLeast(1)).getPolicyCombiningAlgId();
386         verify(mockPolicySetType, atLeast(1)).getPolicySetId();
387         verify(mockPolicySetType, atLeast(1)).getVersion();
388     }
389
390     @Test
391     public void testHtmlProcessorPolicySetNull() {
392         PolicySetType mockPolicySetType = Mockito.mock(PolicySetType.class);
393         TargetType mockTargetType = Mockito.mock(TargetType.class);
394         processor = new HtmlProcessor(temp, mockPolicySetType);
395
396         when(mockPolicySetType.getTarget()).thenReturn(mockTargetType);
397         when(mockTargetType.getAnyOf()).thenReturn(null);
398         when(mockPolicySetType.getPolicySetOrPolicyOrPolicySetIdReference()).thenReturn(null);
399
400         processor.policySet(mockPolicySetType, "");
401         verify(mockPolicySetType, atLeast(1)).getTarget();
402         verify(mockTargetType, atLeast(1)).getAnyOf();
403         verify(mockPolicySetType, atLeast(1)).getPolicySetOrPolicyOrPolicySetIdReference();
404     }
405
406     @Test
407     public void testHtmlProcessorPolicySetEmpty() {
408         PolicySetType mockPolicySetType = Mockito.mock(PolicySetType.class);
409         TargetType mockTargetType = Mockito.mock(TargetType.class);
410         List<AnyOfType> anyOfList = new ArrayList<AnyOfType>();
411         processor = new HtmlProcessor(temp, mockPolicySetType);
412
413         when(mockPolicySetType.getTarget()).thenReturn(mockTargetType);
414         when(mockTargetType.getAnyOf()).thenReturn(anyOfList);
415         when(mockPolicySetType.getPolicySetOrPolicyOrPolicySetIdReference()).thenReturn(null);
416
417         processor.policySet(mockPolicySetType, "");
418         verify(mockPolicySetType, atLeast(1)).getTarget();
419         verify(mockTargetType, atLeast(1)).getAnyOf();
420         verify(mockPolicySetType, atLeast(1)).getPolicySetOrPolicyOrPolicySetIdReference();
421     }
422
423     @Test
424     public void testHtmlProcessorPolicySetNotEmpty() {
425         PolicySetType mockPolicySetType = Mockito.mock(PolicySetType.class);
426         TargetType mockTargetType = Mockito.mock(TargetType.class);
427         List<AnyOfType> anyOfList = new ArrayList<AnyOfType>();
428         anyOfList.add(new AnyOfType());
429
430         when(mockPolicySetType.getTarget()).thenReturn(mockTargetType);
431         when(mockTargetType.getAnyOf()).thenReturn(anyOfList);
432         when(mockPolicySetType.getPolicySetOrPolicyOrPolicySetIdReference()).thenReturn(null);
433
434         processor = new HtmlProcessor(temp, mockPolicySetType);
435         processor.policySet(mockPolicySetType, "");
436         verify(mockPolicySetType, atLeast(1)).getTarget();
437         verify(mockTargetType, atLeast(1)).getAnyOf();
438         verify(mockPolicySetType, atLeast(1)).getPolicySetOrPolicyOrPolicySetIdReference();
439     }
440
441     @Test
442     public void testHtmlProcessorRule() {
443         PolicySetType mockPolicySetType = Mockito.mock(PolicySetType.class);
444         RuleType mockRuleType = Mockito.mock(RuleType.class);
445         ConditionType mockConditionType = Mockito.mock(ConditionType.class);
446         ObligationExpressionsType mockOESType = Mockito.mock(ObligationExpressionsType.class);
447         ObligationExpressionType mockOEType = Mockito.mock(ObligationExpressionType.class);
448         EffectType effectTypePermit = EffectType.PERMIT;
449         processor = new HtmlProcessor(temp, mockPolicySetType);
450
451         List<ObligationExpressionType> oblList = new ArrayList<ObligationExpressionType>();
452         oblList.add(mockOEType);
453
454         when(mockRuleType.getEffect()).thenReturn(effectTypePermit);
455         when(mockRuleType.getRuleId()).thenReturn(null);
456         when(mockRuleType.getTarget()).thenReturn(null);
457         when(mockRuleType.getCondition()).thenReturn(mockConditionType);
458         when(mockRuleType.getObligationExpressions()).thenReturn(mockOESType);
459         when(mockOESType.getObligationExpression()).thenReturn(oblList);
460         when(mockOEType.getFulfillOn()).thenReturn(effectTypePermit);
461
462         processor.rule(mockRuleType);
463
464         verify(mockRuleType, atLeast(1)).getRuleId();
465         verify(mockRuleType, atLeast(1)).getTarget();
466         verify(mockRuleType, atLeast(1)).getCondition();
467         verify(mockRuleType, atLeast(1)).getObligationExpressions();
468         verify(mockOESType, atLeast(1)).getObligationExpression();
469         verify(mockOEType, atLeast(1)).getFulfillOn();
470
471         JAXBElement<?> mockJaxBElement = Mockito.mock(JAXBElement.class);
472         Object mockValueObject = Mockito.mock(Object.class);
473
474         doReturn(mockJaxBElement).when(mockConditionType).getExpression();
475         doReturn(mockValueObject).when(mockJaxBElement).getValue();
476
477         try {
478             processor.rule(mockRuleType);
479             fail();
480         } catch (IllegalArgumentException e) {
481             verify(mockConditionType, atLeast(1)).getExpression();
482             verify(mockJaxBElement, atLeast(1)).getValue();
483         }
484     }
485
486     @Test
487     public void testHtmlProcessorRuleNullEmptyList() {
488         PolicySetType mockPolicySetType = Mockito.mock(PolicySetType.class);
489         RuleType mockRuleType = Mockito.mock(RuleType.class);
490         TargetType mockTargetType = Mockito.mock(TargetType.class);
491         EffectType effectTypePermit = EffectType.PERMIT;
492         AdviceExpressionsType mockAdviceExsType = Mockito.mock(AdviceExpressionsType.class);
493         AdviceExpressionType mockAdviceEx = Mockito.mock(AdviceExpressionType.class);
494         processor = new HtmlProcessor(temp, mockPolicySetType);
495
496         List<AnyOfType> anyOfList = new ArrayList<AnyOfType>();
497         List<AdviceExpressionType> adviceExList = new ArrayList<AdviceExpressionType>();
498         adviceExList.add(mockAdviceEx);
499
500         when(mockRuleType.getEffect()).thenReturn(effectTypePermit);
501         when(mockRuleType.getRuleId()).thenReturn(null);
502         when(mockRuleType.getTarget()).thenReturn(mockTargetType);
503         when(mockRuleType.getObligationExpressions()).thenReturn(null);
504         when(mockRuleType.getAdviceExpressions()).thenReturn(mockAdviceExsType);
505         when(mockTargetType.getAnyOf()).thenReturn(null);
506         when(mockAdviceExsType.getAdviceExpression()).thenReturn(adviceExList);
507         when(mockAdviceEx.getAttributeAssignmentExpression()).thenReturn(null);
508         when(mockAdviceEx.getAppliesTo()).thenReturn(effectTypePermit);
509
510         processor.rule(mockRuleType);
511
512         verify(mockRuleType, atLeast(1)).getEffect();
513         verify(mockRuleType, atLeast(1)).getRuleId();
514         verify(mockRuleType, atLeast(1)).getTarget();
515         verify(mockRuleType, atLeast(1)).getCondition();
516         verify(mockRuleType, atLeast(1)).getObligationExpressions();
517         verify(mockRuleType, atLeast(1)).getAdviceExpressions();
518         verify(mockTargetType, atLeast(1)).getAnyOf();
519         verify(mockAdviceExsType, atLeast(1)).getAdviceExpression();
520         verify(mockAdviceEx, atLeast(1)).getAttributeAssignmentExpression();
521         verify(mockAdviceEx, atLeast(1)).getAppliesTo();
522
523         when(mockTargetType.getAnyOf()).thenReturn(anyOfList);
524         processor.rule(mockRuleType);
525         verify(mockTargetType, atLeast(1)).getAnyOf();
526     }
527
528     @Test
529     public void testHtmlProcessorRuleNonNullObjects() {
530         PolicySetType mockPolicySetType = Mockito.mock(PolicySetType.class);
531         RuleType mockRuleType = Mockito.mock(RuleType.class);
532         TargetType mockTargetType = Mockito.mock(TargetType.class);
533         AdviceExpressionsType mockAdvice = Mockito.mock(AdviceExpressionsType.class);
534         ObligationExpressionsType mockObEx = Mockito.mock(ObligationExpressionsType.class);
535         AdviceExpressionType adviceExpTypeMock = Mockito.mock(AdviceExpressionType.class);
536         ObligationExpressionType mockObExType = Mockito.mock(ObligationExpressionType.class);
537         EffectType effectTypePermit = EffectType.PERMIT;
538         processor = new HtmlProcessor(temp, mockPolicySetType);
539
540         List<AnyOfType> anyOfList = new ArrayList<AnyOfType>();
541         anyOfList.add(new AnyOfType());
542
543         List<AdviceExpressionType> adviceList = new ArrayList<AdviceExpressionType>();
544         adviceList.add(adviceExpTypeMock);
545
546         List<AttributeAssignmentExpressionType> attrList = new ArrayList<AttributeAssignmentExpressionType>();
547
548         List<ObligationExpressionType> obExList = new ArrayList<ObligationExpressionType>();
549         obExList.add(mockObExType);
550
551         List<Object> contentList = new ArrayList<>();
552         contentList.add(new Object());
553
554         when(mockRuleType.getRuleId()).thenReturn("");
555         when(mockRuleType.getTarget()).thenReturn(mockTargetType);
556         when(mockRuleType.getEffect()).thenReturn(effectTypePermit);
557         when(mockTargetType.getAnyOf()).thenReturn(anyOfList);
558         when(mockRuleType.getAdviceExpressions()).thenReturn(mockAdvice);
559         when(mockAdvice.getAdviceExpression()).thenReturn(adviceList);
560         when(mockRuleType.getObligationExpressions()).thenReturn(mockObEx);
561         when(mockObEx.getObligationExpression()).thenReturn(obExList);
562         when(mockObExType.getAttributeAssignmentExpression()).thenReturn(null);
563         when(mockObExType.getFulfillOn()).thenReturn(effectTypePermit);
564         when(adviceExpTypeMock.getAdviceId()).thenReturn("");
565         when(adviceExpTypeMock.getAppliesTo()).thenReturn(effectTypePermit);
566         when(adviceExpTypeMock.getAttributeAssignmentExpression()).thenReturn(attrList);
567
568         processor.rule(mockRuleType);
569
570         verify(mockRuleType, atLeast(1)).getRuleId();
571         verify(mockRuleType, atLeast(1)).getTarget();
572         verify(mockRuleType, atLeast(1)).getEffect();
573         verify(mockRuleType, atLeast(1)).getAdviceExpressions();
574         verify(mockRuleType, atLeast(1)).getObligationExpressions();
575         verify(mockTargetType, atLeast(1)).getAnyOf();
576         verify(mockObEx, atLeast(1)).getObligationExpression();
577         verify(mockObExType, atLeast(1)).getAttributeAssignmentExpression();
578         verify(mockObExType, atLeast(1)).getFulfillOn();
579         verify(mockAdvice, atLeast(1)).getAdviceExpression();
580         verify(adviceExpTypeMock, atLeast(1)).getAdviceId();
581         verify(adviceExpTypeMock, atLeast(1)).getAppliesTo();
582         verify(adviceExpTypeMock, atLeast(1)).getAttributeAssignmentExpression();
583     }
584
585     @Test
586     public void testHtmlProcessorOnPreVisitRule() {
587         PolicySetType mockPolicySetType = Mockito.mock(PolicySetType.class);
588         PolicyType mockPolicyType = null;
589         RuleType mockRuleType = Mockito.mock(RuleType.class);
590         EffectType effectTypePermit = EffectType.PERMIT;
591         TargetType mockTargetType = Mockito.mock(TargetType.class);
592         processor = new HtmlProcessor(temp, mockPolicySetType);
593
594         List<AnyOfType> anyOfList = new ArrayList<AnyOfType>();
595         anyOfList.add(new AnyOfType());
596
597         when(mockRuleType.getCondition()).thenReturn(null);
598         when(mockRuleType.getDescription()).thenReturn(null);
599         when(mockRuleType.getEffect()).thenReturn(effectTypePermit);
600         when(mockRuleType.getTarget()).thenReturn(mockTargetType);
601         when(mockTargetType.getAnyOf()).thenReturn(anyOfList);
602
603         CallbackResult callbackResult = processor.onPreVisitRule(mockPolicyType, mockRuleType);
604         assertNotNull(callbackResult);
605
606         verify(mockRuleType, atLeast(1)).getCondition();
607         verify(mockRuleType, atLeast(1)).getDescription();
608         verify(mockRuleType, atLeast(1)).getEffect();
609         verify(mockRuleType, atLeast(1)).getTarget();
610         verify(mockRuleType, atLeast(1)).getAdviceExpressions();
611         verify(mockTargetType, atLeast(1)).getAnyOf();
612
613         mockPolicyType = Mockito.mock(PolicyType.class);
614         when(mockRuleType.getDescription()).thenReturn("");
615
616         callbackResult = processor.onPreVisitRule(mockPolicyType, mockRuleType);
617         assertNotNull(callbackResult);
618     }
619
620     @Test
621     public void testHtmlProcessorOnPostVisitRule() {
622         PolicySetType mockPolicySetType = Mockito.mock(PolicySetType.class);
623         PolicyType mockPolicyType = null;
624         RuleType mockRuleType = Mockito.mock(RuleType.class);
625
626         processor = new HtmlProcessor(temp, mockPolicySetType);
627         CallbackResult callbackResult = processor.onPostVisitRule(mockPolicyType, mockRuleType);
628         assertNotNull(callbackResult);
629
630         mockPolicyType = Mockito.mock(PolicyType.class);
631         callbackResult = processor.onPostVisitRule(mockPolicyType, mockRuleType);
632         assertNotNull(callbackResult);
633     }
634
635     @Test
636     public void testHtmlProcessorProcessAttributeAssignments() {
637         PolicySetType mockPolicySetType = Mockito.mock(PolicySetType.class);
638         processor = new HtmlProcessor(temp, mockPolicySetType);
639
640         RuleType mockRuleType = Mockito.mock(RuleType.class);
641         EffectType effectTypePermit = EffectType.PERMIT;
642         ObligationExpressionsType mockOblExsType = Mockito.mock(ObligationExpressionsType.class);
643
644         ObligationExpressionType mockOblExTypeListObj = Mockito.mock(ObligationExpressionType.class);
645         List<ObligationExpressionType> oblExTypeList = new ArrayList<ObligationExpressionType>();
646         oblExTypeList.add(mockOblExTypeListObj);
647
648         AttributeAssignmentExpressionType mockattrAssignListObj = Mockito.mock(AttributeAssignmentExpressionType.class);
649         List<AttributeAssignmentExpressionType> attrAssignList = new ArrayList<AttributeAssignmentExpressionType>();
650         attrAssignList.add(mockattrAssignListObj);
651
652         JAXBElement<?> jaxbElementMock = Mockito.mock(JAXBElement.class);
653         AttributeValueType attrValTypeMock = Mockito.mock(AttributeValueType.class);
654         AttributeDesignatorType attrDesignTypeMock = Mockito.mock(AttributeDesignatorType.class);
655         AttributeSelectorType attrSelTypeMock = Mockito.mock(AttributeSelectorType.class);
656         ApplyType applyTypeMock = Mockito.mock(ApplyType.class);
657         Object genericObjectMock = Mockito.mock(Object.class);
658
659         Object mockContentListObject = Mockito.mock(Object.class);
660         List<Object> contentList = new ArrayList<Object>();
661         contentList.add(mockContentListObject);
662         contentList.add(mockContentListObject);
663
664         when(mockRuleType.getEffect()).thenReturn(effectTypePermit);
665         when(mockRuleType.getObligationExpressions()).thenReturn(mockOblExsType);
666         when(mockOblExsType.getObligationExpression()).thenReturn(oblExTypeList);
667         when(mockOblExTypeListObj.getAttributeAssignmentExpression()).thenReturn(attrAssignList);
668         when(mockOblExTypeListObj.getFulfillOn()).thenReturn(effectTypePermit);
669         when(mockattrAssignListObj.getCategory()).thenReturn("");
670         when(mockattrAssignListObj.getAttributeId()).thenReturn("");
671         doReturn(jaxbElementMock).when(mockattrAssignListObj).getExpression();
672         doReturn(attrValTypeMock).when(jaxbElementMock).getValue();
673         when(attrValTypeMock.getContent()).thenReturn(contentList);
674
675         processor.rule(mockRuleType);
676
677         verify(mockRuleType, atLeast(1)).getEffect();
678         verify(mockRuleType, atLeast(1)).getObligationExpressions();
679         verify(mockOblExsType, atLeast(1)).getObligationExpression();
680         verify(mockOblExTypeListObj, atLeast(1)).getAttributeAssignmentExpression();
681         verify(mockOblExTypeListObj, atLeast(1)).getFulfillOn();
682         verify(mockattrAssignListObj, atLeast(1)).getExpression();
683         verify(jaxbElementMock, atLeast(1)).getValue();
684         verify(attrValTypeMock, atLeast(1)).getContent();
685
686         doReturn(attrDesignTypeMock).when(jaxbElementMock).getValue();
687         processor.rule(mockRuleType);
688         verify(jaxbElementMock, atLeast(1)).getValue();
689
690         doReturn(attrSelTypeMock).when(jaxbElementMock).getValue();
691         processor.rule(mockRuleType);
692         verify(jaxbElementMock, atLeast(1)).getValue();
693
694         doReturn(applyTypeMock).when(jaxbElementMock).getValue();
695         processor.rule(mockRuleType);
696         verify(jaxbElementMock, atLeast(1)).getValue();
697
698         doReturn(genericObjectMock).when(jaxbElementMock).getValue();
699         processor.rule(mockRuleType);
700         verify(jaxbElementMock, atLeast(1)).getValue();
701     }
702
703     @Test
704     public void testHtmlProcessorTarget() {
705         PolicySetType mockPolicySetType = Mockito.mock(PolicySetType.class);
706         processor = new HtmlProcessor(temp, mockPolicySetType);
707
708         processor.target(null);
709
710         AnyOfType mockAnyOfType = Mockito.mock(AnyOfType.class);
711         List<AnyOfType> anyOfList = new ArrayList<AnyOfType>();
712         anyOfList.add(mockAnyOfType);
713         anyOfList.add(mockAnyOfType); // adding duplicate element
714
715         AllOfType mockAllOfType = Mockito.mock(AllOfType.class);
716         List<AllOfType> allOfTypeList = new ArrayList<AllOfType>();
717         allOfTypeList.add(mockAllOfType);
718         allOfTypeList.add(mockAllOfType); // adding duplicate element
719
720         MatchType mockMatchType = Mockito.mock(MatchType.class);
721         List<MatchType> matchTypeList = new ArrayList<MatchType>();
722         matchTypeList.add(mockMatchType);
723         matchTypeList.add(mockMatchType); // adding duplicate element
724
725         AttributeValueType mockAttrValType = Mockito.mock(AttributeValueType.class);
726         AttributeDesignatorType mockAttrDesType = Mockito.mock(AttributeDesignatorType.class);
727         AttributeSelectorType mockAttrSelType = Mockito.mock(AttributeSelectorType.class);
728
729         List<Object> contentList = new ArrayList<Object>();
730
731         when(mockAnyOfType.getAllOf()).thenReturn(allOfTypeList);
732         when(mockAllOfType.getMatch()).thenReturn(matchTypeList);
733         when(mockMatchType.getAttributeValue()).thenReturn(mockAttrValType);
734         when(mockMatchType.getMatchId()).thenReturn(XACML1.ID_FUNCTION_STRING_EQUAL.stringValue());
735         when(mockMatchType.getAttributeDesignator()).thenReturn(mockAttrDesType);
736         when(mockAttrValType.getDataType()).thenReturn("");
737         when(mockAttrValType.getContent()).thenReturn(contentList);
738         when(mockAttrDesType.getCategory()).thenReturn("");
739         when(mockAttrDesType.getAttributeId()).thenReturn("");
740         when(mockAttrDesType.getIssuer()).thenReturn("");
741         when(mockAttrDesType.getDataType()).thenReturn("");
742         processor.target(anyOfList);
743         verify(mockAnyOfType, atLeast(1)).getAllOf();
744         verify(mockAllOfType, atLeast(1)).getMatch();
745         verify(mockMatchType, atLeast(1)).getAttributeValue();
746         verify(mockMatchType, atLeast(1)).getMatchId();
747         verify(mockMatchType, atLeast(1)).getAttributeDesignator();
748         verify(mockAttrValType, atLeast(1)).getDataType();
749         verify(mockAttrValType, atLeast(1)).getContent();
750         verify(mockAttrDesType, atLeast(1)).getCategory();
751         verify(mockAttrDesType, atLeast(1)).getAttributeId();
752         verify(mockAttrDesType, atLeast(1)).getIssuer();
753         verify(mockAttrDesType, atLeast(1)).getDataType();
754
755         when(mockMatchType.getAttributeDesignator()).thenReturn(null);
756         when(mockMatchType.getAttributeSelector()).thenReturn(mockAttrSelType);
757         when(mockAttrSelType.getCategory()).thenReturn("");
758         when(mockAttrSelType.getContextSelectorId()).thenReturn("");
759         when(mockAttrSelType.getDataType()).thenReturn("");
760         processor.target(anyOfList);
761         verify(mockMatchType, atLeast(1)).getAttributeDesignator();
762         verify(mockMatchType, atLeast(1)).getAttributeSelector();
763         verify(mockAttrSelType, atLeast(1)).getCategory();
764         verify(mockAttrSelType, atLeast(1)).getContextSelectorId();
765         verify(mockAttrSelType, atLeast(1)).getDataType();
766
767         when(mockMatchType.getAttributeDesignator()).thenReturn(null);
768         when(mockMatchType.getAttributeSelector()).thenReturn(null);
769         processor.target(anyOfList);
770         verify(mockMatchType, atLeast(1)).getAttributeDesignator();
771         verify(mockMatchType, atLeast(1)).getAttributeSelector();
772     }
773
774     @Test
775     public void testHtmlProcessorStringifyExpression() {
776         PolicySetType mockPolicySetType = Mockito.mock(PolicySetType.class);
777         processor = new HtmlProcessor(temp, mockPolicySetType);
778
779         RuleType mockRuleType = Mockito.mock(RuleType.class);
780         ConditionType mockConditionType = Mockito.mock(ConditionType.class);
781         JAXBElement<?> mockJAXBElement = Mockito.mock(JAXBElement.class);
782         EffectType effectTypePermit = EffectType.PERMIT;
783         Object mockExpressObject = Mockito.mock(Object.class);
784         AttributeDesignatorType mockAttrDesType = Mockito.mock(AttributeDesignatorType.class);
785         AttributeSelectorType mockAttrSelType = Mockito.mock(AttributeSelectorType.class);
786         AttributeValueType mockAttrValType = Mockito.mock(AttributeValueType.class);
787         VariableReferenceType mockVarRefType = Mockito.mock(VariableReferenceType.class);
788
789         Object mockContentObject = Mockito.mock(Object.class);
790         List<Object> contentList = new ArrayList<Object>();
791         contentList.add(mockContentObject);
792
793         when(mockRuleType.getEffect()).thenReturn(effectTypePermit);
794         when(mockRuleType.getCondition()).thenReturn(mockConditionType);
795         doReturn(mockJAXBElement).when(mockConditionType).getExpression();
796         doReturn(mockExpressObject).when(mockJAXBElement).getValue();
797
798         try {
799             processor.rule(mockRuleType);
800             fail();
801         } catch (IllegalArgumentException e) {
802             verify(mockRuleType, atLeast(1)).getEffect();
803             verify(mockRuleType, atLeast(1)).getCondition();
804             verify(mockConditionType, atLeast(1)).getExpression();
805             verify(mockJAXBElement, atLeast(1)).getValue();
806         }
807
808         doReturn(mockAttrDesType).when(mockJAXBElement).getValue();
809         when(mockAttrDesType.getCategory()).thenReturn("");
810         when(mockAttrDesType.getAttributeId()).thenReturn("");
811         when(mockAttrDesType.getDataType()).thenReturn("");
812         processor.rule(mockRuleType);
813         verify(mockJAXBElement, atLeast(1)).getValue();
814         verify(mockAttrDesType, atLeast(1)).getCategory();
815         verify(mockAttrDesType, atLeast(1)).getAttributeId();
816         verify(mockAttrDesType, atLeast(1)).getDataType();
817
818         doReturn(mockAttrSelType).when(mockJAXBElement).getValue();
819         when(mockAttrSelType.getPath()).thenReturn("SamplePath/text()");
820         processor.rule(mockRuleType);
821         verify(mockJAXBElement, atLeast(1)).getValue();
822         verify(mockAttrSelType, atLeast(1)).getPath();
823
824         when(mockAttrSelType.getPath()).thenReturn("");
825         processor.rule(mockRuleType);
826         verify(mockJAXBElement, atLeast(1)).getValue();
827         verify(mockAttrSelType, atLeast(1)).getPath();
828
829         doReturn(mockAttrValType).when(mockJAXBElement).getValue();
830         when(mockAttrValType.getContent()).thenReturn(contentList);
831         processor.rule(mockRuleType);
832         verify(mockJAXBElement, atLeast(1)).getValue();
833         verify(mockAttrValType, atLeast(1)).getContent();
834
835         doReturn(mockVarRefType).when(mockJAXBElement).getValue();
836         processor.rule(mockRuleType);
837         verify(mockJAXBElement, atLeast(1)).getValue();
838     }
839 }