Test Coverage in appc-dg-netconf
[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-2018 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  * 
23  * ============LICENSE_END=========================================================
24  */
25
26 package org.onap.appc.dg.netconf.impl;
27
28 import org.onap.appc.exceptions.APPCException;
29 import com.fasterxml.jackson.databind.ObjectMapper;
30 import org.junit.Assert;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 import org.mockito.Matchers;
35 import org.mockito.Mockito;
36 import org.onap.appc.adapter.netconf.*;
37 import org.onap.appc.adapter.netconf.util.Constants;
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, 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     @Test
120     public void testConfigure() throws Exception {
121         shortInit();
122         SvcLogicContext ctx = new SvcLogicContext();
123
124         params = new HashMap<>();
125         params.put(Constants.CONNECTION_DETAILS_FIELD_NAME, connectionDetails);
126         params.put(Constants.FILE_CONTENT_FIELD_NAME, fileContent);
127
128         netconfClientPlugin.configure(params, ctx);
129
130         NetconfClientJschMock client = (NetconfClientJschMock) clientFactory.getNetconfClient(NetconfClientType.SSH);
131
132         try {
133             Assert.assertEquals("wrong configuration", fileContent, client.getConf());
134             Assert.assertEquals("wrong host", host, client.getLastConnectionDetails().getHost());
135             Assert.assertEquals("wrong port", port, client.getLastConnectionDetails().getPort());
136             Assert.assertEquals("wrong username", username, client.getLastConnectionDetails().getUsername());
137             Assert.assertEquals("wrong password", password, client.getLastConnectionDetails().getPassword());
138             Assert.assertFalse(client.isConnection());
139         } catch (Exception e) {
140             Assert.fail("failed with because of " + e.getCause());
141         }
142     }
143
144
145     @Test
146     public void testConfigureNegativeIOException() throws Exception {
147         shortInit();
148         SvcLogicContext ctx = new SvcLogicContext();
149
150         params = new HashMap<>();
151         params.put(Constants.CONNECTION_DETAILS_FIELD_NAME, "{" + connectionDetails);
152         params.put(Constants.FILE_CONTENT_FIELD_NAME, fileContent);
153         NetconfClientJschMock client = (NetconfClientJschMock) clientFactory.getNetconfClient(NetconfClientType.SSH);
154
155         try {
156             netconfClientPlugin.configure(params, ctx);
157             Assert.assertTrue(false);
158         } catch (APPCException e) {
159             Assert.assertNull(client.getLastConnectionDetails());
160             Assert.assertNull(client.getConf());
161         }
162
163     }
164
165     @Test
166     public void testOperationStateValidation() throws Exception {
167         shortInit();
168         SvcLogicContext ctx = new SvcLogicContext();
169         DAOServiceMock daoServiceMock = (DAOServiceMock) dao;
170         daoServiceMock.setConfigFile(fileContent);
171
172         NetconfClientJschMock client = (NetconfClientJschMock) clientFactory.getNetconfClient(NetconfClientType.SSH);
173         client.setAnswer(operationalState);
174
175         params = new HashMap<>();
176         params.put(Constants.VNF_TYPE_FIELD_NAME, vnfType);
177         params.put(Constants.VNF_HOST_IP_ADDRESS_FIELD_NAME, host1);
178         params.put(Constants.CONNECTION_DETAILS_FIELD_NAME, connectionDetails);
179         MockOperationalStateValidatorImpl validatorMock = new MockOperationalStateValidatorImpl();
180         validatorMock.setConfigurationFileName("VnfGetRunningConfig");
181
182         PowerMockito.mockStatic(OperationalStateValidatorFactory.class);
183         when(OperationalStateValidatorFactory.getOperationalStateValidator(Matchers.any(VnfType.class))).thenReturn(validatorMock);
184
185         netconfClientPlugin.operationStateValidation(params, ctx);
186
187         Assert.assertTrue("validation process failed", validatorMock.isValidated());
188         Assert.assertEquals(fileContent, client.getLastMessage());
189     }
190
191     @Test
192     public void testOperationStateValidationNegativeJsonProcessingNullIllegalStateException() throws Exception {
193         shortInit();
194         SvcLogicContext ctx = new SvcLogicContext();
195         DAOServiceMock daoServiceMock = (DAOServiceMock) dao;
196         daoServiceMock.setConfigFile(fileContent);
197
198         NetconfClientJschMock client = (NetconfClientJschMock) clientFactory.getNetconfClient(NetconfClientType.SSH);
199         client.setAnswer(operationalState);
200
201         params = new HashMap<>();
202         params.put(Constants.VNF_TYPE_FIELD_NAME, vnfType);
203         params.put(Constants.VNF_HOST_IP_ADDRESS_FIELD_NAME, host1);
204         params.put(Constants.CONNECTION_DETAILS_FIELD_NAME, connectionDetails);
205         MockOperationalStateValidatorImpl validatorMock = new MockOperationalStateValidatorImpl();
206         validatorMock.setConfigurationFileName("VnfGetRunningConfig");
207
208         PowerMockito.mockStatic(OperationalStateValidatorFactory.class);
209         when(OperationalStateValidatorFactory.getOperationalStateValidator(Matchers.any(VnfType.class))).thenReturn(validatorMock);
210         substituteMapper(true);
211
212         try {
213             netconfClientPlugin.operationStateValidation(params, ctx);
214             substituteMapper(false);
215         } catch (APPCException e) {
216             substituteMapper(false);
217             Assert.assertNotNull(ctx.getAttribute(DG_OUTPUT_STATUS_MESSAGE));
218             Assert.assertFalse(validatorMock.isValidated());
219             Assert.assertNull(client.getLastMessage());
220         }
221     }
222
223     @Test
224     public void testOperationStateValidationNegativeConnectionDetailsAreNullNullPointerException() throws Exception {
225         shortInit();
226         SvcLogicContext ctx = new SvcLogicContext();
227         DAOServiceMock daoServiceMock = (DAOServiceMock) dao;
228         daoServiceMock.setConfigFile(fileContent);
229
230         NetconfClientJschMock client = (NetconfClientJschMock) clientFactory.getNetconfClient(NetconfClientType.SSH);
231         client.setAnswer(operationalState);
232
233         params = new HashMap<>();
234         params.put(Constants.VNF_TYPE_FIELD_NAME, vnfType);
235         params.put(Constants.VNF_HOST_IP_ADDRESS_FIELD_NAME, host1);
236         params.put(Constants.CONNECTION_DETAILS_FIELD_NAME, null);
237         MockOperationalStateValidatorImpl validatorMock = new MockOperationalStateValidatorImpl();
238         validatorMock.setConfigurationFileName("VnfGetRunningConfig");
239
240         PowerMockito.mockStatic(OperationalStateValidatorFactory.class);
241         when(OperationalStateValidatorFactory.getOperationalStateValidator(Matchers.any(VnfType.class))).thenReturn(validatorMock);
242         ObjectMapper mapper = PowerMockito.mock(ObjectMapper.class);
243         final NetconfConnectionDetails netconfConnectionDetails = null;
244         when(mapper.readValue(Matchers.anyString(), Matchers.any(Class.class))).thenReturn(netconfConnectionDetails);
245
246         try {
247             netconfClientPlugin.operationStateValidation(params, ctx);
248             Assert.assertTrue(false);
249         } catch (APPCException e) {
250             Assert.assertNotNull(ctx.getAttribute(DG_OUTPUT_STATUS_MESSAGE));
251             Assert.assertFalse("validation process failed", validatorMock.isValidated());
252         }
253     }
254
255
256     @Test
257     public void testOperationStateValidationNegativeAppcException() throws Exception {
258         shortInit();
259         SvcLogicContext ctx = new SvcLogicContext();
260         DAOServiceMock daoServiceMock = (DAOServiceMock) dao;
261         daoServiceMock.setConfigFile(fileContent);
262
263         NetconfClientJschMock client = (NetconfClientJschMock) clientFactory.getNetconfClient(NetconfClientType.SSH);
264         client.setAnswer("wrong");
265
266         params = new HashMap<>();
267         params.put(Constants.VNF_TYPE_FIELD_NAME, vnfType);
268         params.put(Constants.VNF_HOST_IP_ADDRESS_FIELD_NAME, host1);
269         params.put(Constants.CONNECTION_DETAILS_FIELD_NAME, connectionDetails);
270         MockOperationalStateValidatorImpl validatorMock = new MockOperationalStateValidatorImpl();
271         validatorMock.setConfigurationFileName("VnfGetRunningConfig");
272
273         PowerMockito.mockStatic(OperationalStateValidatorFactory.class);
274         when(OperationalStateValidatorFactory.getOperationalStateValidator(Matchers.any(VnfType.class))).thenReturn(validatorMock);
275
276         try {
277             netconfClientPlugin.operationStateValidation(params, ctx);
278             Assert.assertTrue(false);
279         } catch (APPCException e) {
280             Assert.assertNotNull(ctx.getAttribute(DG_OUTPUT_STATUS_MESSAGE));
281             Assert.assertFalse("validation process failed", validatorMock.isValidated());
282         }
283     }
284
285
286     @Test
287     public void testOperationStateValidatioConnectionDetailsInParamsAreEmpty() throws Exception {
288         shortInit();
289         SvcLogicContext ctx = new SvcLogicContext();
290         DAOServiceMock daoServiceMock = (DAOServiceMock) dao;
291         daoServiceMock.setConfigFile(fileContent);
292
293         NetconfClientJschMock client = (NetconfClientJschMock) clientFactory.getNetconfClient(NetconfClientType.SSH);
294         client.setAnswer(operationalState);
295         ((DAOServiceMock) dao).setConnection(getConnectionDetails());
296
297         params = new HashMap<>();
298         params.put(Constants.VNF_TYPE_FIELD_NAME, vnfType);
299         params.put(Constants.VNF_HOST_IP_ADDRESS_FIELD_NAME, host1);
300         params.put(Constants.CONNECTION_DETAILS_FIELD_NAME, "");
301         MockOperationalStateValidatorImpl validatorMock = new MockOperationalStateValidatorImpl();
302         validatorMock.setConfigurationFileName("VnfGetRunningConfig");
303
304         PowerMockito.mockStatic(OperationalStateValidatorFactory.class);
305         when(OperationalStateValidatorFactory.getOperationalStateValidator(Matchers.any(VnfType.class))).thenReturn(validatorMock);
306
307         netconfClientPlugin.operationStateValidation(params, ctx);
308
309         Assert.assertTrue("validation process failed", validatorMock.isValidated());
310         Assert.assertEquals(fileContent, client.getLastMessage());
311     }
312
313     @Test
314     public void testOperationStateValidatioConnectionDetailsInParamsAreNull() throws Exception {
315         shortInit();
316         SvcLogicContext ctx = new SvcLogicContext();
317         DAOServiceMock daoServiceMock = (DAOServiceMock) dao;
318         daoServiceMock.setConfigFile(fileContent);
319
320         NetconfClientJschMock client = (NetconfClientJschMock) clientFactory.getNetconfClient(NetconfClientType.SSH);
321         client.setAnswer(operationalState);
322         ((DAOServiceMock) dao).setConnection(getConnectionDetails());
323
324         params = new HashMap<>();
325         params.put(Constants.VNF_TYPE_FIELD_NAME, vnfType);
326         params.put(Constants.VNF_HOST_IP_ADDRESS_FIELD_NAME, host1);
327         params.put(Constants.CONNECTION_DETAILS_FIELD_NAME, null);
328         MockOperationalStateValidatorImpl validatorMock = new MockOperationalStateValidatorImpl();
329         validatorMock.setConfigurationFileName("VnfGetRunningConfig");
330
331         PowerMockito.mockStatic(OperationalStateValidatorFactory.class);
332         when(OperationalStateValidatorFactory.getOperationalStateValidator(Matchers.any(VnfType.class))).thenReturn(validatorMock);
333
334         netconfClientPlugin.operationStateValidation(params, ctx);
335
336         Assert.assertTrue("validation process failed", validatorMock.isValidated());
337         Assert.assertEquals(fileContent, client.getLastMessage());
338     }
339
340
341     @Test
342     public void testBackupConfiguration() throws Exception {
343         shortInit();
344         SvcLogicContext ctx = new SvcLogicContext();
345         params = new HashMap<>();
346         params.put(Constants.CONNECTION_DETAILS_FIELD_NAME, connectionDetails);
347         NetconfClientJschMock client = (NetconfClientJschMock) clientFactory.getNetconfClient(NetconfClientType.SSH);
348         client.setConf(fileContent);
349         netconfClientPlugin.backupConfiguration(params, ctx);
350
351         DAOServiceMock mockdao = (DAOServiceMock) dao;
352         DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
353         Date date = new Date();
354         String creationDateExpected = dateFormat.format(date);
355         String creationDateActual = mockdao.getBackupConf().get("creationDate").substring(0, 10);
356
357         Assert.assertEquals("wrong configuration in db", fileContent, mockdao.getBackupConf().get("logText"));
358         Assert.assertEquals(creationDateExpected, creationDateActual);
359     }
360
361     @Test
362     public void testBackupConfigurationNegativeDgErrorFieldName() throws Exception {
363         shortInit();
364         SvcLogicContext ctx = new SvcLogicContext();
365         params = new HashMap<>();
366         params.put(Constants.CONNECTION_DETAILS_FIELD_NAME, "{" + connectionDetails);
367         NetconfClientJschMock client = (NetconfClientJschMock) clientFactory.getNetconfClient(NetconfClientType.SSH);
368         client.setConf(fileContent);
369         try {
370             netconfClientPlugin.backupConfiguration(params, ctx);
371             Assert.assertTrue(false);
372         } catch (APPCException e) {
373             Assert.assertNotNull(ctx.getAttribute(DG_OUTPUT_STATUS_MESSAGE));
374             DAOServiceMock mockdao = (DAOServiceMock) dao;
375             Assert.assertNull(mockdao.getBackupConf());
376         }
377     }
378
379     @Test
380     public void testGetConfig() throws Exception {
381         fullInit();
382         String entity = "123";
383
384         SvcLogicContext ctx = new SvcLogicContext();
385         ctx.setAttribute("entity", entity);
386
387         params = new HashMap<>();
388         params.put("conf-id", "current");
389         params.put(Constants.CONNECTION_DETAILS_FIELD_NAME, connectionDetails);
390         NetconfClientJschMock client = (NetconfClientJschMock) clientFactory.getNetconfClient(NetconfClientType.SSH);
391         client.setConf(fileContent);
392
393         netconfClientPlugin.getConfig(params, ctx);
394
395         Assert.assertEquals("Success", ctx.getAttribute("getConfig_Result"));
396         Assert.assertEquals(fileContent, ctx.getAttribute("fullConfig"));
397         Assert.assertNotNull(ctx.getAttribute(entity + ".Configuration"));
398         Assert.assertEquals(fileContent, ctx.getAttribute(entity + ".Configuration"));
399     }
400
401
402     @Test
403     public void testGetConfigNegativeConfigurationNull() throws Exception {
404         fullInit();
405         String entity = "123";
406
407         SvcLogicContext ctx = new SvcLogicContext();
408         ctx.setAttribute("entity", entity);
409
410         params = new HashMap<>();
411         params.put("conf-id", "current");
412         params.put(Constants.CONNECTION_DETAILS_FIELD_NAME, connectionDetails);
413
414         netconfClientPlugin.getConfig(params, ctx);
415
416         Assert.assertEquals("failure", ctx.getAttribute("getConfig_Result"));
417         Assert.assertNull(ctx.getAttribute("fullConfig"));
418         Assert.assertNull(ctx.getAttribute(entity + ".Configuration"));
419         Assert.assertNull(ctx.getAttribute(entity + ".Configuration"));
420     }
421
422
423     @Test
424     public void testGetConfigNegativeNotSupportedConfId() throws Exception {
425         fullInit();
426         String entity = "123";
427         SvcLogicContext ctx = new SvcLogicContext();
428         ctx.setAttribute("entity", entity);
429
430         params = new HashMap<>();
431         params.put("conf-id", "current1");
432         params.put(Constants.CONNECTION_DETAILS_FIELD_NAME, connectionDetails);
433
434         netconfClientPlugin.getConfig(params, ctx);
435
436         Assert.assertNull(ctx.getAttribute("getConfig_Result"));
437         Assert.assertNull(ctx.getAttribute("fullConfig"));
438         Assert.assertNull(ctx.getAttribute(entity + ".Configuration"));
439         Assert.assertNull(ctx.getAttribute(entity + ".Configuration"));
440     }
441
442     @Test
443     public void testGetConfigNegativeWronjJsonConnectionDetailsException() throws Exception {
444         fullInit();
445         String entity = "123";
446
447         SvcLogicContext ctx = new SvcLogicContext();
448         ctx.setAttribute("entity", entity);
449
450         params = new HashMap<>();
451         params.put("conf-id", "current");
452         params.put(Constants.CONNECTION_DETAILS_FIELD_NAME, "{" + connectionDetails);
453
454         try {
455             netconfClientPlugin.getConfig(params, ctx);
456             Assert.assertTrue(false);
457         } catch (APPCException e) {
458             Assert.assertEquals("failure", ctx.getAttribute("getConfig_Result"));
459             Assert.assertNull(ctx.getAttribute("fullConfig"));
460             Assert.assertNull(ctx.getAttribute(entity + ".Configuration"));
461             Assert.assertNull(ctx.getAttribute(entity + ".Configuration"));
462             Assert.assertNotNull(ctx.getAttribute(DG_OUTPUT_STATUS_MESSAGE));
463         }
464     }
465
466     @Test
467     public void testGetRunningConfig() throws Exception {
468         fullInit();
469         SvcLogicContext ctx = new SvcLogicContext();
470         params = new HashMap<>();
471         params.put("host-ip-address", host);
472         params.put("user-name", username);
473         params.put("password", password);
474         params.put("port-number", String.valueOf(port));
475
476         NetconfClientJschMock client = (NetconfClientJschMock) clientFactory.getNetconfClient(NetconfClientType.SSH);
477         client.setConf(fileContent);
478
479         netconfClientPlugin.getRunningConfig(params, ctx);
480
481         Assert.assertEquals("Success", ctx.getAttribute("getRunningConfig_Result"));
482         Assert.assertEquals(fileContent, ctx.getAttribute("running-config"));
483         Assert.assertEquals("success", ctx.getStatus());
484     }
485
486     @Test
487     public void testGetRunningConfigWithoutPortNumberDgErrorFieldNameException() throws Exception {
488         fullInit();
489         SvcLogicContext ctx = new SvcLogicContext();
490         params = new HashMap<>();
491         params.put("host-ip-address", host);
492         params.put("user-name", username);
493         params.put("password", password);
494
495         NetconfClientJschMock client = (NetconfClientJschMock) clientFactory.getNetconfClient(NetconfClientType.SSH);
496         client.setConf(fileContent);
497
498         try {
499             netconfClientPlugin.getRunningConfig(params, ctx);
500             Assert.assertTrue(false);
501         } catch (APPCException e) {
502             Assert.assertEquals("failure", ctx.getAttribute("getRunningConfig_Result"));
503             Assert.assertNull(ctx.getAttribute("running-config"));
504             Assert.assertNotNull(ctx.getAttribute(DG_OUTPUT_STATUS_MESSAGE));
505         }
506     }
507
508     @Test
509     public void testGetRunningConfigNegativeConfigurationNull() throws Exception {
510         fullInit();
511         SvcLogicContext ctx = new SvcLogicContext();
512         params = new HashMap<>();
513         params.put("host-ip-address", host);
514         params.put("user-name", username);
515         params.put("password", password);
516         params.put("port-number", String.valueOf(port));
517
518         netconfClientPlugin.getRunningConfig(params, ctx);
519
520         Assert.assertEquals("failure", ctx.getAttribute("getRunningConfig_Result"));
521         Assert.assertNull(ctx.getAttribute("running-config"));
522     }
523
524     @Test
525     public void testValidateMandatoryParamNegativeEmptyParamValue() throws Exception {
526         shortInit();
527         String paramName = "test";
528         String paramValue = "";
529
530         try {
531             netconfClientPlugin.validateMandatoryParam(paramName, paramValue);
532             Assert.assertTrue(false);
533         } catch (Exception e) {
534             Assert.assertTrue(true);
535         }
536     }
537
538     @Test
539     public void testRetrieveConnectionDetails() throws Exception {
540         shortInit();
541         DAOServiceMock daoServiceMock = (DAOServiceMock) dao;
542         daoServiceMock.setConfigFile(fileContent);
543         ConnectionDetails connectionDetails1 = getConnectionDetails();
544         daoServiceMock.setConnection(connectionDetails1);
545
546         NetconfConnectionDetails connectionDetailsActual = netconfClientPlugin.retrieveConnectionDetails(VnfType.VNF);
547
548         Assert.assertEquals("wrong host", connectionDetails1.getHost(), connectionDetailsActual.getHost());
549         Assert.assertEquals("wrong password", connectionDetails1.getPassword(), connectionDetailsActual.getPassword());
550         Assert.assertEquals("wrong port", connectionDetails1.getPort(), connectionDetailsActual.getPort());
551         Assert.assertEquals("wrong usename", connectionDetails1.getUsername(), connectionDetailsActual.getUsername());
552     }
553
554
555     @Test
556     public void testRetrieveConnectionDetailsNegativeMissingConfiguration() throws Exception {
557         shortInit();
558         DAOServiceMock daoServiceMock = (DAOServiceMock) dao;
559         daoServiceMock.setConfigFile(fileContent);
560         ConnectionDetails connectionDetails1 = getConnectionDetails();
561         daoServiceMock.setConnection(connectionDetails1);
562
563         NetconfConnectionDetails connectionDetailsActual = null;
564         try {
565             connectionDetailsActual = netconfClientPlugin.retrieveConnectionDetails(VnfType.MOCK);
566             Assert.assertTrue(false);
567         } catch (APPCException e) {
568             Assert.assertNull(connectionDetailsActual);
569         }
570     }
571
572     @Test
573     public void testRetrieveConfigurationFileContent() throws Exception {
574         shortInit();
575
576         DAOServiceMock daoServiceMock = (DAOServiceMock) dao;
577         daoServiceMock.setConfigFile(fileContent);
578
579         Assert.assertEquals("wrong config in a database", fileContent, netconfClientPlugin.retrieveConfigurationFileContent("VnfGetRunningConfig"));
580     }
581
582     private ConnectionDetails getConnectionDetails() {
583         ConnectionDetails connectionDetails = new ConnectionDetails();
584         connectionDetails.setPassword(password);
585         connectionDetails.setPort(port);
586         connectionDetails.setUsername(username);
587         connectionDetails.setHost(host);
588         return connectionDetails;
589     }
590
591
592     private void initDao() throws NoSuchFieldException, IllegalAccessException {
593         dao = new DAOServiceMock();
594         PowerMockito.mockStatic(FrameworkUtil.class);
595         when(FrameworkUtil.getBundle(Matchers.any(Class.class))).thenReturn(bundleService);
596         when(bundleService.getBundleContext()).thenReturn(bundleContext);
597         when(bundleContext.getServiceReference(NetconfDataAccessService.class)).thenReturn(sref1);
598         when(bundleContext.getService(sref1)).thenReturn(dao);
599     }
600
601     private void fullInit() throws NoSuchFieldException, IllegalAccessException {
602         initClientFactory();
603         initClientFactory2();
604         initDao();
605         netconfClientPlugin = new NetconfClientPluginImpl();
606         netconfClientPlugin.setDao(this.dao);
607     }
608
609     private void shortInit() throws NoSuchFieldException, IllegalAccessException {
610         initClientFactory();
611         initDao();
612         netconfClientPlugin = new NetconfClientPluginImpl();
613         netconfClientPlugin.setDao(this.dao);
614     }
615
616     private void initClientFactory() throws NoSuchFieldException, IllegalAccessException {
617         PowerMockito.mockStatic(FrameworkUtil.class);
618         when(FrameworkUtil.getBundle(Matchers.any(Class.class))).thenReturn(bundleService);
619         when(bundleService.getBundleContext()).thenReturn(bundleContext);
620         when(bundleContext.getServiceReference(NetconfClientFactory.class)).thenReturn(sref2);
621         when(bundleContext.getService(sref2)).thenReturn(clientFactory);
622     }
623
624     private void initClientFactory2() {
625         PowerMockito.mockStatic(FrameworkUtil.class);
626         when(FrameworkUtil.getBundle(Matchers.any(Class.class))).thenReturn(bundleService);
627         when(bundleService.getBundleContext()).thenReturn(bundleContext);
628         when(bundleContext.getServiceReference(Matchers.anyString())).thenReturn(sref3);
629         when(bundleContext.getService(sref3)).thenReturn(clientFactory);
630     }
631
632     private void substituteMapper(boolean command) throws NoSuchFieldException, IllegalAccessException {
633         ObjectMapper mapper = new ObjectMapperMock();
634         ObjectMapper mapper2 = new ObjectMapper();
635         Field field = NetconfClientPluginImpl.class.getDeclaredField("mapper");
636         field.setAccessible(true);
637         if (command) {
638             field.set(netconfClientPlugin, mapper);
639         } else {
640             field.set(netconfClientPlugin, mapper2);
641         }
642     }
643
644 }