Add and Modify JUnits for Code Coverage (components, controller)
[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.mockito.Mockito.atLeast;
27 import static org.mockito.Mockito.verify;
28 import static org.mockito.Mockito.when;
29
30 import com.att.research.xacml.util.XACMLPolicyScanner.CallbackResult;
31 import java.io.File;
32 import java.io.IOException;
33 import java.util.ArrayList;
34 import java.util.Collections;
35 import java.util.List;
36 import javax.xml.bind.JAXBElement;
37 import oasis.names.tc.xacml._3_0.core.schema.wd_17.ConditionType;
38 import oasis.names.tc.xacml._3_0.core.schema.wd_17.EffectType;
39 import oasis.names.tc.xacml._3_0.core.schema.wd_17.ObligationExpressionType;
40 import oasis.names.tc.xacml._3_0.core.schema.wd_17.ObligationExpressionsType;
41 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicySetType;
42 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
43 import oasis.names.tc.xacml._3_0.core.schema.wd_17.RuleType;
44 import org.junit.BeforeClass;
45 import org.junit.Test;
46 import org.mockito.Mockito;
47
48 public class HumanPolicyComponentTest {
49
50     private AttributeIdentifiers attrIds;
51     private HtmlProcessor processor;
52     private static File temp;
53     private static File tempAction;
54     private static File tempConfig;
55
56     @Test
57     public void testAttributeIdentifiers() {
58         String testCategory = "testCategory";
59         String testID = "testId";
60         String testType = "testType";
61         String newTestType = "testNewType";
62
63         attrIds = new AttributeIdentifiers(testCategory, testType, testID);
64         assertEquals(testCategory, attrIds.category);
65         assertEquals(testID, attrIds.id);
66         assertEquals(testType, attrIds.getType());
67
68         attrIds.setType(newTestType);
69         assertEquals(newTestType, attrIds.getType());
70     }
71
72     @BeforeClass
73     public static void setup() throws IOException {
74         temp = File.createTempFile("tmpFile", ".tmp");
75         tempAction = File.createTempFile("Action_test", ".tmp");
76         tempConfig = File.createTempFile("Config_test", ".tmp");
77         temp.deleteOnExit();
78         tempAction.deleteOnExit();
79         tempConfig.deleteOnExit();
80     }
81
82     @SuppressWarnings("unchecked")
83     @Test
84     public void testHumanPolicyComponentException() throws IOException {
85         JAXBElement<PolicySetType> mockRoot = Mockito.mock(JAXBElement.class);
86         when(mockRoot.getValue()).thenReturn(null);
87         assertNull(HumanPolicyComponent.DescribePolicy(temp));
88     }
89
90     @Test(expected = IllegalArgumentException.class)
91     public void testHtmlProcessorNull() throws IOException {
92         processor = new HtmlProcessor(null, null);
93     }
94
95     @Test(expected = IllegalArgumentException.class)
96     public void testHtmlProcessor() throws IOException {
97         File tempFile = File.createTempFile("testFile", ".tmp");
98         tempFile.delete();
99         processor = new HtmlProcessor(tempFile, null);
100     }
101
102     @Test(expected = IllegalArgumentException.class)
103     public void testHtmlProcessorInvalidObject() throws IOException {
104         processor = new HtmlProcessor(temp, null);
105     }
106
107     @Test
108     public void testHtmlProcessorConfigPolicySetType() throws IOException {
109         PolicySetType mockPolicySetType = Mockito.mock(PolicySetType.class);
110         processor = new HtmlProcessor(tempConfig, mockPolicySetType);
111         processor.onFinishScan(mockPolicySetType);
112         verify(mockPolicySetType).getVersion();
113     }
114
115     @Test
116     public void testHtmlProcessorActionPolicySetType() throws IOException {
117         PolicySetType mockPolicySetType = Mockito.mock(PolicySetType.class);
118         processor = new HtmlProcessor(tempAction, mockPolicySetType);
119         processor.onFinishScan(mockPolicySetType);
120         verify(mockPolicySetType).getVersion();
121     }
122
123     @Test
124     public void testHtmlProcessorConfigPolicyType() throws IOException {
125         PolicyType mockPolicyType = Mockito.mock(PolicyType.class);
126         processor = new HtmlProcessor(tempConfig, mockPolicyType);
127         verify(mockPolicyType).getVersion();
128     }
129
130     @Test
131     public void testHtmlProcessorActionPolicyType() throws IOException {
132         PolicyType mockPolicyType = Mockito.mock(PolicyType.class);
133         processor = new HtmlProcessor(tempAction, mockPolicyType);
134         assertNotNull(processor.getAttributeIdentifiersMap());
135         verify(mockPolicyType).getVersion();
136     }
137
138     @Test
139     public void testHtmlProcessorOnPreVisitPolicySet() throws IOException {
140         PolicySetType mockPolicySetType = Mockito.mock(PolicySetType.class);
141         PolicySetType mockPolicyParent = Mockito.mock(PolicySetType.class);
142         when(mockPolicySetType.getPolicySetOrPolicyOrPolicySetIdReference()).thenReturn(Collections.emptyList());
143         processor = new HtmlProcessor(temp, mockPolicySetType);
144
145         CallbackResult preResult = processor.onPreVisitPolicySet(mockPolicyParent, mockPolicySetType);
146         assertEquals("CONTINUE", preResult.name());
147
148         CallbackResult postResult = processor.onPostVisitPolicySet(mockPolicyParent, mockPolicySetType);
149         assertEquals("CONTINUE", postResult.name());
150     }
151
152     @Test
153     public void testHtmlProcessorOnPreVisitPolicySetNullParent() throws IOException {
154         PolicySetType mockPolicySetType = Mockito.mock(PolicySetType.class);
155         PolicySetType mockPolicyParent = null;
156         JAXBElement<?> mockElement = Mockito.mock(JAXBElement.class);
157         List<JAXBElement<?>> testList = new ArrayList<JAXBElement<?>>();
158         testList.add(mockElement);
159         when(mockPolicySetType.getPolicySetOrPolicyOrPolicySetIdReference()).thenReturn(testList);
160         processor = new HtmlProcessor(temp, mockPolicySetType);
161
162         CallbackResult res = processor.onPreVisitPolicySet(mockPolicyParent, mockPolicySetType);
163         assertEquals("CONTINUE", res.name());
164
165         CallbackResult postResult = processor.onPostVisitPolicySet(mockPolicyParent, mockPolicySetType);
166         assertEquals("CONTINUE", postResult.name());
167     }
168
169     @Test
170     public void testHtmlProcessorPolicy() throws IOException {
171         PolicySetType mockPolicySetType = Mockito.mock(PolicySetType.class);
172         PolicyType mockPolicyType = Mockito.mock(PolicyType.class);
173         when(mockPolicyType.getRuleCombiningAlgId()).thenReturn(null);
174         when(mockPolicyType.getPolicyId()).thenReturn(null);
175         when(mockPolicyType.getVersion()).thenReturn(null);
176         when(mockPolicyType.getTarget()).thenReturn(null);
177
178         processor = new HtmlProcessor(temp, mockPolicySetType);
179         processor.policy(mockPolicyType);
180         verify(mockPolicyType).getRuleCombiningAlgId();
181         verify(mockPolicyType).getPolicyId();
182         verify(mockPolicyType).getVersion();
183         verify(mockPolicyType).getTarget();
184     }
185
186     @Test
187     public void testHtmlProcessorPolicySet() throws IOException {
188         PolicySetType mockPolicySetType = Mockito.mock(PolicySetType.class);
189         when(mockPolicySetType.getPolicyCombiningAlgId()).thenReturn("");
190         when(mockPolicySetType.getPolicySetId()).thenReturn("");
191         when(mockPolicySetType.getVersion()).thenReturn("");
192
193         processor = new HtmlProcessor(temp, mockPolicySetType);
194         processor.policySet(mockPolicySetType, "");
195         verify(mockPolicySetType, atLeast(1)).getPolicyCombiningAlgId();
196         verify(mockPolicySetType, atLeast(1)).getPolicySetId();
197         verify(mockPolicySetType, atLeast(1)).getVersion();
198     }
199
200     @Test
201     public void testHtmlProcessorRule() throws IOException {
202         PolicySetType mockPolicySetType = Mockito.mock(PolicySetType.class);
203         RuleType mockRuleType = Mockito.mock(RuleType.class);
204         ConditionType mockConditionType = Mockito.mock(ConditionType.class);
205         ObligationExpressionsType mockOESType = Mockito.mock(ObligationExpressionsType.class);
206         ObligationExpressionType mockOEType = Mockito.mock(ObligationExpressionType.class);
207         EffectType effectTypePermit = EffectType.PERMIT;
208
209         List<ObligationExpressionType> oblList = new ArrayList<ObligationExpressionType>();
210         oblList.add(mockOEType);
211
212         when(mockRuleType.getEffect()).thenReturn(effectTypePermit);
213
214         when(mockRuleType.getRuleId()).thenReturn(null);
215         when(mockRuleType.getTarget()).thenReturn(null);
216         when(mockRuleType.getCondition()).thenReturn(mockConditionType);
217         when(mockRuleType.getObligationExpressions()).thenReturn(mockOESType);
218         when(mockOESType.getObligationExpression()).thenReturn(oblList);
219         when(mockOEType.getFulfillOn()).thenReturn(effectTypePermit);
220
221         processor = new HtmlProcessor(temp, mockPolicySetType);
222         processor.rule(mockRuleType);
223
224         verify(mockRuleType, atLeast(1)).getRuleId();
225         verify(mockRuleType, atLeast(1)).getTarget();
226         verify(mockRuleType, atLeast(1)).getCondition();
227         verify(mockRuleType, atLeast(1)).getObligationExpressions();
228         verify(mockOESType, atLeast(1)).getObligationExpression();
229         verify(mockOEType, atLeast(1)).getFulfillOn();
230     }
231
232 }