kbendick commented on a change in pull request #4037:
URL: https://github.com/apache/iceberg/pull/4037#discussion_r809546336



##########
File path: 
core/src/test/java/org/apache/iceberg/rest/requests/TestUpdateNamespacePropertiesRequest.java
##########
@@ -0,0 +1,162 @@
+/*
+ * 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.rest.requests;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import java.util.List;
+import java.util.Map;
+import org.apache.iceberg.AssertHelpers;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap;
+import org.apache.iceberg.rest.RequestResponseTestBase;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class TestUpdateNamespacePropertiesRequest extends 
RequestResponseTestBase<UpdateNamespacePropertiesRequest> {
+
+  /* Values used to fill in request fields */
+  private static final Map<String, String> UPDATES = ImmutableMap.of("owner", 
"Hank");
+  private static final List<String> REMOVALS = ImmutableList.of("foo", "bar");
+  private static final Map<String, String> EMPTY_UPDATES = ImmutableMap.of();
+  private static final List<String> EMPTY_REMOVALS = ImmutableList.of();
+
+
+  @Test
+  public void testRoundTripSerDe() throws JsonProcessingException {
+    // Full request
+    String fullJson = 
"{\"removals\":[\"foo\",\"bar\"],\"updates\":{\"owner\":\"Hank\"}}";
+    assertRoundTripSerializesEquallyFrom(
+        fullJson, 
UpdateNamespacePropertiesRequest.builder().updateAll(UPDATES).removeAll(REMOVALS).build());
+
+    // Only updates
+    String emptyRemoval = "{\"removals\":[],\"updates\":{\"owner\":\"Hank\"}}";
+    assertRoundTripSerializesEquallyFrom(
+        emptyRemoval, 
UpdateNamespacePropertiesRequest.builder().updateAll(UPDATES).removeAll(EMPTY_REMOVALS).build());
+
+    assertRoundTripSerializesEquallyFrom(
+        emptyRemoval, 
UpdateNamespacePropertiesRequest.builder().update("owner", "Hank").build());
+
+    // Only removals
+    String emptyUpdates = "{\"removals\":[\"foo\",\"bar\"],\"updates\":{}}";
+    assertRoundTripSerializesEquallyFrom(
+        emptyUpdates, 
UpdateNamespacePropertiesRequest.builder().removeAll(REMOVALS).updateAll(EMPTY_UPDATES).build());
+
+    assertRoundTripSerializesEquallyFrom(
+        emptyUpdates, 
UpdateNamespacePropertiesRequest.builder().remove("foo").remove("bar").build());
+  }
+
+  @Test
+  // Test cases that can't be constructed with our Builder class e2e but that 
will parse correctly
+  public void testCanDeserializeWithoutDefaultValues() throws 
JsonProcessingException {
+    // `removals` is left empty.
+    UpdateNamespacePropertiesRequest noRemovals = 
UpdateNamespacePropertiesRequest.builder().updateAll(UPDATES).build();
+    String jsonWithNullRemovals = 
"{\"removals\":null,\"updates\":{\"owner\":\"Hank\"}}";
+    UpdateNamespacePropertiesRequest parsed = 
deserialize(jsonWithNullRemovals);
+    assertEquals(parsed, noRemovals);
+
+    // `removals` is missing from the JSON.
+    String jsonWithMissingRemovals = "{\"updates\":{\"owner\":\"Hank\"}}";
+    assertEquals(deserialize(jsonWithMissingRemovals), noRemovals);
+
+    UpdateNamespacePropertiesRequest noUpdates = 
UpdateNamespacePropertiesRequest.builder().removeAll(REMOVALS).build();
+    String jsonWithNullUpdates = 
"{\"removals\":[\"foo\",\"bar\"],\"updates\":null}";
+    assertEquals(deserialize(jsonWithNullUpdates), noUpdates);
+  }
+
+  @Test
+  public void testParseInvalidJson() {
+    // Invalid top-level types
+    String jsonInvalidTypeOnRemovalField =
+        "{\"removals\":{\"foo\":\"bar\"},\"updates\":{\"owner\":\"Hank\"}}";
+    AssertHelpers.assertThrows(
+        "A JSON request with an invalid type for one of the fields should fail 
to parse",
+        JsonProcessingException.class,
+        () -> deserialize(jsonInvalidTypeOnRemovalField).validate()
+    );
+
+    String jsonInvalidTypeOnUpdatesField =
+        "{\"removals\":[\"foo\":\"bar\"],\"updates\":[\"owner\"]}";
+    AssertHelpers.assertThrows(
+        "A JSON value with an invalid type for one of the fields should fail 
to parse",
+        JsonProcessingException.class,
+        () -> deserialize(jsonInvalidTypeOnUpdatesField).validate()
+    );
+
+    // Valid top-level (array) types, but at least one entry in the list is 
not the expected type
+    // NOTE: non-string values that are integral types will still parse into a 
string list.
+    //    e.g. { removals: [ "foo", "bar", 1234 ] } will parse correctly.

Review comment:
       So far I've tested floats and ints (so JSON number types). Maps / arrays 
in the JSON will throw, but I'm guessing that Jackson is just calling 
`.asText()` or something on others.




-- 
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: issues-unsubscr...@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org
For additional commands, e-mail: issues-h...@iceberg.apache.org

Reply via email to