rdblue commented on a change in pull request #4037: URL: https://github.com/apache/iceberg/pull/4037#discussion_r809463045
########## 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. + String invalidJsonWrongTypeInRemovalsList = + "{\"removals\":[\"foo\",\"bar\", {\"owner\": \"Hank\"}],\"updates\":{\"owner\":\"Hank\"}}"; + AssertHelpers.assertThrows( + "A JSON value with an invalid type inside one of the list fields should fail to parse", + JsonProcessingException.class, + () -> deserialize(invalidJsonWrongTypeInRemovalsList).validate() + ); + + // All fields are valid types, but all are empty + String jsonWithAllFieldsAsEmptyCollection = + "{\"removals\":[],\"updates\":{}}"; + AssertHelpers.assertThrows( + "A JSON value with empty collections for all fields should fail to parse and validate", + IllegalArgumentException.class, + "Cannot create a request to update a namespace's properties without specifying any props to update or remove", + () -> deserialize(jsonWithAllFieldsAsEmptyCollection).validate() + ); + + String emptyJson = "{}"; + AssertHelpers.assertThrows( + "An empty JSON should fail to parse", + IllegalArgumentException.class, + "Cannot create a request to update a namespace's properties without specifying any props to update or remove", + () -> deserialize(emptyJson).validate() + ); + + String nullJson = null; + AssertHelpers.assertThrows( + "A null JSON should fail to parse", + IllegalArgumentException.class, + () -> deserialize(nullJson).validate() + ); + } + + @Override + public String[] allFieldsFromSpec() { + return new String[] { "updates", "removals" }; + } + + @Override + public UpdateNamespacePropertiesRequest createExampleInstance() { + return UpdateNamespacePropertiesRequest.builder() + .updateAll(UPDATES) + .removeAll(REMOVALS) + .build(); + } + + @Override + public void assertEquals(UpdateNamespacePropertiesRequest actual, UpdateNamespacePropertiesRequest expected) { + Assert.assertEquals("Properties to update should be equal", actual.updates(), expected.updates()); + Assert.assertEquals("Properties to remove should be equal", actual.removals(), expected.removals()); Review comment: I think this should build a set from both removals lists. Otherwise, this could be flaky because the builder users a set to accumulate removals and doesn't provide order guarantees. -- 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]
