MySQLJsonValueDecoder { public static Serializable decode(final ByteBuf byteBuf) { int valueType = byteBuf.readUnsignedByte() & 0xff; StringBuilder result = new StringBuilder(); decodeValue(valueType, 1, byteBuf, result); return result.toString(); }  static Serializable decode(final ByteBuf byteBuf);  }
@Test public void assertDecodeSmallJsonObjectWithLiteral() { List<JsonEntry> jsonEntries = new LinkedList<>(); jsonEntries.add(new JsonEntry(JsonValueTypes.LITERAL, "key1", JsonValueTypes.LITERAL_NULL)); jsonEntries.add(new JsonEntry(JsonValueTypes.LITERAL, "key2", JsonValueTypes.LITERAL_TRUE)); jsonEntries.add(new JsonEntry(JsonValueTypes.LITERAL, "key3", JsonValueTypes.LITERAL_FALSE)); ByteBuf payload = mockJsonObjectByteBuf(jsonEntries, true); String actual = (String) MySQLJsonValueDecoder.decode(payload); assertThat(actual, is("{\"key1\":null,\"key2\":true,\"key3\":false}")); }
@Test public void assertDecodeLargeJsonObjectWithLiteral() { List<JsonEntry> jsonEntries = new LinkedList<>(); jsonEntries.add(new JsonEntry(JsonValueTypes.LITERAL, "key1", JsonValueTypes.LITERAL_NULL)); jsonEntries.add(new JsonEntry(JsonValueTypes.LITERAL, "key2", JsonValueTypes.LITERAL_TRUE)); jsonEntries.add(new JsonEntry(JsonValueTypes.LITERAL, "key3", JsonValueTypes.LITERAL_FALSE)); ByteBuf payload = mockJsonObjectByteBuf(jsonEntries, false); String actual = (String) MySQLJsonValueDecoder.decode(payload); assertThat(actual, is("{\"key1\":null,\"key2\":true,\"key3\":false}")); }
@Test public void assertDecodeSmallJsonObjectWithInt16() { List<JsonEntry> jsonEntries = new LinkedList<>(); jsonEntries.add(new JsonEntry(JsonValueTypes.INT16, "key1", 0x00007fff)); jsonEntries.add(new JsonEntry(JsonValueTypes.INT16, "key2", 0x00008000)); ByteBuf payload = mockJsonObjectByteBuf(jsonEntries, true); String actual = (String) MySQLJsonValueDecoder.decode(payload); assertThat(actual, is("{\"key1\":32767,\"key2\":-32768}")); }
@Test public void assertDecodeLargeJsonObjectWithInt16() { List<JsonEntry> jsonEntries = new LinkedList<>(); jsonEntries.add(new JsonEntry(JsonValueTypes.INT16, "key1", 0x00007fff)); jsonEntries.add(new JsonEntry(JsonValueTypes.INT16, "key2", 0x00008000)); ByteBuf payload = mockJsonObjectByteBuf(jsonEntries, false); String actual = (String) MySQLJsonValueDecoder.decode(payload); assertThat(actual, is("{\"key1\":32767,\"key2\":-32768}")); }
@Test public void assertDecodeSmallJsonObjectWithUInt16() { List<JsonEntry> jsonEntries = new LinkedList<>(); jsonEntries.add(new JsonEntry(JsonValueTypes.UINT16, "key1", 0x00007fff)); jsonEntries.add(new JsonEntry(JsonValueTypes.UINT16, "key2", 0x00008000)); ByteBuf payload = mockJsonObjectByteBuf(jsonEntries, true); String actual = (String) MySQLJsonValueDecoder.decode(payload); assertThat(actual, is("{\"key1\":32767,\"key2\":32768}")); }
@Test public void assertDecodeLargeJsonObjectWithUInt16() { List<JsonEntry> jsonEntries = new LinkedList<>(); jsonEntries.add(new JsonEntry(JsonValueTypes.UINT16, "key1", 0x00007fff)); jsonEntries.add(new JsonEntry(JsonValueTypes.UINT16, "key2", 0x00008000)); ByteBuf payload = mockJsonObjectByteBuf(jsonEntries, false); String actual = (String) MySQLJsonValueDecoder.decode(payload); assertThat(actual, is("{\"key1\":32767,\"key2\":32768}")); }
@Test public void assertDecodeSmallJsonObjectWithInt32() { List<JsonEntry> jsonEntries = new LinkedList<>(); jsonEntries.add(new JsonEntry(JsonValueTypes.INT32, "key1", Integer.MAX_VALUE)); jsonEntries.add(new JsonEntry(JsonValueTypes.INT32, "key2", Integer.MIN_VALUE)); ByteBuf payload = mockJsonObjectByteBuf(jsonEntries, true); String actual = (String) MySQLJsonValueDecoder.decode(payload); assertThat(actual, is("{\"key1\":2147483647,\"key2\":-2147483648}")); }
@Test public void assertDecodeLargeJsonObjectWithInt32() { List<JsonEntry> jsonEntries = new LinkedList<>(); jsonEntries.add(new JsonEntry(JsonValueTypes.INT32, "key1", Integer.MAX_VALUE)); jsonEntries.add(new JsonEntry(JsonValueTypes.INT32, "key2", Integer.MIN_VALUE)); ByteBuf payload = mockJsonObjectByteBuf(jsonEntries, false); String actual = (String) MySQLJsonValueDecoder.decode(payload); assertThat(actual, is("{\"key1\":2147483647,\"key2\":-2147483648}")); }
@Test public void assertDecodeSmallJsonObjectWithUInt32() { List<JsonEntry> jsonEntries = new LinkedList<>(); jsonEntries.add(new JsonEntry(JsonValueTypes.UINT32, "key1", Integer.MAX_VALUE)); jsonEntries.add(new JsonEntry(JsonValueTypes.UINT32, "key2", Integer.MIN_VALUE)); ByteBuf payload = mockJsonObjectByteBuf(jsonEntries, true); String actual = (String) MySQLJsonValueDecoder.decode(payload); assertThat(actual, is("{\"key1\":2147483647,\"key2\":2147483648}")); }
@Test public void assertDecodeLargeJsonObjectWithUInt32() { List<JsonEntry> jsonEntries = new LinkedList<>(); jsonEntries.add(new JsonEntry(JsonValueTypes.UINT32, "key1", Integer.MAX_VALUE)); jsonEntries.add(new JsonEntry(JsonValueTypes.UINT32, "key2", Integer.MIN_VALUE)); ByteBuf payload = mockJsonObjectByteBuf(jsonEntries, false); String actual = (String) MySQLJsonValueDecoder.decode(payload); assertThat(actual, is("{\"key1\":2147483647,\"key2\":2147483648}")); }
@Test public void assertDecodeSmallJsonObjectWithInt64() { List<JsonEntry> jsonEntries = new LinkedList<>(); jsonEntries.add(new JsonEntry(JsonValueTypes.INT64, "key1", Long.MAX_VALUE)); jsonEntries.add(new JsonEntry(JsonValueTypes.INT64, "key2", Long.MIN_VALUE)); ByteBuf payload = mockJsonObjectByteBuf(jsonEntries, true); String actual = (String) MySQLJsonValueDecoder.decode(payload); assertThat(actual, is("{\"key1\":9223372036854775807,\"key2\":-9223372036854775808}")); }
@Test public void assertDecodeSmallJsonObjectWithUInt64() { List<JsonEntry> jsonEntries = new LinkedList<>(); jsonEntries.add(new JsonEntry(JsonValueTypes.UINT64, "key1", Long.MAX_VALUE)); jsonEntries.add(new JsonEntry(JsonValueTypes.UINT64, "key2", Long.MIN_VALUE)); ByteBuf payload = mockJsonObjectByteBuf(jsonEntries, true); String actual = (String) MySQLJsonValueDecoder.decode(payload); assertThat(actual, is("{\"key1\":9223372036854775807,\"key2\":9223372036854775808}")); }
@Test public void assertDecodeSmallJsonObjectWithDouble() { List<JsonEntry> jsonEntries = new LinkedList<>(); jsonEntries.add(new JsonEntry(JsonValueTypes.DOUBLE, "key1", Double.MAX_VALUE)); ByteBuf payload = mockJsonObjectByteBuf(jsonEntries, true); String actual = (String) MySQLJsonValueDecoder.decode(payload); assertThat(actual, is("{\"key1\":1.7976931348623157E308}")); }
@Test public void assertDecodeSmallJsonObjectWithString() { List<JsonEntry> jsonEntries = new LinkedList<>(); String value1 = ""; String value2 = Strings.repeat("1", (int) (Math.pow(2, 7) - 1)); String value3 = Strings.repeat("1", (int) (Math.pow(2, 7) - 1 + 1)); String value4 = Strings.repeat("1", (int) (Math.pow(2, 14) - 1)); jsonEntries.add(new JsonEntry(JsonValueTypes.STRING, "key1", value1)); jsonEntries.add(new JsonEntry(JsonValueTypes.STRING, "key2", value2)); jsonEntries.add(new JsonEntry(JsonValueTypes.STRING, "key3", value3)); jsonEntries.add(new JsonEntry(JsonValueTypes.STRING, "key4", value4)); ByteBuf payload = mockJsonObjectByteBuf(jsonEntries, true); String actual = (String) MySQLJsonValueDecoder.decode(payload); assertThat(actual, is(String.format("{\"key1\":\"%s\",\"key2\":\"%s\",\"key3\":\"%s\",\"key4\":\"%s\"}", value1, value2, value3, value4))); }
@Test public void assertDecodeSmallJsonObjectWithSubJson() { List<JsonEntry> subJsons = Collections.singletonList(new JsonEntry(JsonValueTypes.INT32, "key1", 111)); ByteBuf payload = mockJsonObjectByteBuf(Collections.singletonList(new JsonEntry(JsonValueTypes.SMALL_JSON_OBJECT, "subJson", subJsons)), true); String actual = (String) MySQLJsonValueDecoder.decode(payload); assertThat(actual, is("{\"subJson\":{\"key1\":111}}")); }
@Test public void assertDecodeLargeJsonObjectWithSubJson() { List<JsonEntry> subJsons = Collections.singletonList(new JsonEntry(JsonValueTypes.INT32, "key1", 111)); ByteBuf payload = mockJsonObjectByteBuf(Collections.singletonList(new JsonEntry(JsonValueTypes.SMALL_JSON_OBJECT, "subJson", subJsons)), false); String actual = (String) MySQLJsonValueDecoder.decode(payload); assertThat(actual, is("{\"subJson\":{\"key1\":111}}")); }
@Test public void assertDecodeSmallJsonObjectWithSubArray() { List<JsonEntry> subArrays = Collections.singletonList(new JsonEntry(JsonValueTypes.INT32, null, 111)); ByteBuf payload = mockJsonObjectByteBuf(Collections.singletonList(new JsonEntry(JsonValueTypes.SMALL_JSON_ARRAY, "subJson", subArrays)), true); String actual = (String) MySQLJsonValueDecoder.decode(payload); assertThat(actual, is("{\"subJson\":[111]}")); }
@Test public void assertDecodeSmallJsonArray() { List<JsonEntry> jsonEntries = new LinkedList<>(); jsonEntries.add(new JsonEntry(JsonValueTypes.INT16, null, 0x00007fff)); jsonEntries.add(new JsonEntry(JsonValueTypes.INT16, null, 0x00008000)); ByteBuf payload = mockJsonArrayByteBuf(jsonEntries, true); String actual = (String) MySQLJsonValueDecoder.decode(payload); assertThat(actual, is("[32767,-32768]")); }
@Test public void assertDecodeLargeJsonArray() { List<JsonEntry> jsonEntries = new LinkedList<>(); jsonEntries.add(new JsonEntry(JsonValueTypes.INT16, null, 0x00007fff)); jsonEntries.add(new JsonEntry(JsonValueTypes.INT16, null, 0x00008000)); ByteBuf payload = mockJsonArrayByteBuf(jsonEntries, false); String actual = (String) MySQLJsonValueDecoder.decode(payload); assertThat(actual, is("[32767,-32768]")); }