Changed to unmaintained
[appc.git] / appc-config / appc-data-services / provider / src / test / java / org / onap / appc / data / services / db / TestDGGeneralDBService.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 Copyright (C) 2018-2019 IBM
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 package org.onap.appc.data.services.db;
26
27 import static org.junit.Assert.assertEquals;
28 import static org.junit.Assert.assertNull;
29 import static org.junit.Assert.assertNotNull;
30 import static org.mockito.Matchers.any;
31 import static org.mockito.Mockito.mock;
32 import static org.mockito.Mockito.times;
33 import static org.mockito.Mockito.verify;
34 import static org.mockito.Mockito.when;
35
36 import java.io.IOException;
37 import java.util.ArrayList;
38
39 import javax.sql.rowset.CachedRowSet;
40
41 import org.apache.commons.io.IOUtils;
42 import org.apache.commons.lang.StringEscapeUtils;
43 import org.junit.Test;
44 import org.onap.ccsdk.sli.core.dblib.DbLibService;
45 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
46 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
47 import org.onap.ccsdk.sli.core.sli.SvcLogicResource.QueryStatus;
48 import org.onap.appc.data.services.db.DGGeneralDBService;
49
50 public class TestDGGeneralDBService {
51     DGGeneralDBService dbService;
52     private static String STRING_ENCODING = "utf-8";
53
54     @Test
55     public void testGetUploadConfig() throws SvcLogicException {
56
57         SvcLogicContext ctx = new SvcLogicContext();
58         ctx.setAttribute("vnf-name", "test");
59         ctx.setAttribute("vnf-id", "test");
60         MockDGGeneralDBService dbService =     MockDGGeneralDBService.initialise();
61         QueryStatus status = dbService.getUploadConfigInfo(ctx, "test");
62         assertEquals(status, QueryStatus.SUCCESS);
63
64     }
65
66     @Test
67     public void testGetDeviceProtocolByVnfType() throws SvcLogicException {
68         SvcLogicContext ctx = new SvcLogicContext();
69         ctx.setAttribute("vnf-type", "test");
70         MockDGGeneralDBService dbService =     MockDGGeneralDBService.initialise();
71         QueryStatus status = dbService.getDeviceProtocolByVnfType(ctx, "test");
72         assertEquals(status, QueryStatus.SUCCESS);
73     }
74
75     @Test
76     public void testGettConfigFileReferenceByFileTypeNVnfType() throws SvcLogicException {
77         SvcLogicContext ctx = new SvcLogicContext();
78         ctx.setAttribute("vnf-type", "test");
79         MockDGGeneralDBService dbService =     MockDGGeneralDBService.initialise();
80         QueryStatus status = dbService.getConfigFileReferenceByFileTypeNVnfType(ctx, "test", "device_configuration");
81         assertEquals(status, QueryStatus.SUCCESS);
82
83     }
84
85     @Test
86     public void testGetDeviceAuthenticationByVnfType() throws Exception {
87         SvcLogicContext ctx = new SvcLogicContext();
88         ctx.setAttribute("vnf-type", "test");
89         MockDGGeneralDBService dbService =     MockDGGeneralDBService.initialise();
90         QueryStatus status = dbService.getDeviceAuthenticationByVnfType(ctx, "test");
91         assertEquals(status, QueryStatus.SUCCESS);
92
93     }
94
95     @Test
96     public void testGetTemplate() throws Exception {
97         SvcLogicContext ctx = new SvcLogicContext();
98         ctx.setAttribute("vnfc-type", "test");
99         ctx.setAttribute("request-action", "Configure");
100         MockDGGeneralDBService dbService =     MockDGGeneralDBService.initialise();
101         QueryStatus status = dbService.getTemplate(ctx, "test", "config_template");
102         assertEquals(status, QueryStatus.SUCCESS);
103
104     }
105
106     @Test
107     public void testGetTemplateByVnfTypeNAction() throws Exception {
108         SvcLogicContext ctx = new SvcLogicContext();
109         ctx.setAttribute("vnf-type", "test");
110         ctx.setAttribute("request-action", "ConfigScaleOut");
111         MockDGGeneralDBService dbService =     MockDGGeneralDBService.initialise();
112         QueryStatus status = dbService.getTemplateByVnfTypeNAction(ctx, "test", "config_template");
113         assertEquals(status, QueryStatus.SUCCESS);
114
115     }
116
117     @Test
118     public void testGetTemplateByTemplateName() throws Exception {
119         SvcLogicContext ctx = new SvcLogicContext();
120         ctx.setAttribute("request-action", "Configure");
121         ctx.setAttribute("vnf-type", "test");
122         MockDGGeneralDBService dbService =     MockDGGeneralDBService.initialise();
123         QueryStatus status = dbService.getTemplateByTemplateName(ctx, "test", "template.json");
124         assertEquals(status, QueryStatus.SUCCESS);
125
126     }
127
128     @Test
129     public void testGetTemplateByVnfType() throws SvcLogicException {
130
131         SvcLogicContext ctx = new SvcLogicContext();
132         ctx.setAttribute("vnf-type", "test");
133         MockDGGeneralDBService dbService =     MockDGGeneralDBService.initialise();
134         QueryStatus status = dbService.getTemplateByVnfType(ctx, "test", "config_template");
135         assertEquals(status, QueryStatus.SUCCESS);
136
137     }
138
139     @Test
140     public void testGetConfigureActionDGByVnfTypeNAction() throws SvcLogicException {
141
142         SvcLogicContext ctx = new SvcLogicContext();
143         ctx.setAttribute("vnf-type", "test");
144         ctx.setAttribute("request-action", "ConfigModify");
145         MockDGGeneralDBService dbService =     MockDGGeneralDBService.initialise();
146         QueryStatus status = dbService.getConfigureActionDGByVnfTypeNAction(ctx, "test");
147         assertEquals(status, QueryStatus.SUCCESS);
148
149     }
150
151     @Test
152     public void testGetConfigureActionDGByVnfType() throws SvcLogicException {
153         SvcLogicContext ctx = new SvcLogicContext();
154         ctx.setAttribute("vnf-type", "test");
155         MockDGGeneralDBService dbService =     MockDGGeneralDBService.initialise();
156         QueryStatus status = dbService.getConfigureActionDGByVnfType(ctx, "test");
157         assertEquals(status, QueryStatus.SUCCESS);
158
159     }
160
161     @Test
162     public void testGetMaxConfigFileId() throws SvcLogicException {
163
164         SvcLogicContext ctx = new SvcLogicContext();
165         ctx.setAttribute("vnf-id", "test");
166         ctx.setAttribute("vm-name", "test");
167         MockDGGeneralDBService dbService =     MockDGGeneralDBService.initialise();
168         QueryStatus status = dbService.getMaxConfigFileId(ctx, "test", "device_configuration");
169         assertEquals(status, QueryStatus.SUCCESS);
170
171     }
172
173     @Test
174     public void testGetConfigFilesByVnfVmNCategory() throws SvcLogicException {
175
176         SvcLogicContext ctx = new SvcLogicContext();
177         ctx.setAttribute("vnf-id", "test");
178         ctx.setAttribute("vm-name", "test");
179         MockDGGeneralDBService dbService =     MockDGGeneralDBService.initialise();
180         QueryStatus status = dbService.getConfigFilesByVnfVmNCategory(ctx, "test", "device_configuration", "test",
181                 "ibcx0001vm001");
182         assertEquals(status, QueryStatus.SUCCESS);
183
184     }
185
186     @Test
187     public void getVnfcReferenceByVnfcTypeNAction() throws SvcLogicException {
188
189         SvcLogicContext ctx = new SvcLogicContext();
190         ctx.setAttribute("vnf-type", "testVnf");
191         ctx.setAttribute("vnfc-type", "testVnfc");
192         ctx.setAttribute("request-action", "Configure");
193         MockDGGeneralDBService dbService =     MockDGGeneralDBService.initialise();
194         QueryStatus status = dbService.getVnfcReferenceByVnfcTypeNAction(ctx, "test");
195         assertEquals(QueryStatus.SUCCESS, status);
196     }
197     
198     @Test
199     public void testSaveConfigFiles() throws SvcLogicException {
200
201         SvcLogicContext ctx = new SvcLogicContext();
202         ctx.setAttribute("data-source", "test");
203         ctx.setAttribute("service-instance-id", "test");
204         ctx.setAttribute("request-action", "test");
205         ctx.setAttribute("vnf-type", "test");
206         ctx.setAttribute("vnfc-type", "test");
207         ctx.setAttribute("vnf-id", "test");
208         ctx.setAttribute("vnf-name", "test");
209         ctx.setAttribute("vm-name", "test");
210         ctx.setAttribute("file-category", "test");
211         ctx.setAttribute("file-content", "test");
212         
213         MockDGGeneralDBService dbService =     MockDGGeneralDBService.initialise();
214         QueryStatus status = dbService.saveConfigFiles(ctx, "test");
215         assertEquals(QueryStatus.SUCCESS, status);
216
217     }
218     
219     @Test
220     public void testSavePrepareRelationship() throws SvcLogicException {
221
222         SvcLogicContext ctx = new SvcLogicContext();
223         ctx.setAttribute("service-instance-id", "test");
224         ctx.setAttribute("request-id", "test");
225         
226         
227         MockDGGeneralDBService dbService =     MockDGGeneralDBService.initialise();
228         QueryStatus status = dbService.savePrepareRelationship(ctx, "test","test", "test");
229         assertEquals(QueryStatus.SUCCESS,status);
230
231     }
232     
233     @Test
234     public void testSavePrepareRelationshipWithSdcInd() throws SvcLogicException {
235
236         SvcLogicContext ctx = new SvcLogicContext();
237         ctx.setAttribute("service-instance-id", "test");
238         ctx.setAttribute("request-id", "test");
239         MockDGGeneralDBService dbService =     MockDGGeneralDBService.initialise();
240         QueryStatus status = dbService.savePrepareRelationship(ctx, "test", "test", "Y");
241         assertEquals(QueryStatus.SUCCESS, status);
242
243     }
244     
245     @Test
246     public void testGetDownloadConfigTemplateByVnf() throws SvcLogicException {
247
248         SvcLogicContext ctx = new SvcLogicContext();
249         ctx.setAttribute("vnf-type", "test");
250         MockDGGeneralDBService dbService =     MockDGGeneralDBService.initialise();
251         QueryStatus status = dbService.getDownloadConfigTemplateByVnf(ctx, "test");
252         assertEquals(QueryStatus.SUCCESS,status);
253     }
254
255     @Test
256     public void testSaveConfigTxLog() throws SvcLogicException, IOException {
257
258         SvcLogicContext ctx = new SvcLogicContext();
259         String message = IOUtils.toString(
260                 TestDGGeneralDBService.class.getClassLoader().getResourceAsStream("query/message3.txt"),
261                 STRING_ENCODING);
262         ctx.setAttribute("request-id", "1234");
263         String escapedMessage = StringEscapeUtils.escapeSql(message);
264         ctx.setAttribute("log-message", escapedMessage);
265         ctx.setAttribute("log-message-type", "request");
266         MockDGGeneralDBService dbService =     MockDGGeneralDBService.initialise();
267         QueryStatus status = dbService.saveConfigTransactionLog(ctx, "test");
268         assertEquals(status, QueryStatus.SUCCESS);
269     }
270
271     @Test
272     public void testGetVnfcReferenceByVnfTypeNActionWithTemplateModelId() throws Exception {
273         MockDGGeneralDBService dbService =     MockDGGeneralDBService.initialise();
274         SvcLogicContext ctx = new SvcLogicContext();
275         String prefix="test";
276         String templateModelId = "template001";
277         dbService.getVnfcReferenceByVnfTypeNActionWithTemplateModelId(ctx, prefix, templateModelId);
278         assertNotNull(dbService);
279     }
280
281     @Test
282     public void testGetTemplateWithTemplateModelId() throws Exception {
283         DbLibService mockDbLibService = mock(DbLibService.class);
284         CachedRowSet mockCachedRowSet = mock(CachedRowSet.class);
285         when(mockCachedRowSet.next()).thenReturn(false);
286         DGGeneralDBService dbService = new DGGeneralDBService(mockDbLibService);
287         SvcLogicContext ctx = new SvcLogicContext();
288         ctx.setAttribute("request-action", "testRequestAction");
289         ctx.setAttribute("vnf-type", "testVnfType");
290         ctx.setAttribute("vnfc-type", "testVnfcType");
291         String expectedStatement = "SELECT artifact_content file_content , asdc_artifacts_id config_file_id FROM ASDC_ARTIFACTS WHERE"
292                 + " asdc_artifacts_id = ( SELECT MAX(a.asdc_artifacts_id) configfileid FROM ASDC_ARTIFACTS a, ASDC_REFERENCE b"
293                 + " WHERE a.artifact_name = b.artifact_name AND file_category = ? AND action = ? AND vnf_type = ? AND vnfc_type = ? )"
294                 + " and ASDC_ARTIFACTS.artifact_name like ? ; ";
295         ArrayList<String> expectedArguments = new ArrayList<>();
296         expectedArguments.add("testFileCategory");
297         expectedArguments.add("testRequestAction");
298         expectedArguments.add("testVnfType");
299         expectedArguments.add("testVnfcType");
300         String templateModelId = "testTemplateModelId";
301         expectedArguments.add("%_"+ templateModelId +"%");
302         when(mockDbLibService.getData(any(), any(), any())).thenReturn(mockCachedRowSet);
303         dbService.getTemplateWithTemplateModelId(ctx, "testPrefix", "testFileCategory", templateModelId);
304         verify(mockDbLibService,times(1)).getData(expectedStatement, expectedArguments, null);
305     }
306
307     @Test
308     public void testgetTemplateByVnfTypeNActionWithTemplateModelId() throws Exception {
309         MockDGGeneralDBService dbService =     MockDGGeneralDBService.initialise();
310         SvcLogicContext ctx = new SvcLogicContext();
311         String prefix="test";
312         String templateModelId = "template001";
313         String fileCategory="testCategory";
314         dbService.getTemplateByVnfTypeNActionWithTemplateModelId(ctx, prefix, fileCategory, templateModelId);
315         assertNotNull(dbService);
316     }
317
318     @Test
319     public void testGetConfigFileReferenceByVnfType() throws Exception {
320         SvcLogicContext ctx = new SvcLogicContext();
321         ctx.setAttribute("vnf-type", "test");
322         ctx.setAttribute("request-action", "Configure");
323         MockDGGeneralDBService dbService =     MockDGGeneralDBService.initialise();
324         QueryStatus status = dbService.getConfigFileReferenceByVnfType(ctx, "test");
325         assertEquals(status, QueryStatus.SUCCESS);
326
327     }
328     
329     @Test
330     public void testGetTemplateByArtifactType() throws Exception {
331         SvcLogicContext ctx = new SvcLogicContext();
332         ctx.setAttribute("vnf-type", "test");
333         ctx.setAttribute("request-action", "Configure");
334         MockDGGeneralDBService dbService =     MockDGGeneralDBService.initialise();
335         QueryStatus status = dbService.getTemplateByArtifactType(ctx, "test","XML","APPC-CONFIG");
336         assertEquals(QueryStatus.SUCCESS, status);
337
338     }
339     
340     @Test
341     public void testGetCapability() throws Exception {
342         SvcLogicContext ctx = new SvcLogicContext();
343         ctx.setAttribute("vnf-type", "test");
344         ctx.setAttribute("artifactName", "template001");
345         ctx.setAttribute("maxInternalVersion", "1234");
346         MockDGGeneralDBService dbService =     MockDGGeneralDBService.initialise();
347         String status = dbService.getCapability(ctx, "test");
348         assertNull(status);
349
350     }
351     
352     @Test
353     public void testCleanContextPropertyByPrefix()
354     {
355         SvcLogicContext ctx = new SvcLogicContext();
356         ctx.setAttribute("vnf-type", "test");
357         ctx.setAttribute("request-action", "Configure");
358         ctx.setAttribute(".vnfc-type", "Configure");
359         MockDGGeneralDBService dbService =     MockDGGeneralDBService.initialise();
360         dbService.cleanContextPropertyByPrefix(ctx, "test");
361         assertEquals(2,ctx.getAttributeKeySet().size());
362     }
363     
364     @Test
365     public void testSaveUploadConfig() throws SvcLogicException
366     {
367         SvcLogicContext ctx = new SvcLogicContext();
368         ctx.setAttribute("request-id", "test");
369         ctx.setAttribute("request-action", "Configure");
370         ctx.setAttribute("originator-id", "test");
371         ctx.setAttribute("vnf-id", "test");
372         ctx.setAttribute("vm-name", "test");
373         ctx.setAttribute("vnf-host-ip-address", "10.0.0.1");
374         ctx.setAttribute("vnf-type", "test");
375         ctx.setAttribute("vnfc-type", "test");
376         ctx.setAttribute("tmp.escaped.devicerunningconfig", "10.0.0.1");
377         
378         MockDGGeneralDBService dbService =     MockDGGeneralDBService.initialise();
379         QueryStatus status= dbService.saveUploadConfig(ctx, "test");
380         assertEquals(QueryStatus.SUCCESS, status);
381     }
382     
383     @Test
384     public void testUpdateUploadConfig() throws SvcLogicException
385     {
386         SvcLogicContext ctx = new SvcLogicContext();
387         
388         ctx.setAttribute("vnf-type", "test");
389         ctx.setAttribute("vnfc-type", "test");
390         
391         MockDGGeneralDBService dbService =     MockDGGeneralDBService.initialise();
392         QueryStatus status= dbService.updateUploadConfig(ctx, "test",10);
393         assertEquals(QueryStatus.SUCCESS, status);
394     }
395     
396     @Test
397     public void testGetVnfcReferenceByVnfTypeNAction() throws SvcLogicException
398     {
399         SvcLogicContext ctx = new SvcLogicContext();
400         
401         ctx.setAttribute("vnf-type", "test");
402         ctx.setAttribute("request-action", "test");
403         
404         MockDGGeneralDBService dbService =     MockDGGeneralDBService.initialise();
405         QueryStatus status= dbService.getVnfcReferenceByVnfTypeNAction(ctx, "test");
406         assertEquals(QueryStatus.SUCCESS, status);
407     }
408 }