f01a462f1fc11fd4f96ba583840c7dece55ebd6f
[appc.git] / appc-dg / appc-dg-shared / appc-dg-netconf / src / test / java / org / openecomp / appc / dg / netconf / impl / NetconfDBPluginImplTest.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.openecomp.appc.dg.netconf.impl;
26
27 import com.fasterxml.jackson.databind.ObjectMapper;
28 import org.junit.*;
29 import org.openecomp.appc.adapter.netconf.ConnectionDetails;
30 import org.openecomp.appc.adapter.netconf.NetconfConnectionDetails;
31 import org.openecomp.appc.adapter.netconf.NetconfDataAccessService;
32 import org.openecomp.appc.adapter.netconf.exception.DataAccessException;
33 import org.openecomp.appc.dg.netconf.impl.NetconfDBPluginImpl;
34 import org.openecomp.appc.exceptions.APPCException;
35 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
36
37 import java.io.IOException;
38 import java.lang.reflect.Field;
39 import java.util.HashMap;
40 import java.util.Map;
41
42 import static org.powermock.api.support.SuppressCode.suppressConstructor;
43
44 public class NetconfDBPluginImplTest {
45     private NetconfDBPluginImpl netconfDBPlugin;
46     private NetconfDataAccessService daoService;
47     private DAOServiceMock daoMock;
48     private Map<String, String> params;
49     private static final String DG_OUTPUT_STATUS_MESSAGE = "output.status.message";
50     String host = "http://www.test.com";
51     String host1 = "http://www.test1.com";
52     int port = 8080;
53     String username = "test";
54     String password = "test";
55     String configContent = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
56             "<rpc message-id=\"101\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n" +
57             "\t<get-config>\n" +
58             "\t\t<source>\n" +
59             "\t\t\t<running/>\n" +
60             "\t\t </source>\n" +
61             "\t</get-config>\n" +
62             "</rpc>'";
63
64
65     @Test
66     public void testRetrieveDSConfiguration() throws Exception {
67         init();
68         params = new HashMap<>();
69         params.put("org.openecomp.appc.vftype", "VNF");
70         params.put("configuration-file-name", "VnfGetRunningConfig");
71         SvcLogicContext ctx = new SvcLogicContext();
72         netconfDBPlugin.retrieveDSConfiguration(params, ctx);
73
74         Assert.assertEquals("lack of success of status", "success", ctx.getStatus());
75         Assert.assertEquals("wrong config file content", configContent, ctx.getAttribute("file-content"));
76     }
77
78
79     @Test
80     public void testRetrieveDSConfigurationNegativeErrorFieldNameDaoException() throws Exception {
81         init();
82         SvcLogicContext ctx = new SvcLogicContext();
83         params = new HashMap<>();
84         params.put("configuration-file-name", "wrong");
85
86         try {
87             netconfDBPlugin.retrieveDSConfiguration(params, ctx);
88         } catch (DataAccessException e) {
89             //Assert.assertNotNull(ctx.getAttribute("org.openecomp.appc.dg.error"));
90             Assert.assertNull(ctx.getAttribute("file-content"));
91         }
92
93
94     }
95
96     @Test
97     public void testRetrieveVMDSConfiguration() throws Exception {
98         init();
99         params = new HashMap<>();
100         params.put("resourceKey", "VNF");
101         SvcLogicContext ctx = new SvcLogicContext();
102         netconfDBPlugin.retrieveVMDSConfiguration(params, ctx);
103
104         Assert.assertEquals("lack of success of retrieveVMDSConfiguration_Result", "success", ctx.getAttribute("retrieveVMDSConfiguration_Result"));
105         Assert.assertEquals("wrong entity", "VNF", ctx.getAttribute("entity"));
106         assertConnectionDetails(ctx, host);
107     }
108
109     @Test
110     public void testRetrieveVMDSConfigurationNegativeMissingConfiguration() throws Exception {
111         init();
112         SvcLogicContext ctx = new SvcLogicContext();
113         params = new HashMap<>();
114         params.put("resourceKey", "MOCK");
115
116         try {
117             netconfDBPlugin.retrieveVMDSConfiguration(params, ctx);
118             Assert.assertTrue(false);
119         } catch (APPCException e) {
120
121             Assert.assertEquals("failure", ctx.getAttribute("retrieveVMDSConfiguration_Result"));
122         }
123     }
124
125
126     @Test
127     public void testRetrieveVMDSConfigurationNegativeJsonProcessingException() throws Exception {
128
129         SvcLogicContext ctx = new SvcLogicContext();
130         params = new HashMap<>();
131         params.put("resourceKey", "VNF");
132
133         init();
134         substituteMapper(true);
135         try {
136             netconfDBPlugin.retrieveVMDSConfiguration(params, ctx);
137             substituteMapper(false);
138             Assert.assertTrue(false);
139
140         } catch (APPCException e) {
141             substituteMapper(false);
142             Assert.assertNotNull(ctx.getAttribute(DG_OUTPUT_STATUS_MESSAGE));
143
144         }
145
146     }
147
148     @Test
149     public void testRetrieveConfigFile() throws Exception {
150         init();
151         SvcLogicContext ctx = new SvcLogicContext();
152         params = new HashMap<>();
153         params.put("configuration-file-name", "VnfGetRunningConfig");
154         netconfDBPlugin.retrieveConfigFile(params, ctx);
155
156         Assert.assertEquals("lack of success of status", "success", ctx.getStatus());
157         Assert.assertEquals("wrong config file content", configContent, ctx.getAttribute("file-content"));
158     }
159
160     @Test
161     public void testRetrieveConnectionDetails() throws Exception {
162         init();
163         params = new HashMap<>();
164         params.put("org.openecomp.appc.vftype", "VNF");
165         params.put("vnf-host-ip-address", host1);
166         SvcLogicContext ctx = new SvcLogicContext();
167         netconfDBPlugin.retrieveConnectionDetails(params, ctx);
168
169         assertConnectionDetails(ctx, host1);
170     }
171
172     @Test
173     public void testRetrieveConnectionDetailsNegativeJsonProcessingException() throws Exception {
174         init();
175         params = new HashMap<>();
176         params.put("org.openecomp.appc.vftype", "MOCK");
177         params.put("vnf-host-ip-address", host1);
178         SvcLogicContext ctx = new SvcLogicContext();
179
180         try {
181             netconfDBPlugin.retrieveConnectionDetails(params, ctx);
182             Assert.assertTrue(false);
183         } catch (APPCException e) {
184             Assert.assertNull(ctx.getAttribute("connection-details"));
185             Assert.assertNotNull(ctx.getAttribute(DG_OUTPUT_STATUS_MESSAGE));
186         }
187
188     }
189
190
191     @Test
192     public void testRetrieveConnectionDetailsNegativeMissingConfiguration() throws Exception {
193         init();
194         params = new HashMap<>();
195         params.put("org.openecomp.appc.vftype", "VNF");
196         params.put("vnf-host-ip-address", host1);
197         SvcLogicContext ctx = new SvcLogicContext();
198         substituteMapper(true);
199
200         try {
201             netconfDBPlugin.retrieveConnectionDetails(params, ctx);
202             substituteMapper(false);
203             Assert.assertTrue(false);
204         } catch (APPCException e) {
205             substituteMapper(false);
206             Assert.assertNull(ctx.getAttribute("connection-details"));
207             Assert.assertNotNull(ctx.getAttribute(DG_OUTPUT_STATUS_MESSAGE));
208         }
209
210     }
211
212     private void assertConnectionDetails(SvcLogicContext ctx, String host) throws IOException {
213         String sConnectionDetails = ctx.getAttribute("connection-details");
214         NetconfConnectionDetails connectionDetails = new ObjectMapper().readValue(sConnectionDetails, NetconfConnectionDetails.class);
215         Assert.assertEquals(host, connectionDetails.getHost());
216         Assert.assertEquals(port, connectionDetails.getPort());
217         Assert.assertEquals(username, connectionDetails.getUsername());
218         Assert.assertEquals(password, connectionDetails.getPassword());
219         Assert.assertNull(connectionDetails.getCapabilities());
220         Assert.assertNull(connectionDetails.getAdditionalProperties());
221     }
222
223     private void init() {
224         netconfDBPlugin = new NetconfDBPluginImpl();
225         daoService = new DAOServiceMock();
226         netconfDBPlugin.setDaoService(daoService);
227         daoMock = (DAOServiceMock) daoService;
228         daoMock.setConfigFile(configContent);
229         daoMock.setConnection(getConnectionDetails());
230
231     }
232
233     private ConnectionDetails getConnectionDetails() {
234         ConnectionDetails connectionDetails = new ConnectionDetails();
235         connectionDetails.setHost(host);
236         connectionDetails.setUsername(username);
237         connectionDetails.setPort(port);
238         connectionDetails.setPassword(password);
239         return connectionDetails;
240     }
241
242     private void substituteMapper(boolean command) throws NoSuchFieldException, IllegalAccessException {
243         ObjectMapper mapper = new ObjectMapperMock();
244         ObjectMapper mapper2 = new ObjectMapper();
245         Field field = NetconfDBPluginImpl.class.getDeclaredField("mapper");
246         field.setAccessible(true);
247         if (command) {
248             field.set(netconfDBPlugin, mapper);
249         } else {
250             field.set(netconfDBPlugin, mapper2);
251         }
252     }
253 }