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