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.mockito.ArgumentMatchers.any;
24 import static org.mockito.ArgumentMatchers.anyBoolean;
25 import static org.mockito.ArgumentMatchers.anyInt;
26 import static org.mockito.ArgumentMatchers.anyString;
27 import static org.mockito.Mockito.when;
29 import io.minio.BucketExistsArgs;
30 import io.minio.MinioClient;
31 import java.io.IOException;
32 import java.io.InputStream;
33 import javax.activation.DataHandler;
34 import org.apache.cxf.jaxrs.ext.multipart.Attachment;
35 import org.junit.jupiter.api.Assertions;
36 import org.junit.jupiter.api.BeforeEach;
37 import org.junit.jupiter.api.Test;
38 import org.junit.jupiter.api.extension.ExtendWith;
39 import org.mockito.Answers;
40 import org.mockito.Mock;
41 import org.mockito.MockedStatic;
42 import org.mockito.Mockito;
43 import org.mockito.MockitoAnnotations;
44 import org.mockito.junit.jupiter.MockitoExtension;
45 import org.openecomp.sdc.be.csar.storage.MinIoStorageArtifactStorageConfig.Credentials;
46 import org.openecomp.sdc.be.csar.storage.MinIoStorageArtifactStorageConfig.EndPoint;
48 @ExtendWith(MockitoExtension.class)
49 class MinIoStorageArtifactStorageManagerTest {
51 public static final String VSP_ID = "vsp-id";
52 public static final String VERSION_ID = "version-id";
53 private MinIoStorageArtifactStorageManager testSubject;
55 private MinioClient minioClient;
57 @Mock(answer = Answers.RETURNS_DEEP_STUBS)
58 private MinioClient.Builder builderMinio;
60 @Mock(answer = Answers.RETURNS_DEEP_STUBS)
61 private BucketExistsArgs.Builder builderBucketExistsArgs;
65 MockitoAnnotations.openMocks(this);
67 try (MockedStatic<MinioClient> utilities = Mockito.mockStatic(MinioClient.class)) {
68 utilities.when(MinioClient::builder).thenReturn(builderMinio);
70 .endpoint(anyString(), anyInt(), anyBoolean())
71 .credentials(anyString(), anyString())
73 ).thenReturn(minioClient);
75 testSubject = new MinIoStorageArtifactStorageManager(
76 new MinIoStorageArtifactStorageConfig(true, new EndPoint("host", 9000, false), new Credentials("accessKey", "secretKey"), ""));
81 void testUpload() throws Exception {
83 when(builderBucketExistsArgs
86 ).thenReturn(new BucketExistsArgs());
87 when(minioClient.bucketExists(any(BucketExistsArgs.class))).thenReturn(true);
89 final Attachment attachment = mockAttachment();
90 final ArtifactInfo result = testSubject.upload(VSP_ID, VERSION_ID, attachment.getDataHandler().getInputStream());
91 Assertions.assertNotNull(result);
92 Assertions.assertTrue(result instanceof MinIoArtifactInfo);
93 Assertions.assertEquals(VSP_ID, ((MinIoArtifactInfo) result).getBucket());
94 Assertions.assertTrue(((MinIoArtifactInfo) result).getObjectName().startsWith(VERSION_ID + "--"));
99 final ArtifactInfo result = testSubject.persist(VSP_ID, VERSION_ID, new MinIoArtifactInfo(VSP_ID, VERSION_ID));
100 Assertions.assertNotNull(result);
101 Assertions.assertTrue(result instanceof MinIoArtifactInfo);
102 Assertions.assertEquals(VSP_ID, ((MinIoArtifactInfo) result).getBucket());
103 Assertions.assertEquals(VERSION_ID, ((MinIoArtifactInfo) result).getObjectName());
107 void testIsEnabled() {
108 Assertions.assertTrue(testSubject.isEnabled());
111 private Attachment mockAttachment() throws IOException {
112 final Attachment attachment = Mockito.mock(Attachment.class);
113 final DataHandler dataHandler = Mockito.mock(DataHandler.class);
114 final InputStream inputStream = Mockito.mock(InputStream.class);
115 when(dataHandler.getInputStream()).thenReturn(inputStream);
116 when(attachment.getDataHandler()).thenReturn(dataHandler);