f5f7811e5a873c24094b8ae46b3ad3fc3d112c17
[appc.git] / appc-dg / appc-dg-shared / appc-dg-netconf / src / test / java / org / openecomp / appc / dg / netconf / impl / NetconfDBPluginImplTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * APPC
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
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
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.
21  */
22
23 package org.openecomp.appc.dg.netconf.impl;
24
25 import com.fasterxml.jackson.databind.ObjectMapper;
26 import org.junit.*;
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;
34
35 import java.io.IOException;
36 import java.lang.reflect.Field;
37 import java.util.HashMap;
38 import java.util.Map;
39
40 import static org.powermock.api.support.SuppressCode.suppressConstructor;
41
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";
50     int port = 8080;
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" +
55             "\t<get-config>\n" +
56             "\t\t<source>\n" +
57             "\t\t\t<running/>\n" +
58             "\t\t </source>\n" +
59             "\t</get-config>\n" +
60             "</rpc>'";
61
62
63     @Test
64     public void testRetrieveDSConfiguration() throws Exception {
65         init();
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);
71
72         Assert.assertEquals("lack of success of status", "success", ctx.getStatus());
73         Assert.assertEquals("wrong config file content", configContent, ctx.getAttribute("file-content"));
74     }
75
76
77     @Test
78     public void testRetrieveDSConfigurationNegativeErrorFieldNameDaoException() throws Exception {
79         init();
80         SvcLogicContext ctx = new SvcLogicContext();
81         params = new HashMap<>();
82         params.put("configuration-file-name", "wrong");
83
84         try {
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"));
89         }
90
91
92     }
93
94     @Test
95     public void testRetrieveVMDSConfiguration() throws Exception {
96         init();
97         params = new HashMap<>();
98         params.put("resourceKey", "VNF");
99         SvcLogicContext ctx = new SvcLogicContext();
100         netconfDBPlugin.retrieveVMDSConfiguration(params, ctx);
101
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);
105     }
106
107     @Test
108     public void testRetrieveVMDSConfigurationNegativeMissingConfiguration() throws Exception {
109         init();
110         SvcLogicContext ctx = new SvcLogicContext();
111         params = new HashMap<>();
112         params.put("resourceKey", "MOCK");
113
114         try {
115             netconfDBPlugin.retrieveVMDSConfiguration(params, ctx);
116             Assert.assertTrue(false);
117         } catch (APPCException e) {
118
119             Assert.assertEquals("failure", ctx.getAttribute("retrieveVMDSConfiguration_Result"));
120         }
121     }
122
123
124     @Test
125     public void testRetrieveVMDSConfigurationNegativeJsonProcessingException() throws Exception {
126
127         SvcLogicContext ctx = new SvcLogicContext();
128         params = new HashMap<>();
129         params.put("resourceKey", "VNF");
130
131         init();
132         substituteMapper(true);
133         try {
134             netconfDBPlugin.retrieveVMDSConfiguration(params, ctx);
135             substituteMapper(false);
136             Assert.assertTrue(false);
137
138         } catch (APPCException e) {
139             substituteMapper(false);
140             Assert.assertNotNull(ctx.getAttribute(DG_OUTPUT_STATUS_MESSAGE));
141
142         }
143
144     }
145
146     @Test
147     public void testRetrieveConfigFile() throws Exception {
148         init();
149         SvcLogicContext ctx = new SvcLogicContext();
150         params = new HashMap<>();
151         params.put("configuration-file-name", "VnfGetRunningConfig");
152         netconfDBPlugin.retrieveConfigFile(params, ctx);
153
154         Assert.assertEquals("lack of success of status", "success", ctx.getStatus());
155         Assert.assertEquals("wrong config file content", configContent, ctx.getAttribute("file-content"));
156     }
157
158     @Test
159     public void testRetrieveConnectionDetails() throws Exception {
160         init();
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);
166
167         assertConnectionDetails(ctx, host1);
168     }
169
170     @Test
171     public void testRetrieveConnectionDetailsNegativeJsonProcessingException() throws Exception {
172         init();
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();
177
178         try {
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));
184         }
185
186     }
187
188
189     @Test
190     public void testRetrieveConnectionDetailsNegativeMissingConfiguration() throws Exception {
191         init();
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);
197
198         try {
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));
206         }
207
208     }
209
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());
219     }
220
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());
228
229     }
230
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;
238     }
239
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);
245         if (command) {
246             field.set(netconfDBPlugin, mapper);
247         } else {
248             field.set(netconfDBPlugin, mapper2);
249         }
250     }
251 }