pbajpai21 commented on code in PR #18050:
URL: https://github.com/apache/iceberg/pull/18050#discussion_r4214800805


##########
core/src/test/java/org/apache/iceberg/util/TestSerializationUtil.java:
##########
@@ -0,0 +1,198 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.iceberg.util;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+import java.io.NotSerializableException;
+import java.io.Serializable;
+import java.io.StreamCorruptedException;
+import java.io.UncheckedIOException;
+import java.util.Map;
+import java.util.function.Function;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.iceberg.hadoop.HadoopConfigurable;
+import org.apache.iceberg.hadoop.SerializableConfiguration;
+import org.apache.iceberg.relocated.com.google.common.collect.Maps;
+import org.junit.jupiter.api.Test;
+
+class TestSerializationUtil {
+
+  @Test
+  void bytesRoundTripPreservesValue() {
+    String original = "s3://bucket/table/metadata/v1.metadata.json";
+    byte[] bytes = SerializationUtil.serializeToBytes(original);
+    String roundTripped = SerializationUtil.deserializeFromBytes(bytes);
+    assertThat(roundTripped).isEqualTo(original);
+  }
+
+  @Test
+  void bytesRoundTripPreservesMapContents() {
+    Map<String, Integer> original = Maps.newHashMap();
+    original.put("added-records", 42);
+    original.put("total-files", 7);
+
+    byte[] bytes = SerializationUtil.serializeToBytes(original);
+    Map<String, Integer> roundTripped = 
SerializationUtil.deserializeFromBytes(bytes);
+    assertThat(roundTripped).isEqualTo(original);
+  }
+
+  @Test
+  void deserializeFromBytesReturnsNullForNullInput() {
+    Object result = SerializationUtil.deserializeFromBytes(null);
+    assertThat(result).isNull();
+  }
+
+  @Test
+  void bytesRoundTripPreservesNull() {
+    // serializeToBytes(null) writes a real serialized null (not an empty 
array), so this exercises
+    // the write-and-read-back path rather than the null-input short circuit 
above.
+    byte[] bytes = SerializationUtil.serializeToBytes(null);
+    assertThat(bytes).isNotNull();
+
+    Object roundTripped = SerializationUtil.deserializeFromBytes(bytes);
+    assertThat(roundTripped).isNull();
+  }
+
+  @Test
+  void base64RoundTripPreservesValue() {
+    String original = "s3://bucket/table/metadata/v1.metadata.json";
+    String encoded = SerializationUtil.serializeToBase64(original);
+    String roundTripped = SerializationUtil.deserializeFromBase64(encoded);
+    assertThat(roundTripped).isEqualTo(original);
+  }
+
+  @Test
+  void deserializeFromBase64ReturnsNullForNullInput() {
+    Object result = SerializationUtil.deserializeFromBase64(null);
+    assertThat(result).isNull();
+  }
+
+  @Test
+  void base64RoundTripHandlesMimeLineWrapping() {
+    // A payload whose base64 exceeds 76 characters forces the MIME encoder to 
insert line breaks;
+    // the round trip verifies the MIME decoder tolerates that wrapping.
+    String original = "a".repeat(1000);
+    String encoded = SerializationUtil.serializeToBase64(original);
+    assertThat(encoded).contains("\r\n");
+
+    String roundTripped = SerializationUtil.deserializeFromBase64(encoded);
+    assertThat(roundTripped).isEqualTo(original);
+  }
+
+  @Test
+  void serializeToBytesAppliesCustomConfSerializerToHadoopConfigurable() {
+    Configuration conf = new Configuration(false);
+    conf.set("test.key", "test.value");
+    HadoopConfigurableFixture configurable = new 
HadoopConfigurableFixture(conf);
+
+    // Return a distinctive supplier whose configuration carries a marker key. 
If serializeToBytes
+    // ignored our serializer's result and built its own, the marker would be 
absent after the
+    // round trip, so asserting on it proves our output is what actually got 
serialized.
+    Function<Configuration, SerializableSupplier<Configuration>> 
confSerializer =
+        c -> {
+          Configuration marked = new Configuration(c);
+          marked.set("custom.serializer.marker", "applied");
+          return new SerializableConfiguration(marked);
+        };
+
+    byte[] bytes = SerializationUtil.serializeToBytes(configurable, 
confSerializer);
+    HadoopConfigurableFixture roundTripped = 
SerializationUtil.deserializeFromBytes(bytes);
+
+    assertThat(configurable.serializeConfWithInvoked)
+        .as("serializeConfWith should be called for a HadoopConfigurable 
object")
+        .isTrue();
+    assertThat(roundTripped.getConf().get("custom.serializer.marker"))
+        .as(
+            "the configuration produced by the provided confSerializer should 
be the one serialized")
+        .isEqualTo("applied");
+  }
+
+  @Test
+  void serializeToBytesWrapsIOException() {
+    // A non-Serializable object makes ObjectOutputStream throw 
NotSerializableException.
+    Object notSerializable = new Object();
+    assertThatThrownBy(() -> 
SerializationUtil.serializeToBytes(notSerializable))
+        .isInstanceOf(UncheckedIOException.class)
+        .hasMessage("Failed to serialize object")
+        .hasCauseInstanceOf(NotSerializableException.class);
+  }
+
+  @Test
+  void deserializeFromBytesWrapsIOException() {
+    // Bytes that are not a valid object stream make ObjectInputStream throw 
an IOException.

Review Comment:
   Done - switched to `hasMessageContaining("serialize")/"deserialize")`. This 
stops pinning the exact wording. Thanks.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to