kbendick commented on code in PR #5118: URL: https://github.com/apache/iceberg/pull/5118#discussion_r909086973
########## core/src/test/java/org/apache/iceberg/rest/responses/TestLoadTableResponse.java: ########## @@ -0,0 +1,218 @@ +/* + * 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.responses; + +import com.fasterxml.jackson.core.JsonProcessingException; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Map; +import org.apache.iceberg.AssertHelpers; +import org.apache.iceberg.NullOrder; +import org.apache.iceberg.PartitionSpec; +import org.apache.iceberg.Schema; +import org.apache.iceberg.Snapshot; +import org.apache.iceberg.SortOrder; +import org.apache.iceberg.TableMetadata; +import org.apache.iceberg.TableMetadataParser; +import org.apache.iceberg.expressions.Expressions; +import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap; +import org.apache.iceberg.relocated.com.google.common.collect.Lists; +import org.apache.iceberg.rest.RequestResponseTestBase; +import org.apache.iceberg.types.Types; +import org.junit.Assert; +import org.junit.Test; + +import static org.apache.iceberg.TestHelpers.assertSameSchemaList; + +public class TestLoadTableResponse extends RequestResponseTestBase<LoadTableResponse> { + + private static final String TEST_METADATA_LOCATION = "s3://bucket/test/location/metadata/v1.metadata.json"; + + private static final String TEST_TABLE_LOCATION = "s3://bucket/test/location"; + + private static final Schema SCHEMA_7 = new Schema(7, + Types.NestedField.required(1, "x", Types.LongType.get()), + Types.NestedField.required(2, "y", Types.LongType.get(), "comment"), + Types.NestedField.required(3, "z", Types.LongType.get()) + ); + + private static final PartitionSpec SPEC_5 = PartitionSpec.builderFor(SCHEMA_7).withSpecId(5).build(); + + private static final SortOrder SORT_ORDER_3 = SortOrder.builderFor(SCHEMA_7) + .withOrderId(3) + .asc("y", NullOrder.NULLS_FIRST) + .desc(Expressions.bucket("z", 4), NullOrder.NULLS_LAST) + .build(); + + private static final Map<String, String> TABLE_PROPS = ImmutableMap.of( + "format-version", "1", + "owner", "hank"); + + private static final Map<String, String> CONFIG = ImmutableMap.of("foo", "bar"); + + @Override + public String[] allFieldsFromSpec() { + return new String[] { "metadata-location", "metadata", "config" }; + } + + @Override + public LoadTableResponse createExampleInstance() { + TableMetadata metadata = + TableMetadata + .buildFrom( + TableMetadata.newTableMetadata(SCHEMA_7, SPEC_5, SORT_ORDER_3, TEST_TABLE_LOCATION, TABLE_PROPS)) + .withMetadataLocation(TEST_METADATA_LOCATION) + .build(); + + return LoadTableResponse.builder() + .withTableMetadata(metadata) + .addAllConfig(CONFIG) + .build(); + } + + @Override + public LoadTableResponse deserialize(String json) throws JsonProcessingException { + LoadTableResponse resp = mapper().readValue(json, LoadTableResponse.class); + resp.validate(); + return resp; + } + + @Test + public void testFailures() { + AssertHelpers.assertThrows( + "Table metadata should be required", + NullPointerException.class, + "Invalid metadata: null", + () -> LoadTableResponse.builder().build()); + } + + @Test + public void testRoundTripSerde() throws Exception { + // Default fields are missing in this JSON + String tableMetadataV1Json = readTableMetadataInputFile("TableMetadataV1Valid.json"); + TableMetadata metadataV1 = TableMetadataParser.fromJson(null, TEST_METADATA_LOCATION, tableMetadataV1Json); + // Convert the TableMetadata JSON from the file to an object and then back to JSON so that missing fields + // are filled in with their default values. + String json1 = String.format( + "{\"metadata-location\":\"%s\",\"metadata\":%s,\"config\":{\"foo\":\"bar\"}}", + TEST_METADATA_LOCATION, TableMetadataParser.toJson(metadataV1)); + LoadTableResponse resp1 = LoadTableResponse.builder() + .withTableMetadata(metadataV1) + .addAllConfig(CONFIG) + .build(); + assertRoundTripSerializesEquallyFrom(json1, resp1); + + String tableMetadataV2Json = readTableMetadataInputFile("TableMetadataV2Valid.json"); + TableMetadata metadataV2 = TableMetadataParser.fromJson(null, TEST_METADATA_LOCATION, tableMetadataV2Json); + // Convert the TableMetadata JSON from the file to an object and then back to JSON so that missing fields + // are filled in with their default values. + String json2 = String.format( + "{\"metadata-location\":\"%s\",\"metadata\":%s,\"config\":{\"foo\":\"bar\"}}", + TEST_METADATA_LOCATION, TableMetadataParser.toJson(metadataV2)); + LoadTableResponse resp2 = LoadTableResponse.builder() + .withTableMetadata(metadataV2) + .addAllConfig(CONFIG) + .build(); + assertRoundTripSerializesEquallyFrom(json2, resp2); + } + + @Test + public void testCanDeserializeWithoutDefaultValues() throws Exception { + String metadataJson = readTableMetadataInputFile("TableMetadataV1Valid.json"); + String json1 = String.format( + "{\"metadata-location\":\"%s\",\"metadata\":%s,\"config\":{\"foo\":\"bar\"}}", + TEST_METADATA_LOCATION, metadataJson); + + TableMetadata metadataV1 = TableMetadataParser.fromJson(null, TEST_METADATA_LOCATION, metadataJson); + LoadTableResponse resp1 = LoadTableResponse.builder() + .withTableMetadata(metadataV1) + .addAllConfig(CONFIG) + .build(); + assertEquals(deserialize(json1), resp1); Review Comment: In the version you were looking at, this was testing that the JSON parses into the class as expected, even if default values are not present. Because default values are not present in the external JSON, I used those to represent missing values (which was the source of confusion). For the `round trip` tests, it tests that the JSON deserializes into the object _as well as that the JSON matches exactly as expected the serialiazed output of the object_ That's the `round trip` in the `assertRoundTripSerialzesEquallyFrom`: https://github.com/apache/iceberg/blob/22298818cb7eba13294eda3b2dd696b773e0f170/core/src/test/java/org/apache/iceberg/rest/RequestResponseTestBase.java#L102-L109 -- 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]
