2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.ccsdk.sli.core.slipluginutils;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertNotNull;
27 import java.util.HashMap;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
32 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
36 public class SliPluginUtils_StaticFunctionsTest {
37 private static final Logger LOG = LoggerFactory.getLogger(SliPluginUtils_StaticFunctionsTest.class);
38 SliPluginUtils utils = new SliPluginUtils();
39 private SvcLogicContext ctx;
40 private HashMap<String, String> parameters;
43 public void setUp() throws Exception {
44 this.ctx = new SvcLogicContext();
45 parameters = new HashMap<String, String>();
50 public final void testCtxGetBeginsWith() {
51 ctx.setAttribute("service-data.oper-status.order-status", "InProgress");
52 ctx.setAttribute("service-data.service-information.service-instance-id", "my-instance");
53 ctx.setAttribute("service-data.service-information.service-type", "my-service");
55 Map<String, String> entries = SliPluginUtils.ctxGetBeginsWith(ctx, "service-data.service-information");
57 assertEquals("my-instance", entries.get("service-data.service-information.service-instance-id"));
58 assertEquals("my-service", entries.get("service-data.service-information.service-type"));
59 assertFalse(entries.containsKey("service-data.oper-status.order-status"));
64 public final void testCtxListRemove_index() throws SvcLogicException {
65 LOG.trace("=== testCtxListRemove_index ===");
66 ctx.setAttribute("service-data.vnf-l3[0].vnf-host-name", "vnf-host-name_0");
67 ctx.setAttribute("service-data.vnf-l3[0].device-host-name", "device-host-name_0");
68 ctx.setAttribute("service-data.vnf-l3[1].vnf-host-name", "vnf-host-name_1");
69 ctx.setAttribute("service-data.vnf-l3[1].device-host-name", "device-host-name_1");
70 ctx.setAttribute("service-data.vnf-l3[2].vnf-host-name", "vnf-host-name_2");
71 ctx.setAttribute("service-data.vnf-l3[2].device-host-name", "device-host-name_2");
72 ctx.setAttribute("service-data.vnf-l3_length", "3");
74 parameters.put("index", "1");
75 parameters.put("list_pfx", "service-data.vnf-l3");
77 utils.ctxListRemove(parameters, ctx);
78 SliPluginUtils.logContextMemory(ctx, LOG, SliPluginUtils.LogLevel.TRACE);
80 assertEquals("2", ctx.getAttribute("service-data.vnf-l3_length"));
81 assertEquals("vnf-host-name_0", ctx.getAttribute("service-data.vnf-l3[0].vnf-host-name"));
82 assertEquals("device-host-name_0", ctx.getAttribute("service-data.vnf-l3[0].device-host-name"));
83 assertEquals("vnf-host-name_2", ctx.getAttribute("service-data.vnf-l3[1].vnf-host-name"));
84 assertEquals("device-host-name_2", ctx.getAttribute("service-data.vnf-l3[1].device-host-name"));
89 public final void textCtxListRemove_keyValue() throws SvcLogicException {
90 LOG.trace("=== textCtxListRemove_keyValue ===");
91 ctx.setAttribute("service-data.vnf-l3[0].vnf-host-name", "vnf-host-name_0");
92 ctx.setAttribute("service-data.vnf-l3[0].device-host-name", "device-host-name_0");
93 ctx.setAttribute("service-data.vnf-l3[1].vnf-host-name", "vnf-host-name_1");
94 ctx.setAttribute("service-data.vnf-l3[1].device-host-name", "device-host-name_1");
95 ctx.setAttribute("service-data.vnf-l3[2].vnf-host-name", "vnf-host-name_2");
96 ctx.setAttribute("service-data.vnf-l3[2].device-host-name", "device-host-name_2");
98 ctx.setAttribute("service-data.vnf-l3[3].vnf-host-name", "vnf-host-name_1");
99 ctx.setAttribute("service-data.vnf-l3[3].device-host-name", "device-host-name_1");
100 ctx.setAttribute("service-data.vnf-l3_length", "4");
102 parameters.put("list_pfx", "service-data.vnf-l3");
103 parameters.put("key", "vnf-host-name");
104 parameters.put("value", "vnf-host-name_1");
106 utils.ctxListRemove(parameters, ctx);
107 SliPluginUtils.logContextMemory(ctx, LOG, SliPluginUtils.LogLevel.TRACE);
109 assertEquals("2", ctx.getAttribute("service-data.vnf-l3_length"));
110 assertEquals("vnf-host-name_0", ctx.getAttribute("service-data.vnf-l3[0].vnf-host-name"));
111 assertEquals("device-host-name_0", ctx.getAttribute("service-data.vnf-l3[0].device-host-name"));
112 assertEquals("vnf-host-name_2", ctx.getAttribute("service-data.vnf-l3[1].vnf-host-name"));
113 assertEquals("device-host-name_2", ctx.getAttribute("service-data.vnf-l3[1].device-host-name"));
118 public final void textCtxListRemove_keyValue_nullkey() throws SvcLogicException {
119 LOG.trace("=== textCtxListRemove_keyValue_nullkey ===");
120 ctx.setAttribute("service-data.vnf-l3[0]", "vnf-host-name_0");
121 ctx.setAttribute("service-data.vnf-l3[1]", "vnf-host-name_1");
122 ctx.setAttribute("service-data.vnf-l3[2]", "vnf-host-name_2");
123 ctx.setAttribute("service-data.vnf-l3_length", "3");
125 parameters.put("list_pfx", "service-data.vnf-l3");
126 parameters.put("value", "vnf-host-name_1");
128 utils.ctxListRemove(parameters, ctx);
129 SliPluginUtils.logContextMemory(ctx, LOG, SliPluginUtils.LogLevel.TRACE);
131 assertEquals("2", ctx.getAttribute("service-data.vnf-l3_length"));
132 assertEquals("vnf-host-name_0", ctx.getAttribute("service-data.vnf-l3[0]"));
133 assertEquals("vnf-host-name_2", ctx.getAttribute("service-data.vnf-l3[1]"));
138 public final void textCtxListRemove_keyValueList() throws SvcLogicException {
139 LOG.trace("=== textCtxListRemove_keyValueList ===");
140 ctx.setAttribute("service-data.vnf-l3[0].vnf-host-name", "vnf-host-name_0");
141 ctx.setAttribute("service-data.vnf-l3[0].device-host-name", "device-host-name_0");
142 ctx.setAttribute("service-data.vnf-l3[1].vnf-host-name", "vnf-host-name_1");
143 ctx.setAttribute("service-data.vnf-l3[1].device-host-name", "device-host-name_1");
144 ctx.setAttribute("service-data.vnf-l3[2].vnf-host-name", "vnf-host-name_2");
145 ctx.setAttribute("service-data.vnf-l3[2].device-host-name", "device-host-name_2");
147 ctx.setAttribute("service-data.vnf-l3[3].vnf-host-name", "vnf-host-name_1");
148 ctx.setAttribute("service-data.vnf-l3[3].device-host-name", "device-host-name_1");
149 // entries with only 1 of 2 key-value pairs matching
150 ctx.setAttribute("service-data.vnf-l3[4].vnf-host-name", "vnf-host-name_1");
151 ctx.setAttribute("service-data.vnf-l3[4].device-host-name", "device-host-name_4");
152 ctx.setAttribute("service-data.vnf-l3[5].vnf-host-name", "vnf-host-name_5");
153 ctx.setAttribute("service-data.vnf-l3[5].device-host-name", "device-host-name_1");
154 ctx.setAttribute("service-data.vnf-l3_length", "6");
156 parameters.put("list_pfx", "service-data.vnf-l3");
157 parameters.put("keys_length", "2");
158 parameters.put("keys[0].key", "vnf-host-name");
159 parameters.put("keys[0].value", "vnf-host-name_1");
160 parameters.put("keys[1].key", "device-host-name");
161 parameters.put("keys[1].value", "device-host-name_1");
163 utils.ctxListRemove(parameters, ctx);
164 SliPluginUtils.logContextMemory(ctx, LOG, SliPluginUtils.LogLevel.TRACE);
166 assertEquals("4", ctx.getAttribute("service-data.vnf-l3_length"));
167 assertEquals("vnf-host-name_0", ctx.getAttribute("service-data.vnf-l3[0].vnf-host-name"));
168 assertEquals("device-host-name_0", ctx.getAttribute("service-data.vnf-l3[0].device-host-name"));
169 assertEquals("vnf-host-name_2", ctx.getAttribute("service-data.vnf-l3[1].vnf-host-name"));
170 assertEquals("device-host-name_2", ctx.getAttribute("service-data.vnf-l3[1].device-host-name"));
171 assertEquals("vnf-host-name_1", ctx.getAttribute("service-data.vnf-l3[2].vnf-host-name"));
172 assertEquals("device-host-name_4", ctx.getAttribute("service-data.vnf-l3[2].device-host-name"));
173 assertEquals("vnf-host-name_5", ctx.getAttribute("service-data.vnf-l3[3].vnf-host-name"));
174 assertEquals("device-host-name_1", ctx.getAttribute("service-data.vnf-l3[3].device-host-name"));
178 @Test(expected = SvcLogicException.class)
179 public final void testCtxListRemove_nullListLength() throws SvcLogicException {
180 LOG.trace("=== testCtxListRemove_nullListLength ===");
181 ctx.setAttribute("service-data.vnf-l3[0].vnf-host-name", "vnf-host-name_0");
182 ctx.setAttribute("service-data.vnf-l3[0].device-host-name", "device-host-name_0");
183 ctx.setAttribute("service-data.vnf-l3[1].vnf-host-name", "vnf-host-name_1");
184 ctx.setAttribute("service-data.vnf-l3[1].device-host-name", "device-host-name_1");
185 ctx.setAttribute("service-data.vnf-l3[2].vnf-host-name", "vnf-host-name_2");
186 ctx.setAttribute("service-data.vnf-l3[2].device-host-name", "device-host-name_2");
188 parameters.put("index", "1");
189 parameters.put("list_pfx", "service-data.vnf-l3");
191 utils.ctxListRemove(parameters, ctx);
196 public final void testCtxPutAll() {
197 HashMap<String, Object> entries = new HashMap<String, Object>();
198 entries.put("service-data.oper-status.order-status", "InProgress");
199 entries.put("service-data.service-information.service-instance-id", "my-instance");
200 entries.put("service-data.request-information.order-number", 1234);
201 entries.put("service-data.request-information.request-id", null);
203 SliPluginUtils.ctxPutAll(ctx, entries);
205 assertEquals("InProgress", ctx.getAttribute("service-data.oper-status.order-status"));
206 assertEquals("my-instance", ctx.getAttribute("service-data.service-information.service-instance-id"));
207 assertEquals("1234", ctx.getAttribute("service-data.request-information.order-number"));
208 assertFalse(ctx.getAttributeKeySet().contains("service-data.request-information.request-id"));
213 public final void testCtxSetAttribute_LOG() {
214 LOG.debug("=== testCtxSetAttribute_LOG ===");
215 Integer i = new Integer(3);
216 SliPluginUtils.ctxSetAttribute(ctx, "test", i, LOG, SliPluginUtils.LogLevel.TRACE);
220 public void printContext() throws SvcLogicException, IOException {
221 String filePath = "/src/test/resources/printContext.txt";
222 parameters.put("filename", filePath);
223 File f = new File(filePath);
225 assert (!f.isDirectory());
226 ctx.setAttribute("hello", "world");
227 ctx.setAttribute("name", "value");
229 SliPluginUtils.printContext(parameters, ctx);
230 BufferedReader br = new BufferedReader(new FileReader(f));
231 String line = br.readLine();
232 assertEquals("#######################################", line);
233 line = br.readLine();
234 assertEquals("hello = world", line);
235 line = br.readLine();
236 assertEquals("name = value", line);
238 Files.delete(Paths.get(filePath));
242 public void setTime() throws SvcLogicException {
243 String outputPath = "output";
244 parameters.put("outputPath", outputPath);
245 SliPluginUtils.setTime(parameters, ctx);
246 assertNotNull(ctx.getAttribute(outputPath));
250 public void containsKey() throws Exception {
251 ctx = new SvcLogicContext();
252 parameters.put(SliStringUtils.INPUT_PARAM_KEY, "key_does_not_exist");
253 String result = SliPluginUtils.containsKey(parameters, ctx);
254 assertEquals(SliStringUtils.FALSE_CONSTANT, result);
256 ctx.setAttribute("a", null);
257 parameters.put(SliStringUtils.INPUT_PARAM_KEY, "a");
258 result = SliPluginUtils.containsKey(parameters, ctx);
259 assertEquals(SliStringUtils.FALSE_CONSTANT, result);
261 ctx.setAttribute("a", "hellworld");
262 parameters.put(SliStringUtils.INPUT_PARAM_KEY, "a");
263 result = SliPluginUtils.containsKey(parameters, ctx);
264 assertEquals(SliStringUtils.TRUE_CONSTANT, result);