2020
2121import java .io .File ;
2222import java .io .IOException ;
23+ import java .lang .annotation .Annotation ;
24+ import java .lang .reflect .InvocationTargetException ;
25+ import java .lang .reflect .Method ;
2326import java .net .URL ;
2427import java .nio .file .Files ;
2528import java .nio .file .Path ;
4043import java .util .stream .Collectors ;
4144
4245import org .junit .jupiter .api .extension .ExtensionConfigurationException ;
46+ import org .junit .platform .commons .support .AnnotationSupport ;
4347import org .microshed .testing .ApplicationEnvironment ;
4448import org .microshed .testing .testcontainers .config .HollowTestcontainersConfiguration ;
4549import 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
0 commit comments