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;
28 import java.util.HashMap;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
34 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
35 import org.onap.ccsdk.sli.core.slipluginutils.SliPluginUtils;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
39 public class SliPluginUtils_StaticFunctionsTest {
40 private static final Logger LOG = LoggerFactory.getLogger(SliPluginUtils_StaticFunctionsTest.class);
41 SliPluginUtils utils = new SliPluginUtils();
42 private SvcLogicContext ctx;
43 private HashMap<String, String> parameters;
46 public void setUp() throws Exception {
47 this.ctx = new SvcLogicContext();
48 parameters = new HashMap<String, String>();
53 public final void testCtxGetBeginsWith() {
54 ctx.setAttribute("service-data.oper-status.order-status", "InProgress");
55 ctx.setAttribute("service-data.service-information.service-instance-id", "my-instance");
56 ctx.setAttribute("service-data.service-information.service-type", "my-service");
58 Map<String, String> entries = SliPluginUtils.ctxGetBeginsWith(ctx, "service-data.service-information");
60 assertEquals("my-instance", entries.get("service-data.service-information.service-instance-id"));
61 assertEquals("my-service", entries.get("service-data.service-information.service-type"));
62 assertFalse(entries.containsKey("service-data.oper-status.order-status"));
67 public final void testCtxListRemove_index() throws SvcLogicException {
68 LOG.trace("=== testCtxListRemove_index ===");
69 ctx.setAttribute("service-data.vnf-l3[0].vnf-host-name", "vnf-host-name_0");
70 ctx.setAttribute("service-data.vnf-l3[0].device-host-name", "device-host-name_0");
71 ctx.setAttribute("service-data.vnf-l3[1].vnf-host-name", "vnf-host-name_1");
72 ctx.setAttribute("service-data.vnf-l3[1].device-host-name", "device-host-name_1");
73 ctx.setAttribute("service-data.vnf-l3[2].vnf-host-name", "vnf-host-name_2");
74 ctx.setAttribute("service-data.vnf-l3[2].device-host-name", "device-host-name_2");
75 ctx.setAttribute("service-data.vnf-l3_length", "3");
77 parameters.put("index", "1");
78 parameters.put("list_pfx", "service-data.vnf-l3");
80 utils.ctxListRemove(parameters, ctx);
81 SliPluginUtils.logContextMemory(ctx, LOG, SliPluginUtils.LogLevel.TRACE);
83 assertEquals("2", ctx.getAttribute("service-data.vnf-l3_length"));
84 assertEquals("vnf-host-name_0", ctx.getAttribute("service-data.vnf-l3[0].vnf-host-name"));
85 assertEquals("device-host-name_0", ctx.getAttribute("service-data.vnf-l3[0].device-host-name"));
86 assertEquals("vnf-host-name_2", ctx.getAttribute("service-data.vnf-l3[1].vnf-host-name"));
87 assertEquals("device-host-name_2", ctx.getAttribute("service-data.vnf-l3[1].device-host-name"));
92 public final void textCtxListRemove_keyValue() throws SvcLogicException {
93 LOG.trace("=== textCtxListRemove_keyValue ===");
94 ctx.setAttribute("service-data.vnf-l3[0].vnf-host-name", "vnf-host-name_0");
95 ctx.setAttribute("service-data.vnf-l3[0].device-host-name", "device-host-name_0");
96 ctx.setAttribute("service-data.vnf-l3[1].vnf-host-name", "vnf-host-name_1");
97 ctx.setAttribute("service-data.vnf-l3[1].device-host-name", "device-host-name_1");
98 ctx.setAttribute("service-data.vnf-l3[2].vnf-host-name", "vnf-host-name_2");
99 ctx.setAttribute("service-data.vnf-l3[2].device-host-name", "device-host-name_2");
101 ctx.setAttribute("service-data.vnf-l3[3].vnf-host-name", "vnf-host-name_1");
102 ctx.setAttribute("service-data.vnf-l3[3].device-host-name", "device-host-name_1");
103 ctx.setAttribute("service-data.vnf-l3_length", "4");
105 parameters.put("list_pfx", "service-data.vnf-l3");
106 parameters.put("key", "vnf-host-name");
107 parameters.put("value", "vnf-host-name_1");
109 utils.ctxListRemove(parameters, ctx);
110 SliPluginUtils.logContextMemory(ctx, LOG, SliPluginUtils.LogLevel.TRACE);
112 assertEquals("2", ctx.getAttribute("service-data.vnf-l3_length"));
113 assertEquals("vnf-host-name_0", ctx.getAttribute("service-data.vnf-l3[0].vnf-host-name"));
114 assertEquals("device-host-name_0", ctx.getAttribute("service-data.vnf-l3[0].device-host-name"));
115 assertEquals("vnf-host-name_2", ctx.getAttribute("service-data.vnf-l3[1].vnf-host-name"));
116 assertEquals("device-host-name_2", ctx.getAttribute("service-data.vnf-l3[1].device-host-name"));
121 public final void textCtxListRemove_keyValue_nullkey() throws SvcLogicException {
122 LOG.trace("=== textCtxListRemove_keyValue_nullkey ===");
123 ctx.setAttribute("service-data.vnf-l3[0]", "vnf-host-name_0");
124 ctx.setAttribute("service-data.vnf-l3[1]", "vnf-host-name_1");
125 ctx.setAttribute("service-data.vnf-l3[2]", "vnf-host-name_2");
126 ctx.setAttribute("service-data.vnf-l3_length", "3");
128 parameters.put("list_pfx", "service-data.vnf-l3");
129 parameters.put("value", "vnf-host-name_1");
131 utils.ctxListRemove(parameters, ctx);
132 SliPluginUtils.logContextMemory(ctx, LOG, SliPluginUtils.LogLevel.TRACE);
134 assertEquals("2", ctx.getAttribute("service-data.vnf-l3_length"));
135 assertEquals("vnf-host-name_0", ctx.getAttribute("service-data.vnf-l3[0]"));
136 assertEquals("vnf-host-name_2", ctx.getAttribute("service-data.vnf-l3[1]"));
141 public final void textCtxListRemove_keyValueList() throws SvcLogicException {
142 LOG.trace("=== textCtxListRemove_keyValueList ===");
143 ctx.setAttribute("service-data.vnf-l3[0].vnf-host-name", "vnf-host-name_0");
144 ctx.setAttribute("service-data.vnf-l3[0].device-host-name", "device-host-name_0");
145 ctx.setAttribute("service-data.vnf-l3[1].vnf-host-name", "vnf-host-name_1");
146 ctx.setAttribute("service-data.vnf-l3[1].device-host-name", "device-host-name_1");
147 ctx.setAttribute("service-data.vnf-l3[2].vnf-host-name", "vnf-host-name_2");
148 ctx.setAttribute("service-data.vnf-l3[2].device-host-name", "device-host-name_2");
150 ctx.setAttribute("service-data.vnf-l3[3].vnf-host-name", "vnf-host-name_1");
151 ctx.setAttribute("service-data.vnf-l3[3].device-host-name", "device-host-name_1");
152 // entries with only 1 of 2 key-value pairs matching
153 ctx.setAttribute("service-data.vnf-l3[4].vnf-host-name", "vnf-host-name_1");
154 ctx.setAttribute("service-data.vnf-l3[4].device-host-name", "device-host-name_4");
155 ctx.setAttribute("service-data.vnf-l3[5].vnf-host-name", "vnf-host-name_5");
156 ctx.setAttribute("service-data.vnf-l3[5].device-host-name", "device-host-name_1");
157 ctx.setAttribute("service-data.vnf-l3_length", "6");
159 parameters.put("list_pfx", "service-data.vnf-l3");
160 parameters.put("keys_length", "2");
161 parameters.put("keys[0].key", "vnf-host-name");
162 parameters.put("keys[0].value", "vnf-host-name_1");
163 parameters.put("keys[1].key", "device-host-name");
164 parameters.put("keys[1].value", "device-host-name_1");
166 utils.ctxListRemove(parameters, ctx);
167 SliPluginUtils.logContextMemory(ctx, LOG, SliPluginUtils.LogLevel.TRACE);
169 assertEquals("4", ctx.getAttribute("service-data.vnf-l3_length"));
170 assertEquals("vnf-host-name_0", ctx.getAttribute("service-data.vnf-l3[0].vnf-host-name"));
171 assertEquals("device-host-name_0", ctx.getAttribute("service-data.vnf-l3[0].device-host-name"));
172 assertEquals("vnf-host-name_2", ctx.getAttribute("service-data.vnf-l3[1].vnf-host-name"));
173 assertEquals("device-host-name_2", ctx.getAttribute("service-data.vnf-l3[1].device-host-name"));
174 assertEquals("vnf-host-name_1", ctx.getAttribute("service-data.vnf-l3[2].vnf-host-name"));
175 assertEquals("device-host-name_4", ctx.getAttribute("service-data.vnf-l3[2].device-host-name"));
176 assertEquals("vnf-host-name_5", ctx.getAttribute("service-data.vnf-l3[3].vnf-host-name"));
177 assertEquals("device-host-name_1", ctx.getAttribute("service-data.vnf-l3[3].device-host-name"));
181 @Test(expected = SvcLogicException.class)
182 public final void testCtxListRemove_nullListLength() throws SvcLogicException {
183 LOG.trace("=== testCtxListRemove_nullListLength ===");
184 ctx.setAttribute("service-data.vnf-l3[0].vnf-host-name", "vnf-host-name_0");
185 ctx.setAttribute("service-data.vnf-l3[0].device-host-name", "device-host-name_0");
186 ctx.setAttribute("service-data.vnf-l3[1].vnf-host-name", "vnf-host-name_1");
187 ctx.setAttribute("service-data.vnf-l3[1].device-host-name", "device-host-name_1");
188 ctx.setAttribute("service-data.vnf-l3[2].vnf-host-name", "vnf-host-name_2");
189 ctx.setAttribute("service-data.vnf-l3[2].device-host-name", "device-host-name_2");
191 parameters.put("index", "1");
192 parameters.put("list_pfx", "service-data.vnf-l3");
194 utils.ctxListRemove(parameters, ctx);
199 public final void testCtxPutAll() {
200 HashMap<String, Object> entries = new HashMap<String, Object>();
201 entries.put("service-data.oper-status.order-status", "InProgress");
202 entries.put("service-data.service-information.service-instance-id", "my-instance");
203 entries.put("service-data.request-information.order-number", 1234);
204 entries.put("service-data.request-information.request-id", null);
206 SliPluginUtils.ctxPutAll(ctx, entries);
208 assertEquals("InProgress", ctx.getAttribute("service-data.oper-status.order-status"));
209 assertEquals("my-instance", ctx.getAttribute("service-data.service-information.service-instance-id"));
210 assertEquals("1234", ctx.getAttribute("service-data.request-information.order-number"));
211 assertFalse(ctx.getAttributeKeySet().contains("service-data.request-information.request-id"));
216 public final void testCtxSetAttribute_LOG() {
217 LOG.debug("=== testCtxSetAttribute_LOG ===");
218 Integer i = new Integer(3);
219 SliPluginUtils.ctxSetAttribute(ctx, "test", i, LOG, SliPluginUtils.LogLevel.TRACE);
223 public void printContext() throws SvcLogicException, IOException {
224 String filePath = "/src/test/resources/printContext.txt";
225 parameters.put("filename", filePath);
226 File f = new File(filePath);
228 assert (!f.isDirectory());
229 ctx.setAttribute("hello", "world");
230 ctx.setAttribute("name", "value");
232 SliPluginUtils.printContext(parameters, ctx);
233 BufferedReader br = new BufferedReader(new FileReader(f));
234 String line = br.readLine();
235 assertEquals("#######################################", line);
236 line = br.readLine();
237 assertEquals("hello = world", line);
238 line = br.readLine();
239 assertEquals("name = value", line);
241 Files.delete(Paths.get(filePath));
245 public void setTime() throws SvcLogicException {
246 String outputPath = "output";
247 parameters.put("outputPath", outputPath);
248 SliPluginUtils.setTime(parameters, ctx);
249 assertNotNull(ctx.getAttribute(outputPath));