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