Unit/SONAR/Checkstyle in ONAP-REST
[policy/engine.git] / ONAP-REST / src / test / java / org / onap / policy / rest / util / PolicyValidationTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-REST
4  * ================================================================================
5  * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2019 Nordix Foundation.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.rest.util;
23
24 import static org.assertj.core.api.Assertions.assertThat;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNotSame;
27 import static org.junit.Assert.assertNull;
28
29 import java.io.IOException;
30 import java.text.SimpleDateFormat;
31 import java.util.ArrayList;
32 import java.util.Date;
33 import java.util.LinkedHashMap;
34 import java.util.List;
35 import java.util.Map;
36 import java.util.UUID;
37
38 import org.junit.After;
39 import org.junit.Before;
40 import org.junit.Test;
41 import org.onap.policy.api.PolicyConfigType;
42 import org.onap.policy.api.PolicyParameters;
43 import org.onap.policy.common.utils.resources.TextFileUtils;
44 import org.onap.policy.rest.adapter.PolicyRestAdapter;
45
46 public class PolicyValidationTest {
47
48     @Before
49     public void setUp() throws Exception {
50     }
51
52     @After
53     public void tearDown() throws Exception {
54     }
55
56     @Test
57     public void microServicePolicyTests() throws Exception {
58         PolicyParameters policyParameters = new PolicyParameters();
59
60         policyParameters.setPolicyConfigType(PolicyConfigType.MicroService);
61         policyParameters.setPolicyName("Test.junitPolicy");
62         policyParameters.setPolicyDescription("This is a sample Micro Service policy Create example");
63         policyParameters.setOnapName("DCAE");
64         policyParameters.setPriority("1");
65         String msJsonString = TextFileUtils.getTextFileAsString("src/test/resources/policies/MicroServicePolicy.json");
66         policyParameters.setConfigBody(msJsonString);
67         policyParameters.setRequestID(UUID.randomUUID());
68         SimpleDateFormat dateformat3 = new SimpleDateFormat("dd/MM/yyyy");
69         Date date = dateformat3.parse("15/10/2016");
70         policyParameters.setTtlDate(date);
71         policyParameters.setGuard(true);
72         policyParameters.setRiskLevel("5");
73         policyParameters.setRiskType("TEST");
74         policyParameters.setRequestID(UUID.randomUUID());
75
76         PolicyValidationRequestWrapper wrapper = new PolicyValidationRequestWrapper();
77         PolicyRestAdapter policyData = wrapper.populateRequestParameters(policyParameters);
78         PolicyValidation validation = new PolicyValidation();
79         String responseString = validation.validatePolicy(policyData).toString();
80
81         assertNotSame("success", responseString);
82
83         new PolicyValidation(null);
84         assertNull(PolicyValidation.getCommonClassDao());
85
86         policyData.setConfigPolicyType("ClosedLoop_Fault");
87         responseString = validation.validatePolicy(policyData).toString();
88         assertThat(responseString).doesNotContain("success");
89
90         policyData.setConfigPolicyType("ClosedLoop_PM");
91         responseString = validation.validatePolicy(policyData).toString();
92         assertThat(responseString).doesNotContain("success");
93
94         policyData.setConfigPolicyType("Enforcer Config");
95         responseString = validation.validatePolicy(policyData).toString();
96         assertThat(responseString).doesNotContain("success");
97
98         policyData.setConfigPolicyType("Optimization");
99         responseString = validation.validatePolicy(policyData).toString();
100         assertThat(responseString).doesNotContain("success");
101
102         policyData.setConfigPolicyType("Strange");
103         responseString = validation.validatePolicy(policyData).toString();
104         assertThat(responseString).doesNotContain("success");
105     }
106
107     @Test
108     public final void testEmailValidation() {
109         PolicyValidation validation = new PolicyValidation();
110         String result = validation.emailValidation("testemail@test.com", "SUCCESS");
111         assertEquals("success", result);
112     }
113
114     @Test
115     public void testPolicyHeadingValidation() throws IOException {
116         PolicyValidation validation = new PolicyValidation();
117         PolicyRestAdapter policyData = new PolicyRestAdapter();
118
119         String responseString = validation.validatePolicy(policyData).toString();
120         assertThat(responseString).contains("PolicyName Should not be empty");
121
122         policyData.setPolicyName("%%%~~~%%%");
123         responseString = validation.validatePolicy(policyData).toString();
124         assertThat(responseString).contains("The Value in Required Field will allow only '{0-9}, {a-z}, {A-Z}");
125
126         policyData.setPolicyName("ALegalPolicyName");
127         policyData.setPolicyDescription("@CreatedBy:");
128         responseString = validation.validatePolicy(policyData).toString();
129         assertThat(responseString)
130                         .contains("The value in the description shouldn't contain @CreatedBy: or @ModifiedBy:");
131         policyData.setPolicyDescription("@CreatedBy:");
132
133         policyData.setPolicyDescription("A Legal Description");
134         responseString = validation.validatePolicy(policyData).toString();
135         assertThat(responseString)
136                         .doesNotContain("The value in the description shouldn't contain @CreatedBy: or @ModifiedBy:");
137     }
138
139     @Test
140     public void testPolicyAttributeValidation() throws IOException {
141         PolicyValidation validation = new PolicyValidation();
142         PolicyRestAdapter policyData = new PolicyRestAdapter();
143         policyData.setPolicyName("ALegalPolicyName");
144         policyData.setPolicyDescription("A Valid Description");
145
146         String responseString = validation.validatePolicy(policyData).toString();
147         assertEquals("success", responseString);
148
149         policyData.setApiflag("API");
150         responseString = validation.validatePolicy(policyData).toString();
151         assertEquals("success", responseString);
152
153         policyData.setApiflag("NOTAPI");
154         responseString = validation.validatePolicy(policyData).toString();
155         assertEquals("success", responseString);
156
157         List<Object> attributes = new ArrayList<>();
158         policyData.setAttributes(attributes);
159         responseString = validation.validatePolicy(policyData).toString();
160         assertEquals("success", responseString);
161
162         attributes.add(new String("hello"));
163         responseString = validation.validatePolicy(policyData).toString();
164         assertEquals("success", responseString);
165         attributes.clear();
166
167         Map<String, String> mapAttribute = new LinkedHashMap<>();
168         attributes.add(mapAttribute);
169         responseString = validation.validatePolicy(policyData).toString();
170         assertThat(responseString).contains("Component Attributes</b>:<i> has one missing Component Attribute value");
171
172         mapAttribute.put("key", "value");
173         responseString = validation.validatePolicy(policyData).toString();
174         assertEquals("<b>Component Attributes</b>:<i> has one missing Component Attribute value</i><br>",
175                         responseString);
176
177         mapAttribute.put("key", "");
178         responseString = validation.validatePolicy(policyData).toString();
179         assertEquals("<b>Attributes or Component Attributes</b>:<i>null : value has spaces or invalid characters</i>"
180                         + "<br><b>Component Attributes</b>:<i> has one missing Component Attribute value</i><br>",
181                         responseString);
182         mapAttribute.clear();
183
184         responseString = validation.validatePolicy(policyData).toString();
185         mapAttribute.put("hello", "aaa");
186         responseString = validation.validatePolicy(policyData).toString();
187         assertEquals("<b>Component Attributes</b>:<i> has one missing Component Attribute key</i><br>"
188                         + "<b>Component Attributes</b>:<i> has one missing Component Attribute value</i><br>",
189                         responseString);
190
191         policyData.setPolicyType("Config");
192         responseString = validation.validatePolicy(policyData).toString();
193         assertEquals("<b>RiskType</b>: Risk Type Should not be Empty</i><br>"
194                         + "<b>RiskLevel</b>: Risk Level Should not be Empty</i><br>"
195                         + "<b>Guard</b>: Guard Value Should not be Empty</i><br>", responseString);
196
197         policyData.setConfigPolicyType("Base");
198         responseString = validation.validatePolicy(policyData).toString();
199         assertThat(responseString).contains("has one missing Attribute key");
200
201         policyData.setConfigPolicyType("BRMS_Param");
202         responseString = validation.validatePolicy(policyData).toString();
203         assertThat(responseString).contains("has one missing Attribute key");
204
205         policyData.setConfigPolicyType("BRMS_Raw");
206         responseString = validation.validatePolicy(policyData).toString();
207         assertThat(responseString).contains("has one missing Attribute key");
208
209         policyData.setConfigPolicyType(null);
210         responseString = validation.validatePolicy(policyData).toString();
211         assertThat(responseString).contains("Risk Level Should not be Empty");
212
213         mapAttribute.clear();
214         mapAttribute.put("value", "thevalue");
215         responseString = validation.validatePolicy(policyData).toString();
216         assertThat(responseString).contains("Risk Level Should not be Empty");
217
218         mapAttribute.put("value", "$$$%%%%");
219         responseString = validation.validatePolicy(policyData).toString();
220         assertThat(responseString).contains("Risk Level Should not be Empty");
221
222         policyData.setConfigPolicyType("Base");
223         responseString = validation.validatePolicy(policyData).toString();
224         assertThat(responseString).contains("value has spaces or invalid characters");
225
226         policyData.setConfigPolicyType("BRMS_Param");
227         responseString = validation.validatePolicy(policyData).toString();
228         assertThat(responseString).contains("value has spaces or invalid characters");
229
230         policyData.setConfigPolicyType("BRMS_Raw");
231         responseString = validation.validatePolicy(policyData).toString();
232         assertThat(responseString).contains("value has spaces or invalid characters");
233
234         policyData.setConfigPolicyType(null);
235         policyData.setPolicyType(null);
236         responseString = validation.validatePolicy(policyData).toString();
237         assertThat(responseString).contains("value has spaces or invalid characters");
238     }
239
240     @Test
241     public void testPolicySettingsValidation() throws IOException {
242         PolicyValidation validation = new PolicyValidation();
243         PolicyRestAdapter policyData = new PolicyRestAdapter();
244         policyData.setPolicyName("ALegalPolicyName");
245         policyData.setPolicyDescription("A Valid Description");
246
247         String responseString = validation.validatePolicy(policyData).toString();
248         assertEquals("success", responseString);
249
250         policyData.setApiflag("API");
251         responseString = validation.validatePolicy(policyData).toString();
252         assertEquals("success", responseString);
253
254         policyData.setApiflag("NOTAPI");
255         responseString = validation.validatePolicy(policyData).toString();
256         assertEquals("success", responseString);
257
258         List<Object> settings = new ArrayList<>();
259         policyData.setSettings(settings);
260         responseString = validation.validatePolicy(policyData).toString();
261         assertEquals("success", responseString);
262
263         settings.add("hello");
264         responseString = validation.validatePolicy(policyData).toString();
265         assertEquals("success", responseString);
266         settings.clear();
267
268         Map<String, String> mapSetting = new LinkedHashMap<>();
269         settings.add(mapSetting);
270         responseString = validation.validatePolicy(policyData).toString();
271         assertThat(responseString).contains("Settings Attributes</b>:<i> has one missing Attribute key");
272
273         mapSetting.put("key", "value");
274         responseString = validation.validatePolicy(policyData).toString();
275         assertEquals("<b>Settings Attributes</b>:<i> has one missing Attribute Value</i><br>", responseString);
276
277         mapSetting.put("key", "");
278         responseString = validation.validatePolicy(policyData).toString();
279         assertEquals("<b>Settings Attributes</b>:<i> has one missing Attribute Value</i><br>", responseString);
280         mapSetting.clear();
281
282         mapSetting.put("value", "thevalue");
283         responseString = validation.validatePolicy(policyData).toString();
284         assertThat(responseString).contains("has one missing Attribute key");
285
286         mapSetting.put("value", "$$$%%%");
287         responseString = validation.validatePolicy(policyData).toString();
288         assertThat(responseString).contains("value has spaces or invalid characters");
289     }
290
291     @Test
292     public void testPolicyRuleAlgorithmsValidation() throws IOException {
293         PolicyValidation validation = new PolicyValidation();
294         PolicyRestAdapter policyData = new PolicyRestAdapter();
295         policyData.setPolicyName("ALegalPolicyName");
296         policyData.setPolicyDescription("A Valid Description");
297
298         String responseString = validation.validatePolicy(policyData).toString();
299         assertEquals("success", responseString);
300
301         policyData.setApiflag("API");
302         responseString = validation.validatePolicy(policyData).toString();
303         assertEquals("success", responseString);
304
305         policyData.setApiflag("NOTAPI");
306         responseString = validation.validatePolicy(policyData).toString();
307         assertEquals("success", responseString);
308
309         List<Object> ruleAlgorithmschoices = new ArrayList<>();
310         policyData.setRuleAlgorithmschoices(ruleAlgorithmschoices);
311         responseString = validation.validatePolicy(policyData).toString();
312         assertEquals("success", responseString);
313
314         ruleAlgorithmschoices.add("hello");
315         responseString = validation.validatePolicy(policyData).toString();
316         assertEquals("success", responseString);
317         ruleAlgorithmschoices.clear();
318
319         Map<String, String> mapChoice = new LinkedHashMap<>();
320         ruleAlgorithmschoices.add(mapChoice);
321         assertNull(validation.validatePolicy(policyData));
322
323         mapChoice.clear();
324         mapChoice.put("id", "TheID");
325         responseString = validation.validatePolicy(policyData).toString();
326         assertThat(responseString).contains("Field 1 value is not selected");
327
328         mapChoice.put("dynamicRuleAlgorithmField1", "Field1");
329         responseString = validation.validatePolicy(policyData).toString();
330         assertThat(responseString).contains("Field 2 value is not selected");
331
332         mapChoice.put("dynamicRuleAlgorithmCombo", "Combo");
333         responseString = validation.validatePolicy(policyData).toString();
334         assertThat(responseString).contains("Field 3 value is empty");
335
336         mapChoice.put("dynamicRuleAlgorithmField2", "Field2");
337         responseString = validation.validatePolicy(policyData).toString();
338         assertEquals("success", responseString);
339
340         mapChoice.put("dynamicRuleAlgorithmField2", "%%%$$$");
341         responseString = validation.validatePolicy(policyData).toString();
342         assertThat(responseString).contains("Field 3 value has special characters");
343     }
344 }

© 2017 ONAP. Copyright © The Linux Foundation ®. All Rights Reserved.
The Linux Foundation has registered trademarks and uses trademarks.
For a list of trademarks of The Linux Foundation, please see our Trademark Usage page.
Linux is a registered trademark of Linus Torvalds.
Privacy Policy and Terms of Use