aboutsummaryrefslogtreecommitdiffstats
path: root/PolicyEngineUtils/src/test/java/org/onap/policy/utils/test/PolicyUtilsTest.java
blob: 8d9116d32fd0802123818de7041d188b7dcc9f78 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*-
 * ============LICENSE_START=======================================================
 * PolicyEngineUtils
 * ================================================================================
 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.utils.test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import java.util.ArrayList;
import java.util.Base64;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.junit.Test;
import org.onap.policy.api.NotificationType;
import org.onap.policy.api.PDPNotification;
import org.onap.policy.api.UpdateType;
import org.onap.policy.std.StdLoadedPolicy;
import org.onap.policy.std.StdPDPNotification;
import org.onap.policy.std.StdRemovedPolicy;
import org.onap.policy.utils.PolicyUtils;
import org.onap.policy.utils.XMLErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

public class PolicyUtilsTest {

    private static final String ERROR =
            "The Value in Required Field will allow only '{0-9}, {a-z}, {A-Z}, _' following set of Combinations";
    private static final String SUCCESS = "success";

    @Test
    public void testJsonConversions() throws Exception {
        StdPDPNotification notification = new StdPDPNotification();
        notification.setNotificationType(NotificationType.BOTH);
        Collection<StdRemovedPolicy> removedPolicies = new ArrayList<>();
        StdRemovedPolicy removedPolicy = new StdRemovedPolicy();
        removedPolicy.setPolicyName("Test");
        removedPolicy.setVersionNo("1");
        removedPolicies.add(removedPolicy);
        StdLoadedPolicy updatedPolicy = new StdLoadedPolicy();
        updatedPolicy.setPolicyName("Testing");
        updatedPolicy.setVersionNo("1");
        updatedPolicy.setUpdateType(UpdateType.NEW);
        Map<String, String> matches = new HashMap<>();
        matches.put("key", "value");
        updatedPolicy.setMatches(matches);
        Collection<StdLoadedPolicy> loadedPolicies = new ArrayList<>();
        loadedPolicies.add(updatedPolicy);
        notification.setRemovedPolicies(removedPolicies);
        notification.setLoadedPolicies(loadedPolicies);

        String json = PolicyUtils.objectToJsonString(notification);
        PDPNotification getBackObject = PolicyUtils.jsonStringToObject(json, StdPDPNotification.class);
        assertEquals(0, getBackObject.getNotificationType().compareTo(notification.getNotificationType()));

    }

    private String encodedValue(String input) {
        return new String(Base64.getEncoder().encode(input.getBytes()));
    }

    @Test
    public void testDecode() throws Exception {
        String value = "test";
        assertEquals(value, PolicyUtils.decode(encodedValue(value)));
        assertNull(PolicyUtils.decode(null));
        assertNull(PolicyUtils.decode(""));
    }

    @Test
    public void testBasicEncoding() throws Exception {
        String userName = "test";
        String key = "pass";
        String[] decodedValue = PolicyUtils.decodeBasicEncoding("Basic " + encodedValue(userName + ":" + key));
        assertEquals(userName, decodedValue[0]);
        assertEquals(key, decodedValue[1]);
        assertEquals(0, PolicyUtils.decodeBasicEncoding(encodedValue(userName + ":" + key)).length);
        assertEquals(0, PolicyUtils.decodeBasicEncoding(null).length);
    }

    @Test
    public void testSpecialCharValidator() {
        assertEquals(ERROR, PolicyUtils.policySpecialCharValidator("$TEST_"));
        assertEquals(ERROR, PolicyUtils.policySpecialCharValidator("$TEST _"));
        assertEquals(ERROR, PolicyUtils.policySpecialCharValidator(""));
        assertEquals(ERROR, PolicyUtils.policySpecialCharValidator("TæST"));
        assertEquals(SUCCESS, PolicyUtils.policySpecialCharValidator("TEST"));
    }

    @Test
    public void testSpecialCharWithSpaceValidator() {
        assertEquals(ERROR, PolicyUtils.policySpecialCharWithSpaceValidator(""));
        assertEquals(ERROR, PolicyUtils.policySpecialCharWithSpaceValidator("$TEST _"));
        assertEquals(SUCCESS, PolicyUtils.policySpecialCharWithSpaceValidator("TE ST"));
    }

    @Test
    public void testDescription() {
        assertEquals(SUCCESS, PolicyUtils.descriptionValidator("Test"));
        assertNotEquals(SUCCESS, PolicyUtils.descriptionValidator("@ModifiedBy:TesterB"));
        assertNotEquals(SUCCESS, PolicyUtils.descriptionValidator("@CreatedBy:TesterA"));
    }

    @Test
    public void testNonAscii() {
        assertTrue(PolicyUtils.containsNonAsciiEmptyChars(null));
        assertTrue(PolicyUtils.containsNonAsciiEmptyChars(""));
        assertTrue(PolicyUtils.containsNonAsciiEmptyChars("T æST"));
        assertTrue(PolicyUtils.containsNonAsciiEmptyChars("TæST"));
        assertFalse(PolicyUtils.containsNonAsciiEmptyChars("TEST"));
    }

    @Test
    public void testInteger() {
        assertFalse(PolicyUtils.isInteger(null));
        assertTrue(PolicyUtils.isInteger("123"));
        assertFalse(PolicyUtils.isInteger("1a23"));
    }

    @Test
    public void testEmailAddress() {
        assertEquals(SUCCESS, PolicyUtils.validateEmailAddress("test@onap.org"));
        assertNotEquals(SUCCESS, PolicyUtils.validateEmailAddress("test@onap"));
    }

    @Test
    public void testBrmsValidate() {
        String rule = "package com.sample;\n" + "import com.sample.DroolsTest.Message;\n" + "declare Params\n"
                + "samPoll : int\n" + "value : String\n" + "end\n" + "///This Rule will be generated by the UI.\n"
                + "rule \"Create parameters structure\"\n" + "salience 1000  \n" + "when\n" + "then\n"
                + "Params params = new Params();\n" + "params.setSamPoll(76);\n" + "params.setValue(\"test\");\n"
                + "insertLogical(params);\n" + "end\n"
                + "rule \"Rule 1: Check parameter structure access from when/then\"\n" + "when\n" + "$param: Params()\n"
                + "Params($param.samPoll > 50)\n" + "then\n" + "System.out.println(\"Firing rule 1\");\n"
                + "System.out.println($param);\n" + "end\n";
        assertEquals(PolicyUtils.brmsRawValidate(rule), "");
        assertTrue(PolicyUtils.brmsRawValidate("error").contains("[ERR"));
        assertFalse(
                PolicyUtils.brmsRawValidate("package com.att.ecomp.policy.controlloop.p_${unique};").contains("[ERR"));
    }

    @Test
    public void testiIsJsonValid() {
        assertTrue(PolicyUtils.isJSONValid("{\"test\":\"test\"}"));
        String value = "{\"test\":\"test\", \"t1\": {\"test\": 12 , \"t2\":\"34\"},\"t2\":[{\"test\":\"val\"}]}";
        assertTrue(PolicyUtils.isJSONValid(value));
        assertFalse(PolicyUtils.isJSONValid("{\"test\":\"test"));
    }

    @Test
    public void testIsXmlValid() throws SAXException {
        assertFalse(PolicyUtils.isXMLValid(null));
        assertFalse(PolicyUtils.isXMLValid(""));
        XMLErrorHandler error = new XMLErrorHandler();
        error.error(new SAXParseException(null, null));
        error.warning(new SAXParseException(null, null));
        assertTrue(PolicyUtils.isXMLValid("<test>123</test>"));
        assertFalse(PolicyUtils.isXMLValid("<test>123</test"));
    }

    @Test
    public void testIsPropValid() {
        assertTrue(PolicyUtils.isPropValid("test=123\n\tval=123"));
        assertFalse(PolicyUtils.isPropValid("test"));
        assertFalse(PolicyUtils.isPropValid("test="));
        assertTrue(PolicyUtils.isPropValid("#test\n\nval=123"));
    }

    @Test
    public void testVersionStringToArray() {
        assertTrue(PolicyUtils.versionStringToArray(null).length == 0);
        assertTrue(PolicyUtils.versionStringToArray("").length == 0);
        assertTrue(PolicyUtils.versionStringToArray("1.2.3").length == 3);
    }

    @Test
    public void testSpecialChar() {
        assertEquals(PolicyUtils.SUCCESS, PolicyUtils.policySpecialCharWithDashValidator("test"));
        assertEquals(PolicyUtils.SUCCESS, PolicyUtils.policySpecialCharWithDashValidator("test-"));
        assertEquals(PolicyUtils.SUCCESS, PolicyUtils.policySpecialCharWithDashValidator("test_"));
        assertNotEquals(PolicyUtils.SUCCESS, PolicyUtils.policySpecialCharWithDashValidator(""));
        assertNotEquals(PolicyUtils.SUCCESS, PolicyUtils.policySpecialCharWithDashValidator("test*"));
    }
}