use of co.cask.cdap.api.metrics.NoopMetricsContext in project cdap by caskdata.
the class HttpHandlerGeneratorTest method testContentConsumer.
@Test
public void testContentConsumer() throws Exception {
HttpHandlerFactory factory = new HttpHandlerFactory("/content", TransactionControl.IMPLICIT);
// Create the file upload handler and starts a netty server with it
final File outputDir = TEMP_FOLDER.newFolder();
HttpHandler httpHandler = factory.createHttpHandler(TypeToken.of(FileHandler.class), new AbstractDelegatorContext<FileHandler>() {
@Override
protected FileHandler createHandler() {
return new FileHandler(outputDir);
}
}, new NoopMetricsContext());
// Creates a Netty http server with 1K request buffer
NettyHttpService service = NettyHttpService.builder("test-content-consumer").setHttpHandlers(httpHandler).setHttpChunkLimit(1024).build();
service.start();
try {
InetSocketAddress bindAddress = service.getBindAddress();
testUpload(outputDir, bindAddress, "");
testUpload(outputDir, bindAddress, "-no-tx");
} finally {
service.stop();
}
}
use of co.cask.cdap.api.metrics.NoopMetricsContext in project cdap by caskdata.
the class HttpHandlerGeneratorTest method testHttpHandlerGenerator.
@Test
public void testHttpHandlerGenerator() throws Exception {
HttpHandlerFactory factory = new HttpHandlerFactory("/prefix", TransactionControl.IMPLICIT);
HttpHandler httpHandler = factory.createHttpHandler(TypeToken.of(MyHttpHandler.class), new AbstractDelegatorContext<MyHttpHandler>() {
@Override
protected MyHttpHandler createHandler() {
return new MyHttpHandler();
}
}, new NoopMetricsContext());
HttpHandler httpHandlerWithoutAnnotation = factory.createHttpHandler(TypeToken.of(NoAnnotationHandler.class), new AbstractDelegatorContext<NoAnnotationHandler>() {
@Override
protected NoAnnotationHandler createHandler() {
return new NoAnnotationHandler();
}
}, new NoopMetricsContext());
NettyHttpService service = NettyHttpService.builder("test-handler-generator").setHttpHandlers(httpHandler, httpHandlerWithoutAnnotation).build();
service.start();
try {
InetSocketAddress bindAddress = service.getBindAddress();
// Make a GET call
URLConnection urlConn = new URL(String.format("http://%s:%d/prefix/p2/handle", bindAddress.getHostName(), bindAddress.getPort())).openConnection();
urlConn.setReadTimeout(2000);
Assert.assertEquals("Hello World", new String(ByteStreams.toByteArray(urlConn.getInputStream()), Charsets.UTF_8));
// Make a POST call
urlConn = new URL(String.format("http://%s:%d/prefix/p2/echo/test", bindAddress.getHostName(), bindAddress.getPort())).openConnection();
urlConn.setReadTimeout(2000);
urlConn.setDoOutput(true);
ByteStreams.copy(ByteStreams.newInputStreamSupplier("Hello".getBytes(Charsets.UTF_8)), urlConn.getOutputStream());
Assert.assertEquals("Hello test", new String(ByteStreams.toByteArray(urlConn.getInputStream()), Charsets.UTF_8));
// Ensure that even though the handler did not have a class-level annotation, we still prefix the path that it
// handles by "/prefix"
urlConn = new URL(String.format("http://%s:%d/prefix/ping", bindAddress.getHostName(), bindAddress.getPort())).openConnection();
urlConn.setReadTimeout(2000);
Assert.assertEquals("OK", new String(ByteStreams.toByteArray(urlConn.getInputStream()), Charsets.UTF_8));
// Call to a method that raise exception
urlConn = new URL(String.format("http://%s:%d/prefix/p2/exception", bindAddress.getHostName(), bindAddress.getPort())).openConnection();
Assert.assertEquals(500, ((HttpURLConnection) urlConn).getResponseCode());
Assert.assertEquals("Exception occurred while handling request: exception", new String(ByteStreams.toByteArray(((HttpURLConnection) urlConn).getErrorStream()), "UTF-8"));
urlConn = new URL(String.format("http://%s:%d/prefix/p2/exceptionNoTx", bindAddress.getHostName(), bindAddress.getPort())).openConnection();
Assert.assertEquals(500, ((HttpURLConnection) urlConn).getResponseCode());
Assert.assertEquals("Exception occurred while handling request: exceptionNoTx", new String(ByteStreams.toByteArray(((HttpURLConnection) urlConn).getErrorStream()), "UTF-8"));
} finally {
service.stop();
}
}
use of co.cask.cdap.api.metrics.NoopMetricsContext in project cdap by caskdata.
the class HttpHandlerGeneratorTest method testContentProducer.
@Test
public void testContentProducer() throws Exception {
HttpHandlerFactory factory = new HttpHandlerFactory("/content", TransactionControl.IMPLICIT);
// Create the file upload handler and starts a netty server with it
final File outputDir = TEMP_FOLDER.newFolder();
HttpHandler httpHandler = factory.createHttpHandler(TypeToken.of(FileHandler.class), new AbstractDelegatorContext<FileHandler>() {
@Override
protected FileHandler createHandler() {
return new FileHandler(outputDir);
}
}, new NoopMetricsContext());
NettyHttpService service = NettyHttpService.builder("test-content-producer").setHttpHandlers(httpHandler).build();
service.start();
try {
// Generate a 100K file
File file = TEMP_FOLDER.newFile();
Files.write(Strings.repeat("0123456789", 10240).getBytes(Charsets.UTF_8), file);
InetSocketAddress bindAddress = service.getBindAddress();
// Upload the generated file
URL uploadURL = new URL(String.format("http://%s:%d/content/upload/test.txt", bindAddress.getHostName(), bindAddress.getPort()));
HttpURLConnection urlConn = (HttpURLConnection) uploadURL.openConnection();
try {
urlConn.setDoOutput(true);
urlConn.setRequestMethod("PUT");
Files.copy(file, urlConn.getOutputStream());
Assert.assertEquals(200, urlConn.getResponseCode());
} finally {
urlConn.disconnect();
}
// Download the file
File downloadFile = TEMP_FOLDER.newFile();
urlConn = (HttpURLConnection) new URL(String.format("http://%s:%d/content/download/test.txt", bindAddress.getHostName(), bindAddress.getPort())).openConnection();
try {
ByteStreams.copy(urlConn.getInputStream(), Files.newOutputStreamSupplier(downloadFile));
} finally {
urlConn.disconnect();
}
// Compare if the file content are the same
Assert.assertTrue(Files.equal(file, downloadFile));
// Download a file that doesn't exist
urlConn = (HttpURLConnection) new URL(String.format("http://%s:%d/content/download/test2.txt", bindAddress.getHostName(), bindAddress.getPort())).openConnection();
try {
Assert.assertEquals(500, urlConn.getResponseCode());
} finally {
urlConn.disconnect();
}
// Upload the file to the POST endpoint. The endpoint should response with the same file content
downloadFile = TEMP_FOLDER.newFile();
urlConn = (HttpURLConnection) uploadURL.openConnection();
try {
urlConn.setDoOutput(true);
urlConn.setRequestMethod("POST");
Files.copy(file, urlConn.getOutputStream());
ByteStreams.copy(urlConn.getInputStream(), Files.newOutputStreamSupplier(downloadFile));
Assert.assertEquals(200, urlConn.getResponseCode());
Assert.assertTrue(Files.equal(file, downloadFile));
} finally {
urlConn.disconnect();
}
} finally {
service.stop();
}
}
Aggregations