2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 Nordix Foundation
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdc.be.csar.storage;
23 import static org.junit.jupiter.api.Assertions.assertNotNull;
24 import static org.junit.jupiter.api.Assertions.assertThrows;
25 import static org.mockito.ArgumentMatchers.any;
26 import static org.mockito.ArgumentMatchers.anyBoolean;
27 import static org.mockito.ArgumentMatchers.anyInt;
28 import static org.mockito.ArgumentMatchers.anyString;
29 import static org.mockito.Mockito.doThrow;
30 import static org.mockito.Mockito.verify;
31 import static org.mockito.Mockito.when;
33 import io.minio.BucketExistsArgs;
34 import io.minio.GetObjectArgs;
35 import io.minio.GetObjectResponse;
36 import io.minio.MakeBucketArgs;
37 import io.minio.MinioClient;
38 import io.minio.RemoveBucketArgs;
39 import io.minio.RemoveObjectArgs;
40 import io.minio.StatObjectArgs;
41 import java.io.FileInputStream;
42 import java.io.IOException;
43 import java.io.InputStream;
44 import java.nio.file.Path;
45 import javax.activation.DataHandler;
46 import okhttp3.Headers;
47 import org.apache.cxf.jaxrs.ext.multipart.Attachment;
48 import org.junit.jupiter.api.Assertions;
49 import org.junit.jupiter.api.BeforeEach;
50 import org.junit.jupiter.api.Test;
51 import org.junit.jupiter.api.extension.ExtendWith;
52 import org.mockito.Answers;
53 import org.mockito.Mock;
54 import org.mockito.MockedStatic;
55 import org.mockito.Mockito;
56 import org.mockito.MockitoAnnotations;
57 import org.mockito.junit.jupiter.MockitoExtension;
58 import org.openecomp.sdc.be.csar.storage.MinIoStorageArtifactStorageConfig.Credentials;
59 import org.openecomp.sdc.be.csar.storage.MinIoStorageArtifactStorageConfig.EndPoint;
60 import org.openecomp.sdc.be.csar.storage.exception.ArtifactStorageException;
62 @ExtendWith(MockitoExtension.class)
63 class MinIoStorageArtifactStorageManagerTest {
65 public static final String VSP_ID = "vsp-id";
66 public static final String VERSION_ID = "version-id";
67 private MinIoStorageArtifactStorageManager testSubject;
69 private MinioClient minioClient;
71 @Mock(answer = Answers.RETURNS_DEEP_STUBS)
72 private MinioClient.Builder builderMinio;
74 @Mock(answer = Answers.RETURNS_DEEP_STUBS)
75 private BucketExistsArgs.Builder builderBucketExistsArgs;
79 MockitoAnnotations.openMocks(this);
81 try (MockedStatic<MinioClient> utilities = Mockito.mockStatic(MinioClient.class)) {
82 utilities.when(MinioClient::builder).thenReturn(builderMinio);
84 .endpoint(anyString(), anyInt(), anyBoolean())
85 .credentials(anyString(), anyString())
87 ).thenReturn(minioClient);
89 testSubject = new MinIoStorageArtifactStorageManager(new MinIoStorageArtifactStorageConfig
90 (true, new EndPoint("host", 9000, false), new Credentials("accessKey", "secretKey"), "", 10_000_000));
95 void testUploadOK() throws Exception {
97 when(builderBucketExistsArgs
100 ).thenReturn(new BucketExistsArgs());
101 when(minioClient.bucketExists(any(BucketExistsArgs.class))).thenReturn(true);
103 final Attachment attachment = mockAttachment();
104 final ArtifactInfo result = testSubject.upload(VSP_ID, VERSION_ID, attachment.getDataHandler().getInputStream());
105 Assertions.assertNotNull(result);
106 Assertions.assertTrue(result instanceof MinIoArtifactInfo);
107 Assertions.assertEquals(VSP_ID, ((MinIoArtifactInfo) result).getBucket());
108 Assertions.assertTrue(((MinIoArtifactInfo) result).getObjectName().startsWith(VERSION_ID));
112 void testUploadFail() throws Exception {
114 when(builderBucketExistsArgs
117 ).thenReturn(new BucketExistsArgs());
118 when(minioClient.bucketExists(any(BucketExistsArgs.class))).thenReturn(false);
120 final Attachment attachment = mockAttachment();
121 doThrow(new RuntimeException()).when(minioClient).makeBucket(any(MakeBucketArgs.class));
122 assertThrows(ArtifactStorageException.class, () -> {
123 testSubject.upload(VSP_ID, VERSION_ID, attachment.getDataHandler().getInputStream());
128 void testPersistOK() {
129 final ArtifactInfo result = testSubject.persist(VSP_ID, VERSION_ID, new MinIoArtifactInfo(VSP_ID, VERSION_ID));
130 Assertions.assertNotNull(result);
131 Assertions.assertTrue(result instanceof MinIoArtifactInfo);
132 Assertions.assertEquals(VSP_ID, ((MinIoArtifactInfo) result).getBucket());
133 Assertions.assertEquals(VERSION_ID, ((MinIoArtifactInfo) result).getObjectName());
137 void testPersistFail() throws Exception {
138 doThrow(new RuntimeException()).when(minioClient).statObject(any(StatObjectArgs.class));
139 assertThrows(ArtifactStorageException.class, () -> {
140 testSubject.persist(VSP_ID, VERSION_ID, new MinIoArtifactInfo(VSP_ID, VERSION_ID));
145 void testIsEnabled() {
146 Assertions.assertTrue(testSubject.isEnabled());
150 void testDeleteVersionFail() throws Exception {
151 doThrow(new RuntimeException()).when(minioClient).removeObject(any(RemoveObjectArgs.class));
152 assertThrows(ArtifactStorageException.class, () -> {
153 testSubject.delete(new MinIoArtifactInfo(VSP_ID, VERSION_ID));
158 void testDeleteVspFail() throws Exception {
159 when(minioClient.bucketExists(BucketExistsArgs.builder().bucket(VSP_ID).build())).thenReturn(true);
160 doThrow(new RuntimeException()).when(minioClient).removeBucket(any(RemoveBucketArgs.class));
161 assertThrows(ArtifactStorageException.class, () -> testSubject.delete(VSP_ID));
165 void testDeleteVspBucketNotFound() throws Exception {
166 final BucketExistsArgs bucketExistsArgs = BucketExistsArgs.builder().bucket(VSP_ID).build();
168 when(minioClient.bucketExists(bucketExistsArgs)).thenReturn(false);
169 testSubject.delete(VSP_ID);
171 verify(minioClient).bucketExists(bucketExistsArgs);
175 void testGetOK() throws Exception {
176 when(minioClient.getObject(any(GetObjectArgs.class))).thenReturn(
177 new GetObjectResponse(Headers.of(), "", "", "",
178 new FileInputStream(Path.of("src/test/resources/s3StoreArtifactStorageManager/dummy.csar").toFile())));
179 final InputStream inputStream = testSubject.get(new MinIoArtifactInfo(VSP_ID, VERSION_ID));
180 assertNotNull(inputStream);
184 void testGetFail() throws Exception {
185 doThrow(new RuntimeException()).when(minioClient).getObject(any(GetObjectArgs.class));
186 assertThrows(ArtifactStorageException.class, () -> {
187 final InputStream inputStream = testSubject.get(new MinIoArtifactInfo(VSP_ID, VERSION_ID));
191 private Attachment mockAttachment() throws IOException {
192 final Attachment attachment = Mockito.mock(Attachment.class);
193 final DataHandler dataHandler = Mockito.mock(DataHandler.class);
194 final InputStream inputStream = Mockito.mock(InputStream.class);
195 when(dataHandler.getInputStream()).thenReturn(inputStream);
196 when(attachment.getDataHandler()).thenReturn(dataHandler);