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