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.when;
32 import io.minio.BucketExistsArgs;
33 import io.minio.GetObjectArgs;
34 import io.minio.GetObjectResponse;
35 import io.minio.MakeBucketArgs;
36 import io.minio.MinioClient;
37 import io.minio.RemoveBucketArgs;
38 import io.minio.RemoveObjectArgs;
39 import io.minio.StatObjectArgs;
40 import java.io.FileInputStream;
41 import java.io.IOException;
42 import java.io.InputStream;
43 import java.nio.file.Path;
44 import javax.activation.DataHandler;
45 import okhttp3.Headers;
46 import org.apache.cxf.jaxrs.ext.multipart.Attachment;
47 import org.junit.jupiter.api.Assertions;
48 import org.junit.jupiter.api.BeforeEach;
49 import org.junit.jupiter.api.Test;
50 import org.junit.jupiter.api.extension.ExtendWith;
51 import org.mockito.Answers;
52 import org.mockito.Mock;
53 import org.mockito.MockedStatic;
54 import org.mockito.Mockito;
55 import org.mockito.MockitoAnnotations;
56 import org.mockito.junit.jupiter.MockitoExtension;
57 import org.openecomp.sdc.be.csar.storage.MinIoStorageArtifactStorageConfig.Credentials;
58 import org.openecomp.sdc.be.csar.storage.MinIoStorageArtifactStorageConfig.EndPoint;
59 import org.openecomp.sdc.be.csar.storage.exception.ArtifactStorageException;
61 @ExtendWith(MockitoExtension.class)
62 class MinIoStorageArtifactStorageManagerTest {
64 public static final String VSP_ID = "vsp-id";
65 public static final String VERSION_ID = "version-id";
66 private MinIoStorageArtifactStorageManager testSubject;
68 private MinioClient minioClient;
70 @Mock(answer = Answers.RETURNS_DEEP_STUBS)
71 private MinioClient.Builder builderMinio;
73 @Mock(answer = Answers.RETURNS_DEEP_STUBS)
74 private BucketExistsArgs.Builder builderBucketExistsArgs;
78 MockitoAnnotations.openMocks(this);
80 try (MockedStatic<MinioClient> utilities = Mockito.mockStatic(MinioClient.class)) {
81 utilities.when(MinioClient::builder).thenReturn(builderMinio);
83 .endpoint(anyString(), anyInt(), anyBoolean())
84 .credentials(anyString(), anyString())
86 ).thenReturn(minioClient);
88 testSubject = new MinIoStorageArtifactStorageManager(new MinIoStorageArtifactStorageConfig
89 (true, new EndPoint("host", 9000, false), new Credentials("accessKey", "secretKey"), "", 10_000_000));
94 void testUploadOK() throws Exception {
96 when(builderBucketExistsArgs
99 ).thenReturn(new BucketExistsArgs());
100 when(minioClient.bucketExists(any(BucketExistsArgs.class))).thenReturn(true);
102 final Attachment attachment = mockAttachment();
103 final ArtifactInfo result = testSubject.upload(VSP_ID, VERSION_ID, attachment.getDataHandler().getInputStream());
104 Assertions.assertNotNull(result);
105 Assertions.assertTrue(result instanceof MinIoArtifactInfo);
106 Assertions.assertEquals(VSP_ID, ((MinIoArtifactInfo) result).getBucket());
107 Assertions.assertTrue(((MinIoArtifactInfo) result).getObjectName().startsWith(VERSION_ID));
111 void testUploadFail() throws Exception {
113 when(builderBucketExistsArgs
116 ).thenReturn(new BucketExistsArgs());
117 when(minioClient.bucketExists(any(BucketExistsArgs.class))).thenReturn(false);
119 final Attachment attachment = mockAttachment();
120 doThrow(new RuntimeException()).when(minioClient).makeBucket(any(MakeBucketArgs.class));
121 assertThrows(ArtifactStorageException.class, () -> {
122 testSubject.upload(VSP_ID, VERSION_ID, attachment.getDataHandler().getInputStream());
127 void testPersistOK() {
128 final ArtifactInfo result = testSubject.persist(VSP_ID, VERSION_ID, new MinIoArtifactInfo(VSP_ID, VERSION_ID));
129 Assertions.assertNotNull(result);
130 Assertions.assertTrue(result instanceof MinIoArtifactInfo);
131 Assertions.assertEquals(VSP_ID, ((MinIoArtifactInfo) result).getBucket());
132 Assertions.assertEquals(VERSION_ID, ((MinIoArtifactInfo) result).getObjectName());
136 void testPersistFail() throws Exception {
137 doThrow(new RuntimeException()).when(minioClient).statObject(any(StatObjectArgs.class));
138 assertThrows(ArtifactStorageException.class, () -> {
139 testSubject.persist(VSP_ID, VERSION_ID, new MinIoArtifactInfo(VSP_ID, VERSION_ID));
144 void testIsEnabled() {
145 Assertions.assertTrue(testSubject.isEnabled());
149 void testDeleteVersionFail() throws Exception {
150 doThrow(new RuntimeException()).when(minioClient).removeObject(any(RemoveObjectArgs.class));
151 assertThrows(ArtifactStorageException.class, () -> {
152 testSubject.delete(new MinIoArtifactInfo(VSP_ID, VERSION_ID));
157 void testDeleteVspFail() throws Exception {
158 doThrow(new RuntimeException()).when(minioClient).removeBucket(any(RemoveBucketArgs.class));
159 assertThrows(ArtifactStorageException.class, () -> {
160 testSubject.delete(VSP_ID);
165 void testGetOK() throws Exception {
166 when(minioClient.getObject(any(GetObjectArgs.class))).thenReturn(
167 new GetObjectResponse(Headers.of(), "", "", "",
168 new FileInputStream(Path.of("src/test/resources/s3StoreArtifactStorageManager/dummy.csar").toFile())));
169 final InputStream inputStream = testSubject.get(new MinIoArtifactInfo(VSP_ID, VERSION_ID));
170 assertNotNull(inputStream);
174 void testGetFail() throws Exception {
175 doThrow(new RuntimeException()).when(minioClient).getObject(any(GetObjectArgs.class));
176 assertThrows(ArtifactStorageException.class, () -> {
177 final InputStream inputStream = testSubject.get(new MinIoArtifactInfo(VSP_ID, VERSION_ID));
181 private Attachment mockAttachment() throws IOException {
182 final Attachment attachment = Mockito.mock(Attachment.class);
183 final DataHandler dataHandler = Mockito.mock(DataHandler.class);
184 final InputStream inputStream = Mockito.mock(InputStream.class);
185 when(dataHandler.getInputStream()).thenReturn(inputStream);
186 when(attachment.getDataHandler()).thenReturn(dataHandler);