Search in sources :

Example 26 with ByteArrayInputStream

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

the class TestSocketClientTransaction method testSendZeroFlowFile.

@Test
public void testSendZeroFlowFile() throws IOException {
    ByteArrayOutputStream serverResponseBos = new ByteArrayOutputStream();
    ByteArrayInputStream bis = new ByteArrayInputStream(serverResponseBos.toByteArray());
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    SocketClientTransaction transaction = getClientTransaction(bis, bos, TransferDirection.SEND);
    execSendZeroFlowFile(transaction);
    // Verify what client has sent.
    DataInputStream sentByClient = new DataInputStream(new ByteArrayInputStream(bos.toByteArray()));
    assertEquals(RequestType.SEND_FLOWFILES, RequestType.readRequestType(sentByClient));
    assertEquals(-1, sentByClient.read());
}
Also used : ByteArrayInputStream(org.apache.nifi.stream.io.ByteArrayInputStream) ByteArrayOutputStream(org.apache.nifi.stream.io.ByteArrayOutputStream) DataInputStream(java.io.DataInputStream) Test(org.junit.Test)

Example 27 with ByteArrayInputStream

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

the class TestSocketClientTransaction method testReceiveWithInvalidChecksum.

@Test
public void testReceiveWithInvalidChecksum() throws IOException {
    ByteArrayOutputStream serverResponseBos = new ByteArrayOutputStream();
    DataOutputStream serverResponse = new DataOutputStream(serverResponseBos);
    ResponseCode.MORE_DATA.writeResponse(serverResponse);
    codec.encode(createDataPacket("contents on server 1"), serverResponse);
    ResponseCode.CONTINUE_TRANSACTION.writeResponse(serverResponse);
    codec.encode(createDataPacket("contents on server 2"), serverResponse);
    ResponseCode.FINISH_TRANSACTION.writeResponse(serverResponse);
    ResponseCode.BAD_CHECKSUM.writeResponse(serverResponse);
    ByteArrayInputStream bis = new ByteArrayInputStream(serverResponseBos.toByteArray());
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    SocketClientTransaction transaction = getClientTransaction(bis, bos, TransferDirection.RECEIVE);
    execReceiveWithInvalidChecksum(transaction);
    // Verify what client has sent.
    DataInputStream sentByClient = new DataInputStream(new ByteArrayInputStream(bos.toByteArray()));
    assertEquals(RequestType.RECEIVE_FLOWFILES, RequestType.readRequestType(sentByClient));
    Response confirmResponse = Response.read(sentByClient);
    assertEquals(ResponseCode.CONFIRM_TRANSACTION, confirmResponse.getCode());
    assertEquals("Checksum should be calculated at client", "2969091230", confirmResponse.getMessage());
    assertEquals(-1, sentByClient.read());
}
Also used : Response(org.apache.nifi.remote.protocol.Response) ByteArrayInputStream(org.apache.nifi.stream.io.ByteArrayInputStream) DataOutputStream(java.io.DataOutputStream) ByteArrayOutputStream(org.apache.nifi.stream.io.ByteArrayOutputStream) DataInputStream(java.io.DataInputStream) Test(org.junit.Test)

Example 28 with ByteArrayInputStream

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

the class TestSplitAvro method checkDataFileSplitSize.

private void checkDataFileSplitSize(List<MockFlowFile> flowFiles, int expectedRecordsPerSplit, boolean checkMetadata) throws IOException {
    for (final MockFlowFile flowFile : flowFiles) {
        try (final ByteArrayInputStream in = new ByteArrayInputStream(flowFile.toByteArray());
            final DataFileStream<GenericRecord> reader = new DataFileStream<>(in, new GenericDatumReader<GenericRecord>())) {
            int count = 0;
            GenericRecord record = null;
            while (reader.hasNext()) {
                record = reader.next(record);
                Assert.assertNotNull(record.get("name"));
                Assert.assertNotNull(record.get("favorite_number"));
                count++;
            }
            assertEquals(expectedRecordsPerSplit, count);
            if (checkMetadata) {
                assertEquals(META_VALUE1, reader.getMetaString(META_KEY1));
                assertEquals(META_VALUE2, reader.getMetaLong(META_KEY2));
                assertEquals(META_VALUE3, new String(reader.getMeta(META_KEY3), "UTF-8"));
            }
        }
    }
}
Also used : MockFlowFile(org.apache.nifi.util.MockFlowFile) ByteArrayInputStream(org.apache.nifi.stream.io.ByteArrayInputStream) GenericRecord(org.apache.avro.generic.GenericRecord) DataFileStream(org.apache.avro.file.DataFileStream)

Example 29 with ByteArrayInputStream

use of org.apache.nifi.stream.io.ByteArrayInputStream in project apex-malhar by apache.

the class NiFiSinglePortInputOperatorTest method getDataPacket.

@NotNull
private DataPacket getDataPacket(final String id) {
    Map<String, String> attrs = new HashMap<>();
    attrs.put("keyA", "valA");
    attrs.put("keyB", "valB");
    attrs.put("key" + id, "val" + id);
    byte[] content = ("content" + id).getBytes(StandardCharsets.UTF_8);
    ByteArrayInputStream in = new ByteArrayInputStream(content);
    return new MockDataPacket(attrs, in, content.length);
}
Also used : MockDataPacket(org.apache.apex.malhar.contrib.nifi.mock.MockDataPacket) HashMap(java.util.HashMap) ByteArrayInputStream(org.apache.nifi.stream.io.ByteArrayInputStream) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

ByteArrayInputStream (org.apache.nifi.stream.io.ByteArrayInputStream)29 ByteArrayOutputStream (org.apache.nifi.stream.io.ByteArrayOutputStream)22 Test (org.junit.Test)20 DataInputStream (java.io.DataInputStream)12 DataOutputStream (java.io.DataOutputStream)10 Peer (org.apache.nifi.remote.Peer)9 SiteToSiteRestApiClient (org.apache.nifi.remote.util.SiteToSiteRestApiClient)9 DataPacket (org.apache.nifi.remote.protocol.DataPacket)8 SiteToSiteTestUtils.createDataPacket (org.apache.nifi.remote.protocol.SiteToSiteTestUtils.createDataPacket)8 TransactionResultEntity (org.apache.nifi.web.api.entity.TransactionResultEntity)8 InputStream (java.io.InputStream)7 Response (org.apache.nifi.remote.protocol.Response)7 HttpCommunicationsSession (org.apache.nifi.remote.io.http.HttpCommunicationsSession)5 GenericRecord (org.apache.avro.generic.GenericRecord)4 CommunicationsSession (org.apache.nifi.remote.protocol.CommunicationsSession)4 MockFlowFile (org.apache.nifi.util.MockFlowFile)4 Mockito.doAnswer (org.mockito.Mockito.doAnswer)4 InvocationOnMock (org.mockito.invocation.InvocationOnMock)4 Answer (org.mockito.stubbing.Answer)4 DataFileStream (org.apache.avro.file.DataFileStream)3