Coverage for appc-artifact-handler package
[appc.git] / appc-inbound / appc-artifact-handler / provider / src / test / java / org / onap / appc / artifact / handler / dbservices / TestDBServiceExceptions.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2018 IBM
6  * ================================================================================
7  * Modifications Copyright (C) 2019 Ericsson
8  * =============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.appc.artifact.handler.dbservices;
25
26 import static org.junit.Assert.assertEquals;
27
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 import org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants;
32 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
33 import org.mockito.Mockito;
34 import org.mockito.runners.MockitoJUnitRunner;
35 import org.powermock.reflect.Whitebox;
36
37 @RunWith(MockitoJUnitRunner.class)
38 public class TestDBServiceExceptions {
39
40     private MockDBService dbService;
41
42     private MockSvcLogicResource mockSVCLogicResource;
43
44     private SvcLogicContext ctx ;
45
46     @Before
47     public void setup(){
48            dbService =  MockDBService.initialise();
49            mockSVCLogicResource = Mockito.spy(MockSvcLogicResource.class);
50            ctx = new SvcLogicContext();
51         }
52
53     @Test
54     public void testSaveArtifacts() throws Exception {
55         String artifactName = "TestArtifact";
56         String prefix = "";
57         ctx.setAttribute("test", "test");
58         String result = dbService.getInternalVersionNumber(ctx, artifactName, prefix);
59         assertEquals("1", result);
60     }
61
62     @Test
63     public void testProcessSdcReferencesForCapability() throws Exception {
64         ctx.setAttribute("test", "test");
65         ctx.setAttribute(SdcArtifactHandlerConstants.FILE_CATEGORY, "capability");
66         boolean isUpdate = false;
67         String expectedKey = "insert into ASDC_REFERENCE set VNFC_TYPE = null  , FILE_CATEGORY = $file-category , " +
68                 "VNF_TYPE = $vnf-type , ACTION = null  , ARTIFACT_TYPE = null  , ARTIFACT_NAME = $artifact-name";
69         dbService.processSdcReferences(ctx, isUpdate);
70         assertEquals(expectedKey, ctx.getAttribute("keys"));
71         }
72
73     @Test
74     public void testProcessSdcReferences() throws Exception {
75         ctx.setAttribute("test", "test");
76         ctx.setAttribute(SdcArtifactHandlerConstants.FILE_CATEGORY, "Test");
77         String expectedKey = "insert into ASDC_REFERENCE set VNFC_TYPE = $vnfc-type , FILE_CATEGORY = $file-category , VNF_TYPE = $vnf-type , ACTION = $action , ARTIFACT_TYPE = $artifact-type , ARTIFACT_NAME = $artifact-name";
78         boolean isUpdate = false;
79         dbService.processSdcReferences(ctx, isUpdate);
80         assertEquals(expectedKey, ctx.getAttribute("keys"));
81     }
82
83     @Test(expected = Exception.class)
84     public void testGetDownLoadDGReference() throws Exception {
85         ctx.setAttribute("test", "test");
86         ctx.setAttribute(SdcArtifactHandlerConstants.DEVICE_PROTOCOL, "");
87         dbService.getDownLoadDGReference(ctx);
88     }
89
90     @Test
91     public void testProcessConfigActionDg() throws Exception {
92         ctx.setAttribute("test", "test");
93         boolean isUpdate = false;
94         ctx.setAttribute(SdcArtifactHandlerConstants.DOWNLOAD_DG_REFERENCE, "Reference");
95         String expectedKey = "insert into CONFIGURE_ACTION_DG set DOWNLOAD_CONFIG_DG = $download-dg-reference , ACTION = $action , VNF_TYPE = $vnf-type";
96         dbService.processConfigActionDg(ctx, isUpdate);
97         assertEquals(expectedKey, ctx.getAttribute("keys"));
98     }
99
100     @Test
101     public void testProcessConfigActionDgForElse() throws Exception {
102         ctx.setAttribute("test", "test");
103         boolean isUpdate = false;
104         String expectedKey = null;
105         dbService.processConfigActionDg(ctx, isUpdate);
106         assertEquals(expectedKey, ctx.getAttribute("keys"));
107     }
108
109     @Test
110     public void testprocessDpwnloadDGReference() throws Exception {
111         ctx.setAttribute("test", "test");
112         boolean isUpdate = false;
113         String expectedKey = "insert into DOWNLOAD_DG_REFERENCE set DOWNLOAD_CONFIG_DG = $download-dg-reference , PROTOCOL = $device-protocol";
114         dbService.processDownloadDgReference(ctx, isUpdate);
115         assertEquals(expectedKey, ctx.getAttribute("keys"));
116     }
117
118     @Test
119     public void testResolveWhereClause() throws Exception {
120         ctx.setAttribute("test", "test");
121         String db="DOWNLOAD_DG_REFERENCE";
122         String whereClause = "";
123         String result =  Whitebox.invokeMethod(dbService, "resolveWhereClause", ctx, db, whereClause);
124         assertEquals(true, result.contains("PROTOCOL"));
125     }
126
127     @Test
128     public void testResolveWhereClauseForDevice_Authentication() throws Exception {
129         ctx.setAttribute("test", "test");
130         String db="DEVICE_AUTHENTICATION";
131         String whereClause = "Test";
132         String result =  Whitebox.invokeMethod(dbService, "resolveWhereClause", ctx, db, whereClause);
133         assertEquals(true, result.contains("Test"));
134     }
135
136     @Test
137     public void testResolveWhereClauseCONFIGURE_ACTION_DG() throws Exception {
138         ctx.setAttribute("test", "test");
139         String db = "CONFIGURE_ACTION_DG";
140         String whereClause = "Test";
141         String result =  Whitebox.invokeMethod(dbService, "resolveWhereClause", ctx, db, whereClause);
142         assertEquals(true, result.contains("Test"));
143     }
144
145     @Test
146     public void testResolveWhereClauseVNFC_REFERENCE() throws Exception {
147         ctx.setAttribute("test", "test");
148         String db = "VNFC_REFERENCE";
149         String whereClause = "TestVNFC_REFERENCE";
150         String result =  Whitebox.invokeMethod(dbService, "resolveWhereClause", ctx, db, whereClause);
151         assertEquals(true, result.contains("TestVNFC_REFERENCE"));
152     }
153
154 }