95672194df65c8fa87f48a1e0141f756b8161453
[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-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
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  * 
23  * ============LICENSE_END=========================================================
24  */
25
26 package org.onap.appc.dg.netconf.impl;
27
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;
38
39 import java.io.IOException;
40 import java.lang.reflect.Field;
41 import java.util.HashMap;
42 import java.util.Map;
43
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";
52     int port = 8080;
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" +
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
65     @Test
66     public void testRetrieveDSConfiguration() throws Exception {
67         init();
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);
73
74         Assert.assertEquals("lack of success of status", "success", ctx.getStatus());
75         Assert.assertEquals("wrong config file content", configContent, ctx.getAttribute("file-content"));
76     }
77
78
79     @Test
80     public void testRetrieveDSConfigurationNegativeErrorFieldNameDaoException() throws Exception {
81         init();
82         SvcLogicContext ctx = new SvcLogicContext();
83         params = new HashMap<>();
84         params.put("configuration-file-name", "wrong");
85
86         try {
87             netconfDBPlugin.retrieveDSConfiguration(params, ctx);
88         } catch (DataAccessException e) {
89             Assert.assertNull(ctx.getAttribute("file-content"));
90         }
91     }
92
93     @Test
94     public void testRetrieveVMDSConfiguration() throws Exception {
95         init();
96         params = new HashMap<>();
97         params.put("resourceKey", "VNF");
98         SvcLogicContext ctx = new SvcLogicContext();
99         netconfDBPlugin.retrieveVMDSConfiguration(params, ctx);
100
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);
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
122     @Test
123     public void testRetrieveVMDSConfigurationNegativeJsonProcessingException() throws Exception {
124
125         SvcLogicContext ctx = new SvcLogicContext();
126         params = new HashMap<>();
127         params.put("resourceKey", "VNF");
128
129         init();
130         substituteMapper(true);
131         try {
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));
138         }
139
140     }
141
142     @Test
143     public void testRetrieveConfigFile() throws Exception {
144         init();
145         SvcLogicContext ctx = new SvcLogicContext();
146         params = new HashMap<>();
147         params.put("configuration-file-name", "VnfGetRunningConfig");
148         netconfDBPlugin.retrieveConfigFile(params, ctx);
149
150         Assert.assertEquals("lack of success of status", "success", ctx.getStatus());
151         Assert.assertEquals("wrong config file content", configContent, ctx.getAttribute("file-content"));
152     }
153
154     @Test
155     public void testRetrieveConnectionDetails() throws Exception {
156         init();
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);
162
163         assertConnectionDetails(ctx, host1);
164     }
165
166     @Test
167     public void testRetrieveConnectionDetailsNegativeJsonProcessingException() throws Exception {
168         init();
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();
173
174         try {
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));
180         }
181     }
182
183
184     @Test
185     public void testRetrieveConnectionDetailsNegativeMissingConfiguration() throws Exception {
186         init();
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);
192
193         try {
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));
201         }
202
203     }
204
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());
214     }
215
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());
223
224     }
225
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;
233     }
234
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);
240         if (command) {
241             field.set(netconfDBPlugin, mapper);
242         } else {
243             field.set(netconfDBPlugin, mapper2);
244         }
245     }
246 }