|
| 1 | +/* |
| 2 | + * Copyright (c) 2012-2024 Snowflake Computing Inc. All rights reserved. |
| 3 | + */ |
| 4 | + |
| 5 | +package net.snowflake.client.core; |
| 6 | + |
| 7 | +import static org.junit.Assert.assertEquals; |
| 8 | + |
| 9 | +import com.fasterxml.jackson.databind.JsonNode; |
| 10 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 11 | +import java.nio.charset.StandardCharsets; |
| 12 | +import java.sql.SQLException; |
| 13 | +import java.util.ArrayList; |
| 14 | +import java.util.Base64; |
| 15 | +import java.util.Collection; |
| 16 | +import java.util.List; |
| 17 | +import net.snowflake.client.jdbc.SnowflakeUtil; |
| 18 | +import org.junit.After; |
| 19 | +import org.junit.Assert; |
| 20 | +import org.junit.Test; |
| 21 | +import org.junit.runner.RunWith; |
| 22 | +import org.junit.runners.Parameterized; |
| 23 | + |
| 24 | +@RunWith(Parameterized.class) |
| 25 | +public class ObjectMapperTest { |
| 26 | + private static final int jacksonDefaultMaxStringLength = 20_000_000; |
| 27 | + |
| 28 | + @Parameterized.Parameters(name = "lobSizeInMB={0}, maxJsonStringLength={1}") |
| 29 | + public static Collection<Object[]> data() { |
| 30 | + int[] lobSizeInMB = new int[] {16, 16, 32, 64, 128}; |
| 31 | + // maxJsonStringLength to be set for the corresponding LOB size |
| 32 | + int[] maxJsonStringLengths = |
| 33 | + new int[] {jacksonDefaultMaxStringLength, 23_000_000, 45_000_000, 90_000_000, 180_000_000}; |
| 34 | + List<Object[]> ret = new ArrayList<>(); |
| 35 | + for (int i = 0; i < lobSizeInMB.length; i++) { |
| 36 | + ret.add(new Object[] {lobSizeInMB[i], maxJsonStringLengths[i]}); |
| 37 | + } |
| 38 | + return ret; |
| 39 | + } |
| 40 | + |
| 41 | + private final int lobSizeInBytes; |
| 42 | + private final int maxJsonStringLength; |
| 43 | + |
| 44 | + @After |
| 45 | + public void clearProperty() { |
| 46 | + System.clearProperty(ObjectMapperFactory.MAX_JSON_STRING_LENGTH_JVM); |
| 47 | + } |
| 48 | + |
| 49 | + public ObjectMapperTest(int lobSizeInMB, int maxJsonStringLength) { |
| 50 | + // convert LOB size from MB to bytes |
| 51 | + this.lobSizeInBytes = lobSizeInMB * 1024 * 1024; |
| 52 | + this.maxJsonStringLength = maxJsonStringLength; |
| 53 | + System.setProperty( |
| 54 | + ObjectMapperFactory.MAX_JSON_STRING_LENGTH_JVM, Integer.toString(maxJsonStringLength)); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + public void testInvalidMaxJsonStringLength() throws SQLException { |
| 59 | + System.setProperty(ObjectMapperFactory.MAX_JSON_STRING_LENGTH_JVM, "abc"); |
| 60 | + // calling getObjectMapper() should log the exception but not throw |
| 61 | + // default maxJsonStringLength value will be used |
| 62 | + ObjectMapper mapper = ObjectMapperFactory.getObjectMapper(); |
| 63 | + int stringLengthInMapper = mapper.getFactory().streamReadConstraints().getMaxStringLength(); |
| 64 | + Assert.assertEquals(ObjectMapperFactory.DEFAULT_MAX_JSON_STRING_LEN, stringLengthInMapper); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void testObjectMapperWithLargeJsonString() { |
| 69 | + ObjectMapper mapper = ObjectMapperFactory.getObjectMapper(); |
| 70 | + try { |
| 71 | + JsonNode jsonNode = mapper.readTree(generateBase64EncodedJsonString(lobSizeInBytes)); |
| 72 | + Assert.assertNotNull(jsonNode); |
| 73 | + } catch (Exception e) { |
| 74 | + // exception is expected when jackson's default maxStringLength value is used while retrieving |
| 75 | + // 16M string data |
| 76 | + assertEquals(jacksonDefaultMaxStringLength, maxJsonStringLength); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + private String generateBase64EncodedJsonString(int numChar) { |
| 81 | + StringBuilder jsonStr = new StringBuilder(); |
| 82 | + String largeStr = SnowflakeUtil.randomAlphaNumeric(numChar); |
| 83 | + |
| 84 | + // encode the string and put it into a JSON formatted string |
| 85 | + jsonStr.append("[\"").append(encodeStringToBase64(largeStr)).append("\"]"); |
| 86 | + return jsonStr.toString(); |
| 87 | + } |
| 88 | + |
| 89 | + private String encodeStringToBase64(String stringToBeEncoded) { |
| 90 | + return Base64.getEncoder().encodeToString(stringToBeEncoded.getBytes(StandardCharsets.UTF_8)); |
| 91 | + } |
| 92 | +} |
0 commit comments