This is an automated email from the ASF dual-hosted git repository.

aldettinger pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel-quarkus.git


The following commit(s) were added to refs/heads/main by this push:
     new 301a666  aws2-lambda: Add alias tests  #2749
301a666 is described below

commit 301a6660ddea8702dddc46fc5a55847b986b3cbe
Author: aldettinger <[email protected]>
AuthorDate: Wed Sep 22 14:07:57 2021 +0200

    aws2-lambda: Add alias tests  #2749
---
 .../aws2/lambda/it/Aws2LambdaResource.java         |  95 +++++++++++-
 .../component/aws2/lambda/it/Aws2LambdaTest.java   | 162 +++++++++++++--------
 2 files changed, 193 insertions(+), 64 deletions(-)

diff --git 
a/integration-test-groups/aws2/aws2-lambda/src/main/java/org/apache/camel/quarkus/component/aws2/lambda/it/Aws2LambdaResource.java
 
b/integration-test-groups/aws2/aws2-lambda/src/main/java/org/apache/camel/quarkus/component/aws2/lambda/it/Aws2LambdaResource.java
index 834f98c..0f50766 100644
--- 
a/integration-test-groups/aws2/aws2-lambda/src/main/java/org/apache/camel/quarkus/component/aws2/lambda/it/Aws2LambdaResource.java
+++ 
b/integration-test-groups/aws2/aws2-lambda/src/main/java/org/apache/camel/quarkus/component/aws2/lambda/it/Aws2LambdaResource.java
@@ -31,6 +31,7 @@ import javax.ws.rs.PUT;
 import javax.ws.rs.Path;
 import javax.ws.rs.PathParam;
 import javax.ws.rs.Produces;
+import javax.ws.rs.QueryParam;
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.Response;
 import javax.ws.rs.core.Response.Status;
@@ -41,11 +42,17 @@ import org.apache.camel.ProducerTemplate;
 import org.apache.camel.component.aws2.lambda.Lambda2Constants;
 import org.apache.camel.component.aws2.lambda.Lambda2Operations;
 import org.eclipse.microprofile.config.inject.ConfigProperty;
+import org.jboss.logging.Logger;
 import software.amazon.awssdk.core.SdkBytes;
+import software.amazon.awssdk.services.lambda.model.AliasConfiguration;
 import software.amazon.awssdk.services.lambda.model.FunctionConfiguration;
+import software.amazon.awssdk.services.lambda.model.GetAliasRequest;
+import software.amazon.awssdk.services.lambda.model.GetAliasResponse;
 import software.amazon.awssdk.services.lambda.model.GetFunctionResponse;
 import 
software.amazon.awssdk.services.lambda.model.InvalidParameterValueException;
 import software.amazon.awssdk.services.lambda.model.LastUpdateStatus;
+import software.amazon.awssdk.services.lambda.model.ListAliasesRequest;
+import software.amazon.awssdk.services.lambda.model.ListAliasesResponse;
 import software.amazon.awssdk.services.lambda.model.ListFunctionsResponse;
 import software.amazon.awssdk.services.lambda.model.Runtime;
 import software.amazon.awssdk.services.lambda.model.UpdateFunctionCodeRequest;
@@ -54,6 +61,9 @@ import 
software.amazon.awssdk.services.lambda.model.UpdateFunctionCodeResponse;
 @Path("/aws2-lambda")
 @ApplicationScoped
 public class Aws2LambdaResource {
+
+    private static final Logger LOG = 
Logger.getLogger(Aws2LambdaResource.class);
+
     @ConfigProperty(name = "aws-lambda.role-arn")
     String roleArn;
 
@@ -134,14 +144,95 @@ public class Aws2LambdaResource {
 
     @Path("/function/delete/{functionName}")
     @DELETE
-    @Produces(MediaType.APPLICATION_JSON)
-    public void delete(@PathParam("functionName") String functionName) {
+    public void deleteFunction(@PathParam("functionName") String functionName) 
{
         producerTemplate.requestBody(
                 componentUri(functionName, Lambda2Operations.deleteFunction),
                 null,
                 Object.class);
     }
 
+    @Path("/alias/create")
+    @POST
+    @Produces(MediaType.TEXT_PLAIN)
+    public Response createAlias(@QueryParam("functionName") String 
functionName,
+            @QueryParam("functionVersion") String functionVersion, 
@QueryParam("aliasName") String aliasName) throws Exception {
+        try {
+            final String response = producerTemplate.requestBodyAndHeaders(
+                    componentUri(functionName, Lambda2Operations.createAlias),
+                    null,
+                    new LinkedHashMap<String, Object>() {
+                        {
+                            put(Lambda2Constants.FUNCTION_ALIAS_NAME, 
aliasName);
+                            put(Lambda2Constants.FUNCTION_VERSION, 
functionVersion);
+                        }
+                    },
+                    String.class);
+            return Response
+                    .created(new URI("https://camel.apache.org/";))
+                    .entity(response)
+                    .build();
+        } catch (Exception e) {
+            LOG.info("Exception caught in alias/create", e);
+            LOG.info("Exception cause in alias/create", e.getCause());
+            throw e;
+        }
+    }
+
+    @Path("/alias/get")
+    @GET
+    @Produces(MediaType.TEXT_PLAIN)
+    public String getAlias(@QueryParam("functionName") String functionName, 
@QueryParam("aliasName") String aliasName) {
+        try {
+            GetAliasRequest getAliasRequest = 
GetAliasRequest.builder().functionName(functionName).name(aliasName).build();
+            String endpointUri = componentUri(functionName, 
Lambda2Operations.getAlias) + "&pojoRequest=true";
+
+            return producerTemplate
+                    .requestBody(endpointUri, getAliasRequest, 
GetAliasResponse.class)
+                    .functionVersion();
+        } catch (Exception e) {
+            LOG.info("Exception caught in alias/get", e);
+            LOG.info("Exception cause in alias/get", e.getCause());
+            throw e;
+        }
+    }
+
+    @Path("/alias/delete")
+    @DELETE
+    public void deleteAlias(@QueryParam("functionName") String functionName, 
@QueryParam("aliasName") String aliasName) {
+        try {
+            producerTemplate.requestBodyAndHeader(
+                    componentUri(functionName, Lambda2Operations.deleteAlias),
+                    null,
+                    Lambda2Constants.FUNCTION_ALIAS_NAME,
+                    aliasName,
+                    Object.class);
+        } catch (Exception e) {
+            LOG.info("Exception caught in alias/delete", e);
+            LOG.info("Exception cause in alias/delete", e.getCause());
+            throw e;
+        }
+    }
+
+    @Path("/alias/list")
+    @GET
+    @Produces(MediaType.APPLICATION_JSON)
+    public List<String> listAliases(@QueryParam("functionName") String 
functionName) {
+        try {
+            ListAliasesRequest listAliasesReq = 
ListAliasesRequest.builder().functionName(functionName).build();
+            return producerTemplate.requestBody(
+                    componentUri(functionName, Lambda2Operations.listAliases) 
+ "&pojoRequest=true",
+                    listAliasesReq,
+                    ListAliasesResponse.class)
+                    .aliases().stream()
+                    .map(AliasConfiguration::name)
+                    .collect(Collectors.toList());
+        } catch (Exception e) {
+            LOG.info("Exception caught in alias/list", e);
+            LOG.info("Exception cause in alias/list", e.getCause());
+            throw e;
+        }
+    }
+
     private static String componentUri(String functionName, Lambda2Operations 
operation) {
         return "aws2-lambda:" + functionName + "?operation=" + operation;
     }
diff --git 
a/integration-test-groups/aws2/aws2-lambda/src/test/java/org/apache/camel/quarkus/component/aws2/lambda/it/Aws2LambdaTest.java
 
b/integration-test-groups/aws2/aws2-lambda/src/test/java/org/apache/camel/quarkus/component/aws2/lambda/it/Aws2LambdaTest.java
index 7539cb0..37758dd 100644
--- 
a/integration-test-groups/aws2/aws2-lambda/src/test/java/org/apache/camel/quarkus/component/aws2/lambda/it/Aws2LambdaTest.java
+++ 
b/integration-test-groups/aws2/aws2-lambda/src/test/java/org/apache/camel/quarkus/component/aws2/lambda/it/Aws2LambdaTest.java
@@ -29,11 +29,14 @@ import io.restassured.RestAssured;
 import io.restassured.http.ContentType;
 import io.restassured.response.ExtractableResponse;
 import org.apache.camel.quarkus.test.support.aws2.Aws2TestResource;
-import org.awaitility.Awaitility;
-import org.hamcrest.Matchers;
 import org.jboss.logging.Logger;
 import org.junit.jupiter.api.Test;
 
+import static org.awaitility.Awaitility.await;
+import static org.hamcrest.Matchers.hasItem;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.not;
+
 @QuarkusTest
 @QuarkusTestResource(Aws2TestResource.class)
 class Aws2LambdaTest {
@@ -42,43 +45,50 @@ class Aws2LambdaTest {
     @Test
     public void performingOperationsOnLambdaFunctionShouldSucceed() {
         final String functionName = "cqFunction" + 
java.util.UUID.randomUUID().toString().replace("-", "");
-        final String name = "Joe " + 
java.util.UUID.randomUUID().toString().replace("-", "");
 
-        /* The role is not created immediately, so we need to retry */
-        Awaitility.await()
-                .pollDelay(6, TimeUnit.SECONDS) // never succeeded earlier 
than 6 seconds after creating the role
+        // The required role to create a function is not created immediately, 
so we need to retry
+        await().pollDelay(6, TimeUnit.SECONDS) // never succeeded earlier than 
6 seconds after creating the role
                 .pollInterval(1, TimeUnit.SECONDS)
                 .atMost(120, TimeUnit.SECONDS)
-                .until(
-                        () -> {
-                            ExtractableResponse<?> response = 
RestAssured.given()
-                                    .contentType("application/zip")
-                                    .body(createInitialLambdaFunctionZip())
-                                    .post("/aws2-lambda/function/create/" + 
functionName)
-                                    .then()
-                                    .extract();
-                            switch (response.statusCode()) {
-                            case 201:
-                                LOG.infof("Lambda function %s created", 
functionName);
-                                return true;
-                            case 400:
-                                LOG.infof("Could not create Lambda function %s 
yet (will retry): %d %s", functionName,
-                                        response.statusCode(),
-                                        response.body().asString());
-                                return false;
-                            default:
-                                throw new RuntimeException(
-                                        "Unexpected status from 
/aws2-lambda/function/create " + response.statusCode() + " "
-                                                + response.body().asString());
-                            }
-                        });
+                .until(() -> {
+                    ExtractableResponse<?> response = RestAssured.given()
+                            .contentType("application/zip")
+                            .body(createInitialLambdaFunctionZip())
+                            .post("/aws2-lambda/function/create/" + 
functionName)
+                            .then()
+                            .extract();
+                    switch (response.statusCode()) {
+                    case 201:
+                        LOG.infof("Lambda function %s created", functionName);
+                        return true;
+                    case 400:
+                        LOG.infof("Could not create Lambda function %s yet 
(will retry): %d %s", functionName,
+                                response.statusCode(), 
response.body().asString());
+                        return false;
+                    default:
+                        throw new RuntimeException("Unexpected status from 
/aws2-lambda/function/create "
+                                + response.statusCode() + " " + 
response.body().asString());
+                    }
+                });
+
+        getUpdateListAndInvokeFunctionShouldSucceed(functionName);
+        createGetDeleteAndListAliasShouldSucceed(functionName);
+
+        RestAssured.given()
+                .delete("/aws2-lambda/function/delete/" + functionName)
+                .then()
+                .statusCode(204);
+    }
+
+    public void getUpdateListAndInvokeFunctionShouldSucceed(String 
functionName) {
+        final String name = "Joe " + 
java.util.UUID.randomUUID().toString().replace("-", "");
 
         RestAssured.given()
                 .accept(ContentType.JSON)
                 .get("/aws2-lambda/function/get/" + functionName)
                 .then()
                 .statusCode(200)
-                .body(Matchers.is(functionName));
+                .body(is(functionName));
 
         RestAssured.given()
                 .contentType("application/zip")
@@ -92,38 +102,66 @@ class Aws2LambdaTest {
                 .get("/aws2-lambda/function/list")
                 .then()
                 .statusCode(200)
-                .body("$", Matchers.hasItem(functionName));
+                .body("$", hasItem(functionName));
 
         /* Sometimes this does not succeed immediately */
-        Awaitility.await()
-                .pollDelay(200, TimeUnit.MILLISECONDS)
+        await().pollDelay(200, TimeUnit.MILLISECONDS)
                 .pollInterval(500, TimeUnit.MILLISECONDS)
                 .atMost(120, TimeUnit.SECONDS)
-                .until(
-                        () -> {
-                            ExtractableResponse<?> response = 
RestAssured.given()
-                                    .contentType(ContentType.JSON)
-                                    .body("{ \"firstName\": \"" + name + "\"}")
-                                    .post("/aws2-lambda/function/invoke/" + 
functionName)
-                                    .then()
-                                    .extract();
-                            String format = "Execution of 
aws2-lambda/invoke/%s returned status %d and content %s";
-                            System.out.println(String.format(format, 
functionName, response.statusCode(), response.asString()));
-                            switch (response.statusCode()) {
-                            case 200:
-                                final String greetings = 
response.jsonPath().getString("greetings");
-                                return greetings;
-                            default:
-                                return null;
-                            }
-                        },
-                        Matchers.is("Hello updated " + name));
+                .until(() -> {
+                    ExtractableResponse<?> response = RestAssured.given()
+                            .contentType(ContentType.JSON)
+                            .body("{ \"firstName\": \"" + name + "\"}")
+                            .post("/aws2-lambda/function/invoke/" + 
functionName)
+                            .then()
+                            .extract();
+                    String format = "Execution of aws2-lambda/invoke/%s 
returned status %d and content %s";
+                    LOG.infof(format, functionName, response.statusCode(), 
response.asString());
+                    switch (response.statusCode()) {
+                    case 200:
+                        final String greetings = 
response.jsonPath().getString("greetings");
+                        return greetings;
+                    default:
+                        return null;
+                    }
+                }, is("Hello updated " + name));
+    }
+
+    public void createGetDeleteAndListAliasShouldSucceed(String functionName) {
+
+        String functionVersion = "$LATEST";
+        String aliasName = "alias_LATEST_" + functionName;
 
         RestAssured.given()
-                .delete("/aws2-lambda/function/delete/" + functionName)
+                .queryParam("functionName", functionName)
+                .queryParam("functionVersion", functionVersion)
+                .queryParam("aliasName", aliasName)
+                .post("/aws2-lambda/alias/create/")
+                .then()
+                .statusCode(201);
+
+        RestAssured.given()
+                .queryParam("functionName", functionName)
+                .queryParam("aliasName", aliasName)
+                .get("/aws2-lambda/alias/get/")
+                .then()
+                .statusCode(200)
+                .body(is("$LATEST"));
+
+        RestAssured.given()
+                .queryParam("functionName", functionName)
+                .queryParam("aliasName", aliasName)
+                .delete("/aws2-lambda/alias/delete")
                 .then()
                 .statusCode(204);
 
+        RestAssured.given()
+                .queryParam("functionName", functionName)
+                .accept(ContentType.JSON)
+                .get("/aws2-lambda/alias/list")
+                .then()
+                .statusCode(200)
+                .body("$", not(hasItem(aliasName)));
     }
 
     static byte[] createInitialLambdaFunctionZip() {
@@ -146,15 +184,15 @@ class Aws2LambdaTest {
         return baos.toByteArray();
     }
 
-    private static final String INITIAL_FUNCTION_SOURCE = "def handler(event, 
context):\n" +
-            "    message = 'Hello {}'.format(event['firstName'])\n" +
-            "    return {\n" +
-            "        'greetings' : message\n" +
-            "    }\n";
+    private static final String INITIAL_FUNCTION_SOURCE = "def handler(event, 
context):\n"
+            + "    message = 'Hello {}'.format(event['firstName'])\n"
+            + "    return {\n"
+            + "        'greetings' : message\n"
+            + "    }\n";
 
-    private static final String UPDATED_FUNCTION_SOURCE = "def handler(event, 
context):\n" +
-            "    message = 'Hello updated {}'.format(event['firstName'])\n" +
-            "    return {\n" +
-            "        'greetings' : message\n" +
+    private static final String UPDATED_FUNCTION_SOURCE = "def handler(event, 
context):\n"
+            + "    message = 'Hello updated {}'.format(event['firstName'])\n"
+            + "    return {\n"
+            + "        'greetings' : message\n" +
             "    }\n";
 }

Reply via email to