2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.openecomp.appc.dg.netconf.impl;
24 import com.fasterxml.jackson.databind.ObjectMapper;
26 import org.openecomp.appc.adapter.netconf.ConnectionDetails;
27 import org.openecomp.appc.adapter.netconf.NetconfConnectionDetails;
28 import org.openecomp.appc.adapter.netconf.NetconfDataAccessService;
29 import org.openecomp.appc.adapter.netconf.exception.DataAccessException;
30 import org.openecomp.appc.dg.netconf.impl.NetconfDBPluginImpl;
31 import org.openecomp.appc.exceptions.APPCException;
32 import org.openecomp.sdnc.sli.SvcLogicContext;
34 import java.io.IOException;
35 import java.lang.reflect.Field;
36 import java.util.HashMap;
39 import static org.powermock.api.support.SuppressCode.suppressConstructor;
41 public class NetconfDBPluginImplTest {
42 private NetconfDBPluginImpl netconfDBPlugin;
43 private NetconfDataAccessService daoService;
44 private DAOServiceMock daoMock;
45 private Map<String, String> params;
46 private static final String DG_OUTPUT_STATUS_MESSAGE = "output.status.message";
47 String host = "http://www.test.com";
48 String host1 = "http://www.test1.com";
50 String username = "test";
51 String password = "test";
52 String configContent = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
53 "<rpc message-id=\"101\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n" +
56 "\t\t\t<running/>\n" +
63 public void testRetrieveDSConfiguration() throws Exception {
65 params = new HashMap<>();
66 params.put("org.openecomp.appc.vftype", "VNF");
67 params.put("configuration-file-name", "VnfGetRunningConfig");
68 SvcLogicContext ctx = new SvcLogicContext();
69 netconfDBPlugin.retrieveDSConfiguration(params, ctx);
71 Assert.assertEquals("lack of success of status", "success", ctx.getStatus());
72 Assert.assertEquals("wrong config file content", configContent, ctx.getAttribute("file-content"));
77 public void testRetrieveDSConfigurationNegativeErrorFieldNameDaoException() throws Exception {
79 SvcLogicContext ctx = new SvcLogicContext();
80 params = new HashMap<>();
81 params.put("configuration-file-name", "wrong");
84 netconfDBPlugin.retrieveDSConfiguration(params, ctx);
85 } catch (DataAccessException e) {
86 //Assert.assertNotNull(ctx.getAttribute("org.openecomp.appc.dg.error"));
87 Assert.assertNull(ctx.getAttribute("file-content"));
94 public void testRetrieveVMDSConfiguration() throws Exception {
96 params = new HashMap<>();
97 params.put("resourceKey", "VNF");
98 SvcLogicContext ctx = new SvcLogicContext();
99 netconfDBPlugin.retrieveVMDSConfiguration(params, ctx);
101 Assert.assertEquals("lack of success of retrieveVMDSConfiguration_Result", "success", ctx.getAttribute("retrieveVMDSConfiguration_Result"));
102 Assert.assertEquals("wrong entity", "VNF", ctx.getAttribute("entity"));
103 assertConnectionDetails(ctx, host);
107 public void testRetrieveVMDSConfigurationNegativeMissingConfiguration() throws Exception {
109 SvcLogicContext ctx = new SvcLogicContext();
110 params = new HashMap<>();
111 params.put("resourceKey", "MOCK");
114 netconfDBPlugin.retrieveVMDSConfiguration(params, ctx);
115 Assert.assertTrue(false);
116 } catch (APPCException e) {
118 Assert.assertEquals("failure", ctx.getAttribute("retrieveVMDSConfiguration_Result"));
124 public void testRetrieveVMDSConfigurationNegativeJsonProcessingException() throws Exception {
126 SvcLogicContext ctx = new SvcLogicContext();
127 params = new HashMap<>();
128 params.put("resourceKey", "VNF");
131 substituteMapper(true);
133 netconfDBPlugin.retrieveVMDSConfiguration(params, ctx);
134 substituteMapper(false);
135 Assert.assertTrue(false);
137 } catch (APPCException e) {
138 substituteMapper(false);
139 Assert.assertNotNull(ctx.getAttribute(DG_OUTPUT_STATUS_MESSAGE));
146 public void testRetrieveConfigFile() throws Exception {
148 SvcLogicContext ctx = new SvcLogicContext();
149 params = new HashMap<>();
150 params.put("configuration-file-name", "VnfGetRunningConfig");
151 netconfDBPlugin.retrieveConfigFile(params, ctx);
153 Assert.assertEquals("lack of success of status", "success", ctx.getStatus());
154 Assert.assertEquals("wrong config file content", configContent, ctx.getAttribute("file-content"));
158 public void testRetrieveConnectionDetails() throws Exception {
160 params = new HashMap<>();
161 params.put("org.openecomp.appc.vftype", "VNF");
162 params.put("vnf-host-ip-address", host1);
163 SvcLogicContext ctx = new SvcLogicContext();
164 netconfDBPlugin.retrieveConnectionDetails(params, ctx);
166 assertConnectionDetails(ctx, host1);
170 public void testRetrieveConnectionDetailsNegativeJsonProcessingException() throws Exception {
172 params = new HashMap<>();
173 params.put("org.openecomp.appc.vftype", "MOCK");
174 params.put("vnf-host-ip-address", host1);
175 SvcLogicContext ctx = new SvcLogicContext();
178 netconfDBPlugin.retrieveConnectionDetails(params, ctx);
179 Assert.assertTrue(false);
180 } catch (APPCException e) {
181 Assert.assertNull(ctx.getAttribute("connection-details"));
182 Assert.assertNotNull(ctx.getAttribute(DG_OUTPUT_STATUS_MESSAGE));
189 public void testRetrieveConnectionDetailsNegativeMissingConfiguration() throws Exception {
191 params = new HashMap<>();
192 params.put("org.openecomp.appc.vftype", "VNF");
193 params.put("vnf-host-ip-address", host1);
194 SvcLogicContext ctx = new SvcLogicContext();
195 substituteMapper(true);
198 netconfDBPlugin.retrieveConnectionDetails(params, ctx);
199 substituteMapper(false);
200 Assert.assertTrue(false);
201 } catch (APPCException e) {
202 substituteMapper(false);
203 Assert.assertNull(ctx.getAttribute("connection-details"));
204 Assert.assertNotNull(ctx.getAttribute(DG_OUTPUT_STATUS_MESSAGE));
209 private void assertConnectionDetails(SvcLogicContext ctx, String host) throws IOException {
210 String sConnectionDetails = ctx.getAttribute("connection-details");
211 NetconfConnectionDetails connectionDetails = new ObjectMapper().readValue(sConnectionDetails, NetconfConnectionDetails.class);
212 Assert.assertEquals(host, connectionDetails.getHost());
213 Assert.assertEquals(port, connectionDetails.getPort());
214 Assert.assertEquals(username, connectionDetails.getUsername());
215 Assert.assertEquals(password, connectionDetails.getPassword());
216 Assert.assertNull(connectionDetails.getCapabilities());
217 Assert.assertNull(connectionDetails.getAdditionalProperties());
220 private void init() {
221 netconfDBPlugin = new NetconfDBPluginImpl();
222 daoService = new DAOServiceMock();
223 netconfDBPlugin.setDaoService(daoService);
224 daoMock = (DAOServiceMock) daoService;
225 daoMock.setConfigFile(configContent);
226 daoMock.setConnection(getConnectionDetails());
230 private ConnectionDetails getConnectionDetails() {
231 ConnectionDetails connectionDetails = new ConnectionDetails();
232 connectionDetails.setHost(host);
233 connectionDetails.setUsername(username);
234 connectionDetails.setPort(port);
235 connectionDetails.setPassword(password);
236 return connectionDetails;
239 private void substituteMapper(boolean command) throws NoSuchFieldException, IllegalAccessException {
240 ObjectMapper mapper = new ObjectMapperMock();
241 ObjectMapper mapper2 = new ObjectMapper();
242 Field field = NetconfDBPluginImpl.class.getDeclaredField("mapper");
243 field.setAccessible(true);
245 field.set(netconfDBPlugin, mapper);
247 field.set(netconfDBPlugin, mapper2);