2 * ============LICENSE_START=======================================================
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
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 * ============LICENSE_END=========================================================
24 package org.onap.appc.dg.netconf.impl;
26 import com.fasterxml.jackson.databind.ObjectMapper;
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;
36 import java.io.IOException;
37 import java.lang.reflect.Field;
38 import java.util.HashMap;
41 import static org.powermock.api.support.SuppressCode.suppressConstructor;
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";
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" +
58 "\t\t\t<running/>\n" +
65 public void testRetrieveDSConfiguration() throws Exception {
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);
73 Assert.assertEquals("lack of success of status", "success", ctx.getStatus());
74 Assert.assertEquals("wrong config file content", configContent, ctx.getAttribute("file-content"));
79 public void testRetrieveDSConfigurationNegativeErrorFieldNameDaoException() throws Exception {
81 SvcLogicContext ctx = new SvcLogicContext();
82 params = new HashMap<>();
83 params.put("configuration-file-name", "wrong");
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"));
96 public void testRetrieveVMDSConfiguration() throws Exception {
98 params = new HashMap<>();
99 params.put("resourceKey", "VNF");
100 SvcLogicContext ctx = new SvcLogicContext();
101 netconfDBPlugin.retrieveVMDSConfiguration(params, ctx);
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);
109 public void testRetrieveVMDSConfigurationNegativeMissingConfiguration() throws Exception {
111 SvcLogicContext ctx = new SvcLogicContext();
112 params = new HashMap<>();
113 params.put("resourceKey", "MOCK");
116 netconfDBPlugin.retrieveVMDSConfiguration(params, ctx);
117 Assert.assertTrue(false);
118 } catch (APPCException e) {
120 Assert.assertEquals("failure", ctx.getAttribute("retrieveVMDSConfiguration_Result"));
126 public void testRetrieveVMDSConfigurationNegativeJsonProcessingException() throws Exception {
128 SvcLogicContext ctx = new SvcLogicContext();
129 params = new HashMap<>();
130 params.put("resourceKey", "VNF");
133 substituteMapper(true);
135 netconfDBPlugin.retrieveVMDSConfiguration(params, ctx);
136 substituteMapper(false);
137 Assert.assertTrue(false);
139 } catch (APPCException e) {
140 substituteMapper(false);
141 Assert.assertNotNull(ctx.getAttribute(DG_OUTPUT_STATUS_MESSAGE));
148 public void testRetrieveConfigFile() throws Exception {
150 SvcLogicContext ctx = new SvcLogicContext();
151 params = new HashMap<>();
152 params.put("configuration-file-name", "VnfGetRunningConfig");
153 netconfDBPlugin.retrieveConfigFile(params, ctx);
155 Assert.assertEquals("lack of success of status", "success", ctx.getStatus());
156 Assert.assertEquals("wrong config file content", configContent, ctx.getAttribute("file-content"));
160 public void testRetrieveConnectionDetails() throws Exception {
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);
168 assertConnectionDetails(ctx, host1);
172 public void testRetrieveConnectionDetailsNegativeJsonProcessingException() throws Exception {
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();
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));
191 public void testRetrieveConnectionDetailsNegativeMissingConfiguration() throws Exception {
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);
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));
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());
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());
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;
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);
247 field.set(netconfDBPlugin, mapper);
249 field.set(netconfDBPlugin, mapper2);