|
| 1 | +package org.cloudfoundry.multiapps.controller.process.util; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 5 | +import static org.mockito.ArgumentMatchers.any; |
| 6 | +import static org.mockito.Mockito.doAnswer; |
| 7 | +import static org.mockito.Mockito.doThrow; |
| 8 | + |
| 9 | +import java.io.InputStream; |
| 10 | +import java.util.List; |
| 11 | +import java.util.function.ObjIntConsumer; |
| 12 | + |
| 13 | +import org.apache.commons.io.input.BoundedInputStream; |
| 14 | +import org.cloudfoundry.multiapps.common.SLException; |
| 15 | +import org.cloudfoundry.multiapps.controller.persistence.services.FileContentConsumer; |
| 16 | +import org.cloudfoundry.multiapps.controller.persistence.services.FileService; |
| 17 | +import org.cloudfoundry.multiapps.mta.util.EntryToInflate; |
| 18 | +import org.cloudfoundry.multiapps.mta.util.InflatorUtil; |
| 19 | +import org.junit.jupiter.api.BeforeEach; |
| 20 | +import org.junit.jupiter.api.Test; |
| 21 | +import org.mockito.Mock; |
| 22 | +import org.mockito.MockitoAnnotations; |
| 23 | + |
| 24 | +class ApplicationDigestCalculatorTest { |
| 25 | + |
| 26 | + private static final String SPACE_GUID = "123"; |
| 27 | + private static final String APP_ARCHIVE_ID = "132"; |
| 28 | + private static final String DB_DIRECTORY_MODULE_DIGEST = "71017C6429E2E1FA4ED2AD97ABF321A0"; |
| 29 | + private static final String WEB_SERVER_MODULE_DIGEST = "4C64A36CDC073B5D07947005F630DACC"; |
| 30 | + |
| 31 | + @Mock |
| 32 | + private FileService fileService; |
| 33 | + @Mock |
| 34 | + private ArchiveEntryExtractor archiveEntryExtractor; |
| 35 | + |
| 36 | + private ApplicationDigestCalculator applicationDigestCalculator; |
| 37 | + |
| 38 | + @BeforeEach |
| 39 | + void setUp() throws Exception { |
| 40 | + MockitoAnnotations.openMocks(this) |
| 41 | + .close(); |
| 42 | + doAnswer(answer -> { |
| 43 | + FileContentConsumer fileContentConsumer = answer.getArgument(2); |
| 44 | + fileContentConsumer.consume(getClass().getResourceAsStream("com.sap.mta.sample-1.2.1-beta.mtar")); |
| 45 | + return null; |
| 46 | + }).when(fileService) |
| 47 | + .consumeFileContent(any(), any(), any()); |
| 48 | + doAnswer(answer -> { |
| 49 | + FileEntryProperties fileEntryProperties = answer.getArgument(0); |
| 50 | + ArchiveEntryWithStreamPositions archiveEntryWithStreamPositions = answer.getArgument(1); |
| 51 | + ObjIntConsumer<byte[]> consumer = answer.getArgument(2); |
| 52 | + InputStream resourceAsStream = getClass().getResourceAsStream("com.sap.mta.sample-1.2.1-beta.mtar"); |
| 53 | + resourceAsStream.skip(archiveEntryWithStreamPositions.getStartPosition()); |
| 54 | + BoundedInputStream boundedInputStream = new BoundedInputStream.Builder().setInputStream(resourceAsStream) |
| 55 | + .setCount(archiveEntryWithStreamPositions.getStartPosition()) |
| 56 | + .setMaxCount(archiveEntryWithStreamPositions.getEndPosition()) |
| 57 | + .get(); |
| 58 | + InflatorUtil.inflate(new EntryToInflate("web/web-server.zip", fileEntryProperties.getMaxFileSizeInBytes(), boundedInputStream), |
| 59 | + consumer); |
| 60 | + return null; |
| 61 | + }).when(archiveEntryExtractor) |
| 62 | + .processFileEntryContent(any(), any(), any()); |
| 63 | + applicationDigestCalculator = new ApplicationDigestCalculator(fileService, new ApplicationArchiveIterator(), archiveEntryExtractor); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + void testDigestCalculationWhenModuleIsDirectory() { |
| 68 | + ArchiveEntryWithStreamPositions directoryModuleEntry = buildDbModule(); |
| 69 | + List<ArchiveEntryWithStreamPositions> archiveEntriesWithStreamPositions = List.of(directoryModuleEntry); |
| 70 | + ApplicationArchiveContext applicationArchiveContext = new ApplicationArchiveContext("db/", |
| 71 | + Integer.MAX_VALUE, |
| 72 | + archiveEntriesWithStreamPositions, |
| 73 | + SPACE_GUID, |
| 74 | + APP_ARCHIVE_ID); |
| 75 | + String appDigest = applicationDigestCalculator.calculateApplicationDigest(applicationArchiveContext); |
| 76 | + assertEquals(DB_DIRECTORY_MODULE_DIGEST, appDigest); |
| 77 | + } |
| 78 | + |
| 79 | + private ArchiveEntryWithStreamPositions buildDbModule() { |
| 80 | + return ImmutableArchiveEntryWithStreamPositions.builder() |
| 81 | + .name("db/") |
| 82 | + .startPosition(0) |
| 83 | + .endPosition(0) |
| 84 | + .compressionMethod(ArchiveEntryWithStreamPositions.CompressionMethod.DEFLATED) |
| 85 | + .isDirectory(true) |
| 86 | + .build(); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + void testDigestCalculationWhenModuleIsDirectoryAndExceedsMaxSizeLimit() { |
| 91 | + ArchiveEntryWithStreamPositions directoryModuleEntry = buildDbModule(); |
| 92 | + List<ArchiveEntryWithStreamPositions> archiveEntriesWithStreamPositions = List.of(directoryModuleEntry); |
| 93 | + ApplicationArchiveContext applicationArchiveContext = new ApplicationArchiveContext("db/", |
| 94 | + 1, |
| 95 | + archiveEntriesWithStreamPositions, |
| 96 | + SPACE_GUID, |
| 97 | + APP_ARCHIVE_ID); |
| 98 | + Exception exception = assertThrows(Exception.class, |
| 99 | + () -> applicationDigestCalculator.calculateApplicationDigest(applicationArchiveContext)); |
| 100 | + assertEquals("The size \"201\" of mta file \"db/\" exceeds the configured max size limit \"1\"", exception.getMessage()); |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + void testDigestCalculationWhenModuleIsZip() { |
| 105 | + ArchiveEntryWithStreamPositions zipModuleEntry = buildWebServerModule(); |
| 106 | + List<ArchiveEntryWithStreamPositions> archiveEntriesWithStreamPositions = List.of(zipModuleEntry); |
| 107 | + ApplicationArchiveContext applicationArchiveContext = new ApplicationArchiveContext("web/web-server.zip", |
| 108 | + Integer.MAX_VALUE, |
| 109 | + archiveEntriesWithStreamPositions, |
| 110 | + SPACE_GUID, |
| 111 | + APP_ARCHIVE_ID); |
| 112 | + String appDigest = applicationDigestCalculator.calculateApplicationDigest(applicationArchiveContext); |
| 113 | + assertEquals(WEB_SERVER_MODULE_DIGEST, appDigest); |
| 114 | + } |
| 115 | + |
| 116 | + private ArchiveEntryWithStreamPositions buildWebServerModule() { |
| 117 | + return ImmutableArchiveEntryWithStreamPositions.builder() |
| 118 | + .name("web/web-server.zip") |
| 119 | + .startPosition(531) |
| 120 | + .endPosition(678) |
| 121 | + .compressionMethod(ArchiveEntryWithStreamPositions.CompressionMethod.DEFLATED) |
| 122 | + .isDirectory(false) |
| 123 | + .build(); |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + void testDigestCalculationWhenModuleIsZipAndExceedsMaxSizeLimit() { |
| 128 | + ArchiveEntryWithStreamPositions zipModuleEntry = buildWebServerModule(); |
| 129 | + List<ArchiveEntryWithStreamPositions> archiveEntriesWithStreamPositions = List.of(zipModuleEntry); |
| 130 | + ApplicationArchiveContext applicationArchiveContext = new ApplicationArchiveContext("web/web-server.zip", |
| 131 | + 1, |
| 132 | + archiveEntriesWithStreamPositions, |
| 133 | + SPACE_GUID, |
| 134 | + APP_ARCHIVE_ID); |
| 135 | + Exception exception = assertThrows(Exception.class, |
| 136 | + () -> applicationDigestCalculator.calculateApplicationDigest(applicationArchiveContext)); |
| 137 | + assertEquals("The size \"203\" of mta file \"web/web-server.zip\" exceeds the configured max size limit \"1\"", |
| 138 | + exception.getMessage()); |
| 139 | + } |
| 140 | + |
| 141 | + @Test |
| 142 | + void testDigestCalculationWhenExceptionIsThrown() { |
| 143 | + ArchiveEntryWithStreamPositions zipModuleEntry = buildWebServerModule(); |
| 144 | + List<ArchiveEntryWithStreamPositions> archiveEntriesWithStreamPositions = List.of(zipModuleEntry); |
| 145 | + ApplicationArchiveContext applicationArchiveContext = new ApplicationArchiveContext("web/web-server.zip", |
| 146 | + 1, |
| 147 | + archiveEntriesWithStreamPositions, |
| 148 | + SPACE_GUID, |
| 149 | + APP_ARCHIVE_ID); |
| 150 | + doThrow(new SLException("Cannot calculate digest")).when(archiveEntryExtractor) |
| 151 | + .processFileEntryContent(any(), any(), any()); |
| 152 | + Exception exception = assertThrows(Exception.class, |
| 153 | + () -> applicationDigestCalculator.calculateApplicationDigest(applicationArchiveContext)); |
| 154 | + assertEquals("Cannot calculate digest", exception.getMessage()); |
| 155 | + } |
| 156 | + |
| 157 | +} |
0 commit comments