Change nexus values to properties
[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  * openECOMP : APP-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                                              reserved.
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  */
21
22 package org.openecomp.appc.dg.netconf.impl;
23
24 import com.fasterxml.jackson.databind.ObjectMapper;
25 import org.junit.*;
26 import org.openecomp.appc.adapter.netconf.ConnectionDetails;
27 import org.openecomp.appc.adapter.netconf.NetconfConnectionDetails;
28 import org.openecomp.appc.adapter.netconf.NetconfDataAccessService;
29 import org.openecomp.appc.adapter.netconf.exception.DataAccessException;
30 import org.openecomp.appc.dg.netconf.impl.NetconfDBPluginImpl;
31 import org.openecomp.appc.exceptions.APPCException;
32 import org.openecomp.sdnc.sli.SvcLogicContext;
33
34 import java.io.IOException;
35 import java.lang.reflect.Field;
36 import java.util.HashMap;
37 import java.util.Map;
38
39 import static org.powermock.api.support.SuppressCode.suppressConstructor;
40
41 public class NetconfDBPluginImplTest {
42     private NetconfDBPluginImpl netconfDBPlugin;
43     private NetconfDataAccessService daoService;
44     private DAOServiceMock daoMock;
45     private Map<String, String> params;
46     private static final String DG_OUTPUT_STATUS_MESSAGE = "output.status.message";
47     String host = "http://www.test.com";
48     String host1 = "http://www.test1.com";
49     int port = 8080;
50     String username = "test";
51     String password = "test";
52     String configContent = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
53             "<rpc message-id=\"101\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n" +
54             "\t<get-config>\n" +
55             "\t\t<source>\n" +
56             "\t\t\t<running/>\n" +
57             "\t\t </source>\n" +
58             "\t</get-config>\n" +
59             "</rpc>'";
60
61
62     @Test
63     public void testRetrieveDSConfiguration() throws Exception {
64         init();
65         params = new HashMap<>();
66         params.put("org.openecomp.appc.vftype", "VNF");
67         params.put("configuration-file-name", "VnfGetRunningConfig");
68         SvcLogicContext ctx = new SvcLogicContext();
69         netconfDBPlugin.retrieveDSConfiguration(params, ctx);
70
71         Assert.assertEquals("lack of success of status", "success", ctx.getStatus());
72         Assert.assertEquals("wrong config file content", configContent, ctx.getAttribute("file-content"));
73     }
74
75
76     @Test
77     public void testRetrieveDSConfigurationNegativeErrorFieldNameDaoException() throws Exception {
78         init();
79         SvcLogicContext ctx = new SvcLogicContext();
80         params = new HashMap<>();
81         params.put("configuration-file-name", "wrong");
82
83         try {
84             netconfDBPlugin.retrieveDSConfiguration(params, ctx);
85         } catch (DataAccessException e) {
86             //Assert.assertNotNull(ctx.getAttribute("org.openecomp.appc.dg.error"));
87             Assert.assertNull(ctx.getAttribute("file-content"));
88         }
89
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
118             Assert.assertEquals("failure", ctx.getAttribute("retrieveVMDSConfiguration_Result"));
119         }
120     }
121
122
123     @Test
124     public void testRetrieveVMDSConfigurationNegativeJsonProcessingException() throws Exception {
125
126         SvcLogicContext ctx = new SvcLogicContext();
127         params = new HashMap<>();
128         params.put("resourceKey", "VNF");
129
130         init();
131         substituteMapper(true);
132         try {
133             netconfDBPlugin.retrieveVMDSConfiguration(params, ctx);
134             substituteMapper(false);
135             Assert.assertTrue(false);
136
137         } catch (APPCException e) {
138             substituteMapper(false);
139             Assert.assertNotNull(ctx.getAttribute(DG_OUTPUT_STATUS_MESSAGE));
140
141         }
142
143     }
144
145     @Test
146     public void testRetrieveConfigFile() throws Exception {
147         init();
148         SvcLogicContext ctx = new SvcLogicContext();
149         params = new HashMap<>();
150         params.put("configuration-file-name", "VnfGetRunningConfig");
151         netconfDBPlugin.retrieveConfigFile(params, ctx);
152
153         Assert.assertEquals("lack of success of status", "success", ctx.getStatus());
154         Assert.assertEquals("wrong config file content", configContent, ctx.getAttribute("file-content"));
155     }
156
157     @Test
158     public void testRetrieveConnectionDetails() throws Exception {
159         init();
160         params = new HashMap<>();
161         params.put("org.openecomp.appc.vftype", "VNF");
162         params.put("vnf-host-ip-address", host1);
163         SvcLogicContext ctx = new SvcLogicContext();
164         netconfDBPlugin.retrieveConnectionDetails(params, ctx);
165
166         assertConnectionDetails(ctx, host1);
167     }
168
169     @Test
170     public void testRetrieveConnectionDetailsNegativeJsonProcessingException() throws Exception {
171         init();
172         params = new HashMap<>();
173         params.put("org.openecomp.appc.vftype", "MOCK");
174         params.put("vnf-host-ip-address", host1);
175         SvcLogicContext ctx = new SvcLogicContext();
176
177         try {
178             netconfDBPlugin.retrieveConnectionDetails(params, ctx);
179             Assert.assertTrue(false);
180         } catch (APPCException e) {
181             Assert.assertNull(ctx.getAttribute("connection-details"));
182             Assert.assertNotNull(ctx.getAttribute(DG_OUTPUT_STATUS_MESSAGE));
183         }
184
185     }
186
187
188     @Test
189     public void testRetrieveConnectionDetailsNegativeMissingConfiguration() throws Exception {
190         init();
191         params = new HashMap<>();
192         params.put("org.openecomp.appc.vftype", "VNF");
193         params.put("vnf-host-ip-address", host1);
194         SvcLogicContext ctx = new SvcLogicContext();
195         substituteMapper(true);
196
197         try {
198             netconfDBPlugin.retrieveConnectionDetails(params, ctx);
199             substituteMapper(false);
200             Assert.assertTrue(false);
201         } catch (APPCException e) {
202             substituteMapper(false);
203             Assert.assertNull(ctx.getAttribute("connection-details"));
204             Assert.assertNotNull(ctx.getAttribute(DG_OUTPUT_STATUS_MESSAGE));
205         }
206
207     }
208
209     private void assertConnectionDetails(SvcLogicContext ctx, String host) throws IOException {
210         String sConnectionDetails = ctx.getAttribute("connection-details");
211         NetconfConnectionDetails connectionDetails = new ObjectMapper().readValue(sConnectionDetails, NetconfConnectionDetails.class);
212         Assert.assertEquals(host, connectionDetails.getHost());
213         Assert.assertEquals(port, connectionDetails.getPort());
214         Assert.assertEquals(username, connectionDetails.getUsername());
215         Assert.assertEquals(password, connectionDetails.getPassword());
216         Assert.assertNull(connectionDetails.getCapabilities());
217         Assert.assertNull(connectionDetails.getAdditionalProperties());
218     }
219
220     private void init() {
221         netconfDBPlugin = new NetconfDBPluginImpl();
222         daoService = new DAOServiceMock();
223         netconfDBPlugin.setDaoService(daoService);
224         daoMock = (DAOServiceMock) daoService;
225         daoMock.setConfigFile(configContent);
226         daoMock.setConnection(getConnectionDetails());
227
228     }
229
230     private ConnectionDetails getConnectionDetails() {
231         ConnectionDetails connectionDetails = new ConnectionDetails();
232         connectionDetails.setHost(host);
233         connectionDetails.setUsername(username);
234         connectionDetails.setPort(port);
235         connectionDetails.setPassword(password);
236         return connectionDetails;
237     }
238
239     private void substituteMapper(boolean command) throws NoSuchFieldException, IllegalAccessException {
240         ObjectMapper mapper = new ObjectMapperMock();
241         ObjectMapper mapper2 = new ObjectMapper();
242         Field field = NetconfDBPluginImpl.class.getDeclaredField("mapper");
243         field.setAccessible(true);
244         if (command) {
245             field.set(netconfDBPlugin, mapper);
246         } else {
247             field.set(netconfDBPlugin, mapper2);
248         }
249     }
250 }