Re-format source code
[policy/engine.git] / PolicyEngineUtils / src / test / java / org / onap / policy / utils / test / PolicyUtilsTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * PolicyEngineUtils
4  * ================================================================================
5  * Copyright (C) 2017-2018 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.utils.test;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertNotEquals;
26 import static org.junit.Assert.assertNull;
27 import static org.junit.Assert.assertTrue;
28
29 import java.util.ArrayList;
30 import java.util.Base64;
31 import java.util.Collection;
32 import java.util.HashMap;
33 import java.util.Map;
34
35 import org.junit.Test;
36 import org.onap.policy.api.NotificationType;
37 import org.onap.policy.api.PDPNotification;
38 import org.onap.policy.api.UpdateType;
39 import org.onap.policy.std.StdLoadedPolicy;
40 import org.onap.policy.std.StdPDPNotification;
41 import org.onap.policy.std.StdRemovedPolicy;
42 import org.onap.policy.utils.PolicyUtils;
43 import org.onap.policy.utils.XMLErrorHandler;
44 import org.xml.sax.SAXException;
45 import org.xml.sax.SAXParseException;
46
47 public class PolicyUtilsTest {
48
49     private static final String ERROR =
50             "The Value in Required Field will allow only '{0-9}, {a-z}, {A-Z}, _' following set of Combinations";
51     private static final String SUCCESS = "success";
52
53     @Test
54     public void testJsonConversions() throws Exception {
55         StdPDPNotification notification = new StdPDPNotification();
56         notification.setNotificationType(NotificationType.BOTH);
57         Collection<StdRemovedPolicy> removedPolicies = new ArrayList<>();
58         StdRemovedPolicy removedPolicy = new StdRemovedPolicy();
59         removedPolicy.setPolicyName("Test");
60         removedPolicy.setVersionNo("1");
61         removedPolicies.add(removedPolicy);
62         StdLoadedPolicy updatedPolicy = new StdLoadedPolicy();
63         updatedPolicy.setPolicyName("Testing");
64         updatedPolicy.setVersionNo("1");
65         updatedPolicy.setUpdateType(UpdateType.NEW);
66         Map<String, String> matches = new HashMap<>();
67         matches.put("key", "value");
68         updatedPolicy.setMatches(matches);
69         Collection<StdLoadedPolicy> loadedPolicies = new ArrayList<>();
70         loadedPolicies.add(updatedPolicy);
71         notification.setRemovedPolicies(removedPolicies);
72         notification.setLoadedPolicies(loadedPolicies);
73
74         String json = PolicyUtils.objectToJsonString(notification);
75         PDPNotification getBackObject = PolicyUtils.jsonStringToObject(json, StdPDPNotification.class);
76         assertEquals(0, getBackObject.getNotificationType().compareTo(notification.getNotificationType()));
77
78     }
79
80     private String encodedValue(String input) {
81         return new String(Base64.getEncoder().encode(input.getBytes()));
82     }
83
84     @Test
85     public void testDecode() throws Exception {
86         String value = "test";
87         assertEquals(value, PolicyUtils.decode(encodedValue(value)));
88         assertNull(PolicyUtils.decode(null));
89         assertNull(PolicyUtils.decode(""));
90     }
91
92     @Test
93     public void testBasicEncoding() throws Exception {
94         String userName = "test";
95         String key = "pass";
96         String[] decodedValue = PolicyUtils.decodeBasicEncoding("Basic " + encodedValue(userName + ":" + key));
97         assertEquals(userName, decodedValue[0]);
98         assertEquals(key, decodedValue[1]);
99         assertEquals(0, PolicyUtils.decodeBasicEncoding(encodedValue(userName + ":" + key)).length);
100         assertEquals(0, PolicyUtils.decodeBasicEncoding(null).length);
101     }
102
103     @Test
104     public void testSpecialCharValidator() {
105         assertEquals(ERROR, PolicyUtils.policySpecialCharValidator("$TEST_"));
106         assertEquals(ERROR, PolicyUtils.policySpecialCharValidator("$TEST _"));
107         assertEquals(ERROR, PolicyUtils.policySpecialCharValidator(""));
108         assertEquals(ERROR, PolicyUtils.policySpecialCharValidator("TæST"));
109         assertEquals(SUCCESS, PolicyUtils.policySpecialCharValidator("TEST"));
110     }
111
112     @Test
113     public void testSpecialCharWithSpaceValidator() {
114         assertEquals(ERROR, PolicyUtils.policySpecialCharWithSpaceValidator(""));
115         assertEquals(ERROR, PolicyUtils.policySpecialCharWithSpaceValidator("$TEST _"));
116         assertEquals(SUCCESS, PolicyUtils.policySpecialCharWithSpaceValidator("TE ST"));
117     }
118
119     @Test
120     public void testDescription() {
121         assertEquals(SUCCESS, PolicyUtils.descriptionValidator("Test"));
122         assertNotEquals(SUCCESS, PolicyUtils.descriptionValidator("@ModifiedBy:TesterB"));
123         assertNotEquals(SUCCESS, PolicyUtils.descriptionValidator("@CreatedBy:TesterA"));
124     }
125
126     @Test
127     public void testNonAscii() {
128         assertTrue(PolicyUtils.containsNonAsciiEmptyChars(null));
129         assertTrue(PolicyUtils.containsNonAsciiEmptyChars(""));
130         assertTrue(PolicyUtils.containsNonAsciiEmptyChars("T æST"));
131         assertTrue(PolicyUtils.containsNonAsciiEmptyChars("TæST"));
132         assertFalse(PolicyUtils.containsNonAsciiEmptyChars("TEST"));
133     }
134
135     @Test
136     public void testInteger() {
137         assertFalse(PolicyUtils.isInteger(null));
138         assertTrue(PolicyUtils.isInteger("123"));
139         assertFalse(PolicyUtils.isInteger("1a23"));
140     }
141
142     @Test
143     public void testEmailAddress() {
144         assertEquals(SUCCESS, PolicyUtils.validateEmailAddress("test@onap.org"));
145         assertNotEquals(SUCCESS, PolicyUtils.validateEmailAddress("test@onap"));
146     }
147
148     @Test
149     public void testBrmsValidate() {
150         String rule = "package com.sample;\n" + "import com.sample.DroolsTest.Message;\n" + "declare Params\n"
151                 + "samPoll : int\n" + "value : String\n" + "end\n" + "///This Rule will be generated by the UI.\n"
152                 + "rule \"Create parameters structure\"\n" + "salience 1000  \n" + "when\n" + "then\n"
153                 + "Params params = new Params();\n" + "params.setSamPoll(76);\n" + "params.setValue(\"test\");\n"
154                 + "insertLogical(params);\n" + "end\n"
155                 + "rule \"Rule 1: Check parameter structure access from when/then\"\n" + "when\n" + "$param: Params()\n"
156                 + "Params($param.samPoll > 50)\n" + "then\n" + "System.out.println(\"Firing rule 1\");\n"
157                 + "System.out.println($param);\n" + "end\n";
158         assertEquals(PolicyUtils.brmsRawValidate(rule), "");
159         assertTrue(PolicyUtils.brmsRawValidate("error").contains("[ERR"));
160         assertFalse(
161                 PolicyUtils.brmsRawValidate("package com.att.ecomp.policy.controlloop.p_${unique};").contains("[ERR"));
162     }
163
164     @Test
165     public void testiIsJsonValid() {
166         assertTrue(PolicyUtils.isJSONValid("{\"test\":\"test\"}"));
167         String value = "{\"test\":\"test\", \"t1\": {\"test\": 12 , \"t2\":\"34\"},\"t2\":[{\"test\":\"val\"}]}";
168         assertTrue(PolicyUtils.isJSONValid(value));
169         assertFalse(PolicyUtils.isJSONValid("{\"test\":\"test"));
170     }
171
172     @Test
173     public void testIsXmlValid() throws SAXException {
174         assertFalse(PolicyUtils.isXMLValid(null));
175         assertFalse(PolicyUtils.isXMLValid(""));
176         XMLErrorHandler error = new XMLErrorHandler();
177         error.error(new SAXParseException(null, null));
178         error.warning(new SAXParseException(null, null));
179         assertTrue(PolicyUtils.isXMLValid("<test>123</test>"));
180         assertFalse(PolicyUtils.isXMLValid("<test>123</test"));
181     }
182
183     @Test
184     public void testIsPropValid() {
185         assertTrue(PolicyUtils.isPropValid("test=123\n\tval=123"));
186         assertFalse(PolicyUtils.isPropValid("test"));
187         assertFalse(PolicyUtils.isPropValid("test="));
188         assertTrue(PolicyUtils.isPropValid("#test\n\nval=123"));
189     }
190
191     @Test
192     public void testVersionStringToArray() {
193         assertTrue(PolicyUtils.versionStringToArray(null).length == 0);
194         assertTrue(PolicyUtils.versionStringToArray("").length == 0);
195         assertTrue(PolicyUtils.versionStringToArray("1.2.3").length == 3);
196     }
197
198     @Test
199     public void testSpecialChar() {
200         assertEquals(PolicyUtils.SUCCESS, PolicyUtils.policySpecialCharWithDashValidator("test"));
201         assertEquals(PolicyUtils.SUCCESS, PolicyUtils.policySpecialCharWithDashValidator("test-"));
202         assertEquals(PolicyUtils.SUCCESS, PolicyUtils.policySpecialCharWithDashValidator("test_"));
203         assertNotEquals(PolicyUtils.SUCCESS, PolicyUtils.policySpecialCharWithDashValidator(""));
204         assertNotEquals(PolicyUtils.SUCCESS, PolicyUtils.policySpecialCharWithDashValidator("test*"));
205     }
206 }