Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add validation to ApplicationContainer#withMpRestClient #191

Merged
merged 1 commit into from
May 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
Expand Down Expand Up @@ -444,11 +446,25 @@ public void setWaitStrategy(WaitStrategy waitStrategy) {
* Configures the application container with the supplied MicroProfile REST Client class that
* will reference the supplied {@code hostUrl}
*
* @param restClientClass The MicroProfile REST Client class
* @param restClientClass The MicroProfile REST Client interface, which must be annotated with
* <code>@RegisterRestClient</code>
* @param hostUrl The URL that the {@code restClientClass} will act as a REST client for
* @throws IllegalArgumentException If the provided restClientClass is not an interface or not
* annotated with <code>@RegisterRestClient</code>
* @throws IllegalArgumentException If hostUrl is not a valid URL
* @return the current instance
*/
public ApplicationContainer withMpRestClient(Class<?> restClientClass, String hostUrl) {
Objects.requireNonNull(restClientClass, "restClientClass must be non-null");
Objects.requireNonNull(hostUrl, "hostUrl must be non-null");
if (!restClientClass.isInterface()) {
throw new IllegalArgumentException("Provided restClientClass " + restClientClass.getCanonicalName() + " must be an interface");
}
try {
new URL(hostUrl);
} catch (MalformedURLException e) {
throw new IllegalArgumentException(e);
}
String configToken = readMpRestClientConfigKey(restClientClass);
if (configToken == null || configToken.isEmpty())
configToken = restClientClass.getCanonicalName();
Expand All @@ -467,9 +483,8 @@ private String readMpRestClientConfigKey(Class<?> restClientClass) {
false,
getClass().getClassLoader());
} catch (ClassNotFoundException | LinkageError notFound) {
return null;
throw new ExtensionConfigurationException("Unable to load @RegisterRestClient", notFound);
}

Method getConfigKey = null;
try {
getConfigKey = RegisterRestClient.getMethod("configKey");
Expand All @@ -479,14 +494,16 @@ private String readMpRestClientConfigKey(Class<?> restClientClass) {
}

Optional<Annotation> foundAnno = (Optional<Annotation>) AnnotationSupport.findAnnotation(restClientClass, RegisterRestClient);
if (!foundAnno.isPresent())
return null;
if (!foundAnno.isPresent()) {
throw new IllegalArgumentException("Provided restClientClass " + restClientClass + " must be annotated with "
+ RegisterRestClient.getSimpleName());
}

Annotation anno = foundAnno.get();
try {
return (String) getConfigKey.invoke(anno);
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
return null;
throw new IllegalArgumentException("Unable to obtain configKey from " + anno + " found on " + restClientClass.getCanonicalName());
}
}

Expand All @@ -496,6 +513,7 @@ private String readMpRestClientConfigKey(Class<?> restClientClass) {
*
* @param restClientClass The MicroProfile REST Client class
* @param hostUrl The URL that the {@code restClientClass} will act as a REST client for
* @throws IllegalArgumentException If hostUrl is not a valid URL
* @return the current instance
*/
public ApplicationContainer withMpRestClient(String restClientClass, String hostUrl) {
Expand All @@ -506,6 +524,11 @@ public ApplicationContainer withMpRestClient(String restClientClass, String host
} else {
restClientClass += "/mp-rest/url";
}
try {
new URL(hostUrl);
} catch (MalformedURLException e) {
throw new IllegalArgumentException(e);
}
return withEnv(restClientClass, hostUrl);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package org.microshed.testing.testcontainers;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.util.Arrays;
import java.util.Map;
Expand All @@ -31,17 +32,24 @@ public class ApplicationContainerTest {

// Base uri configured with: com_example_StringRestClient_mp_rest_url
@RegisterRestClient
public static class SampleRestClient1 {
public static interface SampleRestClient1 {
}

// Base uri configured with: rc_config_key_mp_rest_url
@RegisterRestClient(configKey = "rc-config-key")
public static class SampleRestClient2 {
public static interface SampleRestClient2 {
}

// Base uri configured with: CLIENT_CONFIG_KEY_mp_rest_url
@RegisterRestClient(configKey = "CLIENT_CONFIG_KEY")
public static class SampleRestClient3 {
public static interface SampleRestClient3 {
}

public static interface NoAnnotationClient {
}

@RegisterRestClient
public static class ConcreteClassClient {
}

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

@Test
public void testMpRestClientValidation() {
final String clientUrl = "http://example.com";

assertThrows(IllegalArgumentException.class, () -> dummyApp().withMpRestClient(NoAnnotationClient.class, clientUrl));
assertThrows(IllegalArgumentException.class, () -> dummyApp().withMpRestClient(ConcreteClassClient.class, clientUrl));
assertThrows(NullPointerException.class, () -> dummyApp().withMpRestClient(SampleRestClient1.class, null));
Class<?> nullClass = null;
assertThrows(NullPointerException.class, () -> dummyApp().withMpRestClient(nullClass, clientUrl));
assertThrows(IllegalArgumentException.class, () -> dummyApp().withMpRestClient(SampleRestClient1.class, "bogus"));
assertThrows(IllegalArgumentException.class, () -> dummyApp().withMpRestClient("com.example.StringRestClient/mp-rest/url", "bogus"));
}

@Test
public void testCorrectServerAdapter() {
ApplicationContainer app = dummyApp();
Expand Down