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