kbendick commented on code in PR #4693: URL: https://github.com/apache/iceberg/pull/4693#discussion_r865500738
########## 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: Opened a PR to update this, as the error also exists in `getIntOrNull` as well. https://github.com/apache/iceberg/pull/4696 -- 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]
