2 * ============LICENSE_START=======================================================
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
17 * http://www.apache.org/licenses/LICENSE-2.0
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.
25 * ============LICENSE_END=========================================================
28 package org.onap.appc.artifact.handler.dbservices;
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;
42 import static org.mockito.Mockito.*;
44 import java.util.ArrayList;
46 import javax.sql.rowset.CachedRowSet;
48 public class DBServiceTest {
51 public ExpectedException expectedEx = ExpectedException.none();
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);
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));
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);
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));
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);
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);
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);
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);
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);
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"));
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"));
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);
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"));
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);
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);
254 public void testgetArtifactID() throws Exception {
255 MockDBService dbService = MockDBService.initialise();
256 SvcLogicContext ctx = new SvcLogicContext();
257 ctx.setAttribute("test", "test");
259 dbService.getArtifactID(ctx, db);
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));
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");
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");
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);
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);
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);
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");
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);
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);
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);
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);
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);
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");
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);
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));
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"));
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);
435 public void testProcessConfigureActionDg() throws SvcLogicException {
436 MockDBService dbService = MockDBService.initialise(true);
437 SvcLogicContext ctx = new SvcLogicContext();
438 dbService.processConfigureActionDg(ctx, true);
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);