use of org.apache.nifi.remote.protocol.DataPacket in project nifi by apache.
the class TestSocketClientTransaction method testSendWithInvalidChecksum.
@Test
public void testSendWithInvalidChecksum() throws IOException {
ByteArrayOutputStream serverResponseBos = new ByteArrayOutputStream();
DataOutputStream serverResponse = new DataOutputStream(serverResponseBos);
ResponseCode.CONFIRM_TRANSACTION.writeResponse(serverResponse, "Different checksum");
ByteArrayInputStream bis = new ByteArrayInputStream(serverResponseBos.toByteArray());
ByteArrayOutputStream bos = new ByteArrayOutputStream();
SocketClientTransaction transaction = getClientTransaction(bis, bos, TransferDirection.SEND);
execSendWithInvalidChecksum(transaction);
// Verify what client has sent.
DataInputStream sentByClient = new DataInputStream(new ByteArrayInputStream(bos.toByteArray()));
assertEquals(RequestType.SEND_FLOWFILES, RequestType.readRequestType(sentByClient));
DataPacket packetByClient = codec.decode(sentByClient);
assertEquals("contents on client 1", readContents(packetByClient));
Response continueDataResponse = Response.read(sentByClient);
assertEquals(ResponseCode.CONTINUE_TRANSACTION, continueDataResponse.getCode());
packetByClient = codec.decode(sentByClient);
assertEquals("contents on client 2", readContents(packetByClient));
Response endOfDataResponse = Response.read(sentByClient);
assertEquals(ResponseCode.FINISH_TRANSACTION, endOfDataResponse.getCode());
Response confirmResponse = Response.read(sentByClient);
assertEquals(ResponseCode.BAD_CHECKSUM, confirmResponse.getCode());
assertEquals(-1, sentByClient.read());
}
use of org.apache.nifi.remote.protocol.DataPacket in project nifi by apache.
the class AbstractTransaction method receive.
@Override
public final DataPacket receive() throws IOException {
try {
try {
if (state != TransactionState.DATA_EXCHANGED && state != TransactionState.TRANSACTION_STARTED) {
throw new IllegalStateException("Cannot receive data from " + peer + " because Transaction State is " + state);
}
if (direction == TransferDirection.SEND) {
throw new IllegalStateException("Attempting to receive data from " + peer + " but started a SEND Transaction");
}
// if we already know there's no data, just return null
if (!dataAvailable) {
return null;
}
// if we have already received a packet, check if another is available.
if (transfers > 0) {
// Determine if Peer will send us data or has no data to send us
final Response dataAvailableCode = readTransactionResponse();
switch(dataAvailableCode.getCode()) {
case CONTINUE_TRANSACTION:
logger.debug("{} {} Indicates Transaction should continue", this, peer);
this.dataAvailable = true;
break;
case FINISH_TRANSACTION:
logger.debug("{} {} Indicates Transaction should finish", this, peer);
this.dataAvailable = false;
break;
default:
throw new ProtocolException("Got unexpected response from " + peer + " when asking for data: " + dataAvailableCode);
}
}
// if no data available, return null
if (!dataAvailable) {
return null;
}
logger.debug("{} Receiving data from {}", this, peer);
final InputStream is = peer.getCommunicationsSession().getInput().getInputStream();
final InputStream dataIn = compress ? new CompressionInputStream(is) : is;
final DataPacket packet = codec.decode(new CheckedInputStream(dataIn, crc));
if (packet == null) {
this.dataAvailable = false;
} else {
transfers++;
contentBytes += packet.getSize();
}
this.state = TransactionState.DATA_EXCHANGED;
return packet;
} catch (final IOException ioe) {
throw new IOException("Failed to receive data from " + peer + " due to " + ioe, ioe);
}
} catch (final Exception e) {
error();
throw e;
}
}
use of org.apache.nifi.remote.protocol.DataPacket in project nifi by apache.
the class DataPacketDtoTest method testParserSingle.
@Test
public void testParserSingle() throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
StringBuilder stringBuilder = new StringBuilder("[");
DataPacketDto dataPacketDto = new DataPacketDto("test data".getBytes(StandardCharsets.UTF_8)).putAttribute("key", "value");
stringBuilder.append(objectMapper.writeValueAsString(dataPacketDto));
stringBuilder.append("]");
List<DataPacket> dataPackets = DataPacketDto.getDataPacketStream(new ByteArrayInputStream(stringBuilder.toString().getBytes(StandardCharsets.UTF_8))).collect(Collectors.toList());
assertEquals(1, dataPackets.size());
assertEquals(dataPacketDto.toDataPacket(), dataPackets.get(0));
}
use of org.apache.nifi.remote.protocol.DataPacket in project nifi by apache.
the class DataPacketDtoTest method testParserMultiple.
@Test
public void testParserMultiple() throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
StringBuilder stringBuilder = new StringBuilder("[");
DataPacketDto dataPacketDto = new DataPacketDto("test data".getBytes(StandardCharsets.UTF_8)).putAttribute("key", "value");
stringBuilder.append(objectMapper.writeValueAsString(dataPacketDto));
DataPacketDto dataPacketDto2 = new DataPacketDto("test data 2".getBytes(StandardCharsets.UTF_8)).putAttribute("key2", "value2");
stringBuilder.append(",");
stringBuilder.append(objectMapper.writeValueAsString(dataPacketDto2));
stringBuilder.append("]");
List<DataPacket> dataPackets = DataPacketDto.getDataPacketStream(new ByteArrayInputStream(stringBuilder.toString().getBytes(StandardCharsets.UTF_8))).collect(Collectors.toList());
assertEquals(2, dataPackets.size());
assertEquals(dataPacketDto.toDataPacket(), dataPackets.get(0));
assertEquals(dataPacketDto2.toDataPacket(), dataPackets.get(1));
}
use of org.apache.nifi.remote.protocol.DataPacket in project apex-malhar by apache.
the class NiFiSinglePortOutputOperatorTest method verifyTransactions.
private void verifyTransactions(List<String> expectedContents, List<MockTransaction> transactions) throws IOException {
// convert all the data packets in the transactions to strings
final List<String> dataPacketContents = new ArrayList<>();
for (MockTransaction mockTransaction : transactions) {
List<DataPacket> dps = mockTransaction.getSentDataPackets();
Assert.assertTrue(dps.size() > 0);
for (DataPacket dp : dps) {
final String dpContent = IOUtils.toString(dp.getData());
dataPacketContents.add(dpContent);
}
}
// verify each expected piece of content is found in the data packet contents
for (String expectedContent : expectedContents) {
boolean found = false;
for (String dataPacket : dataPacketContents) {
if (dataPacket.equals(expectedContent)) {
found = true;
break;
}
}
Assert.assertTrue(found);
}
}
Aggregations