Skip to content

Commit bbf4520

Browse files
committed
Enhance withMpRestClient to account for baseUri on MP Rest Client
1 parent d991952 commit bbf4520

4 files changed

Lines changed: 122 additions & 7 deletions

File tree

core/src/main/java/org/microshed/testing/jupiter/MicroShedTestExtension.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,10 @@ private static String createJwtIfNeeded(Field restClientField) {
102102
@SuppressWarnings("unchecked")
103103
private static Optional<Class<? extends Annotation>> getMpRestClient() {
104104
try {
105-
return Optional.of((Class<? extends Annotation>) Class.forName("org.eclipse.microprofile.rest.client.inject.RestClient"));
106-
} catch (ClassNotFoundException e) {
105+
return Optional.of((Class<? extends Annotation>) Class.forName("org.eclipse.microprofile.rest.client.inject.RestClient",
106+
false,
107+
MicroShedTestExtension.class.getClassLoader()));
108+
} catch (ClassNotFoundException | LinkageError e) {
107109
return Optional.empty();
108110
}
109111
}

modules/testcontainers/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ description = "Extensions for using MicroShed Testing with Testcontainers for ma
44
dependencies {
55
compile project(':microshed-testing-core')
66
compile 'org.testcontainers:junit-jupiter:1.12.4'
7+
testCompile 'org.eclipse.microprofile.rest.client:microprofile-rest-client-api:1.3.4'
78
testCompile 'org.slf4j:slf4j-log4j12:1.7.29'
89
testCompile 'org.testcontainers:mockserver:1.12.3'
910
testImplementation 'org.junit.jupiter:junit-jupiter:5.5.2'

modules/testcontainers/src/main/java/org/microshed/testing/testcontainers/ApplicationContainer.java

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020

2121
import java.io.File;
2222
import java.io.IOException;
23+
import java.lang.annotation.Annotation;
24+
import java.lang.reflect.InvocationTargetException;
25+
import java.lang.reflect.Method;
2326
import java.net.URL;
2427
import java.nio.file.Files;
2528
import java.nio.file.Path;
@@ -40,6 +43,7 @@
4043
import java.util.stream.Collectors;
4144

4245
import org.junit.jupiter.api.extension.ExtensionConfigurationException;
46+
import org.junit.platform.commons.support.AnnotationSupport;
4347
import org.microshed.testing.ApplicationEnvironment;
4448
import org.microshed.testing.testcontainers.config.HollowTestcontainersConfiguration;
4549
import org.microshed.testing.testcontainers.config.TestcontainersConfiguration;
@@ -373,7 +377,45 @@ public void setWaitStrategy(WaitStrategy waitStrategy) {
373377
* @return the current instance
374378
*/
375379
public ApplicationContainer withMpRestClient(Class<?> restClientClass, String hostUrl) {
376-
return withMpRestClient(restClientClass.getCanonicalName(), hostUrl);
380+
String configToken = readMpRestClientConfigKey(restClientClass);
381+
if (configToken == null || configToken.isEmpty())
382+
configToken = restClientClass.getCanonicalName();
383+
return withMpRestClient(configToken, hostUrl);
384+
}
385+
386+
/**
387+
* Checks to see if the given restClientClass is annotated with
388+
* <code>@RegisterRestClient(configKey = "...")</code>
389+
*/
390+
@SuppressWarnings("unchecked")
391+
private String readMpRestClientConfigKey(Class<?> restClientClass) {
392+
Class<? extends Annotation> RegisterRestClient = null;
393+
try {
394+
RegisterRestClient = (Class<? extends Annotation>) Class.forName("org.eclipse.microprofile.rest.client.inject.RegisterRestClient",
395+
false,
396+
getClass().getClassLoader());
397+
} catch (ClassNotFoundException | LinkageError notFound) {
398+
return null;
399+
}
400+
401+
Method getConfigKey = null;
402+
try {
403+
getConfigKey = RegisterRestClient.getMethod("configKey");
404+
} catch (NoSuchMethodException | SecurityException e) {
405+
// Using a version of MP Rest Client that does not support configKey
406+
return null;
407+
}
408+
409+
Optional<Annotation> foundAnno = (Optional<Annotation>) AnnotationSupport.findAnnotation(restClientClass, RegisterRestClient);
410+
if (!foundAnno.isPresent())
411+
return null;
412+
413+
Annotation anno = foundAnno.get();
414+
try {
415+
return (String) getConfigKey.invoke(anno);
416+
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
417+
return null;
418+
}
377419
}
378420

379421
/**
@@ -385,10 +427,9 @@ public ApplicationContainer withMpRestClient(Class<?> restClientClass, String ho
385427
* @return the current instance
386428
*/
387429
public ApplicationContainer withMpRestClient(String restClientClass, String hostUrl) {
388-
String envName = restClientClass//
389-
.replaceAll("\\.", "_")
390-
.replaceAll("\\$", "_") +
391-
"_mp_rest_url";
430+
// Sanitize environment variable name using Environment Variables Mapping Rules defined in MP Config:
431+
// https://github.com/eclipse/microprofile-config/blob/master/spec/src/main/asciidoc/configsources.asciidoc#environment-variables-mapping-rules
432+
String envName = restClientClass.replaceAll("[^a-zA-Z0-9_]", "_") + "_mp_rest_url";
392433
return withEnv(envName, hostUrl);
393434
}
394435

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Copyright (c) 2019 IBM Corporation and others
3+
*
4+
* See the NOTICE file(s) distributed with this work for additional
5+
* information regarding copyright ownership.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* You may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
package org.microshed.testing.testcontainers;
20+
21+
import static org.junit.jupiter.api.Assertions.assertEquals;
22+
23+
import java.util.Map;
24+
25+
import org.eclipse.microprofile.rest.client.inject.RegisterRestClient;
26+
import org.junit.jupiter.api.Test;
27+
import org.microshed.testing.testcontainers.config.TestServerAdapter;
28+
29+
public class ApplicationContainerTest {
30+
31+
// Base uri configured with: com_example_StringRestClient_mp_rest_url
32+
@RegisterRestClient
33+
public static class SampleRestClient1 {
34+
}
35+
36+
// Base uri configured with: rc_config_key_mp_rest_url
37+
@RegisterRestClient(configKey = "rc-config-key")
38+
public static class SampleRestClient2 {
39+
}
40+
41+
// Base uri configured with: CLIENT_CONFIG_KEY_mp_rest_url
42+
@RegisterRestClient(configKey = "CLIENT_CONFIG_KEY")
43+
public static class SampleRestClient3 {
44+
}
45+
46+
@Test
47+
public void testMpRestClient() {
48+
final String clientUrl = "http://example.com";
49+
50+
@SuppressWarnings("resource")
51+
ApplicationContainer app = new ApplicationContainer("alpine:3.5")
52+
.withMpRestClient("com.example.StringRestClient", clientUrl)
53+
.withMpRestClient(SampleRestClient1.class, clientUrl)
54+
.withMpRestClient(SampleRestClient2.class, clientUrl)
55+
.withMpRestClient(SampleRestClient3.class, clientUrl);
56+
57+
Map<String, String> appEnv = app.getEnvMap();
58+
assertEquals(clientUrl, appEnv.get("com_example_StringRestClient_mp_rest_url"));
59+
assertEquals(clientUrl, appEnv.get("org_microshed_testing_testcontainers_ApplicationContainerTest_SampleRestClient1_mp_rest_url"));
60+
assertEquals(clientUrl, appEnv.get("rc_config_key_mp_rest_url"));
61+
assertEquals(clientUrl, appEnv.get("CLIENT_CONFIG_KEY_mp_rest_url"));
62+
}
63+
64+
@Test
65+
public void testCorrectServerAdapter() {
66+
@SuppressWarnings("resource")
67+
ApplicationContainer app = new ApplicationContainer("alpine:3.5");
68+
assertEquals(TestServerAdapter.class, app.getServerAdapter().getClass());
69+
}
70+
71+
}

0 commit comments

Comments
 (0)