kbendick commented on a change in pull request #4245:
URL: https://github.com/apache/iceberg/pull/4245#discussion_r835648521



##########
File path: core/src/test/java/org/apache/iceberg/rest/TestHTTPClient.java
##########
@@ -0,0 +1,250 @@
+/*
+ * 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;
+
+import com.fasterxml.jackson.annotation.JsonAutoDetect;
+import com.fasterxml.jackson.annotation.PropertyAccessor;
+import com.fasterxml.jackson.core.JsonFactory;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import java.io.IOException;
+import java.util.Locale;
+import java.util.Objects;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.function.Consumer;
+import org.apache.iceberg.rest.responses.ErrorResponse;
+import org.apache.iceberg.rest.responses.ErrorResponseParser;
+import org.junit.AfterClass;
+import org.junit.Assert;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.junit.jupiter.api.Assertions;
+import org.mockserver.integration.ClientAndServer;
+import org.mockserver.model.HttpRequest;
+import org.mockserver.model.HttpResponse;
+
+import static org.mockserver.integration.ClientAndServer.startClientAndServer;
+import static org.mockserver.model.HttpRequest.request;
+import static org.mockserver.model.HttpResponse.response;
+
+/***
+ * Exercises the RESTClient interface, specifically over a mocked-server using 
the actual HttpRESTClient code.
+ */
+public class TestHTTPClient {
+
+  private static final int port = 1080;
+  private static final String uri = String.format("http://127.0.0.1:%d";, port);
+  private static final JsonFactory FACTORY = new JsonFactory();
+  private static final ObjectMapper MAPPER = new ObjectMapper(FACTORY);
+
+  private static ClientAndServer mockServer;
+  private static RESTClient restClient;
+
+  @BeforeClass
+  public static void beforeClass() {
+    MAPPER.setVisibility(PropertyAccessor.FIELD, 
JsonAutoDetect.Visibility.ANY);
+    mockServer = startClientAndServer(port);
+    restClient = HTTPClient
+        .builder()
+        .uri(uri)
+        .mapper(MAPPER)
+        .withBearerAuth("auth_token")
+        .build();
+  }
+
+  @AfterClass
+  public static void stopServer() throws IOException {
+    mockServer.stop();
+    restClient.close();
+  }
+
+  @Test
+  public void testPostSuccess() throws Exception {
+    testHttpMethodOnSuccess(HttpMethod.POST);
+  }
+
+  @Test
+  public void testPostFailure() throws Exception {
+    testHttpMethodOnFailure(HttpMethod.POST);
+  }
+
+  @Test
+  public void testGetSuccess() throws Exception {
+    testHttpMethodOnSuccess(HttpMethod.GET);
+  }
+
+  @Test
+  public void testGetFailure() throws Exception {
+    testHttpMethodOnFailure(HttpMethod.GET);
+  }
+
+  @Test
+  public void testDeleteSuccess() throws Exception {
+    testHttpMethodOnSuccess(HttpMethod.DELETE);
+  }
+
+  @Test
+  public void testDeleteFailure() throws Exception {
+    testHttpMethodOnFailure(HttpMethod.DELETE);
+  }
+
+  @Test
+  public void testHeadSuccess() throws JsonProcessingException {
+    testHttpMethodOnSuccess(HttpMethod.HEAD);
+  }
+
+  @Test
+  public void testHeadFailure() throws JsonProcessingException {
+    testHttpMethodOnFailure(HttpMethod.HEAD);
+  }
+
+  public static void testHttpMethodOnSuccess(HttpMethod method) throws 
JsonProcessingException {
+    Item body = new Item(0L, "hank");
+    String asJson = MAPPER.writeValueAsString(body);
+    AtomicInteger errorCounter = new AtomicInteger(0);
+    Consumer<ErrorResponse> onError = (error) -> {
+      errorCounter.incrementAndGet();
+      throw new RuntimeException("Failure response");
+    };
+
+    // Using different paths Keeps the expectations unique for the test's mock 
server
+    String path = String.format("%s_success", method);
+
+    HttpRequest mockRequest = request("/" + 
path).withMethod(method.name().toUpperCase(Locale.ROOT));
+    if (method.usesRequestBody()) {
+      mockRequest = mockRequest.withBody(asJson);
+    }
+
+    HttpResponse mockResponse = response().withStatusCode(200);
+    if (method.usesResponseBody()) {
+      mockResponse = mockResponse.withBody(asJson);
+    }
+
+    mockServer
+        .when(mockRequest)
+        .respond(mockResponse);
+
+    Item successResponse = null;
+    switch (method) {
+      case POST:
+        successResponse = restClient.post(path, body, Item.class, onError);
+        break;
+      case GET:
+        successResponse = restClient.get(path, Item.class, onError);
+        break;
+      case HEAD:
+        restClient.head(path, onError);
+        break;
+      case DELETE:
+        successResponse = restClient.delete(path, Item.class, onError);
+        break;
+    }
+
+    if (method.usesRequestBody()) {
+      Assert.assertEquals("On a successful " + method + ", the correct 
response body should be returned",
+          successResponse, body);
+    }
+    Assert.assertEquals("On a successful " + method + ", the error handler 
should not be called",
+        0, errorCounter.get());
+  }
+
+  public static void testHttpMethodOnFailure(HttpMethod method) throws 
JsonProcessingException {
+    Item body = new Item(0L, "hank");
+    int statusCode = 404;
+    String asJson = MAPPER.writeValueAsString(body);
+    AtomicInteger errorCounter = new AtomicInteger(0);
+    ErrorResponse response = ErrorResponse.builder()
+        .responseCode(statusCode).withMessage("Not found").build();
+    Consumer<ErrorResponse> onError = error -> {
+      System.out.println("In the error handler for " + method + " and received 
" + ErrorResponseParser.toJson(error));
+      errorCounter.incrementAndGet();
+      throw new RuntimeException(
+          String.format("Called error handler for method %s due to status 
code: %d", method, statusCode));
+    };
+
+    // Using different paths keeps the expectations unique for the test's mock 
server
+    String path = String.format("%s_failure", method);
+    HttpRequest mockRequest = request("/" + 
path).withMethod(method.name().toUpperCase(Locale.ROOT));
+    if (method.usesRequestBody()) {
+      mockRequest = mockRequest.withBody(asJson);
+    }
+
+    HttpResponse mockResponse = response().withStatusCode(statusCode);
+    if (method.usesResponseBody()) {
+      mockResponse = 
mockResponse.withBody(ErrorResponseParser.toJson(response));
+    }
+
+    mockServer
+        .when(mockRequest)
+        .respond(mockResponse);
+
+    Assertions
+        .assertThrows(RuntimeException.class, () -> {
+          switch (method) {
+            case POST:
+              restClient.post(path, body, Item.class, onError);
+              break;
+            case GET:
+              restClient.get(path, Item.class, onError);
+              break;
+            case HEAD:
+              restClient.head(path, onError);
+              break;
+            case DELETE:
+              restClient.delete(path, Item.class, onError);
+              break;

Review comment:
       Made this into its own function that is shared between the success and 
failure case.




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