Change code in appc dispatcher for new LCMs in R6
[appc.git] / appc-dg / appc-dg-shared / appc-dg-netconf / src / test / java / org / onap / appc / dg / netconf / impl / NetconfClientPluginImplTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * ================================================================================
9  * Modifications (C) 2019 Ericsson
10  * =============================================================================
11  * Licensed under the Apache License, Version 2.0 (the "License");
12  * you may not use this file except in compliance with the License.
13  * You may obtain a copy of the License at
14  *
15  *      http://www.apache.org/licenses/LICENSE-2.0
16  *
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.appc.dg.netconf.impl;
26
27 import org.onap.appc.exceptions.APPCException;
28 import com.fasterxml.jackson.databind.ObjectMapper;
29 import org.junit.Assert;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 import org.mockito.Matchers;
34 import org.mockito.Mockito;
35 import org.onap.appc.adapter.netconf.util.Constants;
36 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
37 import org.onap.appc.adapter.netconf.ConnectionDetails;
38 import org.onap.appc.adapter.netconf.NetconfClientFactory;
39 import org.onap.appc.adapter.netconf.NetconfClientType;
40 import org.onap.appc.adapter.netconf.NetconfConnectionDetails;
41 import org.onap.appc.adapter.netconf.NetconfDataAccessService;
42 import org.onap.appc.adapter.netconf.OperationalStateValidatorFactory;
43 import org.onap.appc.adapter.netconf.VnfType;
44 import org.osgi.framework.Bundle;
45 import org.osgi.framework.BundleContext;
46 import org.osgi.framework.FrameworkUtil;
47 import org.osgi.framework.ServiceReference;
48 import org.powermock.api.mockito.PowerMockito;
49 import org.powermock.core.classloader.annotations.PrepareForTest;
50 import org.powermock.modules.junit4.PowerMockRunner;
51
52 import java.lang.reflect.Field;
53 import java.text.DateFormat;
54 import java.text.SimpleDateFormat;
55 import java.util.Date;
56 import java.util.HashMap;
57 import java.util.Map;
58
59 import static org.powermock.api.mockito.PowerMockito.when;
60
61
62 @RunWith(PowerMockRunner.class)
63 @PrepareForTest({OperationalStateValidatorFactory.class, FrameworkUtil.class, ObjectMapper.class})
64
65 public class NetconfClientPluginImplTest {
66     private NetconfClientPluginImpl netconfClientPlugin;
67     private NetconfDataAccessService dao;
68     private NetconfClientFactory clientFactory;
69     private Map<String, String> params;
70
71     private final BundleContext bundleContext = Mockito.mock(BundleContext.class);
72     private final Bundle bundleService = Mockito.mock(Bundle.class);
73     private final ServiceReference sref1 = Mockito.mock(ServiceReference.class);
74     private final ServiceReference sref2 = Mockito.mock(ServiceReference.class);
75     private final ServiceReference sref3 = Mockito.mock(ServiceReference.class);
76     private static final String DG_OUTPUT_STATUS_MESSAGE = "output.status.message";
77
78
79     String host = "http://www.test.com";
80     String host1 = "http://www.test1.com";
81     String vnfType = "VNF";
82     int port = 8080;
83     String username = "test";
84     String password = "test";
85     String connectionDetails =
86             "{\"host\":\"" + host + "\",\"port\":" + port + ",\"username\":\"" + username
87             + "\",\"password\":\"" + password + "\",\"capabilities\":null,\"additionalProperties\":null}";
88     String fileContent =
89             "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
90             + "<rpc message-id=\"101\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
91             + "\t<get-config>\n"
92             + "\t\t<source>\n"
93             + "\t\t\t<running/>\n"
94             + "\t\t </source>\n"
95             + "\t</get-config>\n"
96             + "</rpc>'";
97     String operationalState =
98             "<rpc message-id=\"101\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
99             + "       <get>\n"
100             + "              <filter>\n"
101             + "                     <ManagedElement xmlns=\"urn:org:onap:appc:Test\">\n"
102             + "                           <VnfFunction xmlns=\"urn:org:openecomop:appc:Test\">\n"
103             + "                                  <ProcessorManagement>\n"
104             + "                                         <MatedPair>\n"
105             + "                                                <operationalState/>\n"
106             + "                                                <PayloadProcessor>\n"
107             + "                                                       <operationalState/>\n"
108             + "                                                </PayloadProcessor>\n"
109             + "                                         </MatedPair>\n"
110             + "                                         <SystemController>\n"
111             + "                                                <operationalState/>\n"
112             + "                                         </SystemController>\n"
113             + "                                  </ProcessorManagement>\n"
114             + "                           </VnfFunction>\n"
115             + "                     </ManagedElement>\n"
116             + "              </filter>\n"
117             + "       </get>\n"
118             + "</rpc>\n";
119
120
121     @Before
122     public void setUp() throws NoSuchFieldException, IllegalAccessException {
123         clientFactory = new NetconfClientFactoryMock();
124     }
125
126
127     @Test
128     public void testConfigure() throws Exception {
129         shortInit();
130         SvcLogicContext ctx = new SvcLogicContext();
131
132         params = new HashMap<>();
133         params.put(Constants.CONNECTION_DETAILS_FIELD_NAME, connectionDetails);
134         params.put(Constants.FILE_CONTENT_FIELD_NAME, fileContent);
135
136         netconfClientPlugin.configure(params, ctx);
137
138         NetconfClientJschMock client = (NetconfClientJschMock) clientFactory.getNetconfClient(NetconfClientType.SSH);
139
140         try {
141             Assert.assertEquals("wrong configuration", fileContent, client.getConf());
142             Assert.assertEquals("wrong host", host, client.getLastConnectionDetails().getHost());
143             Assert.assertEquals("wrong port", port, client.getLastConnectionDetails().getPort());
144             Assert.assertEquals("wrong username", username, client.getLastConnectionDetails().getUsername());
145             Assert.assertEquals("wrong password", password, client.getLastConnectionDetails().getPassword());
146             Assert.assertFalse(client.isConnection());
147         } catch (Exception e) {
148             Assert.fail("failed because of " + e.getCause());
149         }
150     }
151
152
153     @Test
154     public void testConfigureNegativeIOException() throws Exception {
155         shortInit();
156         SvcLogicContext ctx = new SvcLogicContext();
157
158         params = new HashMap<>();
159         params.put(Constants.CONNECTION_DETAILS_FIELD_NAME, "{" + connectionDetails);
160         params.put(Constants.FILE_CONTENT_FIELD_NAME, fileContent);
161         NetconfClientJschMock client = (NetconfClientJschMock) clientFactory.getNetconfClient(NetconfClientType.SSH);
162
163         try {
164             netconfClientPlugin.configure(params, ctx);
165             Assert.assertTrue(false);
166         } catch (APPCException e) {
167             Assert.assertNull(client.getLastConnectionDetails());
168             Assert.assertNull(client.getConf());
169         }
170
171     }
172
173     @Test
174     public void testOperationStateValidation() throws Exception {
175         shortInit();
176         SvcLogicContext ctx = new SvcLogicContext();
177         DAOServiceMock daoServiceMock = (DAOServiceMock) dao;
178         daoServiceMock.setConfigFile(fileContent);
179
180         NetconfClientJschMock client = (NetconfClientJschMock) clientFactory.getNetconfClient(NetconfClientType.SSH);
181         client.setAnswer(operationalState);
182
183         params = new HashMap<>();
184         params.put(Constants.VNF_TYPE_FIELD_NAME, vnfType);
185         params.put(Constants.VNF_HOST_IP_ADDRESS_FIELD_NAME, host1);
186         params.put(Constants.CONNECTION_DETAILS_FIELD_NAME, connectionDetails);
187         MockOperationalStateValidatorImpl validatorMock = new MockOperationalStateValidatorImpl();
188         validatorMock.setConfigurationFileName("VnfGetRunningConfig");
189
190         PowerMockito.mockStatic(OperationalStateValidatorFactory.class);
191         when(OperationalStateValidatorFactory.getOperationalStateValidator(Matchers.any(VnfType.class)))
192                 .thenReturn(validatorMock);
193
194         netconfClientPlugin.operationStateValidation(params, ctx);
195
196         Assert.assertTrue("validation process failed", validatorMock.isValidated());
197         Assert.assertEquals(fileContent, client.getLastMessage());
198     }
199
200     @Test
201     public void testOperationStateValidationNegativeJsonProcessingNullIllegalStateException() throws Exception {
202         shortInit();
203         SvcLogicContext ctx = new SvcLogicContext();
204         DAOServiceMock daoServiceMock = (DAOServiceMock) dao;
205         daoServiceMock.setConfigFile(fileContent);
206
207         NetconfClientJschMock client = (NetconfClientJschMock) clientFactory.getNetconfClient(NetconfClientType.SSH);
208         client.setAnswer(operationalState);
209
210         params = new HashMap<>();
211         params.put(Constants.VNF_TYPE_FIELD_NAME, vnfType);
212         params.put(Constants.VNF_HOST_IP_ADDRESS_FIELD_NAME, host1);
213         params.put(Constants.CONNECTION_DETAILS_FIELD_NAME, connectionDetails);
214         MockOperationalStateValidatorImpl validatorMock = new MockOperationalStateValidatorImpl();
215         validatorMock.setConfigurationFileName("VnfGetRunningConfig");
216
217         PowerMockito.mockStatic(OperationalStateValidatorFactory.class);
218         when(OperationalStateValidatorFactory.getOperationalStateValidator(Matchers.any(VnfType.class)))
219                 .thenReturn(validatorMock);
220         substituteMapper(true);
221
222         try {
223             netconfClientPlugin.operationStateValidation(params, ctx);
224             substituteMapper(false);
225         } catch (APPCException e) {
226             substituteMapper(false);
227             Assert.assertNotNull(ctx.getAttribute(DG_OUTPUT_STATUS_MESSAGE));
228             Assert.assertFalse(validatorMock.isValidated());
229             Assert.assertNull(client.getLastMessage());
230         }
231     }
232
233     @Test
234     public void testOperationStateValidationNegativeConnectionDetailsAreNullNullPointerException() throws Exception {
235         shortInit();
236         SvcLogicContext ctx = new SvcLogicContext();
237         DAOServiceMock daoServiceMock = (DAOServiceMock) dao;
238         daoServiceMock.setConfigFile(fileContent);
239
240         NetconfClientJschMock client = (NetconfClientJschMock) clientFactory.getNetconfClient(NetconfClientType.SSH);
241         client.setAnswer(operationalState);
242
243         params = new HashMap<>();
244         params.put(Constants.VNF_TYPE_FIELD_NAME, vnfType);
245         params.put(Constants.VNF_HOST_IP_ADDRESS_FIELD_NAME, host1);
246         params.put(Constants.CONNECTION_DETAILS_FIELD_NAME, null);
247         MockOperationalStateValidatorImpl validatorMock = new MockOperationalStateValidatorImpl();
248         validatorMock.setConfigurationFileName("VnfGetRunningConfig");
249
250         PowerMockito.mockStatic(OperationalStateValidatorFactory.class);
251         when(OperationalStateValidatorFactory.getOperationalStateValidator(Matchers.any(VnfType.class)))
252                 .thenReturn(validatorMock);
253         ObjectMapper mapper = PowerMockito.mock(ObjectMapper.class);
254         final NetconfConnectionDetails netconfConnectionDetails = null;
255         when(mapper.readValue(Matchers.anyString(), Matchers.any(Class.class))).thenReturn(netconfConnectionDetails);
256
257         try {
258             netconfClientPlugin.operationStateValidation(params, ctx);
259             Assert.assertTrue(false);
260         } catch (APPCException e) {
261             Assert.assertNotNull(ctx.getAttribute(DG_OUTPUT_STATUS_MESSAGE));
262             Assert.assertFalse("validation process failed", validatorMock.isValidated());
263         }
264     }
265
266
267     @Test
268     public void testOperationStateValidationNegativeAppcException() throws Exception {
269         shortInit();
270         SvcLogicContext ctx = new SvcLogicContext();
271         DAOServiceMock daoServiceMock = (DAOServiceMock) dao;
272         daoServiceMock.setConfigFile(fileContent);
273
274         NetconfClientJschMock client = (NetconfClientJschMock) clientFactory.getNetconfClient(NetconfClientType.SSH);
275         client.setAnswer("wrong");
276
277         params = new HashMap<>();
278         params.put(Constants.VNF_TYPE_FIELD_NAME, vnfType);
279         params.put(Constants.VNF_HOST_IP_ADDRESS_FIELD_NAME, host1);
280         params.put(Constants.CONNECTION_DETAILS_FIELD_NAME, connectionDetails);
281         MockOperationalStateValidatorImpl validatorMock = new MockOperationalStateValidatorImpl();
282         validatorMock.setConfigurationFileName("VnfGetRunningConfig");
283
284         PowerMockito.mockStatic(OperationalStateValidatorFactory.class);
285         when(OperationalStateValidatorFactory.getOperationalStateValidator(Matchers.any(VnfType.class)))
286                 .thenReturn(validatorMock);
287
288         try {
289             netconfClientPlugin.operationStateValidation(params, ctx);
290             Assert.assertTrue(false);
291         } catch (APPCException e) {
292             Assert.assertNotNull(ctx.getAttribute(DG_OUTPUT_STATUS_MESSAGE));
293             Assert.assertFalse("validation process failed", validatorMock.isValidated());
294         }
295     }
296
297
298     @Test
299     public void testOperationStateValidatioConnectionDetailsInParamsAreEmpty() throws Exception {
300         shortInit();
301         SvcLogicContext ctx = new SvcLogicContext();
302         DAOServiceMock daoServiceMock = (DAOServiceMock) dao;
303         daoServiceMock.setConfigFile(fileContent);
304
305         NetconfClientJschMock client = (NetconfClientJschMock) clientFactory.getNetconfClient(NetconfClientType.SSH);
306         client.setAnswer(operationalState);
307         ((DAOServiceMock) dao).setConnection(getConnectionDetails());
308
309         params = new HashMap<>();
310         params.put(Constants.VNF_TYPE_FIELD_NAME, vnfType);
311         params.put(Constants.VNF_HOST_IP_ADDRESS_FIELD_NAME, host1);
312         params.put(Constants.CONNECTION_DETAILS_FIELD_NAME, "");
313         MockOperationalStateValidatorImpl validatorMock = new MockOperationalStateValidatorImpl();
314         validatorMock.setConfigurationFileName("VnfGetRunningConfig");
315
316         PowerMockito.mockStatic(OperationalStateValidatorFactory.class);
317         when(OperationalStateValidatorFactory.getOperationalStateValidator(Matchers.any(VnfType.class)))
318                 .thenReturn(validatorMock);
319
320         netconfClientPlugin.operationStateValidation(params, ctx);
321
322         Assert.assertTrue("validation process failed", validatorMock.isValidated());
323         Assert.assertEquals(fileContent, client.getLastMessage());
324     }
325
326     @Test
327     public void testOperationStateValidatioConnectionDetailsInParamsAreNull() throws Exception {
328         shortInit();
329         SvcLogicContext ctx = new SvcLogicContext();
330         DAOServiceMock daoServiceMock = (DAOServiceMock) dao;
331         daoServiceMock.setConfigFile(fileContent);
332
333         NetconfClientJschMock client = (NetconfClientJschMock) clientFactory.getNetconfClient(NetconfClientType.SSH);
334         client.setAnswer(operationalState);
335         ((DAOServiceMock) dao).setConnection(getConnectionDetails());
336
337         params = new HashMap<>();
338         params.put(Constants.VNF_TYPE_FIELD_NAME, vnfType);
339         params.put(Constants.VNF_HOST_IP_ADDRESS_FIELD_NAME, host1);
340         params.put(Constants.CONNECTION_DETAILS_FIELD_NAME, null);
341         MockOperationalStateValidatorImpl validatorMock = new MockOperationalStateValidatorImpl();
342         validatorMock.setConfigurationFileName("VnfGetRunningConfig");
343
344         PowerMockito.mockStatic(OperationalStateValidatorFactory.class);
345         when(OperationalStateValidatorFactory.getOperationalStateValidator(Matchers.any(VnfType.class)))
346                 .thenReturn(validatorMock);
347
348         netconfClientPlugin.operationStateValidation(params, ctx);
349
350         Assert.assertTrue("validation process failed", validatorMock.isValidated());
351         Assert.assertEquals(fileContent, client.getLastMessage());
352     }
353
354
355     @Test
356     public void testBackupConfiguration() throws Exception {
357         shortInit();
358         SvcLogicContext ctx = new SvcLogicContext();
359         params = new HashMap<>();
360         params.put(Constants.CONNECTION_DETAILS_FIELD_NAME, connectionDetails);
361         NetconfClientJschMock client = (NetconfClientJschMock) clientFactory.getNetconfClient(NetconfClientType.SSH);
362         client.setConf(fileContent);
363         netconfClientPlugin.backupConfiguration(params, ctx);
364
365         DAOServiceMock mockdao = (DAOServiceMock) dao;
366         DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
367         Date date = new Date();
368         String creationDateExpected = dateFormat.format(date);
369         String creationDateActual = mockdao.getBackupConf().get("creationDate").substring(0, 10);
370
371         Assert.assertEquals("wrong configuration in db", fileContent, mockdao.getBackupConf().get("logText"));
372         Assert.assertEquals(creationDateExpected, creationDateActual);
373     }
374
375     @Test
376     public void testBackupConfigurationNegativeDgErrorFieldName() throws Exception {
377         shortInit();
378         SvcLogicContext ctx = new SvcLogicContext();
379         params = new HashMap<>();
380         params.put(Constants.CONNECTION_DETAILS_FIELD_NAME, "{" + connectionDetails);
381         NetconfClientJschMock client = (NetconfClientJschMock) clientFactory.getNetconfClient(NetconfClientType.SSH);
382         client.setConf(fileContent);
383         try {
384             netconfClientPlugin.backupConfiguration(params, ctx);
385             Assert.assertTrue(false);
386         } catch (APPCException e) {
387             Assert.assertNotNull(ctx.getAttribute(DG_OUTPUT_STATUS_MESSAGE));
388             DAOServiceMock mockdao = (DAOServiceMock) dao;
389             Assert.assertNull(mockdao.getBackupConf());
390         }
391     }
392
393     @Test
394     public void testGetConfig() throws Exception {
395         fullInit();
396         String entity = "123";
397
398         SvcLogicContext ctx = new SvcLogicContext();
399         ctx.setAttribute("entity", entity);
400
401         params = new HashMap<>();
402         params.put("conf-id", "current");
403         params.put(Constants.CONNECTION_DETAILS_FIELD_NAME, connectionDetails);
404         NetconfClientJschMock client = (NetconfClientJschMock) clientFactory.getNetconfClient(NetconfClientType.SSH);
405         client.setConf(fileContent);
406
407         netconfClientPlugin.getConfig(params, ctx);
408
409         Assert.assertEquals("Success", ctx.getAttribute("getConfig_Result"));
410         Assert.assertEquals(fileContent, ctx.getAttribute("fullConfig"));
411         Assert.assertNotNull(ctx.getAttribute(entity + ".Configuration"));
412         Assert.assertEquals(fileContent, ctx.getAttribute(entity + ".Configuration"));
413     }
414
415
416     @Test
417     public void testGetConfigNegativeConfigurationNull() throws Exception {
418         fullInit();
419         String entity = "123";
420
421         SvcLogicContext ctx = new SvcLogicContext();
422         ctx.setAttribute("entity", entity);
423
424         params = new HashMap<>();
425         params.put("conf-id", "current");
426         params.put(Constants.CONNECTION_DETAILS_FIELD_NAME, connectionDetails);
427
428         netconfClientPlugin.getConfig(params, ctx);
429
430         Assert.assertEquals("failure", ctx.getAttribute("getConfig_Result"));
431         Assert.assertNull(ctx.getAttribute("fullConfig"));
432         Assert.assertNull(ctx.getAttribute(entity + ".Configuration"));
433         Assert.assertNull(ctx.getAttribute(entity + ".Configuration"));
434     }
435
436
437     @Test
438     public void testGetConfigNegativeNotSupportedConfId() throws Exception {
439         fullInit();
440         String entity = "123";
441         SvcLogicContext ctx = new SvcLogicContext();
442         ctx.setAttribute("entity", entity);
443
444         params = new HashMap<>();
445         params.put("conf-id", "current1");
446         params.put(Constants.CONNECTION_DETAILS_FIELD_NAME, connectionDetails);
447
448         netconfClientPlugin.getConfig(params, ctx);
449
450         Assert.assertNull(ctx.getAttribute("getConfig_Result"));
451         Assert.assertNull(ctx.getAttribute("fullConfig"));
452         Assert.assertNull(ctx.getAttribute(entity + ".Configuration"));
453         Assert.assertNull(ctx.getAttribute(entity + ".Configuration"));
454     }
455
456     @Test
457     public void testGetConfigNegativeWronjJsonConnectionDetailsException() throws Exception {
458         fullInit();
459         String entity = "123";
460
461         SvcLogicContext ctx = new SvcLogicContext();
462         ctx.setAttribute("entity", entity);
463
464         params = new HashMap<>();
465         params.put("conf-id", "current");
466         params.put(Constants.CONNECTION_DETAILS_FIELD_NAME, "{" + connectionDetails);
467
468         try {
469             netconfClientPlugin.getConfig(params, ctx);
470             Assert.assertTrue(false);
471         } catch (APPCException e) {
472             Assert.assertEquals("failure", ctx.getAttribute("getConfig_Result"));
473             Assert.assertNull(ctx.getAttribute("fullConfig"));
474             Assert.assertNull(ctx.getAttribute(entity + ".Configuration"));
475             Assert.assertNull(ctx.getAttribute(entity + ".Configuration"));
476             Assert.assertNotNull(ctx.getAttribute(DG_OUTPUT_STATUS_MESSAGE));
477         }
478     }
479
480     @Test
481     public void testGetRunningConfig() throws Exception {
482         fullInit();
483         SvcLogicContext ctx = new SvcLogicContext();
484         params = new HashMap<>();
485         params.put("host-ip-address", host);
486         params.put("user-name", username);
487         params.put("password", password);
488         params.put("port-number", String.valueOf(port));
489
490         NetconfClientJschMock client = (NetconfClientJschMock) clientFactory.getNetconfClient(NetconfClientType.SSH);
491         client.setConf(fileContent);
492
493         netconfClientPlugin.getRunningConfig(params, ctx);
494
495         Assert.assertEquals("Success", ctx.getAttribute("getRunningConfig_Result"));
496         Assert.assertEquals(fileContent, ctx.getAttribute("running-config"));
497         Assert.assertEquals("success", ctx.getStatus());
498     }
499
500     @Test
501     public void testGetRunningConfigWithoutPortNumberDgErrorFieldNameException() throws Exception {
502         fullInit();
503         SvcLogicContext ctx = new SvcLogicContext();
504         params = new HashMap<>();
505         params.put("host-ip-address", host);
506         params.put("user-name", username);
507         params.put("password", password);
508
509         NetconfClientJschMock client = (NetconfClientJschMock) clientFactory.getNetconfClient(NetconfClientType.SSH);
510         client.setConf(fileContent);
511
512         try {
513             netconfClientPlugin.getRunningConfig(params, ctx);
514             Assert.assertTrue(false);
515         } catch (APPCException e) {
516             Assert.assertEquals("failure", ctx.getAttribute("getRunningConfig_Result"));
517             Assert.assertNull(ctx.getAttribute("running-config"));
518             Assert.assertNotNull(ctx.getAttribute(DG_OUTPUT_STATUS_MESSAGE));
519         }
520     }
521
522     @Test
523     public void testGetRunningConfigNegativeConfigurationNull() throws Exception {
524         fullInit();
525         SvcLogicContext ctx = new SvcLogicContext();
526         params = new HashMap<>();
527         params.put("host-ip-address", host);
528         params.put("user-name", username);
529         params.put("password", password);
530         params.put("port-number", String.valueOf(port));
531
532         netconfClientPlugin.getRunningConfig(params, ctx);
533
534         Assert.assertEquals("failure", ctx.getAttribute("getRunningConfig_Result"));
535         Assert.assertNull(ctx.getAttribute("running-config"));
536     }
537
538     @Test
539     public void testValidateMandatoryParamNegativeEmptyParamValue() throws Exception {
540         shortInit();
541         String paramName = "test";
542         String paramValue = "";
543
544         try {
545             netconfClientPlugin.validateMandatoryParam(paramName, paramValue);
546             Assert.assertTrue(false);
547         } catch (Exception e) {
548             Assert.assertTrue(true);
549         }
550     }
551
552     @Test
553     public void testRetrieveConnectionDetails() throws Exception {
554         shortInit();
555         DAOServiceMock daoServiceMock = (DAOServiceMock) dao;
556         daoServiceMock.setConfigFile(fileContent);
557         ConnectionDetails connectionDetails1 = getConnectionDetails();
558         daoServiceMock.setConnection(connectionDetails1);
559
560         NetconfConnectionDetails connectionDetailsActual = netconfClientPlugin.retrieveConnectionDetails(VnfType.VNF);
561
562         Assert.assertEquals("wrong host", connectionDetails1.getHost(), connectionDetailsActual.getHost());
563         Assert.assertEquals("wrong password", connectionDetails1.getPassword(), connectionDetailsActual.getPassword());
564         Assert.assertEquals("wrong port", connectionDetails1.getPort(), connectionDetailsActual.getPort());
565         Assert.assertEquals("wrong username", connectionDetails1.getUsername(), connectionDetailsActual.getUsername());
566     }
567
568
569     @Test
570     public void testRetrieveConnectionDetailsNegativeMissingConfiguration() throws Exception {
571         shortInit();
572         DAOServiceMock daoServiceMock = (DAOServiceMock) dao;
573         daoServiceMock.setConfigFile(fileContent);
574         ConnectionDetails connectionDetails1 = getConnectionDetails();
575         daoServiceMock.setConnection(connectionDetails1);
576
577         NetconfConnectionDetails connectionDetailsActual = null;
578         try {
579             connectionDetailsActual = netconfClientPlugin.retrieveConnectionDetails(VnfType.MOCK);
580             Assert.assertTrue(false);
581         } catch (APPCException e) {
582             Assert.assertNull(connectionDetailsActual);
583         }
584     }
585
586     @Test
587     public void testRetrieveConfigurationFileContent() throws Exception {
588         shortInit();
589
590         DAOServiceMock daoServiceMock = (DAOServiceMock) dao;
591         daoServiceMock.setConfigFile(fileContent);
592
593         Assert.assertEquals("wrong config in a database",
594                 fileContent, netconfClientPlugin.retrieveConfigurationFileContent("VnfGetRunningConfig"));
595     }
596
597     private ConnectionDetails getConnectionDetails() {
598         ConnectionDetails connectionDetails = new ConnectionDetails();
599         connectionDetails.setPassword(password);
600         connectionDetails.setPort(port);
601         connectionDetails.setUsername(username);
602         connectionDetails.setHost(host);
603         return connectionDetails;
604     }
605
606
607     private void initDao() throws NoSuchFieldException, IllegalAccessException {
608         dao = new DAOServiceMock();
609         PowerMockito.mockStatic(FrameworkUtil.class);
610         when(FrameworkUtil.getBundle(Matchers.any(Class.class))).thenReturn(bundleService);
611         when(bundleService.getBundleContext()).thenReturn(bundleContext);
612         when(bundleContext.getServiceReference(NetconfDataAccessService.class)).thenReturn(sref1);
613         when(bundleContext.getService(sref1)).thenReturn(dao);
614     }
615
616     private void fullInit() throws NoSuchFieldException, IllegalAccessException {
617         initClientFactory();
618         initClientFactory2();
619         initDao();
620         netconfClientPlugin = new NetconfClientPluginImpl();
621         netconfClientPlugin.setDao(this.dao);
622     }
623
624     private void shortInit() throws NoSuchFieldException, IllegalAccessException {
625         initClientFactory();
626         initDao();
627         netconfClientPlugin = new NetconfClientPluginImpl();
628         netconfClientPlugin.setDao(this.dao);
629     }
630
631     private void initClientFactory() throws NoSuchFieldException, IllegalAccessException {
632         PowerMockito.mockStatic(FrameworkUtil.class);
633         when(FrameworkUtil.getBundle(Matchers.any(Class.class))).thenReturn(bundleService);
634         when(bundleService.getBundleContext()).thenReturn(bundleContext);
635         when(bundleContext.getServiceReference(NetconfClientFactory.class)).thenReturn(sref2);
636         when(bundleContext.getService(sref2)).thenReturn(clientFactory);
637     }
638
639     private void initClientFactory2() {
640         PowerMockito.mockStatic(FrameworkUtil.class);
641         when(FrameworkUtil.getBundle(Matchers.any(Class.class))).thenReturn(bundleService);
642         when(bundleService.getBundleContext()).thenReturn(bundleContext);
643         when(bundleContext.getServiceReference(Matchers.anyString())).thenReturn(sref3);
644         when(bundleContext.getService(sref3)).thenReturn(clientFactory);
645     }
646
647     private void substituteMapper(boolean command) throws NoSuchFieldException, IllegalAccessException {
648         ObjectMapper mapper = new ObjectMapperMock();
649         ObjectMapper mapper2 = new ObjectMapper();
650         Field field = NetconfClientPluginImpl.class.getDeclaredField("mapper");
651         field.setAccessible(true);
652         if (command) {
653             field.set(netconfClientPlugin, mapper);
654         } else {
655             field.set(netconfClientPlugin, mapper2);
656         }
657     }
658
659 }