use of org.apache.nifi.websocket.SendMessage in project nifi by apache.
the class TestPutWebSocket method testSendFailure.
@Test
public void testSendFailure() throws Exception {
final TestRunner runner = TestRunners.newTestRunner(PutWebSocket.class);
final WebSocketService service = spy(WebSocketService.class);
final WebSocketSession webSocketSession = getWebSocketSession();
final String serviceId = "ws-service";
final String endpointId = "client-1";
final String textMessageFromServer = "message from server.";
when(service.getIdentifier()).thenReturn(serviceId);
doAnswer(invocation -> {
final SendMessage sendMessage = invocation.getArgumentAt(2, SendMessage.class);
sendMessage.send(webSocketSession);
return null;
}).when(service).sendMessage(anyString(), anyString(), any(SendMessage.class));
doThrow(new IOException("Sending message failed.")).when(webSocketSession).sendString(anyString());
runner.addControllerService(serviceId, service);
runner.enableControllerService(service);
final Map<String, String> attributes = new HashMap<>();
attributes.put(ATTR_WS_CS_ID, serviceId);
attributes.put(ATTR_WS_ENDPOINT_ID, endpointId);
attributes.put(ATTR_WS_SESSION_ID, webSocketSession.getSessionId());
runner.enqueue(textMessageFromServer, attributes);
runner.run();
final List<MockFlowFile> succeededFlowFiles = runner.getFlowFilesForRelationship(PutWebSocket.REL_SUCCESS);
assertEquals(0, succeededFlowFiles.size());
final List<MockFlowFile> failedFlowFiles = runner.getFlowFilesForRelationship(PutWebSocket.REL_FAILURE);
assertEquals(1, failedFlowFiles.size());
final MockFlowFile failedFlowFile = failedFlowFiles.iterator().next();
assertNotNull(failedFlowFile.getAttribute(ATTR_WS_FAILURE_DETAIL));
final List<ProvenanceEventRecord> provenanceEvents = runner.getProvenanceEvents();
assertEquals(0, provenanceEvents.size());
}
use of org.apache.nifi.websocket.SendMessage in project nifi by apache.
the class TestPutWebSocket method testSuccess.
@Test
public void testSuccess() throws Exception {
final TestRunner runner = TestRunners.newTestRunner(PutWebSocket.class);
final WebSocketService service = spy(WebSocketService.class);
final WebSocketSession webSocketSession = getWebSocketSession();
final String serviceId = "ws-service";
final String endpointId = "client-1";
final String textMessageFromServer = "message from server.";
when(service.getIdentifier()).thenReturn(serviceId);
doAnswer(invocation -> {
final SendMessage sendMessage = invocation.getArgumentAt(2, SendMessage.class);
sendMessage.send(webSocketSession);
return null;
}).when(service).sendMessage(anyString(), anyString(), any(SendMessage.class));
runner.addControllerService(serviceId, service);
runner.enableControllerService(service);
runner.setProperty(PutWebSocket.PROP_WS_MESSAGE_TYPE, "${" + ATTR_WS_MESSAGE_TYPE + "}");
// Enqueue 1st file as Text.
final Map<String, String> attributes = new HashMap<>();
attributes.put(ATTR_WS_CS_ID, serviceId);
attributes.put(ATTR_WS_ENDPOINT_ID, endpointId);
attributes.put(ATTR_WS_SESSION_ID, webSocketSession.getSessionId());
attributes.put(ATTR_WS_MESSAGE_TYPE, WebSocketMessage.Type.TEXT.name());
runner.enqueue(textMessageFromServer, attributes);
// Enqueue 2nd file as Binary.
attributes.put(ATTR_WS_MESSAGE_TYPE, WebSocketMessage.Type.BINARY.name());
runner.enqueue(textMessageFromServer.getBytes(), attributes);
runner.run(2);
final List<MockFlowFile> succeededFlowFiles = runner.getFlowFilesForRelationship(PutWebSocket.REL_SUCCESS);
assertEquals(2, succeededFlowFiles.size());
assertFlowFile(webSocketSession, serviceId, endpointId, succeededFlowFiles.get(0), WebSocketMessage.Type.TEXT);
assertFlowFile(webSocketSession, serviceId, endpointId, succeededFlowFiles.get(1), WebSocketMessage.Type.BINARY);
final List<MockFlowFile> failedFlowFiles = runner.getFlowFilesForRelationship(PutWebSocket.REL_FAILURE);
assertEquals(0, failedFlowFiles.size());
final List<ProvenanceEventRecord> provenanceEvents = runner.getProvenanceEvents();
assertEquals(2, provenanceEvents.size());
}
Aggregations