2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * Copyright (C) 2017 Amdocs
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=========================================================
20 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 package org.openecomp.appc.dg.netconf.impl;
25 import com.fasterxml.jackson.databind.ObjectMapper;
27 import org.openecomp.appc.adapter.netconf.ConnectionDetails;
28 import org.openecomp.appc.adapter.netconf.NetconfConnectionDetails;
29 import org.openecomp.appc.adapter.netconf.NetconfDataAccessService;
30 import org.openecomp.appc.adapter.netconf.exception.DataAccessException;
31 import org.openecomp.appc.dg.netconf.impl.NetconfDBPluginImpl;
32 import org.openecomp.appc.exceptions.APPCException;
33 import org.openecomp.sdnc.sli.SvcLogicContext;
35 import java.io.IOException;
36 import java.lang.reflect.Field;
37 import java.util.HashMap;
40 import static org.powermock.api.support.SuppressCode.suppressConstructor;
42 public class NetconfDBPluginImplTest {
43 private NetconfDBPluginImpl netconfDBPlugin;
44 private NetconfDataAccessService daoService;
45 private DAOServiceMock daoMock;
46 private Map<String, String> params;
47 private static final String DG_OUTPUT_STATUS_MESSAGE = "output.status.message";
48 String host = "http://www.test.com";
49 String host1 = "http://www.test1.com";
51 String username = "test";
52 String password = "test";
53 String configContent = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
54 "<rpc message-id=\"101\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n" +
57 "\t\t\t<running/>\n" +
64 public void testRetrieveDSConfiguration() throws Exception {
66 params = new HashMap<>();
67 params.put("org.openecomp.appc.vftype", "VNF");
68 params.put("configuration-file-name", "VnfGetRunningConfig");
69 SvcLogicContext ctx = new SvcLogicContext();
70 netconfDBPlugin.retrieveDSConfiguration(params, ctx);
72 Assert.assertEquals("lack of success of status", "success", ctx.getStatus());
73 Assert.assertEquals("wrong config file content", configContent, ctx.getAttribute("file-content"));
78 public void testRetrieveDSConfigurationNegativeErrorFieldNameDaoException() throws Exception {
80 SvcLogicContext ctx = new SvcLogicContext();
81 params = new HashMap<>();
82 params.put("configuration-file-name", "wrong");
85 netconfDBPlugin.retrieveDSConfiguration(params, ctx);
86 } catch (DataAccessException e) {
87 //Assert.assertNotNull(ctx.getAttribute("org.openecomp.appc.dg.error"));
88 Assert.assertNull(ctx.getAttribute("file-content"));
95 public void testRetrieveVMDSConfiguration() throws Exception {
97 params = new HashMap<>();
98 params.put("resourceKey", "VNF");
99 SvcLogicContext ctx = new SvcLogicContext();
100 netconfDBPlugin.retrieveVMDSConfiguration(params, ctx);
102 Assert.assertEquals("lack of success of retrieveVMDSConfiguration_Result", "success", ctx.getAttribute("retrieveVMDSConfiguration_Result"));
103 Assert.assertEquals("wrong entity", "VNF", ctx.getAttribute("entity"));
104 assertConnectionDetails(ctx, host);
108 public void testRetrieveVMDSConfigurationNegativeMissingConfiguration() throws Exception {
110 SvcLogicContext ctx = new SvcLogicContext();
111 params = new HashMap<>();
112 params.put("resourceKey", "MOCK");
115 netconfDBPlugin.retrieveVMDSConfiguration(params, ctx);
116 Assert.assertTrue(false);
117 } catch (APPCException e) {
119 Assert.assertEquals("failure", ctx.getAttribute("retrieveVMDSConfiguration_Result"));
125 public void testRetrieveVMDSConfigurationNegativeJsonProcessingException() throws Exception {
127 SvcLogicContext ctx = new SvcLogicContext();
128 params = new HashMap<>();
129 params.put("resourceKey", "VNF");
132 substituteMapper(true);
134 netconfDBPlugin.retrieveVMDSConfiguration(params, ctx);
135 substituteMapper(false);
136 Assert.assertTrue(false);
138 } catch (APPCException e) {
139 substituteMapper(false);
140 Assert.assertNotNull(ctx.getAttribute(DG_OUTPUT_STATUS_MESSAGE));
147 public void testRetrieveConfigFile() throws Exception {
149 SvcLogicContext ctx = new SvcLogicContext();
150 params = new HashMap<>();
151 params.put("configuration-file-name", "VnfGetRunningConfig");
152 netconfDBPlugin.retrieveConfigFile(params, ctx);
154 Assert.assertEquals("lack of success of status", "success", ctx.getStatus());
155 Assert.assertEquals("wrong config file content", configContent, ctx.getAttribute("file-content"));
159 public void testRetrieveConnectionDetails() throws Exception {
161 params = new HashMap<>();
162 params.put("org.openecomp.appc.vftype", "VNF");
163 params.put("vnf-host-ip-address", host1);
164 SvcLogicContext ctx = new SvcLogicContext();
165 netconfDBPlugin.retrieveConnectionDetails(params, ctx);
167 assertConnectionDetails(ctx, host1);
171 public void testRetrieveConnectionDetailsNegativeJsonProcessingException() throws Exception {
173 params = new HashMap<>();
174 params.put("org.openecomp.appc.vftype", "MOCK");
175 params.put("vnf-host-ip-address", host1);
176 SvcLogicContext ctx = new SvcLogicContext();
179 netconfDBPlugin.retrieveConnectionDetails(params, ctx);
180 Assert.assertTrue(false);
181 } catch (APPCException e) {
182 Assert.assertNull(ctx.getAttribute("connection-details"));
183 Assert.assertNotNull(ctx.getAttribute(DG_OUTPUT_STATUS_MESSAGE));
190 public void testRetrieveConnectionDetailsNegativeMissingConfiguration() throws Exception {
192 params = new HashMap<>();
193 params.put("org.openecomp.appc.vftype", "VNF");
194 params.put("vnf-host-ip-address", host1);
195 SvcLogicContext ctx = new SvcLogicContext();
196 substituteMapper(true);
199 netconfDBPlugin.retrieveConnectionDetails(params, ctx);
200 substituteMapper(false);
201 Assert.assertTrue(false);
202 } catch (APPCException e) {
203 substituteMapper(false);
204 Assert.assertNull(ctx.getAttribute("connection-details"));
205 Assert.assertNotNull(ctx.getAttribute(DG_OUTPUT_STATUS_MESSAGE));
210 private void assertConnectionDetails(SvcLogicContext ctx, String host) throws IOException {
211 String sConnectionDetails = ctx.getAttribute("connection-details");
212 NetconfConnectionDetails connectionDetails = new ObjectMapper().readValue(sConnectionDetails, NetconfConnectionDetails.class);
213 Assert.assertEquals(host, connectionDetails.getHost());
214 Assert.assertEquals(port, connectionDetails.getPort());
215 Assert.assertEquals(username, connectionDetails.getUsername());
216 Assert.assertEquals(password, connectionDetails.getPassword());
217 Assert.assertNull(connectionDetails.getCapabilities());
218 Assert.assertNull(connectionDetails.getAdditionalProperties());
221 private void init() {
222 netconfDBPlugin = new NetconfDBPluginImpl();
223 daoService = new DAOServiceMock();
224 netconfDBPlugin.setDaoService(daoService);
225 daoMock = (DAOServiceMock) daoService;
226 daoMock.setConfigFile(configContent);
227 daoMock.setConnection(getConnectionDetails());
231 private ConnectionDetails getConnectionDetails() {
232 ConnectionDetails connectionDetails = new ConnectionDetails();
233 connectionDetails.setHost(host);
234 connectionDetails.setUsername(username);
235 connectionDetails.setPort(port);
236 connectionDetails.setPassword(password);
237 return connectionDetails;
240 private void substituteMapper(boolean command) throws NoSuchFieldException, IllegalAccessException {
241 ObjectMapper mapper = new ObjectMapperMock();
242 ObjectMapper mapper2 = new ObjectMapper();
243 Field field = NetconfDBPluginImpl.class.getDeclaredField("mapper");
244 field.setAccessible(true);
246 field.set(netconfDBPlugin, mapper);
248 field.set(netconfDBPlugin, mapper2);