Search in sources :

Example 11 with MockFlowFile

use of org.apache.nifi.util.MockFlowFile in project nifi by apache.

the class TestHttpFlowFileServerProtocol method testTransferTwoFiles.

@Test
public void testTransferTwoFiles() throws Exception {
    final String transactionId = "testTransferTwoFiles";
    final Peer peer = getDefaultPeer(transactionId);
    final String endpointUri = "https://remote-host:8443/nifi-api/output-ports/port-id/transactions/" + transactionId + "/flow-files";
    final HttpFlowFileServerProtocol serverProtocol = getDefaultHttpFlowFileServerProtocol();
    final HttpServerCommunicationsSession commsSession = (HttpServerCommunicationsSession) peer.getCommunicationsSession();
    commsSession.putHandshakeParam(HandshakeProperty.BATCH_COUNT, "2");
    commsSession.setUserDn("unit-test");
    commsSession.setDataTransferUrl(endpointUri);
    transferFlowFiles(serverProtocol, transactionId, peer, processSession -> IntStream.of(1, 2).mapToObj(i -> {
        final MockFlowFile flowFile = processSession.createFlowFile(("Server content " + i).getBytes());
        final HashMap<String, String> attributes = new HashMap<>();
        attributes.put("uuid", "server-uuid-" + i);
        attributes.put("filename", "server-filename-" + i);
        attributes.put("server-attr-" + i + "-1", "server-attr-" + i + "-1-value");
        attributes.put("server-attr-" + i + "-2", "server-attr-" + i + "-2-value");
        flowFile.putAttributes(attributes);
        return flowFile;
    }).collect(Collectors.toList()));
    // Commit transaction
    final int flowFileSent = serverProtocol.commitTransferTransaction(peer, "3058746557");
    assertEquals(2, flowFileSent);
    // Assert provenance
    final List<ProvenanceEventRecord> provenanceEvents = sessionState.getProvenanceEvents();
    assertEquals(2, provenanceEvents.size());
    for (final ProvenanceEventRecord provenanceEvent : provenanceEvents) {
        assertEquals(ProvenanceEventType.SEND, provenanceEvent.getEventType());
        assertEquals(endpointUri, provenanceEvent.getTransitUri());
        assertEquals("Remote Host=peer-host, Remote DN=unit-test", provenanceEvent.getDetails());
    }
}
Also used : MockFlowFile(org.apache.nifi.util.MockFlowFile) HttpServerCommunicationsSession(org.apache.nifi.remote.io.http.HttpServerCommunicationsSession) HashMap(java.util.HashMap) Peer(org.apache.nifi.remote.Peer) ProvenanceEventRecord(org.apache.nifi.provenance.ProvenanceEventRecord) Test(org.junit.Test)

Example 12 with MockFlowFile

use of org.apache.nifi.util.MockFlowFile in project nifi by apache.

the class TestHttpFlowFileServerProtocol method testReceiveTwoFiles.

@Test
public void testReceiveTwoFiles() throws Exception {
    final String transactionId = "testReceiveTwoFile";
    final String endpointUri = "https://remote-host:8443/nifi-api/input-ports/port-id/transactions/" + transactionId + "/flow-files";
    final HttpFlowFileServerProtocol serverProtocol = getDefaultHttpFlowFileServerProtocol();
    final Peer peer = getDefaultPeer(transactionId);
    final HttpServerCommunicationsSession commsSession = (HttpServerCommunicationsSession) peer.getCommunicationsSession();
    commsSession.putHandshakeParam(HandshakeProperty.BATCH_COUNT, "2");
    commsSession.setUserDn("unit-test");
    commsSession.setDataTransferUrl(endpointUri);
    receiveFlowFiles(serverProtocol, transactionId, peer, createClientDataPacket(), createClientDataPacket());
    // Commit transaction
    commsSession.setResponseCode(ResponseCode.CONFIRM_TRANSACTION);
    final int flowFileReceived = serverProtocol.commitReceiveTransaction(peer);
    assertEquals(2, flowFileReceived);
    // Assert provenance.
    final List<ProvenanceEventRecord> provenanceEvents = sessionState.getProvenanceEvents();
    assertEquals(2, provenanceEvents.size());
    for (final ProvenanceEventRecord provenanceEvent : provenanceEvents) {
        assertEquals(ProvenanceEventType.RECEIVE, provenanceEvent.getEventType());
        assertEquals(endpointUri, provenanceEvent.getTransitUri());
        assertEquals("Remote Host=peer-host, Remote DN=unit-test", provenanceEvent.getDetails());
    }
    // Assert received flow files.
    processSession.assertAllFlowFilesTransferred(Relationship.ANONYMOUS);
    final List<MockFlowFile> flowFiles = processSession.getFlowFilesForRelationship(Relationship.ANONYMOUS);
    assertEquals(2, flowFiles.size());
    for (final MockFlowFile flowFile : flowFiles) {
        flowFile.assertAttributeEquals(SiteToSiteAttributes.S2S_HOST.key(), peer.getHost());
        flowFile.assertAttributeEquals(SiteToSiteAttributes.S2S_ADDRESS.key(), peer.getHost() + ":" + peer.getPort());
        flowFile.assertAttributeEquals("client-attr-1", "client-attr-1-value");
        flowFile.assertAttributeEquals("client-attr-2", "client-attr-2-value");
    }
}
Also used : MockFlowFile(org.apache.nifi.util.MockFlowFile) HttpServerCommunicationsSession(org.apache.nifi.remote.io.http.HttpServerCommunicationsSession) Peer(org.apache.nifi.remote.Peer) ProvenanceEventRecord(org.apache.nifi.provenance.ProvenanceEventRecord) Test(org.junit.Test)

Example 13 with MockFlowFile

use of org.apache.nifi.util.MockFlowFile in project nifi by apache.

the class TestHttpFlowFileServerProtocol method testTransferOneFileBadChecksum.

@Test
public void testTransferOneFileBadChecksum() throws Exception {
    final HttpFlowFileServerProtocol serverProtocol = getDefaultHttpFlowFileServerProtocol();
    final String transactionId = "testTransferOneFileBadChecksum";
    final Peer peer = getDefaultPeer(transactionId);
    final HttpServerCommunicationsSession commsSession = (HttpServerCommunicationsSession) peer.getCommunicationsSession();
    final String endpointUri = "https://remote-host:8443/nifi-api/output-ports/port-id/transactions/" + transactionId + "/flow-files";
    commsSession.putHandshakeParam(HandshakeProperty.BATCH_COUNT, "1");
    commsSession.setUserDn("unit-test");
    commsSession.setDataTransferUrl(endpointUri);
    transferFlowFiles(serverProtocol, transactionId, peer, processSession -> {
        final MockFlowFile flowFile = processSession.createFlowFile("Server content".getBytes());
        final HashMap<String, String> attributes = new HashMap<>();
        attributes.put("uuid", "server-uuid");
        attributes.put("filename", "server-filename");
        attributes.put("server-attr-1", "server-attr-1-value");
        attributes.put("server-attr-2", "server-attr-2-value");
        flowFile.putAttributes(attributes);
        return Arrays.asList(flowFile);
    });
    // Commit transaction
    try {
        serverProtocol.commitTransferTransaction(peer, "client-sent-wrong-checksum");
        fail();
    } catch (final IOException e) {
        assertTrue(e.getMessage().contains("CRC32 Checksum"));
    }
}
Also used : MockFlowFile(org.apache.nifi.util.MockFlowFile) HttpServerCommunicationsSession(org.apache.nifi.remote.io.http.HttpServerCommunicationsSession) HashMap(java.util.HashMap) Peer(org.apache.nifi.remote.Peer) IOException(java.io.IOException) Test(org.junit.Test)

Example 14 with MockFlowFile

use of org.apache.nifi.util.MockFlowFile in project nifi by apache.

the class PriorityAttributePrioritizerTest method testPrioritizer.

@Test
public void testPrioritizer() throws InstantiationException, IllegalAccessException {
    final Processor processor = new SimpleProcessor();
    final AtomicLong idGenerator = new AtomicLong(0L);
    final MockProcessSession session = new MockProcessSession(new SharedSessionState(processor, idGenerator), Mockito.mock(Processor.class));
    final MockFlowFile ffNoPriority = session.create();
    final MockFlowFile ffPri1 = session.create();
    ffPri1.putAttributes(attrsPri1);
    final MockFlowFile ffPri2 = session.create();
    ffPri2.putAttributes(attrsPri2);
    final MockFlowFile ffPrin1 = session.create();
    ffPrin1.putAttributes(attrsPrin1);
    final MockFlowFile ffPriA = session.create();
    ffPriA.putAttributes(attrsPriA);
    final MockFlowFile ffPriB = session.create();
    ffPriB.putAttributes(attrsPriB);
    final MockFlowFile ffPriLP = session.create();
    ffPriLP.putAttributes(attrsPriLP);
    final MockFlowFile ffPriLN = session.create();
    ffPriLN.putAttributes(attrsPriLN);
    final PriorityAttributePrioritizer prioritizer = new PriorityAttributePrioritizer();
    assertEquals(0, prioritizer.compare(null, null));
    assertEquals(-1, prioritizer.compare(ffNoPriority, null));
    assertEquals(1, prioritizer.compare(null, ffNoPriority));
    assertEquals(0, prioritizer.compare(ffNoPriority, ffNoPriority));
    assertEquals(-1, prioritizer.compare(ffPri1, ffNoPriority));
    assertEquals(1, prioritizer.compare(ffNoPriority, ffPri1));
    assertEquals(0, prioritizer.compare(ffPri1, ffPri1));
    assertEquals(-1, prioritizer.compare(ffPri1, ffPri2));
    assertEquals(1, prioritizer.compare(ffPri2, ffPri1));
    assertEquals(-1, prioritizer.compare(ffPrin1, ffPri1));
    assertEquals(1, prioritizer.compare(ffPri1, ffPrin1));
    assertEquals(-1, prioritizer.compare(ffPri1, ffPriA));
    assertEquals(1, prioritizer.compare(ffPriA, ffPri1));
    assertEquals(0, prioritizer.compare(ffPriA, ffPriA));
    assertEquals(-1, prioritizer.compare(ffPriA, ffPriB));
    assertEquals(1, prioritizer.compare(ffPriB, ffPriA));
    assertEquals(1, prioritizer.compare(ffPriLP, ffPri1));
    assertEquals(-1, prioritizer.compare(ffPri1, ffPriLP));
    assertEquals(-1, prioritizer.compare(ffPriLN, ffPri1));
    assertEquals(1, prioritizer.compare(ffPri1, ffPriLN));
}
Also used : MockFlowFile(org.apache.nifi.util.MockFlowFile) AtomicLong(java.util.concurrent.atomic.AtomicLong) SharedSessionState(org.apache.nifi.util.SharedSessionState) Processor(org.apache.nifi.processor.Processor) AbstractProcessor(org.apache.nifi.processor.AbstractProcessor) MockProcessSession(org.apache.nifi.util.MockProcessSession) Test(org.junit.Test)

Example 15 with MockFlowFile

use of org.apache.nifi.util.MockFlowFile in project nifi by apache.

the class QueryCassandraTest method testProcessorJsonOutput.

@Test
public void testProcessorJsonOutput() {
    setUpStandardProcessorConfig();
    testRunner.setIncomingConnection(false);
    // Test JSON output
    testRunner.setProperty(QueryCassandra.OUTPUT_FORMAT, QueryCassandra.JSON_FORMAT);
    testRunner.run(1, true, true);
    testRunner.assertAllFlowFilesTransferred(QueryCassandra.REL_SUCCESS, 1);
    List<MockFlowFile> files = testRunner.getFlowFilesForRelationship(QueryCassandra.REL_SUCCESS);
    assertNotNull(files);
    assertEquals("One file should be transferred to success", 1, files.size());
    assertEquals("{\"results\":[{\"user_id\":\"user1\",\"first_name\":\"Joe\",\"last_name\":\"Smith\"," + "\"emails\":[\"jsmith@notareal.com\"],\"top_places\":[\"New York, NY\",\"Santa Clara, CA\"]," + "\"todo\":{\"2016-01-03 05:00:00+0000\":\"Set my alarm \\\"for\\\" a month from now\"}," + "\"registered\":\"false\",\"scale\":1.0,\"metric\":2.0}," + "{\"user_id\":\"user2\",\"first_name\":\"Mary\",\"last_name\":\"Jones\"," + "\"emails\":[\"mjones@notareal.com\"],\"top_places\":[\"Orlando, FL\"]," + "\"todo\":{\"2016-02-03 05:00:00+0000\":\"Get milk and bread\"}," + "\"registered\":\"true\",\"scale\":3.0,\"metric\":4.0}]}", new String(files.get(0).toByteArray()));
}
Also used : MockFlowFile(org.apache.nifi.util.MockFlowFile) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Aggregations

MockFlowFile (org.apache.nifi.util.MockFlowFile)1228 Test (org.junit.Test)1198 TestRunner (org.apache.nifi.util.TestRunner)725 HashMap (java.util.HashMap)279 File (java.io.File)155 Matchers.anyString (org.mockito.Matchers.anyString)59 ProvenanceEventRecord (org.apache.nifi.provenance.ProvenanceEventRecord)48 FlowFile (org.apache.nifi.flowfile.FlowFile)42 ByteArrayInputStream (java.io.ByteArrayInputStream)41 ProcessContext (org.apache.nifi.processor.ProcessContext)37 InputStream (java.io.InputStream)34 Connection (java.sql.Connection)31 GenericRecord (org.apache.avro.generic.GenericRecord)31 Statement (java.sql.Statement)30 Schema (org.apache.avro.Schema)30 ArrayList (java.util.ArrayList)29 Map (java.util.Map)27 SQLException (java.sql.SQLException)25 DBCPService (org.apache.nifi.dbcp.DBCPService)25 GenericDatumWriter (org.apache.avro.generic.GenericDatumWriter)24