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