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