Search in sources :

Example 11 with NullOutputStream

use of org.apache.nifi.stream.io.NullOutputStream in project nifi by apache.

the class TestHandleHttpRequest method testRequestAddedToService.

@Test(timeout = 10000)
public void testRequestAddedToService() throws InitializationException, MalformedURLException, IOException, InterruptedException {
    final TestRunner runner = TestRunners.newTestRunner(HandleHttpRequest.class);
    runner.setProperty(HandleHttpRequest.PORT, "0");
    final MockHttpContextMap contextMap = new MockHttpContextMap();
    runner.addControllerService("http-context-map", contextMap);
    runner.enableControllerService(contextMap);
    runner.setProperty(HandleHttpRequest.HTTP_CONTEXT_MAP, "http-context-map");
    // trigger processor to stop but not shutdown.
    runner.run(1, false);
    try {
        final Thread httpThread = new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    final int port = ((HandleHttpRequest) runner.getProcessor()).getPort();
                    final HttpURLConnection connection = (HttpURLConnection) new URL("http://localhost:" + port + "/my/path?query=true&value1=value1&value2=&value3&value4=apple=orange").openConnection();
                    connection.setDoOutput(false);
                    connection.setRequestMethod("GET");
                    connection.setRequestProperty("header1", "value1");
                    connection.setRequestProperty("header2", "");
                    connection.setRequestProperty("header3", "apple=orange");
                    connection.setConnectTimeout(3000);
                    connection.setReadTimeout(3000);
                    StreamUtils.copy(connection.getInputStream(), new NullOutputStream());
                } catch (final Throwable t) {
                    t.printStackTrace();
                    Assert.fail(t.toString());
                }
            }
        });
        httpThread.start();
        while (runner.getFlowFilesForRelationship(HandleHttpRequest.REL_SUCCESS).isEmpty()) {
            // process the request.
            runner.run(1, false, false);
        }
        runner.assertAllFlowFilesTransferred(HandleHttpRequest.REL_SUCCESS, 1);
        assertEquals(1, contextMap.size());
        final MockFlowFile mff = runner.getFlowFilesForRelationship(HandleHttpRequest.REL_SUCCESS).get(0);
        mff.assertAttributeEquals("http.query.param.query", "true");
        mff.assertAttributeEquals("http.query.param.value1", "value1");
        mff.assertAttributeEquals("http.query.param.value2", "");
        mff.assertAttributeEquals("http.query.param.value3", "");
        mff.assertAttributeEquals("http.query.param.value4", "apple=orange");
        mff.assertAttributeEquals("http.headers.header1", "value1");
        mff.assertAttributeEquals("http.headers.header3", "apple=orange");
    } finally {
        // shut down the server
        runner.run(1, true);
    }
}
Also used : MockFlowFile(org.apache.nifi.util.MockFlowFile) HttpURLConnection(java.net.HttpURLConnection) TestRunner(org.apache.nifi.util.TestRunner) URL(java.net.URL) NullOutputStream(org.apache.nifi.stream.io.NullOutputStream) Test(org.junit.Test)

Example 12 with NullOutputStream

use of org.apache.nifi.stream.io.NullOutputStream in project nifi by apache.

the class TestHandleHttpRequest method testSecure.

@Test
public void testSecure() throws InitializationException {
    final TestRunner runner = TestRunners.newTestRunner(HandleHttpRequest.class);
    runner.setProperty(HandleHttpRequest.PORT, "0");
    final MockHttpContextMap contextMap = new MockHttpContextMap();
    runner.addControllerService("http-context-map", contextMap);
    runner.enableControllerService(contextMap);
    runner.setProperty(HandleHttpRequest.HTTP_CONTEXT_MAP, "http-context-map");
    final Map<String, String> sslProperties = getKeystoreProperties();
    sslProperties.putAll(getTruststoreProperties());
    sslProperties.put(StandardSSLContextService.SSL_ALGORITHM.getName(), "TLSv1.2");
    final SSLContext sslContext = useSSLContextService(runner, sslProperties);
    // trigger processor to stop but not shutdown.
    runner.run(1, false);
    try {
        final Thread httpThread = new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    final int port = ((HandleHttpRequest) runner.getProcessor()).getPort();
                    final HttpsURLConnection connection = (HttpsURLConnection) new URL("https://localhost:" + port + "/my/path?query=true&value1=value1&value2=&value3&value4=apple=orange").openConnection();
                    connection.setSSLSocketFactory(sslContext.getSocketFactory());
                    connection.setDoOutput(false);
                    connection.setRequestMethod("GET");
                    connection.setRequestProperty("header1", "value1");
                    connection.setRequestProperty("header2", "");
                    connection.setRequestProperty("header3", "apple=orange");
                    connection.setConnectTimeout(3000);
                    connection.setReadTimeout(3000);
                    StreamUtils.copy(connection.getInputStream(), new NullOutputStream());
                } catch (final Throwable t) {
                    t.printStackTrace();
                    Assert.fail(t.toString());
                }
            }
        });
        httpThread.start();
        while (runner.getFlowFilesForRelationship(HandleHttpRequest.REL_SUCCESS).isEmpty()) {
            // process the request.
            runner.run(1, false, false);
        }
        runner.assertAllFlowFilesTransferred(HandleHttpRequest.REL_SUCCESS, 1);
        assertEquals(1, contextMap.size());
        final MockFlowFile mff = runner.getFlowFilesForRelationship(HandleHttpRequest.REL_SUCCESS).get(0);
        mff.assertAttributeEquals("http.query.param.query", "true");
        mff.assertAttributeEquals("http.query.param.value1", "value1");
        mff.assertAttributeEquals("http.query.param.value2", "");
        mff.assertAttributeEquals("http.query.param.value3", "");
        mff.assertAttributeEquals("http.query.param.value4", "apple=orange");
        mff.assertAttributeEquals("http.headers.header1", "value1");
        mff.assertAttributeEquals("http.headers.header3", "apple=orange");
        mff.assertAttributeEquals("http.protocol", "HTTP/1.1");
    } finally {
        // shut down the server
        runner.run(1, true);
    }
}
Also used : TestRunner(org.apache.nifi.util.TestRunner) SSLContext(javax.net.ssl.SSLContext) URL(java.net.URL) MockFlowFile(org.apache.nifi.util.MockFlowFile) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) NullOutputStream(org.apache.nifi.stream.io.NullOutputStream) Test(org.junit.Test)

Aggregations

NullOutputStream (org.apache.nifi.stream.io.NullOutputStream)12 Test (org.junit.Test)7 IOException (java.io.IOException)5 OutputStream (java.io.OutputStream)5 InputStream (java.io.InputStream)4 HashMap (java.util.HashMap)4 FlowFile (org.apache.nifi.flowfile.FlowFile)4 Ignore (org.junit.Ignore)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 File (java.io.File)3 FileInputStream (java.io.FileInputStream)3 URL (java.net.URL)3 CRC32 (java.util.zip.CRC32)3 CheckedInputStream (java.util.zip.CheckedInputStream)3 TestRunner (org.apache.nifi.util.TestRunner)3 BufferedOutputStream (java.io.BufferedOutputStream)2 DataOutputStream (java.io.DataOutputStream)2 FileOutputStream (java.io.FileOutputStream)2 HttpURLConnection (java.net.HttpURLConnection)2 FileChannel (java.nio.channels.FileChannel)2