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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.openecomp.sdc.be.csar.storage;
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;
31 import java.nio.charset.StandardCharsets;
32 import java.nio.file.Path;
34 import java.util.Map.Entry;
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;
45 class CsarSizeReducerTest {
48 private CsarPackageReducerConfiguration csarPackageReducerConfiguration;
50 private CsarSizeReducer csarSizeReducer;
54 MockitoAnnotations.openMocks(this);
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));
66 final var csarPath = Path.of("src/test/resources/csarSizeReducer/" + fileName);
68 final Map<String, byte[]> originalCsar = ZipUtils.readZip(csarPath.toFile(), false);
70 final byte[] reduce = csarSizeReducer.reduce(csarPath);
72 final Map<String, byte[]> reducedCsar = ZipUtils.readZip(reduce, false);
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));
81 final String extention = fileName.substring(fileName.lastIndexOf('.') + 1);
82 switch (extention.toLowerCase()) {
84 verifyZIP(pathToReduce1, pathToReduce2, sizeLimit, reducedCsar, originalFilePath, originalBytes);
87 verifyCSAR(pathToReduce1, pathToReduce2, sizeLimit, reducedCsar, originalFilePath, originalBytes);
90 fail("Unexpected file extention");
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));
103 assertArrayEquals(originalBytes, reducedCsar.get(originalFilePath),
104 String.format("File '%s' expected to be equal", originalFilePath));
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));
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));
119 assertArrayEquals(originalBytes, reducedCsar.get(originalFilePath),
120 String.format("File '%s' expected to be equal", originalFilePath));