forked from apache/camel-quarkus
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Aws2LambdaTest.java
132 lines (121 loc) · 5.88 KB
/
Aws2LambdaTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
* 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.camel.quarkus.component.aws2.lambda.it;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeUnit;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import io.quarkus.test.common.QuarkusTestResource;
import io.quarkus.test.junit.QuarkusTest;
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;
@QuarkusTest
@QuarkusTestResource(Aws2TestResource.class)
class Aws2LambdaTest {
private static final Logger LOG = Logger.getLogger(Aws2LambdaTest.class);
@Test
public void e2e() {
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
.pollInterval(1, TimeUnit.SECONDS)
.atMost(120, TimeUnit.SECONDS)
.until(
() -> {
ExtractableResponse<?> response = RestAssured.given()
.contentType("application/zip")
.body(createFunctionZip())
.post("/aws2-lambda/create/" + functionName)
.then()
.extract();
switch (response.statusCode()) {
case 201:
LOG.infof("Function %s created", functionName);
return true;
case 400:
LOG.infof("Could not create function %s yet (will retry): %d %s", functionName,
response.statusCode(),
response.body().asString());
return false;
default:
throw new RuntimeException(
"Unexpected status from /aws2-lambda/create " + response.statusCode() + " "
+ response.body().asString());
}
});
RestAssured.given()
.accept(ContentType.JSON)
.get("/aws2-lambda/listFunctions")
.then()
.statusCode(200)
.body("$", Matchers.hasItem(functionName));
/* Sometimes this does not succeed immediately */
Awaitility.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/invoke/" + functionName)
.then()
.extract();
System.out.println("Function " + functionName + " returned " + response.statusCode()
+ "; will retry until timeout");
switch (response.statusCode()) {
case 200:
final String greetings = response.jsonPath().getString("greetings");
return greetings;
default:
return null;
}
},
Matchers.is("Hello " + name));
RestAssured.given()
.delete("/aws2-lambda/delete/" + functionName)
.then()
.statusCode(204);
}
static byte[] createFunctionZip() {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try (ZipOutputStream out = new ZipOutputStream(baos)) {
out.putNextEntry(new ZipEntry("index.py"));
out.write(FUNCTION_SOURCE.getBytes(StandardCharsets.UTF_8));
out.closeEntry();
} catch (IOException e) {
throw new RuntimeException("Could not create a zip file", e);
}
return baos.toByteArray();
}
private static final String FUNCTION_SOURCE = "def handler(event, context):\n" +
" message = 'Hello {}'.format(event['firstName'])\n" +
" return {\n" +
" 'greetings' : message\n" +
" }\n";
}