package io.airlift.skeleton;
import com.google.inject.Injector;
import io.airlift.bootstrap.Bootstrap;
import io.airlift.bootstrap.LifeCycleManager;
import io.airlift.http.client.HttpClient;
import io.airlift.http.client.StatusResponseHandler.StatusResponse;
import io.airlift.http.client.jetty.JettyHttpClient;
import io.airlift.http.server.testing.TestingHttpServer;
import io.airlift.http.server.testing.TestingHttpServerModule;
import io.airlift.jaxrs.JaxrsModule;
import io.airlift.jmx.JmxHttpModule;
import io.airlift.jmx.JmxModule;
import io.airlift.json.JsonModule;
import io.airlift.node.testing.TestingNodeModule;
import io.airlift.testing.Closeables;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import java.net.URI;
import static io.airlift.http.client.Request.Builder.prepareGet;
import static io.airlift.http.client.StatusResponseHandler.createStatusResponseHandler;
import static javax.ws.rs.core.Response.Status.OK;
import static org.testng.Assert.assertEquals;
public class TestServer
{
private HttpClient client;
private TestingHttpServer server;
private LifeCycleManager lifeCycleManager;
@BeforeMethod
public void setup()
throws Exception
{
Bootstrap app = new Bootstrap(
new TestingNodeModule(),
new TestingHttpServerModule(),
new JsonModule(),
new JaxrsModule(true),
new JmxHttpModule(),
new JmxModule(),
new MainModule());
Injector injector = app
.strictConfig()
.doNotInitializeLogging()
.initialize();
lifeCycleManager = injector.getInstance(LifeCycleManager.class);
server = injector.getInstance(TestingHttpServer.class);
client = new JettyHttpClient();
}
@AfterMethod
public void teardown()
throws Exception
{
try {
if (lifeCycleManager != null) {
lifeCycleManager.stop();
}
}
finally {
Closeables.closeQuietly(client);
}
}
@Test
public void testNothing()
throws Exception
{
StatusResponse response = client.execute(
prepareGet().setUri(uriFor("/v1/jmx/mbean")).build(),
createStatusResponseHandler());
assertEquals(response.getStatusCode(), OK.getStatusCode());
}
private URI uriFor(String path)
{
return server.getBaseUrl().resolve(path);
}
}