Implement improved MinIo client
[sdc.git] / common-be / src / test / java / org / openecomp / sdc / be / csar / storage / MinIoStorageArtifactStorageManagerTest.java
1 /*
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
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.be.csar.storage;
22
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;
28
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;
47
48 @ExtendWith(MockitoExtension.class)
49 class MinIoStorageArtifactStorageManagerTest {
50
51     public static final String VSP_ID = "vsp-id";
52     public static final String VERSION_ID = "version-id";
53     private MinIoStorageArtifactStorageManager testSubject;
54     @Mock
55     private MinioClient minioClient;
56
57     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
58     private MinioClient.Builder builderMinio;
59
60     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
61     private BucketExistsArgs.Builder builderBucketExistsArgs;
62
63     @BeforeEach
64     void setUp() {
65         MockitoAnnotations.openMocks(this);
66
67         try (MockedStatic<MinioClient> utilities = Mockito.mockStatic(MinioClient.class)) {
68             utilities.when(MinioClient::builder).thenReturn(builderMinio);
69             when(builderMinio
70                 .endpoint(anyString(), anyInt(), anyBoolean())
71                 .credentials(anyString(), anyString())
72                 .build()
73             ).thenReturn(minioClient);
74
75             testSubject = new MinIoStorageArtifactStorageManager(new MinIoStorageArtifactStorageConfig
76                 (true, new EndPoint("host", 9000, false), new Credentials("accessKey", "secretKey"), "", 10_000_000));
77         }
78     }
79
80     @Test
81     void testUpload() throws Exception {
82
83         when(builderBucketExistsArgs
84             .bucket(anyString())
85             .build()
86         ).thenReturn(new BucketExistsArgs());
87         when(minioClient.bucketExists(any(BucketExistsArgs.class))).thenReturn(true);
88
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));
95     }
96
97     @Test
98     void testPersist() {
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());
104     }
105
106     @Test
107     void testIsEnabled() {
108         Assertions.assertTrue(testSubject.isEnabled());
109     }
110
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);
117         return attachment;
118     }
119
120 }