4d12a0be399a2eafca7d6f71c26794b1a287a8b6
[sdc.git] /
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.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;
32
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;
61
62 @ExtendWith(MockitoExtension.class)
63 class MinIoStorageArtifactStorageManagerTest {
64
65     public static final String VSP_ID = "vsp-id";
66     public static final String VERSION_ID = "version-id";
67     private MinIoStorageArtifactStorageManager testSubject;
68     @Mock
69     private MinioClient minioClient;
70
71     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
72     private MinioClient.Builder builderMinio;
73
74     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
75     private BucketExistsArgs.Builder builderBucketExistsArgs;
76
77     @BeforeEach
78     void setUp() {
79         MockitoAnnotations.openMocks(this);
80
81         try (MockedStatic<MinioClient> utilities = Mockito.mockStatic(MinioClient.class)) {
82             utilities.when(MinioClient::builder).thenReturn(builderMinio);
83             when(builderMinio
84                 .endpoint(anyString(), anyInt(), anyBoolean())
85                 .credentials(anyString(), anyString())
86                 .build()
87             ).thenReturn(minioClient);
88
89             testSubject = new MinIoStorageArtifactStorageManager(new MinIoStorageArtifactStorageConfig
90                 (true, new EndPoint("host", 9000, false), new Credentials("accessKey", "secretKey"), "", 10_000_000));
91         }
92     }
93
94     @Test
95     void testUploadOK() throws Exception {
96
97         when(builderBucketExistsArgs
98             .bucket(anyString())
99             .build()
100         ).thenReturn(new BucketExistsArgs());
101         when(minioClient.bucketExists(any(BucketExistsArgs.class))).thenReturn(true);
102
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));
109     }
110
111     @Test
112     void testUploadFail() throws Exception {
113
114         when(builderBucketExistsArgs
115             .bucket(anyString())
116             .build()
117         ).thenReturn(new BucketExistsArgs());
118         when(minioClient.bucketExists(any(BucketExistsArgs.class))).thenReturn(false);
119
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());
124         });
125     }
126
127     @Test
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());
134     }
135
136     @Test
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));
141         });
142     }
143
144     @Test
145     void testIsEnabled() {
146         Assertions.assertTrue(testSubject.isEnabled());
147     }
148
149     @Test
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));
154         });
155     }
156
157     @Test
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));
162     }
163
164     @Test
165     void testDeleteVspBucketNotFound() throws Exception {
166         final BucketExistsArgs bucketExistsArgs = BucketExistsArgs.builder().bucket(VSP_ID).build();
167         //when
168         when(minioClient.bucketExists(bucketExistsArgs)).thenReturn(false);
169         testSubject.delete(VSP_ID);
170         //then
171         verify(minioClient).bucketExists(bucketExistsArgs);
172     }
173
174     @Test
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);
181     }
182
183     @Test
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));
188         });
189     }
190
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);
197         return attachment;
198     }
199
200 }