ece36850200a8357a4dee0869c1c13bce31dbad4
[appc.git] / appc-dg / appc-dg-shared / appc-dg-netconf / src / test / java / org / onap / appc / dg / netconf / impl / NetconfDBPluginImplTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
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
14  *
15  *      http://www.apache.org/licenses/LICENSE-2.0
16  *
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=========================================================
23  */
24
25 package org.onap.appc.dg.netconf.impl;
26
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;
37
38 import java.io.IOException;
39 import java.lang.reflect.Field;
40 import java.util.HashMap;
41 import java.util.Map;
42
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";
51     int port = 8080;
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"
57             + "\t<get-config>\n"
58             + "\t\t<source>\n"
59             + "\t\t\t<running/>\n"
60             + "\t\t </source>\n"
61             + "\t</get-config>\n"
62             + "</rpc>'";
63
64     @Test
65     public void testRetrieveDSConfiguration() throws Exception {
66         init();
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);
72
73         Assert.assertEquals("lack of success of status", "success", ctx.getStatus());
74         Assert.assertEquals("wrong config file content", configContent, ctx.getAttribute("file-content"));
75     }
76
77
78     @Test
79     public void testRetrieveDSConfigurationNegativeErrorFieldNameDaoException() throws Exception {
80         init();
81         SvcLogicContext ctx = new SvcLogicContext();
82         params = new HashMap<>();
83         params.put("configuration-file-name", "wrong");
84
85         try {
86             netconfDBPlugin.retrieveDSConfiguration(params, ctx);
87         } catch (DataAccessException e) {
88             Assert.assertNull(ctx.getAttribute("file-content"));
89         }
90     }
91
92     @Test
93     public void testRetrieveVMDSConfiguration() throws Exception {
94         init();
95         params = new HashMap<>();
96         params.put("resourceKey", "VNF");
97         SvcLogicContext ctx = new SvcLogicContext();
98         netconfDBPlugin.retrieveVMDSConfiguration(params, ctx);
99
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);
104     }
105
106     @Test
107     public void testRetrieveVMDSConfigurationNegativeMissingConfiguration() throws Exception {
108         init();
109         SvcLogicContext ctx = new SvcLogicContext();
110         params = new HashMap<>();
111         params.put("resourceKey", "MOCK");
112
113         try {
114             netconfDBPlugin.retrieveVMDSConfiguration(params, ctx);
115             Assert.assertTrue(false);
116         } catch (APPCException e) {
117             Assert.assertEquals("failure", ctx.getAttribute("retrieveVMDSConfiguration_Result"));
118         }
119     }
120
121     @Test
122     public void testRetrieveVMDSConfigurationNegativeJsonProcessingException() throws Exception {
123
124         SvcLogicContext ctx = new SvcLogicContext();
125         params = new HashMap<>();
126         params.put("resourceKey", "VNF");
127
128         init();
129         substituteMapper(true);
130         try {
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));
137         }
138
139     }
140
141     @Test
142     public void testRetrieveConfigFile() throws Exception {
143         init();
144         SvcLogicContext ctx = new SvcLogicContext();
145         params = new HashMap<>();
146         params.put("configuration-file-name", "VnfGetRunningConfig");
147         netconfDBPlugin.retrieveConfigFile(params, ctx);
148
149         Assert.assertEquals("lack of success of status", "success", ctx.getStatus());
150         Assert.assertEquals("wrong config file content", configContent, ctx.getAttribute("file-content"));
151     }
152
153     @Test
154     public void testRetrieveConnectionDetails() throws Exception {
155         init();
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);
161
162         assertConnectionDetails(ctx, host1);
163     }
164
165     @Test
166     public void testRetrieveConnectionDetailsNegativeJsonProcessingException() throws Exception {
167         init();
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();
172
173         try {
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));
179         }
180     }
181
182     @Test
183     public void testRetrieveConnectionDetailsNegativeMissingConfiguration() throws Exception {
184         init();
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);
190
191         try {
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));
199         }
200
201     }
202
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());
213     }
214
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());
222     }
223
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;
231     }
232
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);
238         if (command) {
239             field.set(netconfDBPlugin, mapper);
240         } else {
241             field.set(netconfDBPlugin, mapper2);
242         }
243     }
244 }