2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * =============================================================================
9 * Modification Copyright (C) 2018 IBM.
10 * =============================================================================
11 * Modifications Copyright (C) 2018 Ericsson
12 * =============================================================================
13 * Licensed under the Apache License, Version 2.0 (the "License");
14 * you may not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
17 * http://www.apache.org/licenses/LICENSE-2.0
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS,
21 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
25 * ============LICENSE_END=========================================================
28 package org.onap.appc.ccadaptor;
30 import static org.junit.Assert.assertEquals;
31 import static org.junit.Assert.assertThat;
32 import com.sun.jersey.api.client.Client;
33 import com.sun.jersey.api.client.ClientResponse;
34 import com.sun.jersey.api.client.WebResource;
35 import java.io.IOException;
36 import java.util.HashMap;
38 import java.util.Properties;
39 import org.hamcrest.CoreMatchers;
40 import org.junit.Before;
41 import org.junit.Test;
42 import org.junit.runner.RunWith;
43 import org.mockito.Mockito;
44 import org.mockito.runners.MockitoJUnitRunner;
45 import org.onap.ccsdk.sli.core.sli.SvcLogicAdaptor.ConfigStatus;
46 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
48 @RunWith(MockitoJUnitRunner.class)
49 public class ConfigComponentAdaptorTest {
51 private static final String TERMINATE_COMMAND = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
52 + "\n <rpc message-id=\"terminateConnection\" xmlns:netconf=\"urn:ietf:params:xml:ns:netconf:base:1.0\" "
53 + "xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n <close-session/> \n </rpc>\n ]]>]]>";
54 private SshJcraftWrapper mockWrapper;
57 public void setupForTests() {
58 mockWrapper = Mockito.mock(SshJcraftWrapper.class);
62 public void testGet() throws TimedOutException, IOException {
63 Properties props = null;
64 ConfigComponentAdaptor cca = Mockito.spy(new ConfigComponentAdaptor(props));
65 Mockito.doReturn("TEST\nDATA").when(mockWrapper).receiveUntil(Mockito.anyString(),
66 Mockito.anyInt(), Mockito.anyString());
67 Mockito.doReturn(mockWrapper).when(cca).getSshJcraftWrapper();
69 Map<String, String> parameters = new HashMap<>();
70 parameters.put("Host_ip_address", "test");
71 SvcLogicContext ctx = new SvcLogicContext();
73 "service-data.vnf-config-parameters-list.vnf-config-parameters[0].update-configuration[0].block-key-name",
75 assertEquals(ConfigStatus.SUCCESS, cca.configure(key, parameters, ctx));
79 public void testPutExceptionFlow() throws TimedOutException, IOException {
80 Properties props = null;
81 ConfigComponentAdaptor cca = Mockito.spy(new ConfigComponentAdaptor(props));
82 Mockito.doThrow(new IOException()).when(mockWrapper).put(Mockito.anyObject(),
83 Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString());
84 Mockito.doReturn(mockWrapper).when(cca).getSshJcraftWrapper();
86 Map<String, String> parameters = new HashMap<>();
87 parameters.put("data", "test");
88 SvcLogicContext ctx = new SvcLogicContext();
90 "service-data.vnf-config-parameters-list.vnf-config-parameters[0].update-configuration[0].block-key-name",
92 assertEquals(ConfigStatus.FAILURE, cca.configure(key, parameters, ctx));
96 public void testCli() throws TimedOutException, IOException {
97 Properties props = null;
98 ConfigComponentAdaptor cca = Mockito.spy(new ConfigComponentAdaptor(props));
99 Mockito.doReturn("TEST\nDATA").when(mockWrapper).receiveUntil(Mockito.anyString(),
100 Mockito.anyInt(), Mockito.anyString());
101 Mockito.doReturn(mockWrapper).when(cca).getSshJcraftWrapper();
102 String Get_config_template =
103 ("get_config_template\nRequest: \"show config\"\nResponse: Ends_With \"RESPONSE\"");
105 Map<String, String> parameters = new HashMap<>();
106 loadSshParameters(parameters);
107 parameters.put("Get_config_template", Get_config_template);
108 parameters.put("config-component-configUrl", "testUrl");
109 parameters.put("config-component-configPassword", "testPassword");
110 parameters.put("config-component-configUser", "testUser");
111 SvcLogicContext ctx = new SvcLogicContext();
113 "service-data.vnf-config-parameters-list.vnf-config-parameters[0].update-configuration[0].block-key-name",
115 assertEquals(ConfigStatus.SUCCESS, cca.configure(key, parameters, ctx));
119 public void testCliExceptionFlow() throws TimedOutException, IOException {
120 Properties props = null;
121 ConfigComponentAdaptor cca = Mockito.spy(new ConfigComponentAdaptor(props));
122 Mockito.doThrow(new IOException()).when(mockWrapper).connect(Mockito.anyString(),
123 Mockito.anyString(), Mockito.anyString(), Mockito.anyInt());
124 Mockito.doReturn(mockWrapper).when(cca).getSshJcraftWrapper();
125 String Get_config_template =
126 ("get_config_template\nRequest: \"show config\"\nResponse: Ends_With \"RESPONSE\"");
128 Map<String, String> parameters = new HashMap<>();
129 loadSshParameters(parameters);
130 parameters.put("Get_config_template", Get_config_template);
131 parameters.put("config-component-configUrl", "testUrl");
132 parameters.put("config-component-configPassword", "testPassword");
133 parameters.put("config-component-configUser", "testUser");
134 SvcLogicContext ctx = new SvcLogicContext();
136 "service-data.vnf-config-parameters-list.vnf-config-parameters[0].update-configuration[0].block-key-name",
138 assertEquals(ConfigStatus.FAILURE, cca.configure(key, parameters, ctx));
142 public void testEscapeSql() {
143 Properties props = null;
144 ConfigComponentAdaptor cca = new ConfigComponentAdaptor(props);
145 String testArtifactContents = ("\\ \\\\");
146 String key = "escapeSql";
147 Map<String, String> parameters = new HashMap<>();
148 loadSshParameters(parameters);
149 parameters.put("artifactContents", testArtifactContents);
150 SvcLogicContext ctx = new SvcLogicContext();
152 "service-data.vnf-config-parameters-list.vnf-config-parameters[0].update-configuration[0].block-key-name",
154 assertEquals(ConfigStatus.SUCCESS, cca.configure(key, parameters, ctx));
155 assertEquals("\\" + testArtifactContents + "\\\\", ctx.getAttribute("escapedData"));
159 public void testGetCliRunningConfig() throws TimedOutException, IOException {
160 Properties props = null;
161 ConfigComponentAdaptor cca = Mockito.spy(new ConfigComponentAdaptor(props));
162 Mockito.doReturn("TEST\nDATA").when(mockWrapper).receiveUntil(Mockito.anyString(),
163 Mockito.anyInt(), Mockito.anyString());
164 Mockito.doReturn(mockWrapper).when(cca).getSshJcraftWrapper();
165 String Get_config_template =
166 ("get_config_template\nRequest: \"show config\"\nResponse: Ends_With \"RESPONSE\"");
167 String key = "GetCliRunningConfig";
168 Map<String, String> parameters = new HashMap<>();
169 loadSshParameters(parameters);
170 parameters.put("Get_config_template", Get_config_template);
171 parameters.put("config-component-configUrl", "testUrl");
172 parameters.put("config-component-configPassword", "testPassword");
173 parameters.put("config-component-configUser", "testUser");
174 SvcLogicContext ctx = new SvcLogicContext();
176 "service-data.vnf-config-parameters-list.vnf-config-parameters[0].update-configuration[0].block-key-name",
178 assertEquals(ConfigStatus.SUCCESS, cca.configure(key, parameters, ctx));
182 public void testGetCliRunningConfigExceptionFlow() throws TimedOutException, IOException {
183 Properties props = null;
184 ConfigComponentAdaptor cca = Mockito.spy(new ConfigComponentAdaptor(props));
185 Mockito.doThrow(new IOException()).when(mockWrapper).receiveUntil(Mockito.anyString(),
186 Mockito.anyInt(), Mockito.anyString());
187 Mockito.doReturn(mockWrapper).when(cca).getSshJcraftWrapper();
188 String Get_config_template =
189 ("get_config_template\nRequest: \"show config\"\nResponse: Ends_With \"RESPONSE\"");
190 String key = "GetCliRunningConfig";
191 Map<String, String> parameters = new HashMap<>();
192 loadSshParameters(parameters);
193 parameters.put("Get_config_template", Get_config_template);
194 parameters.put("config-component-configUrl", "testUrl");
195 parameters.put("config-component-configPassword", "testPassword");
196 parameters.put("config-component-configUser", "testUser");
197 SvcLogicContext ctx = new SvcLogicContext();
199 "service-data.vnf-config-parameters-list.vnf-config-parameters[0].update-configuration[0].block-key-name",
201 assertEquals(ConfigStatus.FAILURE, cca.configure(key, parameters, ctx));
205 public void testXmlDownload() throws TimedOutException, IOException {
206 Properties props = null;
207 ConfigComponentAdaptor cca = Mockito.spy(new ConfigComponentAdaptor(props));
208 Mockito.doReturn("TEST\nDATA").when(mockWrapper).receiveUntil(Mockito.anyString(),
209 Mockito.anyInt(), Mockito.anyString());
210 Mockito.doReturn(mockWrapper).when(cca).getSshJcraftWrapper();
211 String Get_config_template = ("get_config_template");
212 String key = "xml-download";
213 Map<String, String> parameters = new HashMap<>();
214 loadSshParameters(parameters);
215 parameters.put("Get_config_template", Get_config_template);
217 SvcLogicContext ctx = new SvcLogicContext();
219 "service-data.vnf-config-parameters-list.vnf-config-parameters[0].update-configuration[0].block-key-name",
221 assertEquals(ConfigStatus.SUCCESS, cca.configure(key, parameters, ctx));
225 public void testXmlDownloadExceptionFlow() throws TimedOutException, IOException {
226 Properties props = null;
227 ConfigComponentAdaptor cca = Mockito.spy(new ConfigComponentAdaptor(props));
228 Mockito.doReturn("rpc-error").when(mockWrapper).receiveUntil("</rpc-reply>", 600000, "");
229 Mockito.doThrow(new IOException()).when(mockWrapper).send(TERMINATE_COMMAND);
230 Mockito.doReturn(mockWrapper).when(cca).getSshJcraftWrapper();
231 String Get_config_template = ("get_config_template");
232 String key = "xml-download";
233 Map<String, String> parameters = new HashMap<>();
234 loadSshParameters(parameters);
235 parameters.put("Get_config_template", Get_config_template);
237 SvcLogicContext ctx = new SvcLogicContext();
239 "service-data.vnf-config-parameters-list.vnf-config-parameters[0].update-configuration[0].block-key-name",
241 assertEquals(ConfigStatus.FAILURE, cca.configure(key, parameters, ctx));
245 public void testXmlGetrunningconfig() throws TimedOutException, IOException {
246 Properties props = null;
247 ConfigComponentAdaptor cca = Mockito.spy(new ConfigComponentAdaptor(props));
248 Mockito.doReturn("<configuration xmlns=\"\n<data>\n</data>\n</configuration>")
250 .receiveUntil(Mockito.anyString(), Mockito.anyInt(), Mockito.anyString());
251 Mockito.doReturn(mockWrapper).when(cca).getSshJcraftWrapper();
252 String key = "xml-getrunningconfig";
253 Map<String, String> parameters = new HashMap<>();
254 loadSshParameters(parameters);
255 SvcLogicContext ctx = new SvcLogicContext();
256 assertEquals(ConfigStatus.SUCCESS, cca.configure(key, parameters, ctx));
260 public void testXmlGetrunningconfigExceptionFlow() throws TimedOutException, IOException {
261 Properties props = new Properties();
262 ConfigComponentAdaptor cca = Mockito.spy(new ConfigComponentAdaptor(props));
263 Mockito.doThrow(new IOException()).when(mockWrapper).connect(Mockito.anyString(),
264 Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyInt(),
265 Mockito.anyInt(), Mockito.anyString());
266 Mockito.doReturn(mockWrapper).when(cca).getSshJcraftWrapper();
267 String key = "xml-getrunningconfig";
268 Map<String, String> parameters = new HashMap<>();
269 loadSshParameters(parameters);
270 SvcLogicContext ctx = new SvcLogicContext();
271 assertEquals(ConfigStatus.FAILURE, cca.configure(key, parameters, ctx));
275 public void testDownloadCliConfig() throws TimedOutException, IOException {
276 Properties props = null;
277 ConfigComponentAdaptor cca = Mockito.spy(new ConfigComponentAdaptor(props));
278 Mockito.doReturn("TEST\nDATA").when(mockWrapper).receiveUntil(Mockito.anyString(),
279 Mockito.anyInt(), Mockito.anyString());
280 Mockito.doReturn(mockWrapper).when(cca).getSshJcraftWrapper();
281 String Download_config_template =
282 ("get_config_template\nRequest: \"show config\"\nResponse: Ends_With \"RESPONSE\"");
283 String key = "DownloadCliConfig";
284 Map<String, String> parameters = new HashMap<>();
285 loadSshParameters(parameters);
286 parameters.put("Download_config_template", Download_config_template);
287 SvcLogicContext ctx = new SvcLogicContext();
289 "service-data.vnf-config-parameters-list.vnf-config-parameters[0].update-configuration[0].block-key-name",
291 assertEquals(ConfigStatus.SUCCESS, cca.configure(key, parameters, ctx));
295 public void testDownloadCliConfigExceptionFlow() throws TimedOutException, IOException {
296 Properties props = null;
297 ConfigComponentAdaptor cca = Mockito.spy(new ConfigComponentAdaptor(props));
298 Mockito.doThrow(new IOException("ExceptionFromDownloadCli")).when(mockWrapper)
299 .receiveUntil(Mockito.anyString(), Mockito.anyInt(), Mockito.anyString());
300 Mockito.doReturn(mockWrapper).when(cca).getSshJcraftWrapper();
301 String Download_config_template = ("get_config_template\nRequest: \"show config\""
302 + "\n Execute_config_contents Response: Ends_With\" \"RESPONSE\"\n");
303 String key = "DownloadCliConfig";
304 Map<String, String> parameters = new HashMap<>();
305 loadSshParameters(parameters);
306 parameters.put("Config_contents", "TEST\nDATA");
307 parameters.put("Download_config_template", Download_config_template);
308 SvcLogicContext ctx = new SvcLogicContext();
310 "service-data.vnf-config-parameters-list.vnf-config-parameters[0].update-configuration[0].block-key-name",
312 assertEquals(ConfigStatus.FAILURE, cca.configure(key, parameters, ctx));
316 public void testPrepare() {
317 Client mockClient = Mockito.mock(Client.class);
318 WebResource mockWebResource = Mockito.mock(WebResource.class);
319 ClientResponse mockClientResponse = Mockito.mock(ClientResponse.class);
320 ConfigComponentAdaptor cca = Mockito.spy(new ConfigComponentAdaptor(null));
321 Mockito.doReturn(mockClientResponse).when(cca).getClientResponse(Mockito.anyObject(),
322 Mockito.anyString(), Mockito.anyString());
323 Mockito.doReturn(mockWebResource).when(mockClient).resource(Mockito.anyString());
324 Mockito.doReturn(mockClient).when(cca).getClient();
325 Map<String, String> parameters = new HashMap<>();
326 parameters.put("action", "prepare");
327 SvcLogicContext ctx = new SvcLogicContext();
329 "service-data.vnf-config-parameters-list.vnf-config-parameters[0].update-configuration[0].block-key-name",
331 assertEquals(ConfigStatus.SUCCESS, cca.configure("", parameters, ctx));
335 public void testPrepareExceptionFlow() {
336 ConfigComponentAdaptor cca = new ConfigComponentAdaptor(null);
337 Map<String, String> parameters = new HashMap<>();
338 parameters.put("action", "prepare");
339 SvcLogicContext ctx = new SvcLogicContext();
341 "service-data.vnf-config-parameters-list.vnf-config-parameters[0].scale-configuration[0].network-type",
343 assertEquals(ConfigStatus.FAILURE, cca.configure("", parameters, ctx));
344 assertEquals("500", ctx.getAttribute("error-code"));
348 public void testAudit() {
349 Client mockClient = Mockito.mock(Client.class);
350 WebResource mockWebResource = Mockito.mock(WebResource.class);
351 ClientResponse mockClientResponse = Mockito.mock(ClientResponse.class);
352 ConfigComponentAdaptor cca = Mockito.spy(new ConfigComponentAdaptor(null));
353 Mockito.doReturn(mockClientResponse).when(cca).getClientResponse(Mockito.anyObject(),
354 Mockito.anyString(), Mockito.anyString());
355 Mockito.doReturn(mockWebResource).when(mockClient).resource(Mockito.anyString());
356 Mockito.doReturn(mockClient).when(cca).getClient();
357 Map<String, String> parameters = new HashMap<>();
358 parameters.put("action", "audit");
359 SvcLogicContext ctx = new SvcLogicContext();
361 "service-data.vnf-config-parameters-list.vnf-config-parameters[0].update-configuration[0].block-key-name",
363 assertEquals(ConfigStatus.SUCCESS, cca.configure("", parameters, ctx));
367 public void testActivate() {
368 ClientResponse mockClientResponse = Mockito.mock(ClientResponse.class);
369 ConfigComponentAdaptor cca = Mockito.spy(new ConfigComponentAdaptor(null));
370 Mockito.doReturn(mockClientResponse).when(cca).getClientResponse(Mockito.anyObject(),
371 Mockito.anyString(), Mockito.anyString());
372 Map<String, String> parameters = new HashMap<>();
373 parameters.put("action", "activate");
374 SvcLogicContext ctx = new SvcLogicContext();
376 "service-data.vnf-config-parameters-list.vnf-config-parameters[0].update-configuration[0].block-key-name",
378 assertEquals(ConfigStatus.SUCCESS, cca.configure("", parameters, ctx));
382 public void testConstructorForNonNullProperties() throws Exception {
383 Properties props = new Properties();
384 props.setProperty("configComponent.url", "testConfigUrl");
385 props.setProperty("configComponent.user", "testConfigUser");
386 props.setProperty("configComponent.passwd", "testConfigPwd");
387 props.setProperty("auditComponent.url", "testAuditUrl");
388 props.setProperty("auditComponent.user", "testAuditUser");
389 props.setProperty("auditComponent.passwd", "testAuditPwd");
390 props.setProperty("service-configuration-notification-url", "testServiceNotificationUrl");
391 props.setProperty("audit-configuration-notification-url", "testAuditNotificationUrl");
393 ConfigComponentAdaptor cca = new ConfigComponentAdaptor(props);
394 assertEquals("testConfigUrl", cca.getConfigUrl());
395 assertEquals("testConfigUser", cca.getConfigUser());
396 assertEquals("testConfigPwd", cca.getConfigPassword());
397 assertEquals("testAuditUrl", cca.getAuditUrl());
398 assertEquals("testAuditUser", cca.getAuditUser());
399 assertEquals("testAuditPwd", cca.getAuditPassword());
400 assertEquals("testServiceNotificationUrl", cca.getConfigCallbackUrl());
401 assertEquals("testAuditNotificationUrl", cca.getAuditCallbackUrl());
405 public void testStaticReadFile() {
406 assertThat(ConfigComponentAdaptor._readFile("src/main/resources/config-base.xml"),
407 CoreMatchers.containsString("<configure>"));
411 public void testStaticReadFileExceptionFlow() {
412 assertEquals("", ConfigComponentAdaptor._readFile("NON_EXISTENT_FILE"));
415 private void loadSshParameters(Map<String, String> map) {
416 map.put("Host_ip_address", "test");
417 map.put("User_name", "test");
418 map.put("Password", "password");
419 map.put("Port_number", "22");
420 map.put("portNumber", "22");