2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * ================================================================================
9 * Modifications (C) 2019 Ericsson
10 * =============================================================================
11 * Licensed under the Apache License, Version 2.0 (the "License");
12 * you may not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
15 * http://www.apache.org/licenses/LICENSE-2.0
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS,
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
23 * ============LICENSE_END=========================================================
26 package org.onap.appc.dg.netconf.impl;
28 import com.fasterxml.jackson.databind.ObjectMapper;
29 import org.junit.Assert;
30 import org.junit.Test;
31 import org.onap.appc.adapter.netconf.ConnectionDetails;
32 import org.onap.appc.adapter.netconf.NetconfConnectionDetails;
33 import org.onap.appc.adapter.netconf.NetconfDataAccessService;
34 import org.onap.appc.adapter.netconf.exception.DataAccessException;
35 import org.onap.appc.dg.netconf.impl.NetconfDBPluginImpl;
36 import org.onap.appc.exceptions.APPCException;
37 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
39 import java.io.IOException;
40 import java.lang.reflect.Field;
41 import java.util.HashMap;
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.onap.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.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) {
117 Assert.assertEquals("failure", ctx.getAttribute("retrieveVMDSConfiguration_Result"));
123 public void testRetrieveVMDSConfigurationNegativeJsonProcessingException() throws Exception {
125 SvcLogicContext ctx = new SvcLogicContext();
126 params = new HashMap<>();
127 params.put("resourceKey", "VNF");
130 substituteMapper(true);
132 netconfDBPlugin.retrieveVMDSConfiguration(params, ctx);
133 substituteMapper(false);
134 Assert.assertTrue(false);
135 } catch (APPCException e) {
136 substituteMapper(false);
137 Assert.assertNotNull(ctx.getAttribute(DG_OUTPUT_STATUS_MESSAGE));
143 public void testRetrieveConfigFile() throws Exception {
145 SvcLogicContext ctx = new SvcLogicContext();
146 params = new HashMap<>();
147 params.put("configuration-file-name", "VnfGetRunningConfig");
148 netconfDBPlugin.retrieveConfigFile(params, ctx);
150 Assert.assertEquals("lack of success of status", "success", ctx.getStatus());
151 Assert.assertEquals("wrong config file content", configContent, ctx.getAttribute("file-content"));
155 public void testRetrieveConnectionDetails() throws Exception {
157 params = new HashMap<>();
158 params.put("org.onap.appc.vftype", "VNF");
159 params.put("vnf-host-ip-address", host1);
160 SvcLogicContext ctx = new SvcLogicContext();
161 netconfDBPlugin.retrieveConnectionDetails(params, ctx);
163 assertConnectionDetails(ctx, host1);
167 public void testRetrieveConnectionDetailsNegativeJsonProcessingException() throws Exception {
169 params = new HashMap<>();
170 params.put("org.onap.appc.vftype", "MOCK");
171 params.put("vnf-host-ip-address", host1);
172 SvcLogicContext ctx = new SvcLogicContext();
175 netconfDBPlugin.retrieveConnectionDetails(params, ctx);
176 Assert.assertTrue(false);
177 } catch (APPCException e) {
178 Assert.assertNull(ctx.getAttribute("connection-details"));
179 Assert.assertNotNull(ctx.getAttribute(DG_OUTPUT_STATUS_MESSAGE));
185 public void testRetrieveConnectionDetailsNegativeMissingConfiguration() throws Exception {
187 params = new HashMap<>();
188 params.put("org.onap.appc.vftype", "VNF");
189 params.put("vnf-host-ip-address", host1);
190 SvcLogicContext ctx = new SvcLogicContext();
191 substituteMapper(true);
194 netconfDBPlugin.retrieveConnectionDetails(params, ctx);
195 substituteMapper(false);
196 Assert.assertTrue(false);
197 } catch (APPCException e) {
198 substituteMapper(false);
199 Assert.assertNull(ctx.getAttribute("connection-details"));
200 Assert.assertNotNull(ctx.getAttribute(DG_OUTPUT_STATUS_MESSAGE));
205 private void assertConnectionDetails(SvcLogicContext ctx, String host) throws IOException {
206 String sConnectionDetails = ctx.getAttribute("connection-details");
207 NetconfConnectionDetails connectionDetails = new ObjectMapper().readValue(sConnectionDetails, NetconfConnectionDetails.class);
208 Assert.assertEquals(host, connectionDetails.getHost());
209 Assert.assertEquals(port, connectionDetails.getPort());
210 Assert.assertEquals(username, connectionDetails.getUsername());
211 Assert.assertEquals(password, connectionDetails.getPassword());
212 Assert.assertNull(connectionDetails.getCapabilities());
213 Assert.assertNull(connectionDetails.getAdditionalProperties());
216 private void init() {
217 netconfDBPlugin = new NetconfDBPluginImpl();
218 daoService = new DAOServiceMock();
219 netconfDBPlugin.setDaoService(daoService);
220 daoMock = (DAOServiceMock) daoService;
221 daoMock.setConfigFile(configContent);
222 daoMock.setConnection(getConnectionDetails());
226 private ConnectionDetails getConnectionDetails() {
227 ConnectionDetails connectionDetails = new ConnectionDetails();
228 connectionDetails.setHost(host);
229 connectionDetails.setUsername(username);
230 connectionDetails.setPort(port);
231 connectionDetails.setPassword(password);
232 return connectionDetails;
235 private void substituteMapper(boolean command) throws NoSuchFieldException, IllegalAccessException {
236 ObjectMapper mapper = new ObjectMapperMock();
237 ObjectMapper mapper2 = new ObjectMapper();
238 Field field = NetconfDBPluginImpl.class.getDeclaredField("mapper");
239 field.setAccessible(true);
241 field.set(netconfDBPlugin, mapper);
243 field.set(netconfDBPlugin, mapper2);