Skip to content

Commit d7b18dc

Browse files
Fix AWS Translate integration tests using WireMock
- Introduced WireMock to mock TranslateText operation (unsupported in LocalStack Community) - Improved Aws2TestResource to skip starting LocalStack if no services are requested - Updated Aws2TestEnvContext to export credentials in mock mode even without LocalStack - Added onServerStart hook to WireMock support for easier stub registration
1 parent 4ca824f commit d7b18dc

8 files changed

Lines changed: 128 additions & 25 deletions

File tree

integration-test-groups/aws2/aws2-translate/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@
6565
<type>test-jar</type>
6666
<scope>test</scope>
6767
</dependency>
68+
<dependency>
69+
<groupId>org.apache.camel.quarkus</groupId>
70+
<artifactId>camel-quarkus-integration-wiremock-support</artifactId>
71+
<version>${project.version}</version>
72+
<scope>test</scope>
73+
</dependency>
6874
</dependencies>
6975

7076
<profiles>

integration-test-groups/aws2/aws2-translate/src/test/java/org/apache/camel/quarkus/component/aws2/translate/it/Aws2TranslateTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
@QuarkusTest
2929
@QuarkusTestResource(Aws2TestResource.class)
30+
@QuarkusTestResource(Aws2TranslateTestResource.class)
3031
class Aws2TranslateTest extends BaseAWs2TestSupport {
3132

3233
public Aws2TranslateTest() {
@@ -45,6 +46,18 @@ public void translate() {
4546
.body(is("Bonjour"));
4647
}
4748

49+
@Override
50+
@org.junit.jupiter.api.Disabled("LocalStack not supported for Translate, skipping credential tests")
51+
public void successfulDefaultCredentialsProviderTest() {
52+
super.successfulDefaultCredentialsProviderTest();
53+
}
54+
55+
@Override
56+
@org.junit.jupiter.api.Disabled("LocalStack not supported for Translate, skipping credential tests")
57+
public void failingDefaultCredentialsProviderTest() {
58+
super.failingDefaultCredentialsProviderTest();
59+
}
60+
4861
@Override
4962
public void testMethodForDefaultCredentialsProvider() {
5063
RestAssured.given()

integration-test-groups/aws2/aws2-translate/src/test/java/org/apache/camel/quarkus/component/aws2/translate/it/Aws2TranslateTestEnvCustomizer.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ public class Aws2TranslateTestEnvCustomizer implements Aws2TestEnvCustomizer {
2424

2525
@Override
2626
public Service[] localstackServices() {
27+
return new Service[] {};
28+
}
29+
30+
@Override
31+
public Service[] exportCredentialsForLocalstackServices() {
2732
return new Service[] { Service.TRANSLATE };
2833
}
2934

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.camel.quarkus.component.aws2.translate.it;
18+
19+
import java.util.Map;
20+
21+
import org.apache.camel.quarkus.test.wiremock.WireMockTestResourceLifecycleManager;
22+
23+
public class Aws2TranslateTestResource extends WireMockTestResourceLifecycleManager {
24+
25+
@Override
26+
public Map<String, String> start() {
27+
Map<String, String> properties = super.start();
28+
String wiremockUrl = properties.get("wiremock.url");
29+
if (wiremockUrl != null) {
30+
properties.put("camel.component.aws2-translate.override-endpoint", "true");
31+
properties.put("camel.component.aws2-translate.uri-endpoint-override", wiremockUrl);
32+
}
33+
return properties;
34+
}
35+
36+
@Override
37+
protected String getRecordTargetBaseUrl() {
38+
return "https://translate.us-east-1.amazonaws.com";
39+
}
40+
41+
@Override
42+
protected boolean isMockingEnabled() {
43+
return org.apache.camel.quarkus.test.mock.backend.MockBackendUtils.startMockBackend(false);
44+
}
45+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"request": {
3+
"method": "POST",
4+
"url": "/",
5+
"headers": {
6+
"X-Amz-Target": {
7+
"equalTo": "AWSShineFrontendService_20170701.TranslateText"
8+
}
9+
}
10+
},
11+
"response": {
12+
"status": 200,
13+
"headers": {
14+
"Content-Type": "application/x-amzn-json-1.1"
15+
},
16+
"jsonBody": {
17+
"TranslatedText": "Bonjour",
18+
"SourceLanguageCode": "en",
19+
"TargetLanguageCode": "fr"
20+
}
21+
}
22+
}

integration-tests-support/aws2/src/test/java/org/apache/camel/quarkus/test/support/aws2/Aws2TestEnvContext.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -67,22 +67,22 @@ public Aws2TestEnvContext(String accessKey, String secretKey, String region, boo
6767
this.credentialsProvider = useDefaultCredentialsProvider ? CredentialsProvider.defaultProvider
6868
: CredentialsProvider.staticProvider;
6969

70-
localstack.ifPresent(ls -> {
71-
for (Service service : exportCredentialsServices) {
72-
String s = camelServiceComponentName(service);
73-
if (s != null) {
74-
if (credentialsProvider == CredentialsProvider.staticProvider) {
75-
properties.put(s + ".access-key", accessKey);
76-
properties.put(s + ".secret-key", secretKey);
77-
}
78-
properties.put(s + ".region", region);
70+
for (Service service : exportCredentialsServices) {
71+
String s = camelServiceComponentName(service);
72+
if (s != null) {
73+
if (credentialsProvider == CredentialsProvider.staticProvider) {
74+
properties.put(s + ".access-key", accessKey);
75+
properties.put(s + ".secret-key", secretKey);
76+
}
77+
properties.put(s + ".region", region);
7978

79+
localstack.ifPresent(ls -> {
8080
properties.put(s + ".override-endpoint", "true");
8181
properties.put(s + ".uri-endpoint-override",
8282
ls.getEndpoint().toString());
83-
}
83+
});
8484
}
85-
});
85+
}
8686
}
8787

8888
/**

integration-tests-support/aws2/src/test/java/org/apache/camel/quarkus/test/support/aws2/Aws2TestResource.java

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -81,19 +81,26 @@ public Map<String, String> start() {
8181
.distinct()
8282
.toArray(Service[]::new);
8383

84-
DockerImageName imageName = DockerImageName
85-
.parse(ConfigProvider.getConfig().getValue("localstack.container.image", String.class))
86-
.asCompatibleSubstituteFor("localstack/localstack");
87-
LocalStackContainer localstack = new LocalStackContainer(imageName)
88-
.withServices(services);
89-
localstack.withEnv("LS_LOG", localstackLogLevel);
90-
localstack.withEnv("AWS_ACCESS_KEY_ID", "testAccessKeyId"); //has to be longer then `test`, to work on FIPS systems
91-
localstack.withEnv("AWS_SECRET_ACCESS_KEY", "testSecretKeyId");
92-
localstack.withLogConsumer(new Slf4jLogConsumer(LOG));
93-
localstack.start();
94-
95-
envContext = new Aws2TestEnvContext(localstack.getAccessKey(), localstack.getSecretKey(), localstack.getRegion(),
96-
useDefaultCredentialsProvider, Optional.of(localstack), exportCredentialsServices);
84+
if (services.length > 0) {
85+
DockerImageName imageName = DockerImageName
86+
.parse(ConfigProvider.getConfig().getValue("localstack.container.image", String.class))
87+
.asCompatibleSubstituteFor("localstack/localstack");
88+
LocalStackContainer localstack = new LocalStackContainer(imageName)
89+
.withServices(services);
90+
localstack.withEnv("LS_LOG", localstackLogLevel);
91+
localstack.withEnv("AWS_ACCESS_KEY_ID", "testAccessKeyId"); //has to be longer then `test`, to work on FIPS systems
92+
localstack.withEnv("AWS_SECRET_ACCESS_KEY", "testSecretKeyId");
93+
localstack.withLogConsumer(new Slf4jLogConsumer(LOG));
94+
localstack.start();
95+
96+
envContext = new Aws2TestEnvContext(localstack.getAccessKey(), localstack.getSecretKey(),
97+
localstack.getRegion(),
98+
useDefaultCredentialsProvider, Optional.of(localstack), exportCredentialsServices);
99+
} else {
100+
LOG.info("No LocalStack services requested, skipping container startup");
101+
envContext = new Aws2TestEnvContext("testAccessKeyId", "testSecretKeyId", "us-east-1",
102+
useDefaultCredentialsProvider, Optional.empty(), exportCredentialsServices);
103+
}
97104

98105
} else {
99106
if (!startMockBackend && !realCredentialsProvided && !useDefaultCredentialsProvider) {

integration-tests-support/wiremock/src/main/java/org/apache/camel/quarkus/test/wiremock/WireMockTestResourceLifecycleManager.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,12 @@ private WireMockServer createServer() {
236236
// Read mapping resources from the classpath in playback mode
237237
configuration.fileSource(new CamelQuarkusFileSource());
238238
}
239-
return new WireMockServer(configuration);
239+
WireMockServer server = new WireMockServer(configuration);
240+
onServerStart(server);
241+
return server;
242+
}
243+
244+
protected void onServerStart(WireMockServer server) {
240245
}
241246

242247
/**

0 commit comments

Comments
 (0)