added assert statement in dbServiceTest.java
[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         assertNotNull(dbService);
61     }
62
63     @Test
64     public void testLogData() throws Exception {
65         MockDBService dbService = MockDBService.initialise();
66         SvcLogicContext ctx = new SvcLogicContext();
67         ctx.setAttribute("test", "test");
68         String prefix = "test";
69         assertEquals(QueryStatus.SUCCESS, dbService.logData(ctx, prefix));
70     }
71
72     @Test
73     public void testProcessConfigActionDg() throws Exception {
74         MockDBService dbService = MockDBService.initialise();
75         SvcLogicContext ctx = new SvcLogicContext();
76         ctx.setAttribute("test", "test");
77         boolean isUpdate = true;
78         ctx.setAttribute(SdcArtifactHandlerConstants.DOWNLOAD_DG_REFERENCE, "Reference");
79         dbService.processConfigActionDg(ctx, isUpdate);
80         assertNotNull(ctx);
81     }
82
83
84     @Test
85     public void testGetModelDataInformationbyArtifactName() throws Exception {
86         MockDBService dbService = MockDBService.initialise();
87         SvcLogicContext ctx = new SvcLogicContext();
88         ctx.setAttribute("test", "test");
89         String artifactName = "test";
90         assertNotNull(dbService.getModelDataInformationbyArtifactName(artifactName));
91     }
92
93     @Test
94     public void testUpdateYangContents() throws Exception {
95         MockDBService dbService = MockDBService.initialise();
96         SvcLogicContext ctx = new SvcLogicContext();
97         ctx.setAttribute("test", "test");
98         String artifactId = "TestArtifact";
99         String yangContents = "TestYangContents";
100         dbService.updateYangContents(ctx, artifactId, yangContents);
101         assertNotNull(yangContents);
102     }
103
104     @Test
105     public void testInsertProtocolReference() throws Exception {
106         MockDBService dbService = MockDBService.initialise();
107         SvcLogicContext ctx = new SvcLogicContext();
108         ctx.setAttribute("test", "test");
109         String vnfType = "testVnf";
110         String protocol = "testProtocol";
111         String action = "testAction";
112         String actionLevel = "testActionLevel";
113         String template = "testTemplateData";
114         dbService.insertProtocolReference(ctx, vnfType, protocol, action, actionLevel, template);
115         assertNotNull(template);
116     }
117
118
119     @Test
120     public void testInsertProtocolReferenceException() throws Exception {
121         MockDBService dbService = MockDBService.initialise();
122         SvcLogicContext ctx = new SvcLogicContext();
123         ctx.setAttribute("test", "test");
124         String vnfType = "testVnf";
125         String protocol = "testProtocol";
126         String action = "testAction";
127         String actionLevel = "testActionLevel";
128         String template = "testTemplateData";
129         dbService.insertProtocolReference(ctx, vnfType, protocol, action, actionLevel, template);
130         assertNotNull(action);
131     }
132
133     @Test
134     public void testProcessDownloadDGReference() throws Exception {
135         MockDBService dbService = MockDBService.initialise();
136         SvcLogicContext ctx = new SvcLogicContext();
137         ctx.setAttribute("test", "test");
138         boolean isUpdate = true;
139         dbService.processDownloadDgReference(ctx, isUpdate);
140         assertNotNull(dbService);
141     }
142
143     @Test
144     public void testProcessVnfcReference() throws Exception {
145         MockDBService dbService = MockDBService.initialise();
146         SvcLogicContext ctx = new SvcLogicContext();
147         ctx.setAttribute("test", "test");
148         boolean isUpdate = false;
149         dbService.processVnfcReference(ctx, isUpdate);
150         assertNotNull(dbService);
151     }
152
153     @Test
154     public void testProcessDeviceAuthentication() throws Exception {
155         MockDBService dbService = MockDBService.initialise();
156         SvcLogicContext ctx = new SvcLogicContext();
157         ctx.setAttribute("test", "test");
158         ctx.setAttribute("url", "");
159         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";
160         boolean isUpdate = true;
161         dbService.processDeviceAuthentication(ctx, isUpdate);
162         assertEquals(expectedKey,ctx.getAttribute("keys"));
163     }
164
165     @Test
166     public void testProcessDeviceAuthenticationforFalse() throws Exception {
167         MockDBService dbService = MockDBService.initialise();
168         SvcLogicContext ctx = new SvcLogicContext();
169         ctx.setAttribute("test", "test");
170         ctx.setAttribute("url", "");
171         boolean isUpdate = false;
172         dbService.processDeviceAuthentication(ctx, isUpdate);
173         assertEquals(true,ctx.getAttribute("keys").contains("DEVICE_AUTHENTICATION"));
174     }
175
176     @Test
177     public void testProcessDeviceInterfaceProtocol() throws Exception {
178         DbLibService mockDbLibService = mock(DbLibService.class);
179         DBService dbService = new DBService(mockDbLibService);
180         SvcLogicContext ctx = new SvcLogicContext();
181         ctx.setAttribute(SdcArtifactHandlerConstants.DEVICE_PROTOCOL, "testDeviceProtocol");
182         ctx.setAttribute(SdcArtifactHandlerConstants.VNF_TYPE, "testVnfType");
183         boolean isUpdate = true;
184         String expectedStatement = "update DEVICE_INTERFACE_PROTOCOL set PROTOCOL = ?"
185                 +" , DG_RPC = 'getDeviceRunningConfig'"
186                 + " , MODULE = 'APPC' " + "where VNF_TYPE = ? ";
187         ArrayList<String> expectedArguments = new ArrayList<>();
188         expectedArguments.add("testDeviceProtocol");
189         expectedArguments.add("testVnfType");
190         when(mockDbLibService.writeData(any(), any(), any())).thenReturn(true);
191         dbService.processDeviceInterfaceProtocol(ctx, isUpdate);
192         verify(mockDbLibService,times(1)).writeData(expectedStatement, expectedArguments, null);
193         
194     }
195
196     @Test
197     public void testProcessDeviceInterfaceProtocolForFalse() throws Exception {
198         MockDBService dbService = MockDBService.initialise();
199         SvcLogicContext ctx = new SvcLogicContext();
200         ctx.setAttribute("test", "test");
201         boolean isUpdate = false;
202         dbService.processDeviceInterfaceProtocol(ctx, isUpdate);
203         assertEquals(true,ctx.getAttribute("keys").contains("DEVICE_INTERFACE_PROTOCOL"));
204     }
205
206     @Test
207     public void testProcessSdcReferences() throws Exception {
208         DbLibService mockDbLibService = mock(DbLibService.class);
209         DBService dbService = new DBService(mockDbLibService);
210         SvcLogicContext ctx = new SvcLogicContext();
211         ctx.setAttribute(SdcArtifactHandlerConstants.ARTIFACT_NAME, "testArtifactName");
212         ctx.setAttribute(SdcArtifactHandlerConstants.VNF_TYPE, "testVnfType");
213         ctx.setAttribute(SdcArtifactHandlerConstants.VNFC_TYPE, "testVnfcType");
214         ctx.setAttribute(SdcArtifactHandlerConstants.FILE_CATEGORY, "testFileCategory");
215         ctx.setAttribute(SdcArtifactHandlerConstants.ACTION, "testAction");
216         String expectedStatement = "update ASDC_REFERENCE set ARTIFACT_NAME = ? where VNFC_TYPE = ? "
217                 + "and FILE_CATEGORY = ? and ACTION = ? and VNF_TYPE = ? AND ARTIFACT_NAME like ? ";
218         ArrayList<String> expectedArguments = new ArrayList<>();
219         expectedArguments.add("testArtifactName");
220         expectedArguments.add("testVnfcType");
221         expectedArguments.add("testFileCategory");
222         expectedArguments.add("testAction");
223         expectedArguments.add("testVnfType");
224         expectedArguments.add("%_testModelId.%");
225         when(mockDbLibService.writeData(any(), any(), any())).thenReturn(true);
226         CachedRowSet crs = mock(CachedRowSet.class);
227         when(crs.next()).thenReturn(false);
228         when(mockDbLibService.getData(any(), any(), any())).thenReturn(crs);
229         dbService.processSdcReferences(ctx, true, "testModelId");
230         verify(mockDbLibService,times(1)).writeData(expectedStatement, expectedArguments, null);
231     }
232
233     @Test
234     public void testIsArtifactUpdateRequired() throws Exception {
235         DbLibService mockDbLibService = mock(DbLibService.class);
236         DBService dbService = new DBService(mockDbLibService);
237         SvcLogicContext ctx = new SvcLogicContext();
238         ctx.setAttribute(SdcArtifactHandlerConstants.DEVICE_PROTOCOL, "testDeviceProtocol");
239         ctx.setAttribute(SdcArtifactHandlerConstants.VNF_TYPE, "testVnfType");
240         ctx.setAttribute(SdcArtifactHandlerConstants.VNFC_TYPE, "testVnfcType");
241         ctx.setAttribute(SdcArtifactHandlerConstants.FILE_CATEGORY, "testFileCategory");
242         ctx.setAttribute(SdcArtifactHandlerConstants.ACTION, "testAction");
243         String db = SdcArtifactHandlerConstants.DB_SDC_REFERENCE;
244         String expectedStatement = "select COUNT(*) from ASDC_REFERENCE where VNF_TYPE = ? and VNFC_TYPE = ?"
245                 + " and FILE_CATEGORY = ? and ACTION = ? AND ARTIFACT_NAME like ? ";
246         ArrayList<String> expectedArguments = new ArrayList<>();
247         expectedArguments.add("testVnfType");
248         expectedArguments.add("testVnfcType");
249         expectedArguments.add("testFileCategory");
250         expectedArguments.add("testAction");
251         expectedArguments.add("%_testModelId.%");
252         when(mockDbLibService.writeData(any(), any(), any())).thenReturn(true);
253         CachedRowSet crs = mock(CachedRowSet.class);
254         when(crs.next()).thenReturn(false);
255         when(mockDbLibService.getData(any(), any(), any())).thenReturn(crs);
256         dbService.isArtifactUpdateRequired(ctx, db, "testModelId");
257         verify(mockDbLibService,times(1)).getData(expectedStatement, expectedArguments, null);
258     }
259
260     @Test
261     public void testgetArtifactID() throws Exception {
262         MockDBService dbService = MockDBService.initialise();
263         SvcLogicContext ctx = new SvcLogicContext();
264         ctx.setAttribute("test", "test");
265         String db = "db";
266         dbService.getArtifactID(ctx, db);
267         assertNotNull(dbService);
268     }
269
270     @Test
271     public void testGetDownLoadDGReference() throws Exception {
272         MockDBService dbService = MockDBService.initialise();
273         SvcLogicContext ctx = new SvcLogicContext();
274         ctx.setAttribute("test", "test");
275         ctx.setAttribute(SdcArtifactHandlerConstants.DEVICE_PROTOCOL, "CLI");
276         assertEquals("TestDG", dbService.getDownLoadDGReference(ctx));
277     }
278
279     @Test
280     public void testGetInternalVersionNumberException() throws SvcLogicException {
281         MockDBService dbService = MockDBService.initialise(true);
282         SvcLogicContext ctx = new SvcLogicContext();
283         expectedEx.expect(SvcLogicException.class);
284         expectedEx.expectMessage("Error - getting internal Artifact Number");
285         dbService.getInternalVersionNumber(ctx, "artifactName", "prefix");
286     }
287
288     @Test
289     public void testGetArtifactIDException() throws SvcLogicException {
290         MockDBService dbService = MockDBService.initialise(true);
291         SvcLogicContext ctx = new SvcLogicContext();
292         expectedEx.expect(SvcLogicException.class);
293         expectedEx.expectMessage("Error - getting  Artifact ID from database");
294         dbService.getArtifactID(ctx, "artifactName");
295     }
296
297     @Test
298     public void testSaveArtifactsException() throws Exception {
299         MockDBService dbService = MockDBService.initialise(true);
300         SvcLogicContext ctx = new SvcLogicContext();
301         expectedEx.expect(SvcLogicException.class);
302         expectedEx.expectMessage("Error While processing storing Artifact: null");
303         dbService.saveArtifacts(ctx, -1);
304     }
305
306     @Test
307     public void testLogDataException() throws Exception {
308         MockDBService dbService = MockDBService.initialise(true);
309         SvcLogicContext ctx = new SvcLogicContext();
310         expectedEx.expect(SvcLogicException.class);
311         expectedEx.expectMessage("Error while logging data");
312         dbService.logData(ctx, null);
313     }
314
315     @Test
316     public void testProcessSdcReferencesException() throws Exception {
317         MockDBService dbService = MockDBService.initialise(true);
318         SvcLogicContext ctx = new SvcLogicContext();
319         ctx.setAttribute("file-category", "test");
320         expectedEx.expect(SvcLogicException.class);
321         expectedEx.expectMessage("Error While processing sdc_reference table ");
322         dbService.processSdcReferences(ctx, false);
323     }
324
325     @Test
326     public void testIsArtifactUpdateRequiredException() throws Exception {
327         MockDBService dbService = MockDBService.initialise(true);
328         SvcLogicContext ctx = new SvcLogicContext();
329         expectedEx.expect(DBException.class);
330         expectedEx.expectMessage("An error occurred while checking for artifact update");
331         dbService.isArtifactUpdateRequired(ctx, "db");
332     }
333
334     @Test
335     public void testProcessDeviceInterfaceProtocolException() throws Exception {
336         MockDBService dbService = MockDBService.initialise(true);
337         SvcLogicContext ctx = new SvcLogicContext();
338         expectedEx.expect(SvcLogicException.class);
339         expectedEx.expectMessage("Error While processing DEVICE_INTERFACE_PROTOCOL table ");
340         dbService.processDeviceInterfaceProtocol(ctx, false);
341     }
342
343     @Test
344     public void testProcessDeviceAuthenticationException() throws Exception {
345         MockDBService dbService = MockDBService.initialise(true);
346         SvcLogicContext ctx = new SvcLogicContext();
347         boolean isUpdate = true;
348         expectedEx.expect(DBException.class);
349         expectedEx.expectMessage("An error occurred when processing device authentication");
350         dbService.processDeviceAuthentication(ctx, isUpdate);
351     }
352
353     @Test
354     public void testProcessVnfcReferenceException() throws Exception {
355         MockDBService dbService = MockDBService.initialise(true);
356         SvcLogicContext ctx = new SvcLogicContext();
357         ctx.setAttribute("test", "test");
358         expectedEx.expect(SvcLogicException.class);
359         expectedEx.expectMessage("Error While processing VNFC_REFERENCE table ");
360         dbService.processVnfcReference(ctx, true);
361     }
362
363     @Test
364     public void testProcessDownloadDGReferenceException() throws Exception {
365         MockDBService dbService = MockDBService.initialise(true);
366         SvcLogicContext ctx = new SvcLogicContext();
367         ctx.setAttribute("test", "test");
368         expectedEx.expect(SvcLogicException.class);
369         expectedEx.expectMessage("Error While processing DOWNLOAD_DG_REFERENCE table ");
370         dbService.processDownloadDgReference(ctx, false);
371     }
372
373     @Test
374     public void testProcessConfigActionDgException() throws Exception {
375         MockDBService dbService = MockDBService.initialise(true);
376         SvcLogicContext ctx = new SvcLogicContext();
377         ctx.setAttribute("test", "test");
378         ctx.setAttribute(SdcArtifactHandlerConstants.DOWNLOAD_DG_REFERENCE, "Reference");
379         expectedEx.expect(SvcLogicException.class);
380         expectedEx.expectMessage("Error While processing Configure DG Action table ");
381         dbService.processConfigActionDg(ctx, true);
382     }
383
384     @Test
385     public void testGetModelDataInformationbyArtifactNameException() throws Exception {
386         MockDBService dbService = MockDBService.initialise(true);
387         SvcLogicContext ctx = new SvcLogicContext();
388         ctx.setAttribute("test", "test");
389         expectedEx.expect(SvcLogicException.class);
390         expectedEx.expectMessage("Error While processing is ArtifactUpdateRequiredforPD table ");
391         dbService.getModelDataInformationbyArtifactName("test");
392     }
393
394     @Test
395     public void testUpdateYangContentsException() throws Exception {
396         MockDBService dbService = MockDBService.initialise(true);
397         SvcLogicContext ctx = new SvcLogicContext();
398         ctx.setAttribute("test", "test");
399         String artifactId = "TestArtifact";
400         String yangContents = "TestYangContents";
401         expectedEx.expect(SvcLogicException.class);
402         expectedEx.expectMessage("Error While processing Configure DG Action table ");
403         dbService.updateYangContents(ctx, artifactId, yangContents);
404     }
405
406     @Test
407     public void testGetDownLoadDGReferenceException() throws Exception {
408         MockDBService dbService = MockDBService.initialise(true);
409         SvcLogicContext ctx = new SvcLogicContext();
410         ctx.setAttribute("test", "test");
411         ctx.setAttribute(SdcArtifactHandlerConstants.DEVICE_PROTOCOL, "CLI");
412         expectedEx.expect(DBException.class);
413         expectedEx.expectMessage("An error occurred when getting DG reference");
414         assertEquals("TestDG", dbService.getDownLoadDGReference(ctx));
415     }
416     
417     @Test
418     public void testcreateQueryListForTemplateIds() {
419         MockDBService dbService = MockDBService.initialise(true);
420         SvcLogicContext ctx = new SvcLogicContext();
421         String queryPart = dbService.createQueryListForTemplateIds("modelId", ctx);
422         String expectedQuery = " AND ARTIFACT_NAME like $model-id ";
423         String expectedAttribute = "%_modelId.%";
424         assertEquals(expectedQuery, queryPart);
425         assertEquals(expectedAttribute,ctx.getAttribute("model-id"));
426     }
427     
428     @Test
429     public void testisProtocolReferenceUpdateRequired() throws SvcLogicException {
430         MockDBService dbService = MockDBService.initialise();
431         SvcLogicContext ctx = new SvcLogicContext();
432         ctx.setAttribute("test", "test");
433         String vnfType = "testVnf";
434         String protocol = "testProtocol";
435         String action = "testAction";
436         String actionLevel = "testActionLevel";
437         String template = "testTemplateData";
438         boolean result = dbService.isProtocolReferenceUpdateRequired(ctx, vnfType, protocol, action, actionLevel, template);
439         assertTrue(result);
440     }
441     
442     @Test
443     public void testProcessConfigureActionDg() throws SvcLogicException {
444         MockDBService dbService = MockDBService.initialise(true);
445         SvcLogicContext ctx = new SvcLogicContext();
446         dbService.processConfigureActionDg(ctx, true);
447         assertNotNull(ctx);
448     }
449     
450     @Test
451     public void testUpdateProtocolReference() throws SvcLogicException {
452         MockDBService dbService = MockDBService.initialise();
453         SvcLogicContext ctx = new SvcLogicContext();
454         ctx.setAttribute("test", "test");
455         String vnfType = "testVnf";
456         String protocol = "testProtocol";
457         String action = "testAction";
458         String actionLevel = "testActionLevel";
459         String template = "testTemplateData";
460         dbService.updateProtocolReference(ctx, vnfType, protocol, action, actionLevel, template);
461         assertNotNull(vnfType);
462     }
463     
464 }
465