kbendick commented on code in PR #4693:
URL: https://github.com/apache/iceberg/pull/4693#discussion_r865496498


##########
core/src/test/java/org/apache/iceberg/rest/requests/TestUpdateRequirementParser.java:
##########
@@ -0,0 +1,312 @@
+/*
+ * 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 java.util.List;
+import org.apache.iceberg.AssertHelpers;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList;
+import org.apache.iceberg.rest.requests.UpdateTableRequest.UpdateRequirement;
+import org.assertj.core.api.Assertions;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class TestUpdateRequirementParser {
+
+  @Test
+  public void testUpdateRequirementWithoutRequirementTypeCannotParse() {
+    List<String> invalidJson = ImmutableList.of(
+        "{\"type\":null,\"uuid\":\"2cc52516-5e73-41f2-b139-545d41a4e151\"}",
+        "{\"uuid\":\"2cc52516-5e73-41f2-b139-545d41a4e151\"}"
+    );
+
+    for (String json : invalidJson) {
+      AssertHelpers.assertThrows(
+          "UpdateRequirement without a recognized requirement type should fail 
to deserialize",
+          IllegalArgumentException.class,
+          "Cannot parse update requirement. Missing field: type",
+          () -> UpdateRequirementParser.fromJson(json));
+    }
+  }
+
+  @Test
+  public void testAssertUUIDFromJson() {
+    String requirementType = UpdateRequirementParser.ASSERT_TABLE_UUID;
+    String uuid = "2cc52516-5e73-41f2-b139-545d41a4e151";
+    String json = 
String.format("{\"type\":\"assert-table-uuid\",\"uuid\":\"%s\"}", uuid);
+    UpdateRequirement expected = new UpdateRequirement.AssertTableUUID(uuid);
+    assertEquals(requirementType, expected, 
UpdateRequirementParser.fromJson(json));
+  }
+
+  @Test
+  public void testAssertUUIDToJson() {
+    String uuid = "2cc52516-5e73-41f2-b139-545d41a4e151";
+    String expected = 
String.format("{\"type\":\"assert-table-uuid\",\"uuid\":\"%s\"}", uuid);
+    UpdateRequirement actual = new UpdateRequirement.AssertTableUUID(uuid);
+    Assert.assertEquals("AssertTableUUID should convert to the correct JSON 
value",
+        expected, UpdateRequirementParser.toJson(actual));
+  }
+
+  @Test
+  public void testAssertTableDoesNotExistFromJson() {
+    String requirementType = 
UpdateRequirementParser.ASSERT_TABLE_DOES_NOT_EXIST;
+    String json = "{\"type\":\"assert-create\"}";
+    UpdateRequirement expected = new 
UpdateRequirement.AssertTableDoesNotExist();
+    assertEquals(requirementType, expected, 
UpdateRequirementParser.fromJson(json));
+  }
+
+  @Test
+  public void testAssertTableDoesNotExistToJson() {
+    String expected  = "{\"type\":\"assert-create\"}";
+    UpdateRequirement actual = new UpdateRequirement.AssertTableDoesNotExist();
+    Assert.assertEquals("AssertTableDoesNotExist should convert to the correct 
JSON value",
+        expected, UpdateRequirementParser.toJson(actual));
+  }
+
+  @Test
+  public void testAssertRefSnapshotIdToJson() {
+    String requirementType = UpdateRequirementParser.ASSERT_REF_SNAPSHOT_ID;
+    String name = "snapshot-name";
+    Long snapshotId = 1L;
+    String json = 
String.format("{\"type\":\"%s\",\"name\":\"%s\",\"snapshot-id\":%d}",
+        requirementType, name, snapshotId);
+    UpdateRequirement expected = new 
UpdateRequirement.AssertRefSnapshotID(name, snapshotId);
+    assertEquals(requirementType, expected, 
UpdateRequirementParser.fromJson(json));
+  }
+
+  @Test
+  public void testAssertRefSnapshotIdFromJson() {

Review Comment:
   Added the tests. There seems to be a bug in `JsonUtils.getLongOrNull`. 
Should I open a separate PR for that for the benefit of people who cherry-pick?
   
   The check should be `node.hasNonNull`. I'm going to open a separate PR 
because there's also issues with the int one I think. 
   
   
https://github.com/apache/iceberg/blob/4ae2002bd46bf8e1c20db03cffc6319237e4d74a/core/src/main/java/org/apache/iceberg/util/JsonUtil.java#L71-L79_



-- 
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