2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * =============================================================================
9 * Modifications Copyright (C) 2018-2019 IBM.
10 * =============================================================================
11 * Modifications Copyright (C) 2018-2019 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 testXmlGetrunningconfigLongResponse() throws TimedOutException, IOException {
261 Properties props = null;
262 ConfigComponentAdaptor cca = Mockito.spy(new ConfigComponentAdaptor(props));
263 Mockito.doReturn("<configuration xmlns=\"\">\"\n<data>\nData line 1\nData line 2\nData line 3\nData line 4\n"
264 + "Data line 5\nData line 6\nData line 7\nData line 8\nData line 9\n Data line 10\n"
265 + "Data line 11\nData line 12\nData line 13\nData line 14\nData line 15\n"
266 + "Data line 16\nData line 17\nData line 18\nData line 19\nData line 20</data>\n</configuration>")
268 .receiveUntil(Mockito.anyString(), Mockito.anyInt(), Mockito.anyString());
269 Mockito.doReturn(mockWrapper).when(cca).getSshJcraftWrapper();
270 String key = "xml-getrunningconfig";
271 Map<String, String> parameters = new HashMap<>();
272 loadSshParameters(parameters);
273 SvcLogicContext ctx = new SvcLogicContext();
274 assertEquals(ConfigStatus.SUCCESS, cca.configure(key, parameters, ctx));
278 public void testXmlGetrunningconfigExceptionFlow() throws TimedOutException, IOException {
279 Properties props = new Properties();
280 ConfigComponentAdaptor cca = Mockito.spy(new ConfigComponentAdaptor(props));
281 Mockito.doThrow(new IOException()).when(mockWrapper).connect(Mockito.anyString(),
282 Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyInt(),
283 Mockito.anyInt(), Mockito.anyString());
284 Mockito.doReturn(mockWrapper).when(cca).getSshJcraftWrapper();
285 String key = "xml-getrunningconfig";
286 Map<String, String> parameters = new HashMap<>();
287 loadSshParameters(parameters);
288 SvcLogicContext ctx = new SvcLogicContext();
289 assertEquals(ConfigStatus.FAILURE, cca.configure(key, parameters, ctx));
293 public void testDownloadCliConfig() throws TimedOutException, IOException {
294 Properties props = null;
295 ConfigComponentAdaptor cca = Mockito.spy(new ConfigComponentAdaptor(props));
296 Mockito.doReturn("TEST\nDATA").when(mockWrapper).receiveUntil(Mockito.anyString(),
297 Mockito.anyInt(), Mockito.anyString());
298 Mockito.doReturn(mockWrapper).when(cca).getSshJcraftWrapper();
299 String Download_config_template =
300 ("get_config_template\nRequest: \"show config\"\nResponse: Ends_With \"RESPONSE\"");
301 String key = "DownloadCliConfig";
302 Map<String, String> parameters = new HashMap<>();
303 loadSshParameters(parameters);
304 parameters.put("Download_config_template", Download_config_template);
305 SvcLogicContext ctx = new SvcLogicContext();
307 "service-data.vnf-config-parameters-list.vnf-config-parameters[0].update-configuration[0].block-key-name",
309 assertEquals(ConfigStatus.SUCCESS, cca.configure(key, parameters, ctx));
313 public void testDownloadCliConfigExceptionFlow() throws TimedOutException, IOException {
314 Properties props = null;
315 ConfigComponentAdaptor cca = Mockito.spy(new ConfigComponentAdaptor(props));
316 Mockito.doThrow(new IOException("ExceptionFromDownloadCli")).when(mockWrapper)
317 .receiveUntil(Mockito.anyString(), Mockito.anyInt(), Mockito.anyString());
318 Mockito.doReturn(mockWrapper).when(cca).getSshJcraftWrapper();
319 String Download_config_template = ("get_config_template\nRequest: \"show config\""
320 + "\n Execute_config_contents Response: Ends_With\" \"RESPONSE\"\n");
321 String key = "DownloadCliConfig";
322 Map<String, String> parameters = new HashMap<>();
323 loadSshParameters(parameters);
324 parameters.put("Config_contents", "TEST\nDATA");
325 parameters.put("Download_config_template", Download_config_template);
326 SvcLogicContext ctx = new SvcLogicContext();
328 "service-data.vnf-config-parameters-list.vnf-config-parameters[0].update-configuration[0].block-key-name",
330 assertEquals(ConfigStatus.FAILURE, cca.configure(key, parameters, ctx));
334 public void testPrepare() {
335 Client mockClient = Mockito.mock(Client.class);
336 WebResource mockWebResource = Mockito.mock(WebResource.class);
337 ClientResponse mockClientResponse = Mockito.mock(ClientResponse.class);
338 ConfigComponentAdaptor cca = Mockito.spy(new ConfigComponentAdaptor(null));
339 Mockito.doReturn(mockClientResponse).when(cca).getClientResponse(Mockito.anyObject(),
340 Mockito.anyString(), Mockito.anyString());
341 Mockito.doReturn(mockWebResource).when(mockClient).resource(Mockito.anyString());
342 Mockito.doReturn(mockClient).when(cca).getClient();
343 Map<String, String> parameters = new HashMap<>();
344 parameters.put("action", "prepare");
345 SvcLogicContext ctx = new SvcLogicContext();
347 "service-data.vnf-config-parameters-list.vnf-config-parameters[0].update-configuration[0].block-key-name",
349 assertEquals(ConfigStatus.SUCCESS, cca.configure("", parameters, ctx));
353 public void testPrepareExceptionFlow() {
354 ConfigComponentAdaptor cca = new ConfigComponentAdaptor(null);
355 Map<String, String> parameters = new HashMap<>();
356 parameters.put("action", "prepare");
357 SvcLogicContext ctx = new SvcLogicContext();
359 "service-data.vnf-config-parameters-list.vnf-config-parameters[0].scale-configuration[0].network-type",
361 assertEquals(ConfigStatus.FAILURE, cca.configure("", parameters, ctx));
362 assertEquals("500", ctx.getAttribute("error-code"));
366 * Test method to add coverage to buildNetworkData2(..) and expandRepeats(..) methods in
367 * ConfigComponentAdaptor by inputting a more complex XML document
370 public void testPrepareComplexTemplate() {
371 Client mockClient = Mockito.mock(Client.class);
372 WebResource mockWebResource = Mockito.mock(WebResource.class);
373 ClientResponse mockClientResponse = Mockito.mock(ClientResponse.class);
374 ConfigComponentAdaptor cca = Mockito.spy(new ConfigComponentAdaptor(null));
375 Mockito.doReturn(mockClientResponse).when(cca).getClientResponse(Mockito.anyObject(),
376 Mockito.anyString(), Mockito.anyString());
377 Mockito.doReturn(mockWebResource).when(mockClient).resource(Mockito.anyString());
378 Mockito.doReturn(mockClient).when(cca).getClient();
379 String complexTemplateString = cca.readFile("/prepare.xml");
380 Mockito.when(cca.readFile(Mockito.anyString())).thenReturn(complexTemplateString);
381 Map<String, String> parameters = new HashMap<>();
382 parameters.put("action", "prepare");
383 SvcLogicContext ctx = new SvcLogicContext();
385 "service-data.vnf-config-parameters-list.vnf-config-parameters[0].update-configuration[0].block-key-name",
387 assertEquals(ConfigStatus.SUCCESS, cca.configure("", parameters, ctx));
391 public void testAudit() {
392 Client mockClient = Mockito.mock(Client.class);
393 WebResource mockWebResource = Mockito.mock(WebResource.class);
394 ClientResponse mockClientResponse = Mockito.mock(ClientResponse.class);
395 ConfigComponentAdaptor cca = Mockito.spy(new ConfigComponentAdaptor(null));
396 Mockito.doReturn(mockClientResponse).when(cca).getClientResponse(Mockito.anyObject(),
397 Mockito.anyString(), Mockito.anyString());
398 Mockito.doReturn(mockWebResource).when(mockClient).resource(Mockito.anyString());
399 Mockito.doReturn(mockClient).when(cca).getClient();
400 Map<String, String> parameters = new HashMap<>();
401 parameters.put("action", "audit");
402 SvcLogicContext ctx = new SvcLogicContext();
404 "service-data.vnf-config-parameters-list.vnf-config-parameters[0].update-configuration[0].block-key-name",
406 assertEquals(ConfigStatus.SUCCESS, cca.configure("", parameters, ctx));
410 public void testActivate() {
411 ClientResponse mockClientResponse = Mockito.mock(ClientResponse.class);
412 ConfigComponentAdaptor cca = Mockito.spy(new ConfigComponentAdaptor(null));
413 Mockito.doReturn(mockClientResponse).when(cca).getClientResponse(Mockito.anyObject(),
414 Mockito.anyString(), Mockito.anyString());
415 Map<String, String> parameters = new HashMap<>();
416 parameters.put("action", "activate");
417 SvcLogicContext ctx = new SvcLogicContext();
419 "service-data.vnf-config-parameters-list.vnf-config-parameters[0].update-configuration[0].block-key-name",
421 assertEquals(ConfigStatus.SUCCESS, cca.configure("", parameters, ctx));
425 public void testConstructorForNonNullProperties() throws Exception {
426 Properties props = new Properties();
427 props.setProperty("configComponent.url", "testConfigUrl");
428 props.setProperty("configComponent.user", "testConfigUser");
429 props.setProperty("configComponent.passwd", "testConfigPwd");
430 props.setProperty("auditComponent.url", "testAuditUrl");
431 props.setProperty("auditComponent.user", "testAuditUser");
432 props.setProperty("auditComponent.passwd", "testAuditPwd");
433 props.setProperty("service-configuration-notification-url", "testServiceNotificationUrl");
434 props.setProperty("audit-configuration-notification-url", "testAuditNotificationUrl");
436 ConfigComponentAdaptor cca = new ConfigComponentAdaptor(props);
437 assertEquals("testConfigUrl", cca.getConfigUrl());
438 assertEquals("testConfigUser", cca.getConfigUser());
439 assertEquals("testConfigPwd", cca.getConfigPassword());
440 assertEquals("testAuditUrl", cca.getAuditUrl());
441 assertEquals("testAuditUser", cca.getAuditUser());
442 assertEquals("testAuditPwd", cca.getAuditPassword());
443 assertEquals("testServiceNotificationUrl", cca.getConfigCallbackUrl());
444 assertEquals("testAuditNotificationUrl", cca.getAuditCallbackUrl());
448 public void testStaticReadFile() {
449 assertThat(ConfigComponentAdaptor._readFile("src/main/resources/config-base.xml"),
450 CoreMatchers.containsString("<configure>"));
454 public void testActivateMethod() {
455 ConfigComponentAdaptor config= new ConfigComponentAdaptor(new Properties());
456 assertEquals(ConfigStatus.SUCCESS, config.activate("test", new SvcLogicContext()));
460 public void testDeactivateMethod() {
461 ConfigComponentAdaptor config= new ConfigComponentAdaptor(new Properties());
462 assertEquals(ConfigStatus.SUCCESS, config.deactivate("test", new SvcLogicContext()));
466 public void testStaticReadFileExceptionFlow() {
467 assertEquals("", ConfigComponentAdaptor._readFile("NON_EXISTENT_FILE"));
470 private void loadSshParameters(Map<String, String> map) {
471 map.put("Host_ip_address", "test");
472 map.put("User_name", "test");
473 map.put("Password", "password");
474 map.put("Port_number", "22");
475 map.put("portNumber", "22");