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.openecomp.sdnc.sli.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.openecomp.sdnc.sli.SvcLogicContext;
34 import org.openecomp.sdnc.sli.SvcLogicException;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
38 public class SliPluginUtils_StaticFunctionsTest {
39 private static final Logger LOG = LoggerFactory.getLogger(SliPluginUtils_StaticFunctionsTest.class);
40 SliPluginUtils utils = new SliPluginUtils();
41 private SvcLogicContext ctx;
42 private HashMap<String, String> parameters;
45 public void setUp() throws Exception {
46 this.ctx = new SvcLogicContext();
47 parameters = new HashMap<String, String>();
52 public final void testCtxGetBeginsWith() {
53 ctx.setAttribute("service-data.oper-status.order-status", "InProgress");
54 ctx.setAttribute("service-data.service-information.service-instance-id", "my-instance");
55 ctx.setAttribute("service-data.service-information.service-type", "my-service");
57 Map<String, String> entries = SliPluginUtils.ctxGetBeginsWith(ctx, "service-data.service-information");
59 assertEquals("my-instance", entries.get("service-data.service-information.service-instance-id"));
60 assertEquals("my-service", entries.get("service-data.service-information.service-type"));
61 assertFalse(entries.containsKey("service-data.oper-status.order-status"));
66 public final void testCtxListRemove_index() throws SvcLogicException {
67 LOG.trace("=== testCtxListRemove_index ===");
68 ctx.setAttribute("service-data.vnf-l3[0].vnf-host-name", "vnf-host-name_0");
69 ctx.setAttribute("service-data.vnf-l3[0].device-host-name", "device-host-name_0");
70 ctx.setAttribute("service-data.vnf-l3[1].vnf-host-name", "vnf-host-name_1");
71 ctx.setAttribute("service-data.vnf-l3[1].device-host-name", "device-host-name_1");
72 ctx.setAttribute("service-data.vnf-l3[2].vnf-host-name", "vnf-host-name_2");
73 ctx.setAttribute("service-data.vnf-l3[2].device-host-name", "device-host-name_2");
74 ctx.setAttribute("service-data.vnf-l3_length", "3");
76 parameters.put("index", "1");
77 parameters.put("list_pfx", "service-data.vnf-l3");
79 utils.ctxListRemove(parameters, ctx);
80 SliPluginUtils.logContextMemory(ctx, LOG, SliPluginUtils.LogLevel.TRACE);
82 assertEquals("2", ctx.getAttribute("service-data.vnf-l3_length"));
83 assertEquals("vnf-host-name_0", ctx.getAttribute("service-data.vnf-l3[0].vnf-host-name"));
84 assertEquals("device-host-name_0", ctx.getAttribute("service-data.vnf-l3[0].device-host-name"));
85 assertEquals("vnf-host-name_2", ctx.getAttribute("service-data.vnf-l3[1].vnf-host-name"));
86 assertEquals("device-host-name_2", ctx.getAttribute("service-data.vnf-l3[1].device-host-name"));
91 public final void textCtxListRemove_keyValue() throws SvcLogicException {
92 LOG.trace("=== textCtxListRemove_keyValue ===");
93 ctx.setAttribute("service-data.vnf-l3[0].vnf-host-name", "vnf-host-name_0");
94 ctx.setAttribute("service-data.vnf-l3[0].device-host-name", "device-host-name_0");
95 ctx.setAttribute("service-data.vnf-l3[1].vnf-host-name", "vnf-host-name_1");
96 ctx.setAttribute("service-data.vnf-l3[1].device-host-name", "device-host-name_1");
97 ctx.setAttribute("service-data.vnf-l3[2].vnf-host-name", "vnf-host-name_2");
98 ctx.setAttribute("service-data.vnf-l3[2].device-host-name", "device-host-name_2");
100 ctx.setAttribute("service-data.vnf-l3[3].vnf-host-name", "vnf-host-name_1");
101 ctx.setAttribute("service-data.vnf-l3[3].device-host-name", "device-host-name_1");
102 ctx.setAttribute("service-data.vnf-l3_length", "4");
104 parameters.put("list_pfx", "service-data.vnf-l3");
105 parameters.put("key", "vnf-host-name");
106 parameters.put("value", "vnf-host-name_1");
108 utils.ctxListRemove(parameters, ctx);
109 SliPluginUtils.logContextMemory(ctx, LOG, SliPluginUtils.LogLevel.TRACE);
111 assertEquals("2", ctx.getAttribute("service-data.vnf-l3_length"));
112 assertEquals("vnf-host-name_0", ctx.getAttribute("service-data.vnf-l3[0].vnf-host-name"));
113 assertEquals("device-host-name_0", ctx.getAttribute("service-data.vnf-l3[0].device-host-name"));
114 assertEquals("vnf-host-name_2", ctx.getAttribute("service-data.vnf-l3[1].vnf-host-name"));
115 assertEquals("device-host-name_2", ctx.getAttribute("service-data.vnf-l3[1].device-host-name"));
120 public final void textCtxListRemove_keyValue_nullkey() throws SvcLogicException {
121 LOG.trace("=== textCtxListRemove_keyValue_nullkey ===");
122 ctx.setAttribute("service-data.vnf-l3[0]", "vnf-host-name_0");
123 ctx.setAttribute("service-data.vnf-l3[1]", "vnf-host-name_1");
124 ctx.setAttribute("service-data.vnf-l3[2]", "vnf-host-name_2");
125 ctx.setAttribute("service-data.vnf-l3_length", "3");
127 parameters.put("list_pfx", "service-data.vnf-l3");
128 parameters.put("value", "vnf-host-name_1");
130 utils.ctxListRemove(parameters, ctx);
131 SliPluginUtils.logContextMemory(ctx, LOG, SliPluginUtils.LogLevel.TRACE);
133 assertEquals("2", ctx.getAttribute("service-data.vnf-l3_length"));
134 assertEquals("vnf-host-name_0", ctx.getAttribute("service-data.vnf-l3[0]"));
135 assertEquals("vnf-host-name_2", ctx.getAttribute("service-data.vnf-l3[1]"));
140 public final void textCtxListRemove_keyValueList() throws SvcLogicException {
141 LOG.trace("=== textCtxListRemove_keyValueList ===");
142 ctx.setAttribute("service-data.vnf-l3[0].vnf-host-name", "vnf-host-name_0");
143 ctx.setAttribute("service-data.vnf-l3[0].device-host-name", "device-host-name_0");
144 ctx.setAttribute("service-data.vnf-l3[1].vnf-host-name", "vnf-host-name_1");
145 ctx.setAttribute("service-data.vnf-l3[1].device-host-name", "device-host-name_1");
146 ctx.setAttribute("service-data.vnf-l3[2].vnf-host-name", "vnf-host-name_2");
147 ctx.setAttribute("service-data.vnf-l3[2].device-host-name", "device-host-name_2");
149 ctx.setAttribute("service-data.vnf-l3[3].vnf-host-name", "vnf-host-name_1");
150 ctx.setAttribute("service-data.vnf-l3[3].device-host-name", "device-host-name_1");
151 // entries with only 1 of 2 key-value pairs matching
152 ctx.setAttribute("service-data.vnf-l3[4].vnf-host-name", "vnf-host-name_1");
153 ctx.setAttribute("service-data.vnf-l3[4].device-host-name", "device-host-name_4");
154 ctx.setAttribute("service-data.vnf-l3[5].vnf-host-name", "vnf-host-name_5");
155 ctx.setAttribute("service-data.vnf-l3[5].device-host-name", "device-host-name_1");
156 ctx.setAttribute("service-data.vnf-l3_length", "6");
158 parameters.put("list_pfx", "service-data.vnf-l3");
159 parameters.put("keys_length", "2");
160 parameters.put("keys[0].key", "vnf-host-name");
161 parameters.put("keys[0].value", "vnf-host-name_1");
162 parameters.put("keys[1].key", "device-host-name");
163 parameters.put("keys[1].value", "device-host-name_1");
165 utils.ctxListRemove(parameters, ctx);
166 SliPluginUtils.logContextMemory(ctx, LOG, SliPluginUtils.LogLevel.TRACE);
168 assertEquals("4", ctx.getAttribute("service-data.vnf-l3_length"));
169 assertEquals("vnf-host-name_0", ctx.getAttribute("service-data.vnf-l3[0].vnf-host-name"));
170 assertEquals("device-host-name_0", ctx.getAttribute("service-data.vnf-l3[0].device-host-name"));
171 assertEquals("vnf-host-name_2", ctx.getAttribute("service-data.vnf-l3[1].vnf-host-name"));
172 assertEquals("device-host-name_2", ctx.getAttribute("service-data.vnf-l3[1].device-host-name"));
173 assertEquals("vnf-host-name_1", ctx.getAttribute("service-data.vnf-l3[2].vnf-host-name"));
174 assertEquals("device-host-name_4", ctx.getAttribute("service-data.vnf-l3[2].device-host-name"));
175 assertEquals("vnf-host-name_5", ctx.getAttribute("service-data.vnf-l3[3].vnf-host-name"));
176 assertEquals("device-host-name_1", ctx.getAttribute("service-data.vnf-l3[3].device-host-name"));
180 @Test(expected = SvcLogicException.class)
181 public final void testCtxListRemove_nullListLength() throws SvcLogicException {
182 LOG.trace("=== testCtxListRemove_nullListLength ===");
183 ctx.setAttribute("service-data.vnf-l3[0].vnf-host-name", "vnf-host-name_0");
184 ctx.setAttribute("service-data.vnf-l3[0].device-host-name", "device-host-name_0");
185 ctx.setAttribute("service-data.vnf-l3[1].vnf-host-name", "vnf-host-name_1");
186 ctx.setAttribute("service-data.vnf-l3[1].device-host-name", "device-host-name_1");
187 ctx.setAttribute("service-data.vnf-l3[2].vnf-host-name", "vnf-host-name_2");
188 ctx.setAttribute("service-data.vnf-l3[2].device-host-name", "device-host-name_2");
190 parameters.put("index", "1");
191 parameters.put("list_pfx", "service-data.vnf-l3");
193 utils.ctxListRemove(parameters, ctx);
198 public final void testCtxPutAll() {
199 HashMap<String, Object> entries = new HashMap<String, Object>();
200 entries.put("service-data.oper-status.order-status", "InProgress");
201 entries.put("service-data.service-information.service-instance-id", "my-instance");
202 entries.put("service-data.request-information.order-number", 1234);
203 entries.put("service-data.request-information.request-id", null);
205 SliPluginUtils.ctxPutAll(ctx, entries);
207 assertEquals("InProgress", ctx.getAttribute("service-data.oper-status.order-status"));
208 assertEquals("my-instance", ctx.getAttribute("service-data.service-information.service-instance-id"));
209 assertEquals("1234", ctx.getAttribute("service-data.request-information.order-number"));
210 assertFalse(ctx.getAttributeKeySet().contains("service-data.request-information.request-id"));
215 public final void testCtxSetAttribute_LOG() {
216 LOG.debug("=== testCtxSetAttribute_LOG ===");
217 Integer i = new Integer(3);
218 SliPluginUtils.ctxSetAttribute(ctx, "test", i, LOG, SliPluginUtils.LogLevel.TRACE);
222 public void printContext() throws SvcLogicException, IOException {
223 String filePath = "/src/test/resources/printContext.txt";
224 parameters.put("filename", filePath);
225 File f = new File(filePath);
227 assert (!f.isDirectory());
228 ctx.setAttribute("hello", "world");
229 ctx.setAttribute("name", "value");
231 SliPluginUtils.printContext(parameters, ctx);
232 BufferedReader br = new BufferedReader(new FileReader(f));
233 String line = br.readLine();
234 assertEquals("#######################################", line);
235 line = br.readLine();
236 assertEquals("hello = world", line);
237 line = br.readLine();
238 assertEquals("name = value", line);
240 Files.delete(Paths.get(filePath));
244 public void setTime() throws SvcLogicException {
245 String outputPath = "output";
246 parameters.put("outputPath", outputPath);
247 SliPluginUtils.setTime(parameters, ctx);
248 assertNotNull(ctx.getAttribute(outputPath));