2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2019 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.
22 * ============LICENSE_END=========================================================
25 package org.onap.appc.dg.netconf.impl;
27 import com.fasterxml.jackson.databind.ObjectMapper;
28 import org.junit.Assert;
29 import org.junit.Test;
30 import org.onap.appc.adapter.netconf.ConnectionDetails;
31 import org.onap.appc.adapter.netconf.NetconfConnectionDetails;
32 import org.onap.appc.adapter.netconf.NetconfDataAccessService;
33 import org.onap.appc.adapter.netconf.exception.DataAccessException;
34 import org.onap.appc.dg.netconf.impl.NetconfDBPluginImpl;
35 import org.onap.appc.exceptions.APPCException;
36 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
38 import java.io.IOException;
39 import java.lang.reflect.Field;
40 import java.util.HashMap;
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 =
55 "<?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"
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.assertNull(ctx.getAttribute("file-content"));
93 public void testRetrieveVMDSConfiguration() throws Exception {
95 params = new HashMap<>();
96 params.put("resourceKey", "VNF");
97 SvcLogicContext ctx = new SvcLogicContext();
98 netconfDBPlugin.retrieveVMDSConfiguration(params, ctx);
100 Assert.assertEquals("lack of success of retrieveVMDSConfiguration_Result",
101 "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"));
122 public void testRetrieveVMDSConfigurationNegativeJsonProcessingException() throws Exception {
124 SvcLogicContext ctx = new SvcLogicContext();
125 params = new HashMap<>();
126 params.put("resourceKey", "VNF");
129 substituteMapper(true);
131 netconfDBPlugin.retrieveVMDSConfiguration(params, ctx);
132 substituteMapper(false);
133 Assert.assertTrue(false);
134 } catch (APPCException e) {
135 substituteMapper(false);
136 Assert.assertNotNull(ctx.getAttribute(DG_OUTPUT_STATUS_MESSAGE));
142 public void testRetrieveConfigFile() throws Exception {
144 SvcLogicContext ctx = new SvcLogicContext();
145 params = new HashMap<>();
146 params.put("configuration-file-name", "VnfGetRunningConfig");
147 netconfDBPlugin.retrieveConfigFile(params, ctx);
149 Assert.assertEquals("lack of success of status", "success", ctx.getStatus());
150 Assert.assertEquals("wrong config file content", configContent, ctx.getAttribute("file-content"));
154 public void testRetrieveConnectionDetails() throws Exception {
156 params = new HashMap<>();
157 params.put("org.onap.appc.vftype", "VNF");
158 params.put("vnf-host-ip-address", host1);
159 SvcLogicContext ctx = new SvcLogicContext();
160 netconfDBPlugin.retrieveConnectionDetails(params, ctx);
162 assertConnectionDetails(ctx, host1);
166 public void testRetrieveConnectionDetailsNegativeJsonProcessingException() throws Exception {
168 params = new HashMap<>();
169 params.put("org.onap.appc.vftype", "MOCK");
170 params.put("vnf-host-ip-address", host1);
171 SvcLogicContext ctx = new SvcLogicContext();
174 netconfDBPlugin.retrieveConnectionDetails(params, ctx);
175 Assert.assertTrue(false);
176 } catch (APPCException e) {
177 Assert.assertNull(ctx.getAttribute("connection-details"));
178 Assert.assertNotNull(ctx.getAttribute(DG_OUTPUT_STATUS_MESSAGE));
183 public void testRetrieveConnectionDetailsNegativeMissingConfiguration() throws Exception {
185 params = new HashMap<>();
186 params.put("org.onap.appc.vftype", "VNF");
187 params.put("vnf-host-ip-address", host1);
188 SvcLogicContext ctx = new SvcLogicContext();
189 substituteMapper(true);
192 netconfDBPlugin.retrieveConnectionDetails(params, ctx);
193 substituteMapper(false);
194 Assert.assertTrue(false);
195 } catch (APPCException e) {
196 substituteMapper(false);
197 Assert.assertNull(ctx.getAttribute("connection-details"));
198 Assert.assertNotNull(ctx.getAttribute(DG_OUTPUT_STATUS_MESSAGE));
203 private void assertConnectionDetails(SvcLogicContext ctx, String host) throws IOException {
204 String sConnectionDetails = ctx.getAttribute("connection-details");
205 NetconfConnectionDetails connectionDetails =
206 new ObjectMapper().readValue(sConnectionDetails, NetconfConnectionDetails.class);
207 Assert.assertEquals(host, connectionDetails.getHost());
208 Assert.assertEquals(port, connectionDetails.getPort());
209 Assert.assertEquals(username, connectionDetails.getUsername());
210 Assert.assertEquals(password, connectionDetails.getPassword());
211 Assert.assertNull(connectionDetails.getCapabilities());
212 Assert.assertNull(connectionDetails.getAdditionalProperties());
215 private void init() {
216 netconfDBPlugin = new NetconfDBPluginImpl();
217 daoService = new DAOServiceMock();
218 netconfDBPlugin.setDaoService(daoService);
219 daoMock = (DAOServiceMock) daoService;
220 daoMock.setConfigFile(configContent);
221 daoMock.setConnection(getConnectionDetails());
224 private ConnectionDetails getConnectionDetails() {
225 ConnectionDetails connectionDetails = new ConnectionDetails();
226 connectionDetails.setHost(host);
227 connectionDetails.setUsername(username);
228 connectionDetails.setPort(port);
229 connectionDetails.setPassword(password);
230 return connectionDetails;
233 private void substituteMapper(boolean command) throws NoSuchFieldException, IllegalAccessException {
234 ObjectMapper mapper = new ObjectMapperMock();
235 ObjectMapper mapper2 = new ObjectMapper();
236 Field field = NetconfDBPluginImpl.class.getDeclaredField("mapper");
237 field.setAccessible(true);
239 field.set(netconfDBPlugin, mapper);
241 field.set(netconfDBPlugin, mapper2);