Implement 'Signed Large CSAR' support
[sdc.git] / common-be / src / test / java / org / openecomp / sdc / be / csar / storage / CsarSizeReducerTest.java
1 /*
2  * -
3  *  ============LICENSE_START=======================================================
4  *  Copyright (C) 2021 Nordix Foundation.
5  *  ================================================================================
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *       http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================
20  */
21
22 package org.openecomp.sdc.be.csar.storage;
23
24 import static org.junit.jupiter.api.Assertions.assertArrayEquals;
25 import static org.junit.jupiter.api.Assertions.assertEquals;
26 import static org.junit.jupiter.api.Assertions.assertNotEquals;
27 import static org.junit.jupiter.api.Assertions.assertTrue;
28 import static org.junit.jupiter.api.Assertions.fail;
29 import static org.mockito.Mockito.when;
30
31 import java.nio.charset.StandardCharsets;
32 import java.nio.file.Path;
33 import java.util.Map;
34 import java.util.Map.Entry;
35 import java.util.Set;
36 import org.junit.jupiter.api.BeforeEach;
37 import org.junit.jupiter.params.ParameterizedTest;
38 import org.junit.jupiter.params.provider.ValueSource;
39 import org.mockito.InjectMocks;
40 import org.mockito.Mock;
41 import org.mockito.MockitoAnnotations;
42 import org.openecomp.sdc.common.zip.ZipUtils;
43 import org.openecomp.sdc.common.zip.exception.ZipException;
44
45 class CsarSizeReducerTest {
46
47     @Mock
48     private CsarPackageReducerConfiguration csarPackageReducerConfiguration;
49     @InjectMocks
50     private CsarSizeReducer csarSizeReducer;
51
52     @BeforeEach
53     void setUp() {
54         MockitoAnnotations.openMocks(this);
55     }
56
57     @ParameterizedTest
58     @ValueSource(strings = {"dummyToReduce.zip", "dummyToReduce.csar", "dummyToNotReduce.csar"})
59     void reduceByPathAndSizeTest(String fileName) throws ZipException {
60         final var pathToReduce1 = Path.of("Files/images");
61         final var pathToReduce2 = Path.of("Files/Scripts/my_script.sh");
62         final var sizeLimit = 150000L;
63         when(csarPackageReducerConfiguration.getSizeLimit()).thenReturn(sizeLimit);
64         when(csarPackageReducerConfiguration.getFoldersToStrip()).thenReturn(Set.of(pathToReduce1, pathToReduce2));
65
66         final var csarPath = Path.of("src/test/resources/csarSizeReducer/" + fileName);
67
68         final Map<String, byte[]> originalCsar = ZipUtils.readZip(csarPath.toFile(), false);
69
70         final byte[] reduce = csarSizeReducer.reduce(csarPath);
71
72         final Map<String, byte[]> reducedCsar = ZipUtils.readZip(reduce, false);
73
74         assertEquals(originalCsar.keySet().size(), reducedCsar.keySet().size(), "No file should be removed");
75         for (final Entry<String, byte[]> originalEntry : originalCsar.entrySet()) {
76             final var originalFilePath = originalEntry.getKey();
77             final byte[] originalBytes = originalEntry.getValue();
78             assertTrue(reducedCsar.containsKey(originalFilePath),
79                 String.format("No file should be removed, but it is missing original file '%s'", originalFilePath));
80
81             final String extention = fileName.substring(fileName.lastIndexOf('.') + 1);
82             switch (extention.toLowerCase()) {
83                 case "zip":
84                     verifyZIP(pathToReduce1, pathToReduce2, sizeLimit, reducedCsar, originalFilePath, originalBytes);
85                     break;
86                 case "csar":
87                     verifyCSAR(pathToReduce1, pathToReduce2, sizeLimit, reducedCsar, originalFilePath, originalBytes);
88                     break;
89                 default:
90                     fail("Unexpected file extention");
91                     break;
92             }
93         }
94     }
95
96     private void verifyCSAR(final Path pathToReduce1, final Path pathToReduce2, final long sizeLimit, final Map<String, byte[]> reducedCsar,
97                             final String originalFilePath, final byte[] originalBytes) {
98         if (originalFilePath.startsWith(pathToReduce1.toString()) || originalFilePath.startsWith(pathToReduce2.toString())
99             || originalBytes.length > sizeLimit) {
100             assertArrayEquals("".getBytes(StandardCharsets.UTF_8), reducedCsar.get(originalFilePath),
101                 String.format("File '%s' expected to be reduced to empty string", originalFilePath));
102         } else {
103             assertArrayEquals(originalBytes, reducedCsar.get(originalFilePath),
104                 String.format("File '%s' expected to be equal", originalFilePath));
105         }
106     }
107
108     private void verifyZIP(final Path pathToReduce1, final Path pathToReduce2, final long sizeLimit, final Map<String, byte[]> reducedCsar,
109                            final String originalFilePath, final byte[] originalBytes) {
110         if (originalFilePath.startsWith(pathToReduce1.toString()) || originalFilePath.startsWith(pathToReduce2.toString())
111             || originalBytes.length > sizeLimit) {
112             assertArrayEquals("".getBytes(StandardCharsets.UTF_8), reducedCsar.get(originalFilePath),
113                 String.format("File '%s' expected to be reduced to empty string", originalFilePath));
114         } else {
115             if (originalFilePath.endsWith(".csar") && csarSizeReducer.getReduced().get()) {
116                 assertNotEquals(originalBytes.length, reducedCsar.get(originalFilePath).length,
117                     String.format("File '%s' expected to be NOT equal", originalFilePath));
118             } else {
119                 assertArrayEquals(originalBytes, reducedCsar.get(originalFilePath),
120                     String.format("File '%s' expected to be equal", originalFilePath));
121             }
122         }
123     }
124 }