From 128257564dcfe72efc84eb8bf0091f34da117db1 Mon Sep 17 00:00:00 2001 From: Aliaksandr Stsiapanay Date: Fri, 6 Sep 2024 16:53:57 +0300 Subject: [PATCH] fix: GCP Core deployment start to fail after some time #472 (#475) --- .../epam/aidial/core/util/Compression.java | 14 ++----- .../aidial/core/util/CompressionTest.java | 38 +++++++++++++++++++ 2 files changed, 41 insertions(+), 11 deletions(-) create mode 100644 src/test/java/com/epam/aidial/core/util/CompressionTest.java diff --git a/src/main/java/com/epam/aidial/core/util/Compression.java b/src/main/java/com/epam/aidial/core/util/Compression.java index 49c78e0ba..430c8ec62 100644 --- a/src/main/java/com/epam/aidial/core/util/Compression.java +++ b/src/main/java/com/epam/aidial/core/util/Compression.java @@ -29,24 +29,16 @@ public byte[] compress(String type, byte[] data) { } @SneakyThrows - public InputStream decompress(String type, InputStream input) { + public byte[] decompress(String type, byte[] input) { if (!type.equals("gzip")) { throw new IllegalArgumentException("Unsupported compression: " + type); } - - try { - return new GZIPInputStream(input); + try (InputStream decompressed = new GZIPInputStream(new ByteArrayInputStream(input))) { + return decompressed.readAllBytes(); } catch (ZipException e) { // special case for GCP cloud storage, due to jclouds bug https://issues.apache.org/jira/projects/JCLOUDS/issues/JCLOUDS-1633 log.warn("Failed to decompress provided input: {}", e.getMessage()); return input; } } - - @SneakyThrows - public byte[] decompress(String type, byte[] input) { - try (InputStream decompressed = decompress(type, new ByteArrayInputStream(input))) { - return decompressed.readAllBytes(); - } - } } \ No newline at end of file diff --git a/src/test/java/com/epam/aidial/core/util/CompressionTest.java b/src/test/java/com/epam/aidial/core/util/CompressionTest.java new file mode 100644 index 000000000..e32ccf2ef --- /dev/null +++ b/src/test/java/com/epam/aidial/core/util/CompressionTest.java @@ -0,0 +1,38 @@ +package com.epam.aidial.core.util; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class CompressionTest { + + @Test + public void testNormalFlow() { + byte[] content = "Hello world!".getBytes(); + + assertThrows(IllegalArgumentException.class, () -> Compression.compress("wrong", content)); + + byte[] compressed = Compression.compress("gzip", content); + + assertNotNull(compressed); + assertTrue(compressed.length > 0); + assertThrows(IllegalArgumentException.class, () -> Compression.decompress("wrong", compressed)); + + byte[] actual = Compression.decompress("gzip", compressed); + + assertArrayEquals(content, actual); + } + + @Test + public void testGcpWorkaround() { + byte[] content = "Hello world!".getBytes(); + + byte[] actual = Compression.decompress("gzip", content); + + assertEquals(content, actual); + } +}