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