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