added test case to 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-2018 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.sli.SvcLogicContext;
35 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
36 import org.onap.ccsdk.sli.core.sli.SvcLogicResource.QueryStatus;
37 import static org.junit.Assert.assertEquals;
38 import static org.junit.Assert.assertNotNull;
39
40 public class DBServiceTest {
41
42     @Rule
43     public ExpectedException expectedEx = ExpectedException.none();
44
45     @Test
46     public void testSaveArtifacts() throws Exception {
47         MockDBService dbService = MockDBService.initialise();
48         SvcLogicContext ctx = new SvcLogicContext();
49         ctx.setAttribute("test", "test");
50         int internalVersion = 1;
51         dbService.saveArtifacts(ctx, internalVersion);
52     }
53
54     @Test
55     public void testLogData() throws Exception {
56         MockDBService dbService = MockDBService.initialise();
57         SvcLogicContext ctx = new SvcLogicContext();
58         ctx.setAttribute("test", "test");
59         String prefix = "test";
60         assertEquals(QueryStatus.SUCCESS, dbService.logData(ctx, prefix));
61     }
62
63     @Test
64     public void testProcessConfigActionDg() throws Exception {
65         MockDBService dbService = MockDBService.initialise();
66         SvcLogicContext ctx = new SvcLogicContext();
67         ctx.setAttribute("test", "test");
68         boolean isUpdate = true;
69         ctx.setAttribute(SdcArtifactHandlerConstants.DOWNLOAD_DG_REFERENCE, "Reference");
70         dbService.processConfigActionDg(ctx, isUpdate);
71     }
72
73
74     @Test
75     public void testGetModelDataInformationbyArtifactName() throws Exception {
76         MockDBService dbService = MockDBService.initialise();
77         SvcLogicContext ctx = new SvcLogicContext();
78         ctx.setAttribute("test", "test");
79         String artifactName = "test";
80         assertNotNull(dbService.getModelDataInformationbyArtifactName(artifactName));
81     }
82
83     @Test
84     public void testUpdateYangContents() throws Exception {
85         MockDBService dbService = MockDBService.initialise();
86         SvcLogicContext ctx = new SvcLogicContext();
87         ctx.setAttribute("test", "test");
88         String artifactId = "TestArtifact";
89         String yangContents = "TestYangContents";
90         dbService.updateYangContents(ctx, artifactId, yangContents);
91     }
92
93     @Test
94     public void testInsertProtocolReference() throws Exception {
95         MockDBService dbService = MockDBService.initialise();
96         SvcLogicContext ctx = new SvcLogicContext();
97         ctx.setAttribute("test", "test");
98         String vnfType = "testVnf";
99         String protocol = "testProtocol";
100         String action = "testAction";
101         String actionLevel = "testActionLevel";
102         String template = "testTemplateData";
103         dbService.insertProtocolReference(ctx, vnfType, protocol, action, actionLevel, template);
104     }
105
106
107     @Test
108     public void testInsertProtocolReferenceException() throws Exception {
109         MockDBService dbService = MockDBService.initialise();
110         SvcLogicContext ctx = new SvcLogicContext();
111         ctx.setAttribute("test", "test");
112         String vnfType = "testVnf";
113         String protocol = "testProtocol";
114         String action = "testAction";
115         String actionLevel = "testActionLevel";
116         String template = "testTemplateData";
117         dbService.insertProtocolReference(ctx, vnfType, protocol, action, actionLevel, template);
118     }
119
120     @Test
121     public void testProcessDownloadDGReference() throws Exception {
122         MockDBService dbService = MockDBService.initialise();
123         SvcLogicContext ctx = new SvcLogicContext();
124         ctx.setAttribute("test", "test");
125         boolean isUpdate = true;
126         dbService.processDownloadDgReference(ctx, isUpdate);
127     }
128
129     @Test
130     public void testProcessVnfcReference() throws Exception {
131         MockDBService dbService = MockDBService.initialise();
132         SvcLogicContext ctx = new SvcLogicContext();
133         ctx.setAttribute("test", "test");
134         boolean isUpdate = false;
135         dbService.processVnfcReference(ctx, isUpdate);
136     }
137
138     @Test
139     public void testProcessDeviceAuthentication() throws Exception {
140         MockDBService dbService = MockDBService.initialise();
141         SvcLogicContext ctx = new SvcLogicContext();
142         ctx.setAttribute("test", "test");
143         ctx.setAttribute("url", "");
144         String expectedKey ="update DEVICE_AUTHENTICATION set USER_NAME = '' , PORT_NUMBER = 0, URL = ''  where VNF_TYPE = $vnf-type  AND PROTOCOL = $device-protocol AND  ACTION = $action";
145         boolean isUpdate = true;
146         dbService.processDeviceAuthentication(ctx, isUpdate);
147         assertEquals(expectedKey,ctx.getAttribute("keys"));
148     }
149
150     @Test
151     public void testProcessDeviceAuthenticationforFalse() throws Exception {
152         MockDBService dbService = MockDBService.initialise();
153         SvcLogicContext ctx = new SvcLogicContext();
154         ctx.setAttribute("test", "test");
155         ctx.setAttribute("url", "");
156         boolean isUpdate = false;
157         dbService.processDeviceAuthentication(ctx, isUpdate);
158         assertEquals(true,ctx.getAttribute("keys").contains("DEVICE_AUTHENTICATION"));
159     }
160
161     @Test
162     public void testProcessDeviceInterfaceProtocol() throws Exception {
163         MockDBService dbService = MockDBService.initialise();
164         SvcLogicContext ctx = new SvcLogicContext();
165         ctx.setAttribute("test", "test");
166         boolean isUpdate = true;
167         dbService.processDeviceInterfaceProtocol(ctx, isUpdate);
168     }
169
170     @Test
171     public void testProcessDeviceInterfaceProtocolForFalse() throws Exception {
172         MockDBService dbService = MockDBService.initialise();
173         SvcLogicContext ctx = new SvcLogicContext();
174         ctx.setAttribute("test", "test");
175         boolean isUpdate = false;
176         dbService.processDeviceInterfaceProtocol(ctx, isUpdate);
177         assertEquals(true,ctx.getAttribute("keys").contains("DEVICE_INTERFACE_PROTOCOL"));
178     }
179
180     @Test
181     public void testProcessSdcReferences() throws Exception {
182         MockDBService dbService = MockDBService.initialise();
183         SvcLogicContext ctx = new SvcLogicContext();
184         ctx.setAttribute("test", "test");
185         ctx.setAttribute(SdcArtifactHandlerConstants.FILE_CATEGORY, "testCategory");
186         boolean isUpdate = true;
187         dbService.processSdcReferences(ctx, isUpdate);
188     }
189
190     @Test
191     public void testIsArtifactUpdateRequired() throws Exception {
192         MockDBService dbService = MockDBService.initialise();
193         SvcLogicContext ctx = new SvcLogicContext();
194         ctx.setAttribute("test", "test");
195         String db = "db";
196         dbService.isArtifactUpdateRequired(ctx, db);
197     }
198
199     @Test
200     public void testgetArtifactID() throws Exception {
201         MockDBService dbService = MockDBService.initialise();
202         SvcLogicContext ctx = new SvcLogicContext();
203         ctx.setAttribute("test", "test");
204         String db = "db";
205         dbService.getArtifactID(ctx, db);
206     }
207
208     @Test
209     public void testGetDownLoadDGReference() throws Exception {
210         MockDBService dbService = MockDBService.initialise();
211         SvcLogicContext ctx = new SvcLogicContext();
212         ctx.setAttribute("test", "test");
213         ctx.setAttribute(SdcArtifactHandlerConstants.DEVICE_PROTOCOL, "CLI");
214         assertEquals("TestDG", dbService.getDownLoadDGReference(ctx));
215     }
216
217     @Test
218     public void testInitialise() {
219         DBService dbService = DBService.initialise();
220         assertNotNull(dbService);
221     }
222
223     @Test
224     public void testGetInternalVersionNumberException() throws SvcLogicException {
225         MockDBService dbService = MockDBService.initialise(true);
226         SvcLogicContext ctx = new SvcLogicContext();
227         expectedEx.expect(SvcLogicException.class);
228         expectedEx.expectMessage("Error - getting internal Artifact Number");
229         dbService.getInternalVersionNumber(ctx, "artifactName", "prefix");
230     }
231
232     @Test
233     public void testGetArtifactIDException() throws SvcLogicException {
234         MockDBService dbService = MockDBService.initialise(true);
235         SvcLogicContext ctx = new SvcLogicContext();
236         expectedEx.expect(SvcLogicException.class);
237         expectedEx.expectMessage("Error - getting  Artifact ID from database");
238         dbService.getArtifactID(ctx, "artifactName");
239     }
240
241     @Test
242     public void testSaveArtifactsException() throws Exception {
243         MockDBService dbService = MockDBService.initialise(true);
244         SvcLogicContext ctx = new SvcLogicContext();
245         expectedEx.expect(SvcLogicException.class);
246         expectedEx.expectMessage("Error While processing storing Artifact: null");
247         dbService.saveArtifacts(ctx, -1);
248     }
249
250     @Test
251     public void testLogDataException() throws Exception {
252         MockDBService dbService = MockDBService.initialise(true);
253         SvcLogicContext ctx = new SvcLogicContext();
254         expectedEx.expect(SvcLogicException.class);
255         expectedEx.expectMessage("Error while logging data");
256         dbService.logData(ctx, null);
257     }
258
259     @Test
260     public void testProcessSdcReferencesException() throws Exception {
261         MockDBService dbService = MockDBService.initialise(true);
262         SvcLogicContext ctx = new SvcLogicContext();
263         ctx.setAttribute("file-category", "test");
264         expectedEx.expect(SvcLogicException.class);
265         expectedEx.expectMessage("Error While processing sdc_reference table ");
266         dbService.processSdcReferences(ctx, false);
267     }
268
269     @Test
270     public void testIsArtifactUpdateRequiredException() throws Exception {
271         MockDBService dbService = MockDBService.initialise(true);
272         SvcLogicContext ctx = new SvcLogicContext();
273         expectedEx.expect(DBException.class);
274         expectedEx.expectMessage("An error occurred while checking for artifact update");
275         dbService.isArtifactUpdateRequired(ctx, "db");
276     }
277
278     @Test
279     public void testProcessDeviceInterfaceProtocolException() throws Exception {
280         MockDBService dbService = MockDBService.initialise(true);
281         SvcLogicContext ctx = new SvcLogicContext();
282         expectedEx.expect(SvcLogicException.class);
283         expectedEx.expectMessage("Error While processing DEVICE_INTERFACE_PROTOCOL table ");
284         dbService.processDeviceInterfaceProtocol(ctx, false);
285     }
286
287     @Test
288     public void testProcessDeviceAuthenticationException() throws Exception {
289         MockDBService dbService = MockDBService.initialise(true);
290         SvcLogicContext ctx = new SvcLogicContext();
291         boolean isUpdate = true;
292         expectedEx.expect(DBException.class);
293         expectedEx.expectMessage("An error occurred when processing device authentication");
294         dbService.processDeviceAuthentication(ctx, isUpdate);
295     }
296
297     @Test
298     public void testProcessVnfcReferenceException() throws Exception {
299         MockDBService dbService = MockDBService.initialise(true);
300         SvcLogicContext ctx = new SvcLogicContext();
301         ctx.setAttribute("test", "test");
302         expectedEx.expect(SvcLogicException.class);
303         expectedEx.expectMessage("Error While processing VNFC_REFERENCE table ");
304         dbService.processVnfcReference(ctx, true);
305     }
306
307     @Test
308     public void testProcessDownloadDGReferenceException() throws Exception {
309         MockDBService dbService = MockDBService.initialise(true);
310         SvcLogicContext ctx = new SvcLogicContext();
311         ctx.setAttribute("test", "test");
312         expectedEx.expect(SvcLogicException.class);
313         expectedEx.expectMessage("Error While processing DOWNLOAD_DG_REFERENCE table ");
314         dbService.processDownloadDgReference(ctx, false);
315     }
316
317     @Test
318     public void testProcessConfigActionDgException() throws Exception {
319         MockDBService dbService = MockDBService.initialise(true);
320         SvcLogicContext ctx = new SvcLogicContext();
321         ctx.setAttribute("test", "test");
322         ctx.setAttribute(SdcArtifactHandlerConstants.DOWNLOAD_DG_REFERENCE, "Reference");
323         expectedEx.expect(SvcLogicException.class);
324         expectedEx.expectMessage("Error While processing Configure DG Action table ");
325         dbService.processConfigActionDg(ctx, true);
326     }
327
328     @Test
329     public void testGetModelDataInformationbyArtifactNameException() throws Exception {
330         MockDBService dbService = MockDBService.initialise(true);
331         SvcLogicContext ctx = new SvcLogicContext();
332         ctx.setAttribute("test", "test");
333         expectedEx.expect(SvcLogicException.class);
334         expectedEx.expectMessage("Error While processing is ArtifactUpdateRequiredforPD table ");
335         dbService.getModelDataInformationbyArtifactName("test");
336     }
337
338     @Test
339     public void testUpdateYangContentsException() throws Exception {
340         MockDBService dbService = MockDBService.initialise(true);
341         SvcLogicContext ctx = new SvcLogicContext();
342         ctx.setAttribute("test", "test");
343         String artifactId = "TestArtifact";
344         String yangContents = "TestYangContents";
345         expectedEx.expect(SvcLogicException.class);
346         expectedEx.expectMessage("Error While processing Configure DG Action table ");
347         dbService.updateYangContents(ctx, artifactId, yangContents);
348     }
349
350     @Test
351     public void testGetDownLoadDGReferenceException() throws Exception {
352         MockDBService dbService = MockDBService.initialise(true);
353         SvcLogicContext ctx = new SvcLogicContext();
354         ctx.setAttribute("test", "test");
355         ctx.setAttribute(SdcArtifactHandlerConstants.DEVICE_PROTOCOL, "CLI");
356         expectedEx.expect(DBException.class);
357         expectedEx.expectMessage("An error occurred when getting DG reference");
358         assertEquals("TestDG", dbService.getDownLoadDGReference(ctx));
359     }
360     
361     @Test
362     public void testcreateQueryListForTemplateIds() {
363         MockDBService dbService = MockDBService.initialise(true);
364         String queryPart = dbService.createQueryListForTemplateIds("modelId");
365         String expected = " AND ARTIFACT_NAME like '%_modelId.%'";
366         assertEquals(expected, queryPart);
367     }
368 }
369