09e62841e6ea037ff6f95ec98a1e36732e905075
[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.when;
31
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;
60
61 @ExtendWith(MockitoExtension.class)
62 class MinIoStorageArtifactStorageManagerTest {
63
64     public static final String VSP_ID = "vsp-id";
65     public static final String VERSION_ID = "version-id";
66     private MinIoStorageArtifactStorageManager testSubject;
67     @Mock
68     private MinioClient minioClient;
69
70     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
71     private MinioClient.Builder builderMinio;
72
73     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
74     private BucketExistsArgs.Builder builderBucketExistsArgs;
75
76     @BeforeEach
77     void setUp() {
78         MockitoAnnotations.openMocks(this);
79
80         try (MockedStatic<MinioClient> utilities = Mockito.mockStatic(MinioClient.class)) {
81             utilities.when(MinioClient::builder).thenReturn(builderMinio);
82             when(builderMinio
83                 .endpoint(anyString(), anyInt(), anyBoolean())
84                 .credentials(anyString(), anyString())
85                 .build()
86             ).thenReturn(minioClient);
87
88             testSubject = new MinIoStorageArtifactStorageManager(new MinIoStorageArtifactStorageConfig
89                 (true, new EndPoint("host", 9000, false), new Credentials("accessKey", "secretKey"), "", 10_000_000));
90         }
91     }
92
93     @Test
94     void testUploadOK() throws Exception {
95
96         when(builderBucketExistsArgs
97             .bucket(anyString())
98             .build()
99         ).thenReturn(new BucketExistsArgs());
100         when(minioClient.bucketExists(any(BucketExistsArgs.class))).thenReturn(true);
101
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));
108     }
109
110     @Test
111     void testUploadFail() throws Exception {
112
113         when(builderBucketExistsArgs
114             .bucket(anyString())
115             .build()
116         ).thenReturn(new BucketExistsArgs());
117         when(minioClient.bucketExists(any(BucketExistsArgs.class))).thenReturn(false);
118
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());
123         });
124     }
125
126     @Test
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());
133     }
134
135     @Test
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));
140         });
141     }
142
143     @Test
144     void testIsEnabled() {
145         Assertions.assertTrue(testSubject.isEnabled());
146     }
147
148     @Test
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));
153         });
154     }
155
156     @Test
157     void testDeleteVspFail() throws Exception {
158         doThrow(new RuntimeException()).when(minioClient).removeBucket(any(RemoveBucketArgs.class));
159         assertThrows(ArtifactStorageException.class, () -> {
160             testSubject.delete(VSP_ID);
161         });
162     }
163
164     @Test
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);
171     }
172
173     @Test
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));
178         });
179     }
180
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);
187         return attachment;
188     }
189
190 }