Test coverage for sdc-listener-bundle
[appc.git] / appc-sdc-listener / appc-sdc-listener-bundle / src / test / java / org / onap / appc / sdc / artifacts / helper / TestArtifactStorageService.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2019 Ericsson. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
6  * file except in compliance with the License. You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software distributed under the License
11  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12  * or implied. See the License for the specific language governing permissions and limitations under
13  * the License.
14  *
15  * SPDX-License-Identifier: Apache-2.0
16  * ============LICENSE_END=========================================================
17  */
18
19 package org.onap.appc.sdc.artifacts.helper;
20
21 import static org.mockito.Matchers.anyObject;
22 import static org.mockito.Matchers.eq;
23 import static org.mockito.Mockito.times;
24 import static org.mockito.Mockito.verify;
25 import static org.mockito.Mockito.when;
26 import java.sql.SQLException;
27 import javax.sql.rowset.CachedRowSet;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.mockito.Mockito;
31 import org.mockito.internal.util.reflection.Whitebox;
32 import org.onap.appc.exceptions.APPCException;
33 import org.onap.appc.sdc.artifacts.object.SDCArtifact;
34 import org.onap.appc.sdc.artifacts.object.SDCReference;
35 import org.onap.ccsdk.sli.core.dblib.DbLibService;
36
37 /**
38  * This class contains test methods for Artifact Storage Service.
39  *
40  */
41
42 public class TestArtifactStorageService {
43
44   private ArtifactStorageService artifactStorageService;
45
46   private ArtifactStorageService artifactStorageServiceSpy;
47
48   private SDCArtifact sdcArtifact;
49
50   private SDCReference sdcReference;
51
52   private DbLibService dbLibService;
53
54   private CachedRowSet cachedRowSet;
55
56   @Before
57   public void setup() throws Exception {
58     artifactStorageService = new ArtifactStorageService();
59     sdcArtifact = Mockito.mock(SDCArtifact.class);
60     dbLibService = Mockito.mock(DbLibService.class);
61     sdcReference = Mockito.mock(SDCReference.class);
62     cachedRowSet = Mockito.mock(CachedRowSet.class);
63     Whitebox.setInternalState(artifactStorageService, "dbLibService", dbLibService);
64     artifactStorageServiceSpy = Mockito.spy(artifactStorageService);
65   }
66
67   /**
68    * This method tests the store Artifacts method success scenario.
69    * 
70    * @throws APPCException if the there are any exception in sdc artifacts
71    * @throws SQLException if there are any sql exception while storing the sdc artifacts
72    */
73   @Test
74   public void testStoreSDCArtifact() throws APPCException, SQLException {
75     when(dbLibService.writeData(anyObject(), anyObject(), anyObject())).thenReturn(true);
76     artifactStorageServiceSpy.storeSDCArtifact(sdcArtifact);
77     verify(artifactStorageServiceSpy, times(1)).storeSDCArtifact(sdcArtifact);
78   }
79
80   /**
81    * This method tests the store Artifacts method failure scenario.
82    * 
83    * @throws APPCException if the there are any exception in sdc artifacts
84    * @throws SQLException if there are any sql exception while storing the sdc artifacts
85    */
86   @Test(expected = APPCException.class)
87   public void testStoreSDCArtifactWithException() throws APPCException, SQLException {
88     when(dbLibService.writeData(anyObject(), anyObject(), anyObject()))
89         .thenThrow(new SQLException());
90     artifactStorageService.storeSDCArtifact(sdcArtifact);
91   }
92
93   /**
94    * This method tests the store Artifacts with reference method success scenario.
95    * 
96    * @throws APPCException if the there are any exception in sdc artifacts
97    * @throws SQLException if there are any sql exception while storing the sdc artifacts
98    */
99   @Test
100   public void testStoreSDCArtifactWithReference() throws APPCException, SQLException {
101     when(dbLibService.writeData(anyObject(), anyObject(), anyObject())).thenReturn(true);
102     when(dbLibService.getData(anyObject(), anyObject(), eq("sdnctl"))).thenReturn(cachedRowSet);
103     when(cachedRowSet.first()).thenReturn(true);
104     artifactStorageServiceSpy.storeSDCArtifactWithReference(sdcArtifact, sdcReference);
105     verify(artifactStorageServiceSpy, times(1)).storeSDCArtifactWithReference(sdcArtifact,
106         sdcReference);
107   }
108
109   /**
110    * This method tests the store Artifacts with reference method success scenario.
111    * 
112    * @throws APPCException if the there are any exception in sdc artifacts
113    * @throws SQLException if there are any sql exception while storing the sdc artifacts
114    */
115   @Test
116   public void testArtifactWithExistingRef() throws APPCException, SQLException {
117     when(dbLibService.writeData(anyObject(), anyObject(), anyObject())).thenReturn(true);
118     when(dbLibService.getData(anyObject(), anyObject(), eq("sdnctl"))).thenReturn(cachedRowSet);
119     ArtifactStorageService artifactStorageServiceSpy = Mockito.spy(artifactStorageService);
120     artifactStorageServiceSpy.storeSDCArtifactWithReference(sdcArtifact, sdcReference);
121     verify(artifactStorageServiceSpy, times(1)).storeSDCArtifactWithReference(sdcArtifact,
122         sdcReference);
123   }
124
125   /**
126    * This method tests the store Artifacts with reference method failure scenario.
127    * 
128    * @throws APPCException if the there are any exception in sdc artifacts
129    * @throws SQLException if there are any sql exception while storing the sdc artifacts
130    */
131   @Test(expected = APPCException.class)
132   public void testArtifactWithExceptionScenario1() throws APPCException, SQLException {
133     when(dbLibService.getData(anyObject(), anyObject(), eq("sdnctl")))
134         .thenThrow(new SQLException());
135     artifactStorageService.storeSDCArtifactWithReference(sdcArtifact, sdcReference);
136   }
137
138   /**
139    * This method tests the store Artifacts with reference method failure scenario.
140    * 
141    * @throws APPCException if the there are any exception in sdc artifacts
142    * @throws SQLException if there are any sql exception while storing the sdc artifacts
143    */
144   @Test(expected = APPCException.class)
145   public void testArtifactWithExceptionScenario2() throws APPCException, SQLException {
146     when(dbLibService.writeData(anyObject(), anyObject(), anyObject()))
147         .thenThrow(new SQLException());
148     when(dbLibService.getData(anyObject(), anyObject(), eq("sdnctl"))).thenReturn(cachedRowSet);
149     artifactStorageService.storeSDCArtifactWithReference(sdcArtifact, sdcReference);
150   }
151
152   /**
153    * This method tests the retrieve Artifacts with reference method failure scenario scenario.
154    * 
155    * @throws APPCException if the there are any exception in sdc artifacts
156    * @throws SQLException if there are any sql exception while storing the sdc artifacts
157    */
158   @Test(expected = APPCException.class)
159   public void testRetrieveSDCReference() throws APPCException, SQLException {
160     when(dbLibService.getData(anyObject(), anyObject(), eq("sdnctl")))
161         .thenThrow(new SQLException());
162     artifactStorageService.retrieveSDCReference("vnfType", "category");
163   }
164 }