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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
15 * SPDX-License-Identifier: Apache-2.0
16 * ============LICENSE_END=========================================================
19 package org.onap.appc.sdc.artifacts.helper;
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;
38 * This class contains test methods for Artifact Storage Service.
42 public class TestArtifactStorageService {
44 private ArtifactStorageService artifactStorageService;
46 private ArtifactStorageService artifactStorageServiceSpy;
48 private SDCArtifact sdcArtifact;
50 private SDCReference sdcReference;
52 private DbLibService dbLibService;
54 private CachedRowSet cachedRowSet;
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);
68 * This method tests the store Artifacts method success scenario.
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
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);
81 * This method tests the store Artifacts method failure scenario.
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
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);
94 * This method tests the store Artifacts with reference method success scenario.
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
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,
110 * This method tests the store Artifacts with reference method success scenario.
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
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,
126 * This method tests the store Artifacts with reference method failure scenario.
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
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);
139 * This method tests the store Artifacts with reference method failure scenario.
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
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);
153 * This method tests the retrieve Artifacts with reference method failure scenario scenario.
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
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");