2 * ============LICENSE_START=======================================================
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
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.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 import static org.junit.Assert.assertTrue;
41 public class DBServiceTest {
44 public ExpectedException expectedEx = ExpectedException.none();
47 public void testSaveArtifacts() throws Exception {
48 MockDBService dbService = MockDBService.initialise();
49 SvcLogicContext ctx = new SvcLogicContext();
50 ctx.setAttribute("test", "test");
51 int internalVersion = 1;
52 dbService.saveArtifacts(ctx, internalVersion);
56 public void testLogData() throws Exception {
57 MockDBService dbService = MockDBService.initialise();
58 SvcLogicContext ctx = new SvcLogicContext();
59 ctx.setAttribute("test", "test");
60 String prefix = "test";
61 assertEquals(QueryStatus.SUCCESS, dbService.logData(ctx, prefix));
65 public void testProcessConfigActionDg() throws Exception {
66 MockDBService dbService = MockDBService.initialise();
67 SvcLogicContext ctx = new SvcLogicContext();
68 ctx.setAttribute("test", "test");
69 boolean isUpdate = true;
70 ctx.setAttribute(SdcArtifactHandlerConstants.DOWNLOAD_DG_REFERENCE, "Reference");
71 dbService.processConfigActionDg(ctx, isUpdate);
76 public void testGetModelDataInformationbyArtifactName() throws Exception {
77 MockDBService dbService = MockDBService.initialise();
78 SvcLogicContext ctx = new SvcLogicContext();
79 ctx.setAttribute("test", "test");
80 String artifactName = "test";
81 assertNotNull(dbService.getModelDataInformationbyArtifactName(artifactName));
85 public void testUpdateYangContents() throws Exception {
86 MockDBService dbService = MockDBService.initialise();
87 SvcLogicContext ctx = new SvcLogicContext();
88 ctx.setAttribute("test", "test");
89 String artifactId = "TestArtifact";
90 String yangContents = "TestYangContents";
91 dbService.updateYangContents(ctx, artifactId, yangContents);
95 public void testInsertProtocolReference() throws Exception {
96 MockDBService dbService = MockDBService.initialise();
97 SvcLogicContext ctx = new SvcLogicContext();
98 ctx.setAttribute("test", "test");
99 String vnfType = "testVnf";
100 String protocol = "testProtocol";
101 String action = "testAction";
102 String actionLevel = "testActionLevel";
103 String template = "testTemplateData";
104 dbService.insertProtocolReference(ctx, vnfType, protocol, action, actionLevel, template);
109 public void testInsertProtocolReferenceException() throws Exception {
110 MockDBService dbService = MockDBService.initialise();
111 SvcLogicContext ctx = new SvcLogicContext();
112 ctx.setAttribute("test", "test");
113 String vnfType = "testVnf";
114 String protocol = "testProtocol";
115 String action = "testAction";
116 String actionLevel = "testActionLevel";
117 String template = "testTemplateData";
118 dbService.insertProtocolReference(ctx, vnfType, protocol, action, actionLevel, template);
122 public void testProcessDownloadDGReference() throws Exception {
123 MockDBService dbService = MockDBService.initialise();
124 SvcLogicContext ctx = new SvcLogicContext();
125 ctx.setAttribute("test", "test");
126 boolean isUpdate = true;
127 dbService.processDownloadDgReference(ctx, isUpdate);
131 public void testProcessVnfcReference() throws Exception {
132 MockDBService dbService = MockDBService.initialise();
133 SvcLogicContext ctx = new SvcLogicContext();
134 ctx.setAttribute("test", "test");
135 boolean isUpdate = false;
136 dbService.processVnfcReference(ctx, isUpdate);
140 public void testProcessDeviceAuthentication() throws Exception {
141 MockDBService dbService = MockDBService.initialise();
142 SvcLogicContext ctx = new SvcLogicContext();
143 ctx.setAttribute("test", "test");
144 ctx.setAttribute("url", "");
145 String expectedKey ="update DEVICE_AUTHENTICATION set USER_NAME = '' , PORT_NUMBER = 0, URL = '' where VNF_TYPE = $vnf-type AND PROTOCOL = $device-protocol AND ACTION = $action";
146 boolean isUpdate = true;
147 dbService.processDeviceAuthentication(ctx, isUpdate);
148 assertEquals(expectedKey,ctx.getAttribute("keys"));
152 public void testProcessDeviceAuthenticationforFalse() throws Exception {
153 MockDBService dbService = MockDBService.initialise();
154 SvcLogicContext ctx = new SvcLogicContext();
155 ctx.setAttribute("test", "test");
156 ctx.setAttribute("url", "");
157 boolean isUpdate = false;
158 dbService.processDeviceAuthentication(ctx, isUpdate);
159 assertEquals(true,ctx.getAttribute("keys").contains("DEVICE_AUTHENTICATION"));
163 public void testProcessDeviceInterfaceProtocol() throws Exception {
164 MockDBService dbService = MockDBService.initialise();
165 SvcLogicContext ctx = new SvcLogicContext();
166 ctx.setAttribute("test", "test");
167 boolean isUpdate = true;
168 dbService.processDeviceInterfaceProtocol(ctx, isUpdate);
172 public void testProcessDeviceInterfaceProtocolForFalse() throws Exception {
173 MockDBService dbService = MockDBService.initialise();
174 SvcLogicContext ctx = new SvcLogicContext();
175 ctx.setAttribute("test", "test");
176 boolean isUpdate = false;
177 dbService.processDeviceInterfaceProtocol(ctx, isUpdate);
178 assertEquals(true,ctx.getAttribute("keys").contains("DEVICE_INTERFACE_PROTOCOL"));
182 public void testProcessSdcReferences() throws Exception {
183 MockDBService dbService = MockDBService.initialise();
184 SvcLogicContext ctx = new SvcLogicContext();
185 ctx.setAttribute("test", "test");
186 ctx.setAttribute(SdcArtifactHandlerConstants.FILE_CATEGORY, "testCategory");
187 boolean isUpdate = true;
188 dbService.processSdcReferences(ctx, isUpdate);
192 public void testIsArtifactUpdateRequired() throws Exception {
193 MockDBService dbService = MockDBService.initialise();
194 SvcLogicContext ctx = new SvcLogicContext();
195 ctx.setAttribute("test", "test");
197 dbService.isArtifactUpdateRequired(ctx, db);
201 public void testgetArtifactID() throws Exception {
202 MockDBService dbService = MockDBService.initialise();
203 SvcLogicContext ctx = new SvcLogicContext();
204 ctx.setAttribute("test", "test");
206 dbService.getArtifactID(ctx, db);
210 public void testGetDownLoadDGReference() throws Exception {
211 MockDBService dbService = MockDBService.initialise();
212 SvcLogicContext ctx = new SvcLogicContext();
213 ctx.setAttribute("test", "test");
214 ctx.setAttribute(SdcArtifactHandlerConstants.DEVICE_PROTOCOL, "CLI");
215 assertEquals("TestDG", dbService.getDownLoadDGReference(ctx));
219 public void testInitialise() {
220 DBService dbService = DBService.initialise();
221 assertNotNull(dbService);
225 public void testGetInternalVersionNumberException() throws SvcLogicException {
226 MockDBService dbService = MockDBService.initialise(true);
227 SvcLogicContext ctx = new SvcLogicContext();
228 expectedEx.expect(SvcLogicException.class);
229 expectedEx.expectMessage("Error - getting internal Artifact Number");
230 dbService.getInternalVersionNumber(ctx, "artifactName", "prefix");
234 public void testGetArtifactIDException() throws SvcLogicException {
235 MockDBService dbService = MockDBService.initialise(true);
236 SvcLogicContext ctx = new SvcLogicContext();
237 expectedEx.expect(SvcLogicException.class);
238 expectedEx.expectMessage("Error - getting Artifact ID from database");
239 dbService.getArtifactID(ctx, "artifactName");
243 public void testSaveArtifactsException() throws Exception {
244 MockDBService dbService = MockDBService.initialise(true);
245 SvcLogicContext ctx = new SvcLogicContext();
246 expectedEx.expect(SvcLogicException.class);
247 expectedEx.expectMessage("Error While processing storing Artifact: null");
248 dbService.saveArtifacts(ctx, -1);
252 public void testLogDataException() throws Exception {
253 MockDBService dbService = MockDBService.initialise(true);
254 SvcLogicContext ctx = new SvcLogicContext();
255 expectedEx.expect(SvcLogicException.class);
256 expectedEx.expectMessage("Error while logging data");
257 dbService.logData(ctx, null);
261 public void testProcessSdcReferencesException() throws Exception {
262 MockDBService dbService = MockDBService.initialise(true);
263 SvcLogicContext ctx = new SvcLogicContext();
264 ctx.setAttribute("file-category", "test");
265 expectedEx.expect(SvcLogicException.class);
266 expectedEx.expectMessage("Error While processing sdc_reference table ");
267 dbService.processSdcReferences(ctx, false);
271 public void testIsArtifactUpdateRequiredException() throws Exception {
272 MockDBService dbService = MockDBService.initialise(true);
273 SvcLogicContext ctx = new SvcLogicContext();
274 expectedEx.expect(DBException.class);
275 expectedEx.expectMessage("An error occurred while checking for artifact update");
276 dbService.isArtifactUpdateRequired(ctx, "db");
280 public void testProcessDeviceInterfaceProtocolException() throws Exception {
281 MockDBService dbService = MockDBService.initialise(true);
282 SvcLogicContext ctx = new SvcLogicContext();
283 expectedEx.expect(SvcLogicException.class);
284 expectedEx.expectMessage("Error While processing DEVICE_INTERFACE_PROTOCOL table ");
285 dbService.processDeviceInterfaceProtocol(ctx, false);
289 public void testProcessDeviceAuthenticationException() throws Exception {
290 MockDBService dbService = MockDBService.initialise(true);
291 SvcLogicContext ctx = new SvcLogicContext();
292 boolean isUpdate = true;
293 expectedEx.expect(DBException.class);
294 expectedEx.expectMessage("An error occurred when processing device authentication");
295 dbService.processDeviceAuthentication(ctx, isUpdate);
299 public void testProcessVnfcReferenceException() throws Exception {
300 MockDBService dbService = MockDBService.initialise(true);
301 SvcLogicContext ctx = new SvcLogicContext();
302 ctx.setAttribute("test", "test");
303 expectedEx.expect(SvcLogicException.class);
304 expectedEx.expectMessage("Error While processing VNFC_REFERENCE table ");
305 dbService.processVnfcReference(ctx, true);
309 public void testProcessDownloadDGReferenceException() throws Exception {
310 MockDBService dbService = MockDBService.initialise(true);
311 SvcLogicContext ctx = new SvcLogicContext();
312 ctx.setAttribute("test", "test");
313 expectedEx.expect(SvcLogicException.class);
314 expectedEx.expectMessage("Error While processing DOWNLOAD_DG_REFERENCE table ");
315 dbService.processDownloadDgReference(ctx, false);
319 public void testProcessConfigActionDgException() throws Exception {
320 MockDBService dbService = MockDBService.initialise(true);
321 SvcLogicContext ctx = new SvcLogicContext();
322 ctx.setAttribute("test", "test");
323 ctx.setAttribute(SdcArtifactHandlerConstants.DOWNLOAD_DG_REFERENCE, "Reference");
324 expectedEx.expect(SvcLogicException.class);
325 expectedEx.expectMessage("Error While processing Configure DG Action table ");
326 dbService.processConfigActionDg(ctx, true);
330 public void testGetModelDataInformationbyArtifactNameException() throws Exception {
331 MockDBService dbService = MockDBService.initialise(true);
332 SvcLogicContext ctx = new SvcLogicContext();
333 ctx.setAttribute("test", "test");
334 expectedEx.expect(SvcLogicException.class);
335 expectedEx.expectMessage("Error While processing is ArtifactUpdateRequiredforPD table ");
336 dbService.getModelDataInformationbyArtifactName("test");
340 public void testUpdateYangContentsException() throws Exception {
341 MockDBService dbService = MockDBService.initialise(true);
342 SvcLogicContext ctx = new SvcLogicContext();
343 ctx.setAttribute("test", "test");
344 String artifactId = "TestArtifact";
345 String yangContents = "TestYangContents";
346 expectedEx.expect(SvcLogicException.class);
347 expectedEx.expectMessage("Error While processing Configure DG Action table ");
348 dbService.updateYangContents(ctx, artifactId, yangContents);
352 public void testGetDownLoadDGReferenceException() throws Exception {
353 MockDBService dbService = MockDBService.initialise(true);
354 SvcLogicContext ctx = new SvcLogicContext();
355 ctx.setAttribute("test", "test");
356 ctx.setAttribute(SdcArtifactHandlerConstants.DEVICE_PROTOCOL, "CLI");
357 expectedEx.expect(DBException.class);
358 expectedEx.expectMessage("An error occurred when getting DG reference");
359 assertEquals("TestDG", dbService.getDownLoadDGReference(ctx));
363 public void testcreateQueryListForTemplateIds() {
364 MockDBService dbService = MockDBService.initialise(true);
365 String queryPart = dbService.createQueryListForTemplateIds("modelId");
366 String expected = " AND ARTIFACT_NAME like '%_modelId.%'";
367 assertEquals(expected, queryPart);
371 public void testisProtocolReferenceUpdateRequired() throws SvcLogicException {
372 MockDBService dbService = MockDBService.initialise();
373 SvcLogicContext ctx = new SvcLogicContext();
374 ctx.setAttribute("test", "test");
375 String vnfType = "testVnf";
376 String protocol = "testProtocol";
377 String action = "testAction";
378 String actionLevel = "testActionLevel";
379 String template = "testTemplateData";
380 boolean result = dbService.isProtocolReferenceUpdateRequired(ctx, vnfType, protocol, action, actionLevel, template);