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