ab8c11c7c139c5331c13ed8c6ffc1af9ac4fc8da
[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.fail;
24 import static org.mockito.Mockito.when;
25
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.net.URL;
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;
45
46 @TestMethodOrder(OrderAnnotation.class)
47 class PersistentVolumeArtifactStorageManagerTest {
48
49     private static final String SRC_TEST_RESOURCES = "src/test/resources/";
50
51     private PersistentVolumeArtifactStorageManager testSubject;
52
53     @BeforeEach
54     void setUp() {
55         testSubject = new PersistentVolumeArtifactStorageManager(new PersistentVolumeArtifactStorageConfig(true, Path.of(SRC_TEST_RESOURCES)));
56     }
57
58     @AfterAll
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 -> {
63             try {
64                 Files.deleteIfExists(path);
65             } catch (IOException e) {
66                 e.printStackTrace();
67             }
68         });
69         Files.deleteIfExists(Path.of(SRC_TEST_RESOURCES + "vspId/versionId/"));
70         Files.deleteIfExists(Path.of(SRC_TEST_RESOURCES + "vspId/"));
71     }
72
73     @Test
74     @Order(1)
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/")));
81     }
82
83     @Test
84     @Order(2)
85     void testPersist() {
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/")));
91     }
92
93     @Test
94     void testIsEnabled() {
95         Assertions.assertTrue(testSubject.isEnabled());
96     }
97
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)) {
108             try {
109                 bytes = IOUtils.toByteArray(fileToUpload);
110             } catch (final IOException e) {
111                 fail("Not able to convert file to byte array");
112             }
113         }
114         when(attachment.getObject(ArgumentMatchers.any())).thenReturn(bytes);
115         return attachment;
116     }
117
118 }