2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.openecomp.appc.dg.netconf.impl;
27 import com.fasterxml.jackson.databind.ObjectMapper;
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;
37 import java.io.IOException;
38 import java.lang.reflect.Field;
39 import java.util.HashMap;
42 import static org.powermock.api.support.SuppressCode.suppressConstructor;
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";
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" +
59 "\t\t\t<running/>\n" +
66 public void testRetrieveDSConfiguration() throws Exception {
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);
74 Assert.assertEquals("lack of success of status", "success", ctx.getStatus());
75 Assert.assertEquals("wrong config file content", configContent, ctx.getAttribute("file-content"));
80 public void testRetrieveDSConfigurationNegativeErrorFieldNameDaoException() throws Exception {
82 SvcLogicContext ctx = new SvcLogicContext();
83 params = new HashMap<>();
84 params.put("configuration-file-name", "wrong");
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"));
97 public void testRetrieveVMDSConfiguration() throws Exception {
99 params = new HashMap<>();
100 params.put("resourceKey", "VNF");
101 SvcLogicContext ctx = new SvcLogicContext();
102 netconfDBPlugin.retrieveVMDSConfiguration(params, ctx);
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);
110 public void testRetrieveVMDSConfigurationNegativeMissingConfiguration() throws Exception {
112 SvcLogicContext ctx = new SvcLogicContext();
113 params = new HashMap<>();
114 params.put("resourceKey", "MOCK");
117 netconfDBPlugin.retrieveVMDSConfiguration(params, ctx);
118 Assert.assertTrue(false);
119 } catch (APPCException e) {
121 Assert.assertEquals("failure", ctx.getAttribute("retrieveVMDSConfiguration_Result"));
127 public void testRetrieveVMDSConfigurationNegativeJsonProcessingException() throws Exception {
129 SvcLogicContext ctx = new SvcLogicContext();
130 params = new HashMap<>();
131 params.put("resourceKey", "VNF");
134 substituteMapper(true);
136 netconfDBPlugin.retrieveVMDSConfiguration(params, ctx);
137 substituteMapper(false);
138 Assert.assertTrue(false);
140 } catch (APPCException e) {
141 substituteMapper(false);
142 Assert.assertNotNull(ctx.getAttribute(DG_OUTPUT_STATUS_MESSAGE));
149 public void testRetrieveConfigFile() throws Exception {
151 SvcLogicContext ctx = new SvcLogicContext();
152 params = new HashMap<>();
153 params.put("configuration-file-name", "VnfGetRunningConfig");
154 netconfDBPlugin.retrieveConfigFile(params, ctx);
156 Assert.assertEquals("lack of success of status", "success", ctx.getStatus());
157 Assert.assertEquals("wrong config file content", configContent, ctx.getAttribute("file-content"));
161 public void testRetrieveConnectionDetails() throws Exception {
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);
169 assertConnectionDetails(ctx, host1);
173 public void testRetrieveConnectionDetailsNegativeJsonProcessingException() throws Exception {
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();
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));
192 public void testRetrieveConnectionDetailsNegativeMissingConfiguration() throws Exception {
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);
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));
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());
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());
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;
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);
248 field.set(netconfDBPlugin, mapper);
250 field.set(netconfDBPlugin, mapper2);