eae53a8365afb3e9cace6dd71d26fc7af3425388
[appc.git] / appc-config / appc-config-adaptor / provider / src / test / java / org / onap / appc / ccadaptor / ConfigComponentAdaptorTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
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
16  *
17  *      http://www.apache.org/licenses/LICENSE-2.0
18  *
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.
24  *
25  * ============LICENSE_END=========================================================
26  */
27
28 package org.onap.appc.ccadaptor;
29
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;
37 import java.util.Map;
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;
47
48 @RunWith(MockitoJUnitRunner.class)
49 public class ConfigComponentAdaptorTest {
50
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;
55
56     @Before
57     public void setupForTests() {
58         mockWrapper = Mockito.mock(SshJcraftWrapper.class);
59     }
60
61     @Test
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();
68         String key = "get";
69         Map<String, String> parameters = new HashMap<>();
70         parameters.put("Host_ip_address", "test");
71         SvcLogicContext ctx = new SvcLogicContext();
72         ctx.setAttribute(
73                 "service-data.vnf-config-parameters-list.vnf-config-parameters[0].update-configuration[0].block-key-name",
74                 "test");
75         assertEquals(ConfigStatus.SUCCESS, cca.configure(key, parameters, ctx));
76     }
77
78     @Test
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();
85         String key = "put";
86         Map<String, String> parameters = new HashMap<>();
87         parameters.put("data", "test");
88         SvcLogicContext ctx = new SvcLogicContext();
89         ctx.setAttribute(
90                 "service-data.vnf-config-parameters-list.vnf-config-parameters[0].update-configuration[0].block-key-name",
91                 "test");
92         assertEquals(ConfigStatus.FAILURE, cca.configure(key, parameters, ctx));
93     }
94
95     @Test
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\"");
104         String key = "cli";
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();
112         ctx.setAttribute(
113                 "service-data.vnf-config-parameters-list.vnf-config-parameters[0].update-configuration[0].block-key-name",
114                 "test");
115         assertEquals(ConfigStatus.SUCCESS, cca.configure(key, parameters, ctx));
116     }
117
118     @Test
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\"");
127         String key = "cli";
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();
135         ctx.setAttribute(
136                 "service-data.vnf-config-parameters-list.vnf-config-parameters[0].update-configuration[0].block-key-name",
137                 "test");
138         assertEquals(ConfigStatus.FAILURE, cca.configure(key, parameters, ctx));
139     }
140
141     @Test
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();
151         ctx.setAttribute(
152                 "service-data.vnf-config-parameters-list.vnf-config-parameters[0].update-configuration[0].block-key-name",
153                 "test");
154         assertEquals(ConfigStatus.SUCCESS, cca.configure(key, parameters, ctx));
155         assertEquals("\\" + testArtifactContents + "\\\\", ctx.getAttribute("escapedData"));
156     }
157
158     @Test
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();
175         ctx.setAttribute(
176                 "service-data.vnf-config-parameters-list.vnf-config-parameters[0].update-configuration[0].block-key-name",
177                 "test");
178         assertEquals(ConfigStatus.SUCCESS, cca.configure(key, parameters, ctx));
179     }
180
181     @Test
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();
198         ctx.setAttribute(
199                 "service-data.vnf-config-parameters-list.vnf-config-parameters[0].update-configuration[0].block-key-name",
200                 "test");
201         assertEquals(ConfigStatus.FAILURE, cca.configure(key, parameters, ctx));
202     }
203
204     @Test
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);
216
217         SvcLogicContext ctx = new SvcLogicContext();
218         ctx.setAttribute(
219                 "service-data.vnf-config-parameters-list.vnf-config-parameters[0].update-configuration[0].block-key-name",
220                 "test");
221         assertEquals(ConfigStatus.SUCCESS, cca.configure(key, parameters, ctx));
222     }
223
224     @Test
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);
236
237         SvcLogicContext ctx = new SvcLogicContext();
238         ctx.setAttribute(
239                 "service-data.vnf-config-parameters-list.vnf-config-parameters[0].update-configuration[0].block-key-name",
240                 "test");
241         assertEquals(ConfigStatus.FAILURE, cca.configure(key, parameters, ctx));
242     }
243
244     @Test
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>")
249                 .when(mockWrapper)
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));
257     }
258
259     @Test
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));
272     }
273
274     @Test
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();
288         ctx.setAttribute(
289                 "service-data.vnf-config-parameters-list.vnf-config-parameters[0].update-configuration[0].block-key-name",
290                 "test");
291         assertEquals(ConfigStatus.SUCCESS, cca.configure(key, parameters, ctx));
292     }
293
294     @Test
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();
309         ctx.setAttribute(
310                 "service-data.vnf-config-parameters-list.vnf-config-parameters[0].update-configuration[0].block-key-name",
311                 "test");
312         assertEquals(ConfigStatus.FAILURE, cca.configure(key, parameters, ctx));
313     }
314
315     @Test
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();
328         ctx.setAttribute(
329                 "service-data.vnf-config-parameters-list.vnf-config-parameters[0].update-configuration[0].block-key-name",
330                 "test");
331         assertEquals(ConfigStatus.SUCCESS, cca.configure("", parameters, ctx));
332     }
333
334     @Test
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();
340         ctx.setAttribute(
341                 "service-data.vnf-config-parameters-list.vnf-config-parameters[0].scale-configuration[0].network-type",
342                 "test");
343         assertEquals(ConfigStatus.FAILURE, cca.configure("", parameters, ctx));
344         assertEquals("500", ctx.getAttribute("error-code"));
345     }
346
347     @Test
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();
360         ctx.setAttribute(
361                 "service-data.vnf-config-parameters-list.vnf-config-parameters[0].update-configuration[0].block-key-name",
362                 "test");
363         assertEquals(ConfigStatus.SUCCESS, cca.configure("", parameters, ctx));
364     }
365
366     @Test
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();
375         ctx.setAttribute(
376                 "service-data.vnf-config-parameters-list.vnf-config-parameters[0].update-configuration[0].block-key-name",
377                 "test");
378         assertEquals(ConfigStatus.SUCCESS, cca.configure("", parameters, ctx));
379     }
380
381     @Test
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");
392
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());
402     }
403
404     @Test
405     public void testStaticReadFile() {
406         assertThat(ConfigComponentAdaptor._readFile("src/main/resources/config-base.xml"),
407                 CoreMatchers.containsString("<configure>"));
408     }
409
410     @Test
411     public void testStaticReadFileExceptionFlow() {
412         assertEquals("", ConfigComponentAdaptor._readFile("NON_EXISTENT_FILE"));
413     }
414
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");
421     }
422 }