Skip to content

Commit b53e314

Browse files
committed
Add validation to ApplicationContainer#withMpRestClient
1 parent 4264c95 commit b53e314

2 files changed

Lines changed: 53 additions & 9 deletions

File tree

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

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import java.lang.annotation.Annotation;
2424
import java.lang.reflect.InvocationTargetException;
2525
import java.lang.reflect.Method;
26+
import java.net.MalformedURLException;
27+
import java.net.URL;
2628
import java.nio.file.Files;
2729
import java.nio.file.Path;
2830
import java.nio.file.Paths;
@@ -444,11 +446,25 @@ public void setWaitStrategy(WaitStrategy waitStrategy) {
444446
* Configures the application container with the supplied MicroProfile REST Client class that
445447
* will reference the supplied {@code hostUrl}
446448
*
447-
* @param restClientClass The MicroProfile REST Client class
449+
* @param restClientClass The MicroProfile REST Client interface, which must be annotated with
450+
* <code>@RegisterRestClient</code>
448451
* @param hostUrl The URL that the {@code restClientClass} will act as a REST client for
452+
* @throws IllegalArgumentException If the provided restClientClass is not an interface or not
453+
* annotated with <code>@RegisterRestClient</code>
454+
* @throws IllegalArgumentException If hostUrl is not a valid URL
449455
* @return the current instance
450456
*/
451457
public ApplicationContainer withMpRestClient(Class<?> restClientClass, String hostUrl) {
458+
Objects.requireNonNull(restClientClass, "restClientClass must be non-null");
459+
Objects.requireNonNull(hostUrl, "hostUrl must be non-null");
460+
if (!restClientClass.isInterface()) {
461+
throw new IllegalArgumentException("Provided restClientClass " + restClientClass.getCanonicalName() + " must be an interface");
462+
}
463+
try {
464+
new URL(hostUrl);
465+
} catch (MalformedURLException e) {
466+
throw new IllegalArgumentException(e);
467+
}
452468
String configToken = readMpRestClientConfigKey(restClientClass);
453469
if (configToken == null || configToken.isEmpty())
454470
configToken = restClientClass.getCanonicalName();
@@ -467,9 +483,8 @@ private String readMpRestClientConfigKey(Class<?> restClientClass) {
467483
false,
468484
getClass().getClassLoader());
469485
} catch (ClassNotFoundException | LinkageError notFound) {
470-
return null;
486+
throw new ExtensionConfigurationException("Unable to load @RegisterRestClient", notFound);
471487
}
472-
473488
Method getConfigKey = null;
474489
try {
475490
getConfigKey = RegisterRestClient.getMethod("configKey");
@@ -479,14 +494,16 @@ private String readMpRestClientConfigKey(Class<?> restClientClass) {
479494
}
480495

481496
Optional<Annotation> foundAnno = (Optional<Annotation>) AnnotationSupport.findAnnotation(restClientClass, RegisterRestClient);
482-
if (!foundAnno.isPresent())
483-
return null;
497+
if (!foundAnno.isPresent()) {
498+
throw new IllegalArgumentException("Provided restClientClass " + restClientClass + " must be annotated with "
499+
+ RegisterRestClient.getSimpleName());
500+
}
484501

485502
Annotation anno = foundAnno.get();
486503
try {
487504
return (String) getConfigKey.invoke(anno);
488505
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
489-
return null;
506+
throw new IllegalArgumentException("Unable to obtain configKey from " + anno + " found on " + restClientClass.getCanonicalName());
490507
}
491508
}
492509

@@ -496,6 +513,7 @@ private String readMpRestClientConfigKey(Class<?> restClientClass) {
496513
*
497514
* @param restClientClass The MicroProfile REST Client class
498515
* @param hostUrl The URL that the {@code restClientClass} will act as a REST client for
516+
* @throws IllegalArgumentException If hostUrl is not a valid URL
499517
* @return the current instance
500518
*/
501519
public ApplicationContainer withMpRestClient(String restClientClass, String hostUrl) {
@@ -506,6 +524,11 @@ public ApplicationContainer withMpRestClient(String restClientClass, String host
506524
} else {
507525
restClientClass += "/mp-rest/url";
508526
}
527+
try {
528+
new URL(hostUrl);
529+
} catch (MalformedURLException e) {
530+
throw new IllegalArgumentException(e);
531+
}
509532
return withEnv(restClientClass, hostUrl);
510533
}
511534

modules/testcontainers/src/test/java/org/microshed/testing/testcontainers/ApplicationContainerTest.java

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
package org.microshed.testing.testcontainers;
2020

2121
import static org.junit.jupiter.api.Assertions.assertEquals;
22+
import static org.junit.jupiter.api.Assertions.assertThrows;
2223

2324
import java.util.Arrays;
2425
import java.util.Map;
@@ -31,17 +32,24 @@ public class ApplicationContainerTest {
3132

3233
// Base uri configured with: com_example_StringRestClient_mp_rest_url
3334
@RegisterRestClient
34-
public static class SampleRestClient1 {
35+
public static interface SampleRestClient1 {
3536
}
3637

3738
// Base uri configured with: rc_config_key_mp_rest_url
3839
@RegisterRestClient(configKey = "rc-config-key")
39-
public static class SampleRestClient2 {
40+
public static interface SampleRestClient2 {
4041
}
4142

4243
// Base uri configured with: CLIENT_CONFIG_KEY_mp_rest_url
4344
@RegisterRestClient(configKey = "CLIENT_CONFIG_KEY")
44-
public static class SampleRestClient3 {
45+
public static interface SampleRestClient3 {
46+
}
47+
48+
public static interface NoAnnotationClient {
49+
}
50+
51+
@RegisterRestClient
52+
public static class ConcreteClassClient {
4553
}
4654

4755
@Test
@@ -61,6 +69,19 @@ public void testMpRestClient() {
6169
assertEquals(clientUrl, appEnv.get("CLIENT_CONFIG_KEY/mp-rest/url"), appEnv.toString());
6270
}
6371

72+
@Test
73+
public void testMpRestClientValidation() {
74+
final String clientUrl = "http://example.com";
75+
76+
assertThrows(IllegalArgumentException.class, () -> dummyApp().withMpRestClient(NoAnnotationClient.class, clientUrl));
77+
assertThrows(IllegalArgumentException.class, () -> dummyApp().withMpRestClient(ConcreteClassClient.class, clientUrl));
78+
assertThrows(NullPointerException.class, () -> dummyApp().withMpRestClient(SampleRestClient1.class, null));
79+
Class<?> nullClass = null;
80+
assertThrows(NullPointerException.class, () -> dummyApp().withMpRestClient(nullClass, clientUrl));
81+
assertThrows(IllegalArgumentException.class, () -> dummyApp().withMpRestClient(SampleRestClient1.class, "bogus"));
82+
assertThrows(IllegalArgumentException.class, () -> dummyApp().withMpRestClient("com.example.StringRestClient/mp-rest/url", "bogus"));
83+
}
84+
6485
@Test
6586
public void testCorrectServerAdapter() {
6687
ApplicationContainer app = dummyApp();

0 commit comments

Comments
 (0)