uros-b commented on code in PR #18420:
URL: https://github.com/apache/iceberg/pull/18420#discussion_r4411855813


##########
bigquery/src/test/java/org/apache/iceberg/gcp/bigquery/TestBigQueryMetastoreClientImpl.java:
##########
@@ -0,0 +1,348 @@
+/*
+ * 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.gcp.bigquery;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+import com.google.api.client.http.GenericUrl;
+import com.google.api.client.http.HttpRequest;
+import com.google.api.client.http.HttpTransport;
+import com.google.api.client.http.LowLevelHttpRequest;
+import com.google.api.client.http.LowLevelHttpResponse;
+import com.google.api.services.bigquery.model.Dataset;
+import com.google.api.services.bigquery.model.DatasetReference;
+import com.google.api.services.bigquery.model.ExternalCatalogTableOptions;
+import com.google.api.services.bigquery.model.Table;
+import com.google.api.services.bigquery.model.TableReference;
+import com.google.auth.oauth2.AccessToken;
+import com.google.auth.oauth2.GoogleCredentials;
+import com.google.cloud.bigquery.BigQueryOptions;
+import com.google.cloud.http.HttpTransportOptions;
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.charset.StandardCharsets;
+import java.time.Instant;
+import java.util.Date;
+import org.apache.iceberg.BaseMetastoreTableOperations;
+import org.apache.iceberg.exceptions.AlreadyExistsException;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap;
+import org.junit.jupiter.api.Test;
+
+class TestBigQueryMetastoreClientImpl {
+
+  private static final String DATASET_OK_JSON =
+      "{\"kind\":\"bigquery#dataset\","
+          + 
"\"datasetReference\":{\"projectId\":\"test-project\",\"datasetId\":\"test-dataset\"}}";
+
+  private static final String TABLE_OK_JSON =
+      "{\"kind\":\"bigquery#table\","
+          + "\"tableReference\":{\"projectId\":\"test-project\","
+          + "\"datasetId\":\"test-dataset\",\"tableId\":\"test-table\"}}";
+
+  private static final String CONFLICT_JSON =
+      "{\"error\":{\"code\":409,\"message\":\"Already Exists\","
+          + "\"status\":\"ALREADY_EXISTS\","
+          + "\"errors\":[{\"message\":\"Already 
Exists\",\"domain\":\"global\","
+          + "\"reason\":\"duplicate\"}]}}";
+
+  private static GoogleCredentials credentials(String token) {
+    return GoogleCredentials.create(
+        new AccessToken(token, Date.from(Instant.now().plusSeconds(3600))));
+  }
+
+  private static BigQueryOptions optionsWithDefaults() {
+    return BigQueryOptions.newBuilder()
+        .setProjectId("test-project")
+        .setCredentials(credentials("default-token"))
+        .build();
+  }
+
+  private static BigQueryOptions optionsWithTransport(HttpTransportOptions 
transportOptions) {
+    return BigQueryOptions.newBuilder()
+        .setProjectId("test-project")
+        .setCredentials(credentials("default-token"))
+        .setTransportOptions(transportOptions)
+        .build();
+  }
+
+  private static Dataset testDataset() {
+    return new Dataset()
+        .setDatasetReference(
+            new 
DatasetReference().setProjectId("test-project").setDatasetId("test-dataset"));
+  }
+
+  private static Table testTable() {
+    return new Table()
+        .setTableReference(
+            new TableReference()
+                .setProjectId("test-project")
+                .setDatasetId("test-dataset")
+                .setTableId("test-table"))
+        .setExternalCatalogTableOptions(
+            new ExternalCatalogTableOptions()
+                .setParameters(
+                    ImmutableMap.of(
+                        BaseMetastoreTableOperations.METADATA_LOCATION_PROP,
+                        "gs://bucket/path/meta",
+                        BaseMetastoreTableOperations.TABLE_TYPE_PROP,
+                        
BaseMetastoreTableOperations.ICEBERG_TABLE_TYPE_VALUE)));
+  }
+
+  /**
+   * Fake transport that captures the connect/read timeouts and Authorization 
header from the last
+   * executed HTTP request. LowLevelHttpRequest.setTimeout(connect, read) is 
the single entry point
+   * through which HttpRequest propagates both timeout values to the low-level 
transport.
+   */
+  private static class CapturingTransport extends HttpTransport {
+    // Integer.MIN_VALUE means setTimeout was never called.
+    volatile int capturedConnectTimeout = Integer.MIN_VALUE;
+    volatile int capturedReadTimeout = Integer.MIN_VALUE;
+    volatile String capturedAuthHeader;
+
+    private final int responseStatus;
+    private final String responseBody;
+
+    CapturingTransport(int responseStatus, String responseBody) {
+      this.responseStatus = responseStatus;
+      this.responseBody = responseBody;
+    }
+
+    @Override
+    protected LowLevelHttpRequest buildRequest(String method, String url) {
+      return new LowLevelHttpRequest() {
+        private int connectMs = Integer.MIN_VALUE;
+        private int readMs = Integer.MIN_VALUE;
+        private String authHeader;
+
+        @Override
+        public void setTimeout(int connectTimeout, int readTimeout) {
+          connectMs = connectTimeout;
+          readMs = readTimeout;
+        }
+
+        @Override
+        public void addHeader(String name, String value) {
+          if ("Authorization".equalsIgnoreCase(name)) {
+            authHeader = value;
+          }
+        }
+
+        @Override
+        public LowLevelHttpResponse execute() throws IOException {
+          capturedConnectTimeout = connectMs;
+          capturedReadTimeout = readMs;
+          capturedAuthHeader = authHeader;
+          return new FakeHttpResponse(responseStatus, responseBody);
+        }
+      };
+    }
+  }
+
+  private static class FakeHttpResponse extends LowLevelHttpResponse {

Review Comment:
   FakeHttpResponse hand-rolls about 59 lines of LowLevelHttpResponse 
overrides, but com.google.api.client.testing.http.MockLowLevelHttpResponse (in 
the google-http-client jar this module already resolves) provides the status, 
reason phrase, content and content-type setters the fake implements. Swapping 
it in, and dropping the unneeded throws Exception on the nine tests, is the 
most direct remaining trim from the prior round.



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