org.kiwiproject.test.dropwizard.app.DropwizardAppTests Maven / Gradle / Ivy
package org.kiwiproject.test.dropwizard.app;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toSet;
import com.google.common.annotations.Beta;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.Sets;
import io.dropwizard.Configuration;
import io.dropwizard.jersey.DropwizardResourceConfig;
import io.dropwizard.jersey.setup.JerseyEnvironment;
import io.dropwizard.lifecycle.JettyManaged;
import io.dropwizard.lifecycle.Managed;
import io.dropwizard.lifecycle.ServerLifecycleListener;
import io.dropwizard.lifecycle.setup.LifecycleEnvironment;
import io.dropwizard.testing.junit5.DropwizardAppExtension;
import lombok.experimental.UtilityClass;
import org.eclipse.jetty.util.component.LifeCycle;
import org.glassfish.jersey.internal.inject.InstanceBinding;
import org.kiwiproject.reflect.KiwiReflection;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.SortedSet;
import java.util.function.Predicate;
import java.util.stream.Stream;
/**
* Test utility for testing Dropwizard apps when using {@link DropwizardAppExtension}.
*/
@UtilityClass
public class DropwizardAppTests {
/**
* Dropwizard does not expose this as a public class, so we have to resort to reflection and hackery to get at
* this class. It is, unfortunately, the class used when you register a {@link ServerLifecycleListener} in
* a Dropwizard application, so we need to access it somehow.
*/
@VisibleForTesting
static final String DROPWIZARD_PRIVATE_SERVER_LISTENER_CLASS_NAME =
"io.dropwizard.lifecycle.setup.LifecycleEnvironment$ServerListener";
// Resources
/**
* Find the resource classes registered in the given Dropwizard app.
*
* @param app the DropwizardAppExtension containing the Dropwizard app being tested
* @param the configuration type
* @return list containing registered resource types
*/
public static List> registeredResourceClassesOf(DropwizardAppExtension app) {
return registeredResourceClassesOf(app.getEnvironment().jersey());
}
/**
* Find the resource classes registered in the given {@link JerseyEnvironment}.
*
* @param jersey the {@link JerseyEnvironment} associated with the Dropwizard app being tested
* @return list containing registered resource types
*/
public static List> registeredResourceClassesOf(JerseyEnvironment jersey) {
return registeredResourceObjectsOf(jersey)
.stream()
.map(Object::getClass)
.collect(toList());
}
/**
* Find the resource objects registered in the given Dropwizard app.
*
* @param app the DropwizardAppExtension containing the Dropwizard app being tested
* @param the configuration type
* @return list containing registered resource objects
*/
public static Set