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.fail;
24 import static org.mockito.Mockito.when;
26 import java.io.IOException;
27 import java.io.InputStream;
29 import java.nio.file.Files;
30 import java.nio.file.Path;
31 import java.util.Objects;
32 import javax.activation.DataHandler;
33 import org.apache.commons.io.IOUtils;
34 import org.apache.cxf.jaxrs.ext.multipart.Attachment;
35 import org.apache.cxf.jaxrs.ext.multipart.ContentDisposition;
36 import org.junit.jupiter.api.AfterAll;
37 import org.junit.jupiter.api.Assertions;
38 import org.junit.jupiter.api.BeforeEach;
39 import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
40 import org.junit.jupiter.api.Order;
41 import org.junit.jupiter.api.Test;
42 import org.junit.jupiter.api.TestMethodOrder;
43 import org.mockito.ArgumentMatchers;
44 import org.mockito.Mockito;
46 @TestMethodOrder(OrderAnnotation.class)
47 class PersistentVolumeArtifactStorageManagerTest {
49 private static final String SRC_TEST_RESOURCES = "src/test/resources/";
51 private PersistentVolumeArtifactStorageManager testSubject;
55 testSubject = new PersistentVolumeArtifactStorageManager(new PersistentVolumeArtifactStorageConfig(true, Path.of(SRC_TEST_RESOURCES)));
59 static void tearDown() throws IOException {
60 Files.move(Path.of(SRC_TEST_RESOURCES + "vspId/versionId/versionId"),
61 Path.of(SRC_TEST_RESOURCES + "persistentVolumeArtifactStorageManager/dummy.csar"));
62 Files.list(Path.of("src/test/resources/vspId/versionId/")).forEach(path -> {
64 Files.deleteIfExists(path);
65 } catch (IOException e) {
69 Files.deleteIfExists(Path.of(SRC_TEST_RESOURCES + "vspId/versionId/"));
70 Files.deleteIfExists(Path.of(SRC_TEST_RESOURCES + "vspId/"));
75 void testUpload() throws IOException {
76 final Attachment attachment = mockAttachment("dummy.csar", this.getClass().getResource("/persistentVolumeArtifactStorageManager/dummy.csar"));
77 final ArtifactInfo result = testSubject.upload("vspId", "versionId", attachment.getDataHandler().getInputStream());
78 Assertions.assertNotNull(result);
79 Assertions.assertNotNull(result.getPath());
80 Assertions.assertTrue(result.getPath().startsWith(Path.of(SRC_TEST_RESOURCES + "vspId/versionId/")));
86 final ArtifactInfo result = testSubject.persist("vspId", "versionId",
87 new PersistentStorageArtifactInfo(Path.of(SRC_TEST_RESOURCES + "persistentVolumeArtifactStorageManager/dummy.csar")));
88 Assertions.assertNotNull(result);
89 Assertions.assertNotNull(result.getPath());
90 Assertions.assertTrue(result.getPath().startsWith(Path.of(SRC_TEST_RESOURCES + "vspId/versionId/")));
94 void testIsEnabled() {
95 Assertions.assertTrue(testSubject.isEnabled());
98 private Attachment mockAttachment(final String fileName, final URL fileToUpload) throws IOException {
99 final Attachment attachment = Mockito.mock(Attachment.class);
100 when(attachment.getContentDisposition()).thenReturn(new ContentDisposition("test"));
101 final DataHandler dataHandler = Mockito.mock(DataHandler.class);
102 when(dataHandler.getName()).thenReturn(fileName);
103 final InputStream inputStream = Mockito.mock(InputStream.class);
104 when(dataHandler.getInputStream()).thenReturn(inputStream);
105 when(attachment.getDataHandler()).thenReturn(dataHandler);
106 byte[] bytes = "upload package Test".getBytes();
107 if (Objects.nonNull(fileToUpload)) {
109 bytes = IOUtils.toByteArray(fileToUpload);
110 } catch (final IOException e) {
111 fail("Not able to convert file to byte array");
114 when(attachment.getObject(ArgumentMatchers.any())).thenReturn(bytes);