jackye1995 commented on code in PR #8091:
URL: https://github.com/apache/iceberg/pull/8091#discussion_r1267434048


##########
aws/src/test/java/org/apache/iceberg/aws/lambda/rest/TestLambdaRESTInvoker.java:
##########
@@ -0,0 +1,297 @@
+/*
+ * 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.aws.lambda.rest;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.doThrow;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import java.util.Locale;
+import java.util.Map;
+import java.util.Objects;
+import java.util.function.Consumer;
+import org.apache.hc.core5.http.ContentType;
+import org.apache.hc.core5.http.HttpHeaders;
+import org.apache.iceberg.IcebergBuild;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap;
+import org.apache.iceberg.rest.ErrorHandler;
+import org.apache.iceberg.rest.HttpMethod;
+import org.apache.iceberg.rest.RESTClient;
+import org.apache.iceberg.rest.RESTClientProperties;
+import org.apache.iceberg.rest.RESTObjectMapper;
+import org.apache.iceberg.rest.RESTRequest;
+import org.apache.iceberg.rest.RESTResponse;
+import org.apache.iceberg.rest.responses.ErrorResponse;
+import org.apache.iceberg.rest.responses.ErrorResponseParser;
+import org.assertj.core.api.Assertions;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+import software.amazon.awssdk.core.SdkBytes;
+import software.amazon.awssdk.services.lambda.LambdaClient;
+import software.amazon.awssdk.services.lambda.model.InvokeRequest;
+import software.amazon.awssdk.services.lambda.model.InvokeResponse;
+
+public class TestLambdaRESTInvoker {
+
+  private static final int PORT = 1080;
+  private static final String BEARER_AUTH_TOKEN = "auth_token";
+  private static final String URI = String.format("http://127.0.0.1:%d";, PORT);
+  private static final String FUNCTION_ARN = "some-arn";
+  private static final ObjectMapper MAPPER = RESTObjectMapper.mapper();
+
+  private static Map<String, String> baseHeaders;
+  private static LambdaClient lambda;
+  private static RESTClient restClient;
+
+  @BeforeAll
+  public static void beforeClass() {
+    lambda = mock(LambdaClient.class);
+    String icebergBuildGitCommitShort = IcebergBuild.gitCommitShortId();
+    String icebergBuildFullVersion = IcebergBuild.fullVersion();
+    baseHeaders =
+        ImmutableMap.of(
+            "Authorization",
+            "Bearer " + BEARER_AUTH_TOKEN,
+            RESTClientProperties.CLIENT_VERSION_HEADER,
+            icebergBuildFullVersion,
+            RESTClientProperties.CLIENT_GIT_COMMIT_SHORT_HEADER,
+            icebergBuildGitCommitShort);
+    restClient = new LambdaRESTInvoker(URI, MAPPER, baseHeaders, lambda, 
FUNCTION_ARN);
+  }
+
+  @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");
+    int statusCode = 200;
+
+    ErrorHandler onError = mock(ErrorHandler.class);
+    doThrow(new RuntimeException("Failure 
response")).when(onError).accept(any());
+
+    String path = addRequestTestCaseAndGetPath(method, body, statusCode);
+
+    Item successResponse =
+        doExecuteRequest(method, path, body, onError, h -> 
assertThat(h).isNotEmpty());
+
+    if (method.usesRequestBody()) {
+      Assertions.assertThat(body)
+          .as("On a successful " + method + ", the correct response body 
should be returned")
+          .isEqualTo(successResponse);
+    }
+
+    verify(onError, never()).accept(any());
+  }
+
+  public static void testHttpMethodOnFailure(HttpMethod method) throws 
JsonProcessingException {
+    Item body = new Item(0L, "hank");
+    int statusCode = 404;
+
+    ErrorHandler onError = mock(ErrorHandler.class);
+    doThrow(
+            new RuntimeException(
+                String.format(
+                    "Called error handler for method %s due to status code: 
%d",
+                    method, statusCode)))
+        .when(onError)
+        .accept(any());
+
+    String path = addRequestTestCaseAndGetPath(method, body, statusCode);
+
+    Assertions.assertThatThrownBy(() -> doExecuteRequest(method, path, body, 
onError, h -> {}))
+        .isInstanceOf(RuntimeException.class)
+        .hasMessage(
+            String.format(
+                "Called error handler for method %s due to status code: %d", 
method, statusCode));
+
+    verify(onError).accept(any());
+  }
+
+  // Adds a request that the mock-server can match against, based on the 
method, path, body, and
+  // headers.
+  // Return the path generated for the test case, so that the client can call 
that path to exercise
+  // it.
+  private static String addRequestTestCaseAndGetPath(HttpMethod method, Item 
body, int statusCode)
+      throws JsonProcessingException {
+
+    // Build the path route, which must be unique per test case.
+    boolean isSuccess = statusCode == 200;

Review Comment:
   This is mostly copied from `TestHTTPClient`, I only changed the mock logic.



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