c71f56d1161d78348e5ee8c2c6314e9531f3f1ce
[appc.git] / appc-inbound / appc-artifact-handler / provider / src / test / java / org / onap / appc / artifact / handler / dbservices / DBServiceTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * =============================================================================
9  * Modifications Copyright (C) 2018-2019 IBM
10  * ================================================================================
11  * Modifications Copyright (C) 2019 Ericsson
12  * ================================================================================
13  * Licensed under the Apache License, Version 2.0 (the "License");
14  * you may not use this file except in compliance with the License.
15  * You may obtain a copy of the License at
16  *
17  *      http://www.apache.org/licenses/LICENSE-2.0
18  *
19  * Unless required by applicable law or agreed to in writing, software
20  * distributed under the License is distributed on an "AS IS" BASIS,
21  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22  * See the License for the specific language governing permissions and
23  * limitations under the License.
24  *
25  * ============LICENSE_END=========================================================
26  */
27
28 package org.onap.appc.artifact.handler.dbservices;
29
30 import org.junit.Rule;
31 import org.junit.Test;
32 import org.junit.rules.ExpectedException;
33 import org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants;
34 import org.onap.ccsdk.sli.core.dblib.DbLibService;
35 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
36 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
37 import org.onap.ccsdk.sli.core.sli.SvcLogicResource.QueryStatus;
38 import static org.junit.Assert.assertEquals;
39 import static org.junit.Assert.assertNotNull;
40 import static org.junit.Assert.assertTrue;
41
42 import static org.mockito.Mockito.*;
43
44 import java.util.ArrayList;
45
46 import javax.sql.rowset.CachedRowSet;
47
48 public class DBServiceTest {
49
50     @Rule
51     public ExpectedException expectedEx = ExpectedException.none();
52
53     @Test
54     public void testSaveArtifacts() throws Exception {
55         MockDBService dbService = MockDBService.initialise();
56         SvcLogicContext ctx = new SvcLogicContext();
57         ctx.setAttribute("test", "test");
58         int internalVersion = 1;
59         dbService.saveArtifacts(ctx, internalVersion);
60     }
61
62     @Test
63     public void testLogData() throws Exception {
64         MockDBService dbService = MockDBService.initialise();
65         SvcLogicContext ctx = new SvcLogicContext();
66         ctx.setAttribute("test", "test");
67         String prefix = "test";
68         assertEquals(QueryStatus.SUCCESS, dbService.logData(ctx, prefix));
69     }
70
71     @Test
72     public void testProcessConfigActionDg() throws Exception {
73         MockDBService dbService = MockDBService.initialise();
74         SvcLogicContext ctx = new SvcLogicContext();
75         ctx.setAttribute("test", "test");
76         boolean isUpdate = true;
77         ctx.setAttribute(SdcArtifactHandlerConstants.DOWNLOAD_DG_REFERENCE, "Reference");
78         dbService.processConfigActionDg(ctx, isUpdate);
79     }
80
81
82     @Test
83     public void testGetModelDataInformationbyArtifactName() throws Exception {
84         MockDBService dbService = MockDBService.initialise();
85         SvcLogicContext ctx = new SvcLogicContext();
86         ctx.setAttribute("test", "test");
87         String artifactName = "test";
88         assertNotNull(dbService.getModelDataInformationbyArtifactName(artifactName));
89     }
90
91     @Test
92     public void testUpdateYangContents() throws Exception {
93         MockDBService dbService = MockDBService.initialise();
94         SvcLogicContext ctx = new SvcLogicContext();
95         ctx.setAttribute("test", "test");
96         String artifactId = "TestArtifact";
97         String yangContents = "TestYangContents";
98         dbService.updateYangContents(ctx, artifactId, yangContents);
99     }
100
101     @Test
102     public void testInsertProtocolReference() throws Exception {
103         MockDBService dbService = MockDBService.initialise();
104         SvcLogicContext ctx = new SvcLogicContext();
105         ctx.setAttribute("test", "test");
106         String vnfType = "testVnf";
107         String protocol = "testProtocol";
108         String action = "testAction";
109         String actionLevel = "testActionLevel";
110         String template = "testTemplateData";
111         dbService.insertProtocolReference(ctx, vnfType, protocol, action, actionLevel, template);
112     }
113
114
115     @Test
116     public void testInsertProtocolReferenceException() throws Exception {
117         MockDBService dbService = MockDBService.initialise();
118         SvcLogicContext ctx = new SvcLogicContext();
119         ctx.setAttribute("test", "test");
120         String vnfType = "testVnf";
121         String protocol = "testProtocol";
122         String action = "testAction";
123         String actionLevel = "testActionLevel";
124         String template = "testTemplateData";
125         dbService.insertProtocolReference(ctx, vnfType, protocol, action, actionLevel, template);
126     }
127
128     @Test
129     public void testProcessDownloadDGReference() throws Exception {
130         MockDBService dbService = MockDBService.initialise();
131         SvcLogicContext ctx = new SvcLogicContext();
132         ctx.setAttribute("test", "test");
133         boolean isUpdate = true;
134         dbService.processDownloadDgReference(ctx, isUpdate);
135     }
136
137     @Test
138     public void testProcessVnfcReference() throws Exception {
139         MockDBService dbService = MockDBService.initialise();
140         SvcLogicContext ctx = new SvcLogicContext();
141         ctx.setAttribute("test", "test");
142         boolean isUpdate = false;
143         dbService.processVnfcReference(ctx, isUpdate);
144     }
145
146     @Test
147     public void testProcessDeviceAuthentication() throws Exception {
148         MockDBService dbService = MockDBService.initialise();
149         SvcLogicContext ctx = new SvcLogicContext();
150         ctx.setAttribute("test", "test");
151         ctx.setAttribute("url", "");
152         String expectedKey ="update DEVICE_AUTHENTICATION set USER_NAME = $user-name , PORT_NUMBER = $port-number , URL = $url  where VNF_TYPE = $vnf-type  AND PROTOCOL = $device-protocol AND  ACTION = $action";
153         boolean isUpdate = true;
154         dbService.processDeviceAuthentication(ctx, isUpdate);
155         assertEquals(expectedKey,ctx.getAttribute("keys"));
156     }
157
158     @Test
159     public void testProcessDeviceAuthenticationforFalse() throws Exception {
160         MockDBService dbService = MockDBService.initialise();
161         SvcLogicContext ctx = new SvcLogicContext();
162         ctx.setAttribute("test", "test");
163         ctx.setAttribute("url", "");
164         boolean isUpdate = false;
165         dbService.processDeviceAuthentication(ctx, isUpdate);
166         assertEquals(true,ctx.getAttribute("keys").contains("DEVICE_AUTHENTICATION"));
167     }
168
169     @Test
170     public void testProcessDeviceInterfaceProtocol() throws Exception {
171         DbLibService mockDbLibService = mock(DbLibService.class);
172         DBService dbService = new DBService(mockDbLibService);
173         SvcLogicContext ctx = new SvcLogicContext();
174         ctx.setAttribute(SdcArtifactHandlerConstants.DEVICE_PROTOCOL, "testDeviceProtocol");
175         ctx.setAttribute(SdcArtifactHandlerConstants.VNF_TYPE, "testVnfType");
176         boolean isUpdate = true;
177         String expectedStatement = "update DEVICE_INTERFACE_PROTOCOL set PROTOCOL = ?"
178                 +" , DG_RPC = 'getDeviceRunningConfig'"
179                 + " , MODULE = 'APPC' " + "where VNF_TYPE = ? ";
180         ArrayList<String> expectedArguments = new ArrayList<>();
181         expectedArguments.add("testDeviceProtocol");
182         expectedArguments.add("testVnfType");
183         when(mockDbLibService.writeData(any(), any(), any())).thenReturn(true);
184         dbService.processDeviceInterfaceProtocol(ctx, isUpdate);
185         verify(mockDbLibService,times(1)).writeData(expectedStatement, expectedArguments, null);
186         
187     }
188
189     @Test
190     public void testProcessDeviceInterfaceProtocolForFalse() throws Exception {
191         MockDBService dbService = MockDBService.initialise();
192         SvcLogicContext ctx = new SvcLogicContext();
193         ctx.setAttribute("test", "test");
194         boolean isUpdate = false;
195         dbService.processDeviceInterfaceProtocol(ctx, isUpdate);
196         assertEquals(true,ctx.getAttribute("keys").contains("DEVICE_INTERFACE_PROTOCOL"));
197     }
198
199     @Test
200     public void testProcessSdcReferences() throws Exception {
201         DbLibService mockDbLibService = mock(DbLibService.class);
202         DBService dbService = new DBService(mockDbLibService);
203         SvcLogicContext ctx = new SvcLogicContext();
204         ctx.setAttribute(SdcArtifactHandlerConstants.ARTIFACT_NAME, "testArtifactName");
205         ctx.setAttribute(SdcArtifactHandlerConstants.VNF_TYPE, "testVnfType");
206         ctx.setAttribute(SdcArtifactHandlerConstants.VNFC_TYPE, "testVnfcType");
207         ctx.setAttribute(SdcArtifactHandlerConstants.FILE_CATEGORY, "testFileCategory");
208         ctx.setAttribute(SdcArtifactHandlerConstants.ACTION, "testAction");
209         String expectedStatement = "update ASDC_REFERENCE set ARTIFACT_NAME = ? where VNFC_TYPE = ? "
210                 + "and FILE_CATEGORY = ? and ACTION = ? and VNF_TYPE = ? AND ARTIFACT_NAME like ? ";
211         ArrayList<String> expectedArguments = new ArrayList<>();
212         expectedArguments.add("testArtifactName");
213         expectedArguments.add("testVnfcType");
214         expectedArguments.add("testFileCategory");
215         expectedArguments.add("testAction");
216         expectedArguments.add("testVnfType");
217         expectedArguments.add("%_testModelId.%");
218         when(mockDbLibService.writeData(any(), any(), any())).thenReturn(true);
219         CachedRowSet crs = mock(CachedRowSet.class);
220         when(crs.next()).thenReturn(false);
221         when(mockDbLibService.getData(any(), any(), any())).thenReturn(crs);
222         dbService.processSdcReferences(ctx, true, "testModelId");
223         verify(mockDbLibService,times(1)).writeData(expectedStatement, expectedArguments, null);
224     }
225
226     @Test
227     public void testIsArtifactUpdateRequired() throws Exception {
228         DbLibService mockDbLibService = mock(DbLibService.class);
229         DBService dbService = new DBService(mockDbLibService);
230         SvcLogicContext ctx = new SvcLogicContext();
231         ctx.setAttribute(SdcArtifactHandlerConstants.DEVICE_PROTOCOL, "testDeviceProtocol");
232         ctx.setAttribute(SdcArtifactHandlerConstants.VNF_TYPE, "testVnfType");
233         ctx.setAttribute(SdcArtifactHandlerConstants.VNFC_TYPE, "testVnfcType");
234         ctx.setAttribute(SdcArtifactHandlerConstants.FILE_CATEGORY, "testFileCategory");
235         ctx.setAttribute(SdcArtifactHandlerConstants.ACTION, "testAction");
236         String db = SdcArtifactHandlerConstants.DB_SDC_REFERENCE;
237         String expectedStatement = "select COUNT(*) from ASDC_REFERENCE where VNF_TYPE = ? and VNFC_TYPE = ?"
238                 + " and FILE_CATEGORY = ? and ACTION = ? AND ARTIFACT_NAME like ? ";
239         ArrayList<String> expectedArguments = new ArrayList<>();
240         expectedArguments.add("testVnfType");
241         expectedArguments.add("testVnfcType");
242         expectedArguments.add("testFileCategory");
243         expectedArguments.add("testAction");
244         expectedArguments.add("%_testModelId.%");
245         when(mockDbLibService.writeData(any(), any(), any())).thenReturn(true);
246         CachedRowSet crs = mock(CachedRowSet.class);
247         when(crs.next()).thenReturn(false);
248         when(mockDbLibService.getData(any(), any(), any())).thenReturn(crs);
249         dbService.isArtifactUpdateRequired(ctx, db, "testModelId");
250         verify(mockDbLibService,times(1)).getData(expectedStatement, expectedArguments, null);
251     }
252
253     @Test
254     public void testgetArtifactID() throws Exception {
255         MockDBService dbService = MockDBService.initialise();
256         SvcLogicContext ctx = new SvcLogicContext();
257         ctx.setAttribute("test", "test");
258         String db = "db";
259         dbService.getArtifactID(ctx, db);
260     }
261
262     @Test
263     public void testGetDownLoadDGReference() throws Exception {
264         MockDBService dbService = MockDBService.initialise();
265         SvcLogicContext ctx = new SvcLogicContext();
266         ctx.setAttribute("test", "test");
267         ctx.setAttribute(SdcArtifactHandlerConstants.DEVICE_PROTOCOL, "CLI");
268         assertEquals("TestDG", dbService.getDownLoadDGReference(ctx));
269     }
270
271     @Test
272     public void testGetInternalVersionNumberException() throws SvcLogicException {
273         MockDBService dbService = MockDBService.initialise(true);
274         SvcLogicContext ctx = new SvcLogicContext();
275         expectedEx.expect(SvcLogicException.class);
276         expectedEx.expectMessage("Error - getting internal Artifact Number");
277         dbService.getInternalVersionNumber(ctx, "artifactName", "prefix");
278     }
279
280     @Test
281     public void testGetArtifactIDException() throws SvcLogicException {
282         MockDBService dbService = MockDBService.initialise(true);
283         SvcLogicContext ctx = new SvcLogicContext();
284         expectedEx.expect(SvcLogicException.class);
285         expectedEx.expectMessage("Error - getting  Artifact ID from database");
286         dbService.getArtifactID(ctx, "artifactName");
287     }
288
289     @Test
290     public void testSaveArtifactsException() throws Exception {
291         MockDBService dbService = MockDBService.initialise(true);
292         SvcLogicContext ctx = new SvcLogicContext();
293         expectedEx.expect(SvcLogicException.class);
294         expectedEx.expectMessage("Error While processing storing Artifact: null");
295         dbService.saveArtifacts(ctx, -1);
296     }
297
298     @Test
299     public void testLogDataException() throws Exception {
300         MockDBService dbService = MockDBService.initialise(true);
301         SvcLogicContext ctx = new SvcLogicContext();
302         expectedEx.expect(SvcLogicException.class);
303         expectedEx.expectMessage("Error while logging data");
304         dbService.logData(ctx, null);
305     }
306
307     @Test
308     public void testProcessSdcReferencesException() throws Exception {
309         MockDBService dbService = MockDBService.initialise(true);
310         SvcLogicContext ctx = new SvcLogicContext();
311         ctx.setAttribute("file-category", "test");
312         expectedEx.expect(SvcLogicException.class);
313         expectedEx.expectMessage("Error While processing sdc_reference table ");
314         dbService.processSdcReferences(ctx, false);
315     }
316
317     @Test
318     public void testIsArtifactUpdateRequiredException() throws Exception {
319         MockDBService dbService = MockDBService.initialise(true);
320         SvcLogicContext ctx = new SvcLogicContext();
321         expectedEx.expect(DBException.class);
322         expectedEx.expectMessage("An error occurred while checking for artifact update");
323         dbService.isArtifactUpdateRequired(ctx, "db");
324     }
325
326     @Test
327     public void testProcessDeviceInterfaceProtocolException() throws Exception {
328         MockDBService dbService = MockDBService.initialise(true);
329         SvcLogicContext ctx = new SvcLogicContext();
330         expectedEx.expect(SvcLogicException.class);
331         expectedEx.expectMessage("Error While processing DEVICE_INTERFACE_PROTOCOL table ");
332         dbService.processDeviceInterfaceProtocol(ctx, false);
333     }
334
335     @Test
336     public void testProcessDeviceAuthenticationException() throws Exception {
337         MockDBService dbService = MockDBService.initialise(true);
338         SvcLogicContext ctx = new SvcLogicContext();
339         boolean isUpdate = true;
340         expectedEx.expect(DBException.class);
341         expectedEx.expectMessage("An error occurred when processing device authentication");
342         dbService.processDeviceAuthentication(ctx, isUpdate);
343     }
344
345     @Test
346     public void testProcessVnfcReferenceException() throws Exception {
347         MockDBService dbService = MockDBService.initialise(true);
348         SvcLogicContext ctx = new SvcLogicContext();
349         ctx.setAttribute("test", "test");
350         expectedEx.expect(SvcLogicException.class);
351         expectedEx.expectMessage("Error While processing VNFC_REFERENCE table ");
352         dbService.processVnfcReference(ctx, true);
353     }
354
355     @Test
356     public void testProcessDownloadDGReferenceException() throws Exception {
357         MockDBService dbService = MockDBService.initialise(true);
358         SvcLogicContext ctx = new SvcLogicContext();
359         ctx.setAttribute("test", "test");
360         expectedEx.expect(SvcLogicException.class);
361         expectedEx.expectMessage("Error While processing DOWNLOAD_DG_REFERENCE table ");
362         dbService.processDownloadDgReference(ctx, false);
363     }
364
365     @Test
366     public void testProcessConfigActionDgException() throws Exception {
367         MockDBService dbService = MockDBService.initialise(true);
368         SvcLogicContext ctx = new SvcLogicContext();
369         ctx.setAttribute("test", "test");
370         ctx.setAttribute(SdcArtifactHandlerConstants.DOWNLOAD_DG_REFERENCE, "Reference");
371         expectedEx.expect(SvcLogicException.class);
372         expectedEx.expectMessage("Error While processing Configure DG Action table ");
373         dbService.processConfigActionDg(ctx, true);
374     }
375
376     @Test
377     public void testGetModelDataInformationbyArtifactNameException() throws Exception {
378         MockDBService dbService = MockDBService.initialise(true);
379         SvcLogicContext ctx = new SvcLogicContext();
380         ctx.setAttribute("test", "test");
381         expectedEx.expect(SvcLogicException.class);
382         expectedEx.expectMessage("Error While processing is ArtifactUpdateRequiredforPD table ");
383         dbService.getModelDataInformationbyArtifactName("test");
384     }
385
386     @Test
387     public void testUpdateYangContentsException() throws Exception {
388         MockDBService dbService = MockDBService.initialise(true);
389         SvcLogicContext ctx = new SvcLogicContext();
390         ctx.setAttribute("test", "test");
391         String artifactId = "TestArtifact";
392         String yangContents = "TestYangContents";
393         expectedEx.expect(SvcLogicException.class);
394         expectedEx.expectMessage("Error While processing Configure DG Action table ");
395         dbService.updateYangContents(ctx, artifactId, yangContents);
396     }
397
398     @Test
399     public void testGetDownLoadDGReferenceException() throws Exception {
400         MockDBService dbService = MockDBService.initialise(true);
401         SvcLogicContext ctx = new SvcLogicContext();
402         ctx.setAttribute("test", "test");
403         ctx.setAttribute(SdcArtifactHandlerConstants.DEVICE_PROTOCOL, "CLI");
404         expectedEx.expect(DBException.class);
405         expectedEx.expectMessage("An error occurred when getting DG reference");
406         assertEquals("TestDG", dbService.getDownLoadDGReference(ctx));
407     }
408     
409     @Test
410     public void testcreateQueryListForTemplateIds() {
411         MockDBService dbService = MockDBService.initialise(true);
412         SvcLogicContext ctx = new SvcLogicContext();
413         String queryPart = dbService.createQueryListForTemplateIds("modelId", ctx);
414         String expectedQuery = " AND ARTIFACT_NAME like $model-id ";
415         String expectedAttribute = "%_modelId.%";
416         assertEquals(expectedQuery, queryPart);
417         assertEquals(expectedAttribute,ctx.getAttribute("model-id"));
418     }
419     
420     @Test
421     public void testisProtocolReferenceUpdateRequired() throws SvcLogicException {
422         MockDBService dbService = MockDBService.initialise();
423         SvcLogicContext ctx = new SvcLogicContext();
424         ctx.setAttribute("test", "test");
425         String vnfType = "testVnf";
426         String protocol = "testProtocol";
427         String action = "testAction";
428         String actionLevel = "testActionLevel";
429         String template = "testTemplateData";
430         boolean result = dbService.isProtocolReferenceUpdateRequired(ctx, vnfType, protocol, action, actionLevel, template);
431         assertTrue(result);
432     }
433     
434     @Test
435     public void testProcessConfigureActionDg() throws SvcLogicException {
436         MockDBService dbService = MockDBService.initialise(true);
437         SvcLogicContext ctx = new SvcLogicContext();
438         dbService.processConfigureActionDg(ctx, true);
439     }
440     
441     @Test
442     public void testUpdateProtocolReference() throws SvcLogicException {
443         MockDBService dbService = MockDBService.initialise();
444         SvcLogicContext ctx = new SvcLogicContext();
445         ctx.setAttribute("test", "test");
446         String vnfType = "testVnf";
447         String protocol = "testProtocol";
448         String action = "testAction";
449         String actionLevel = "testActionLevel";
450         String template = "testTemplateData";
451         dbService.updateProtocolReference(ctx, vnfType, protocol, action, actionLevel, template);
452        
453     }
454     
455 }
456