rdblue commented on code in PR #7741: URL: https://github.com/apache/iceberg/pull/7741#discussion_r1216995446
########## core/src/test/java/org/apache/iceberg/rest/requests/TestCommitTransactionRequestParser.java: ########## @@ -0,0 +1,245 @@ +/* + * 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 static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import com.fasterxml.jackson.databind.JsonNode; +import org.apache.iceberg.MetadataUpdate; +import org.apache.iceberg.catalog.TableIdentifier; +import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList; +import org.apache.iceberg.rest.requests.UpdateTableRequest.UpdateRequirement; +import org.junit.jupiter.api.Test; + +public class TestCommitTransactionRequestParser { + + @Test + public void nullAndEmptyCheck() { + assertThatThrownBy(() -> CommitTransactionRequestParser.toJson(null)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Invalid commit tx request: null"); + + assertThatThrownBy(() -> CommitTransactionRequestParser.fromJson((JsonNode) null)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Cannot parse commit tx request from null object"); + + assertThatThrownBy(() -> CommitTransactionRequestParser.fromJson("{}")) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Cannot parse missing field: table-changes"); + + assertThatThrownBy(() -> CommitTransactionRequestParser.fromJson("{\"table-changes\":{}}")) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Cannot parse commit tx request from non-array: {}"); + + assertThatThrownBy(() -> CommitTransactionRequestParser.fromJson("{\"table-changes\":[]}")) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Invalid table changes: empty"); + } + + @Test + public void invalidTableIdentifier() { + assertThatThrownBy( + () -> + CommitTransactionRequestParser.fromJson( + "{\"table-changes\":[{\"ns1.table1\" : \"ns1.table1\"}]}")) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Cannot parse missing field: identifier"); + + assertThatThrownBy( + () -> + CommitTransactionRequestParser.fromJson( + "{\"table-changes\":[{\"identifier\" : {}}]}")) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Cannot parse missing string: name"); + + assertThatThrownBy( + () -> + CommitTransactionRequestParser.fromJson( + "{\"table-changes\":[{\"identifier\" : { \"name\": 23}}]}")) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Cannot parse to a string value: name: 23"); + } + + @Test + public void invalidRequirements() { + assertThatThrownBy( + () -> + CommitTransactionRequestParser.fromJson( + "{\"table-changes\":[{\"identifier\":{\"namespace\":[\"ns1\"],\"name\":\"table1\"}}]}")) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Cannot parse missing field: requirements"); + + assertThatThrownBy( + () -> + CommitTransactionRequestParser.fromJson( + "{\"table-changes\":[{\"identifier\":{\"namespace\":[\"ns1\"],\"name\":\"table1\"}," + + "\"requirements\":[23],\"updates\":[]}]}")) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Cannot parse update requirement from non-object value: 23"); + + assertThatThrownBy( + () -> + CommitTransactionRequestParser.fromJson( + "{\"table-changes\":[{\"identifier\":{\"namespace\":[\"ns1\"],\"name\":\"table1\"}," + + "\"requirements\":[{}],\"updates\":[]}]}")) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Cannot parse update requirement. Missing field: type"); + + assertThatThrownBy( + () -> + CommitTransactionRequestParser.fromJson( + "{\"table-changes\":[{\"identifier\":{\"namespace\":[\"ns1\"],\"name\":\"table1\"}," + + "\"requirements\":[{\"type\":\"assert-table-uuid\"}],\"updates\":[]}]}")) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Cannot parse missing string: uuid"); + } + + @Test + public void invalidMetadataUpdates() { + assertThatThrownBy( + () -> + CommitTransactionRequestParser.fromJson( + "{\"table-changes\":[{\"identifier\":{\"namespace\":[\"ns1\"],\"name\":\"table1\"},\"requirements\":[]}]}")) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Cannot parse missing field: updates"); Review Comment: I think that this is currently allowed and I think we should continue to allow it. I have two reasons: 1. It's easy for a service to detect a no-op request and return a 200 code immediately. That's much better than having the service fail because a client wasn't smart enough to detect that the request wasn't necessary. 2. There will be requests with only requirements produced by multi-table commits. For example, if you read a table then you have to validate that it has the same or an equivalent table state. That will produce a `AssertRefSnapshotID` requirement with no changes. -- 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]
