Search in sources :

Example 6 with NoopMetricsContext

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();
    }
}
Also used : HttpHandler(co.cask.http.HttpHandler) InetSocketAddress(java.net.InetSocketAddress) NettyHttpService(co.cask.http.NettyHttpService) NoopMetricsContext(co.cask.cdap.api.metrics.NoopMetricsContext) File(java.io.File) Test(org.junit.Test)

Example 7 with NoopMetricsContext

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();
    }
}
Also used : HttpHandler(co.cask.http.HttpHandler) InetSocketAddress(java.net.InetSocketAddress) NoopMetricsContext(co.cask.cdap.api.metrics.NoopMetricsContext) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection) URL(java.net.URL) NettyHttpService(co.cask.http.NettyHttpService) Test(org.junit.Test)

Example 8 with NoopMetricsContext

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();
    }
}
Also used : HttpHandler(co.cask.http.HttpHandler) HttpURLConnection(java.net.HttpURLConnection) InetSocketAddress(java.net.InetSocketAddress) NettyHttpService(co.cask.http.NettyHttpService) NoopMetricsContext(co.cask.cdap.api.metrics.NoopMetricsContext) File(java.io.File) URL(java.net.URL) Test(org.junit.Test)

Aggregations

NoopMetricsContext (co.cask.cdap.api.metrics.NoopMetricsContext)8 Test (org.junit.Test)6 HttpHandler (co.cask.http.HttpHandler)5 NettyHttpService (co.cask.http.NettyHttpService)4 InetSocketAddress (java.net.InetSocketAddress)4 HttpURLConnection (java.net.HttpURLConnection)3 URL (java.net.URL)3 ArrayList (java.util.ArrayList)3 DatasetService (co.cask.cdap.data2.datafabric.dataset.service.DatasetService)2 DatasetOpExecutor (co.cask.cdap.data2.datafabric.dataset.service.executor.DatasetOpExecutor)2 DatasetFramework (co.cask.cdap.data2.dataset2.DatasetFramework)2 DatumReaderFactory (co.cask.cdap.internal.io.DatumReaderFactory)2 SchemaGenerator (co.cask.cdap.internal.io.SchemaGenerator)2 MetricDatasetFactory (co.cask.cdap.metrics.store.MetricDatasetFactory)2 File (java.io.File)2 HashSet (java.util.HashSet)2 TransactionManager (org.apache.tephra.TransactionManager)2 TimeValue (co.cask.cdap.api.dataset.lib.cube.TimeValue)1 MetricDataQuery (co.cask.cdap.api.metrics.MetricDataQuery)1 MetricStore (co.cask.cdap.api.metrics.MetricStore)1