use of org.apache.nifi.processors.standard.util.WrappedMessageProducer in project nifi by apache.
the class TestPutJMS method testPutCommitRoutesToFailure.
@Test
public void testPutCommitRoutesToFailure() throws JMSException, NoSuchFieldException, IllegalAccessException {
final PutJMS putJMS = spy(new PutJMS());
final TestRunner runnerPut = TestRunners.newTestRunner(putJMS);
runnerPut.setProperty(JmsProperties.JMS_PROVIDER, TEST_PROVIDER);
runnerPut.setProperty(JmsProperties.URL, TEST_URL);
runnerPut.setProperty(JmsProperties.DESTINATION_TYPE, TEST_DEST_TYPE);
runnerPut.setProperty(JmsProperties.DESTINATION_NAME, TEST_DEST_NAME + testQueueSuffix());
final ProcessContext context = runnerPut.getProcessContext();
final Queue<WrappedMessageProducer> wrappedMessageProducerQueue = (Queue) spy(new LinkedBlockingQueue<>());
injectFieldValue(PutJMS.class, putJMS, "producerQueue", wrappedMessageProducerQueue);
final WrappedMessageProducer wrappedMessageProducer = spy(JmsFactory.createMessageProducer(context, true));
final MessageProducer messageProducer = spy(wrappedMessageProducer.getProducer());
final Connection connection = JmsFactory.createConnection(context);
final Session jmsSession = spy(JmsFactory.createSession(context, connection, true));
doAnswer(new Answer<WrappedMessageProducer>() {
@Override
public WrappedMessageProducer answer(InvocationOnMock invocationOnMock) {
return wrappedMessageProducer;
}
}).when(wrappedMessageProducerQueue).poll();
doAnswer(new Answer<MessageProducer>() {
@Override
public MessageProducer answer(InvocationOnMock invocationOnMock) {
return messageProducer;
}
}).when(wrappedMessageProducer).getProducer();
doAnswer(new Answer<Session>() {
@Override
public Session answer(InvocationOnMock invocationOnMock) {
return jmsSession;
}
}).when(wrappedMessageProducer).getSession();
doThrow(new JMSException("force commit to fail")).when(jmsSession).commit();
final Map<String, String> attributes = new HashMap<>();
attributes.put("filename", "file1.txt");
runnerPut.enqueue("putCommitRoutesToFailure".getBytes(), attributes);
runnerPut.run();
assertEquals(0, runnerPut.getFlowFilesForRelationship(PutJMS.REL_SUCCESS).size());
assertEquals(1, runnerPut.getFlowFilesForRelationship(PutJMS.REL_FAILURE).size());
final List<MockFlowFile> flowFilesFail = runnerPut.getFlowFilesForRelationship(PutJMS.REL_FAILURE);
assertEquals(1, flowFilesFail.size());
}
use of org.apache.nifi.processors.standard.util.WrappedMessageProducer in project nifi by apache.
the class TestPutJMS method testCreateMessageDirectly.
@Test
public void testCreateMessageDirectly() throws JMSException {
final PutJMS putJMS = new PutJMS();
final TestRunner runnerPut = TestRunners.newTestRunner(putJMS);
runnerPut.setProperty(JmsProperties.JMS_PROVIDER, TEST_PROVIDER);
runnerPut.setProperty(JmsProperties.URL, TEST_URL);
runnerPut.setProperty(JmsProperties.DESTINATION_TYPE, TEST_DEST_TYPE);
runnerPut.setProperty(JmsProperties.DESTINATION_NAME, TEST_DEST_NAME + testQueueSuffix());
runnerPut.setProperty(JmsProperties.ACKNOWLEDGEMENT_MODE, TEST_ACK_MODE);
final WrappedMessageProducer wrappedProducer = JmsFactory.createMessageProducer(runnerPut.getProcessContext(), true);
final Session jmsSession = wrappedProducer.getSession();
final MessageProducer producer = wrappedProducer.getProducer();
final Message message = jmsSession.createTextMessage("createMessageDirectly");
producer.send(message);
jmsSession.commit();
final GetJMSQueue getJmsQueue = new GetJMSQueue();
final TestRunner runnerGet = TestRunners.newTestRunner(getJmsQueue);
runnerGet.setProperty(JmsProperties.JMS_PROVIDER, TEST_PROVIDER);
runnerGet.setProperty(JmsProperties.URL, TEST_URL);
runnerGet.setProperty(JmsProperties.DESTINATION_NAME, TEST_DEST_NAME + testQueueSuffix());
runnerGet.setProperty(JmsProperties.ACKNOWLEDGEMENT_MODE, TEST_ACK_MODE);
runnerGet.run();
final List<MockFlowFile> flowFiles = runnerGet.getFlowFilesForRelationship(new Relationship.Builder().name("success").build());
assertEquals(1, flowFiles.size());
final MockFlowFile successFlowFile = flowFiles.get(0);
successFlowFile.assertContentEquals("createMessageDirectly");
successFlowFile.assertAttributeEquals("jms.JMSDestination", TEST_DEST_NAME + testQueueSuffix());
producer.close();
jmsSession.close();
}
use of org.apache.nifi.processors.standard.util.WrappedMessageProducer in project nifi by apache.
the class TestGetJMSQueue method testSendMapToQueue.
@org.junit.Ignore
public void testSendMapToQueue() throws Exception {
final TestRunner runner = TestRunners.newTestRunner(GetJMSQueue.class);
runner.setProperty(JmsProperties.JMS_PROVIDER, JmsProperties.ACTIVEMQ_PROVIDER);
runner.setProperty(JmsProperties.URL, "tcp://localhost:61616");
runner.setProperty(JmsProperties.DESTINATION_TYPE, JmsProperties.DESTINATION_TYPE_QUEUE);
runner.setProperty(JmsProperties.DESTINATION_NAME, "queue.testing");
runner.setProperty(JmsProperties.ACKNOWLEDGEMENT_MODE, JmsProperties.ACK_MODE_AUTO);
WrappedMessageProducer wrappedProducer = JmsFactory.createMessageProducer(runner.getProcessContext(), true);
final Session jmsSession = wrappedProducer.getSession();
final MessageProducer producer = wrappedProducer.getProducer();
final MapMessage message = jmsSession.createMapMessage();
message.setString("foo!", "bar");
message.setString("bacon", "meat");
producer.send(message);
jmsSession.commit();
producer.close();
jmsSession.close();
}
use of org.apache.nifi.processors.standard.util.WrappedMessageProducer in project nifi by apache.
the class TestGetJMSQueue method testSendTextToQueue.
@Test
public void testSendTextToQueue() throws Exception {
PutJMS putJms = new PutJMS();
TestRunner putRunner = TestRunners.newTestRunner(putJms);
putRunner.setProperty(JmsProperties.JMS_PROVIDER, JmsProperties.ACTIVEMQ_PROVIDER);
putRunner.setProperty(JmsProperties.URL, "vm://localhost?broker.persistent=false");
putRunner.setProperty(JmsProperties.DESTINATION_TYPE, JmsProperties.DESTINATION_TYPE_QUEUE);
putRunner.setProperty(JmsProperties.DESTINATION_NAME, "queue.testing");
putRunner.setProperty(JmsProperties.ACKNOWLEDGEMENT_MODE, JmsProperties.ACK_MODE_AUTO);
WrappedMessageProducer wrappedProducer = JmsFactory.createMessageProducer(putRunner.getProcessContext(), true);
final Session jmsSession = wrappedProducer.getSession();
final MessageProducer producer = wrappedProducer.getProducer();
final Message message = jmsSession.createTextMessage("Hello World");
producer.send(message);
jmsSession.commit();
GetJMSQueue getJmsQueue = new GetJMSQueue();
TestRunner runner = TestRunners.newTestRunner(getJmsQueue);
runner.setProperty(JmsProperties.JMS_PROVIDER, JmsProperties.ACTIVEMQ_PROVIDER);
runner.setProperty(JmsProperties.URL, "vm://localhost?broker.persistent=false");
runner.setProperty(JmsProperties.DESTINATION_NAME, "queue.testing");
runner.setProperty(JmsProperties.ACKNOWLEDGEMENT_MODE, JmsProperties.ACK_MODE_AUTO);
runner.run();
List<MockFlowFile> flowFiles = runner.getFlowFilesForRelationship(new Relationship.Builder().name("success").build());
assertTrue(flowFiles.size() == 1);
MockFlowFile successFlowFile = flowFiles.get(0);
successFlowFile.assertContentEquals("Hello World");
successFlowFile.assertAttributeEquals("jms.JMSDestination", "queue.testing");
producer.close();
jmsSession.close();
}
use of org.apache.nifi.processors.standard.util.WrappedMessageProducer in project nifi by apache.
the class TestPutJMS method testCleanupResources.
@Test
public void testCleanupResources() throws JMSException, NoSuchFieldException, IllegalAccessException {
final PutJMS putJMS = new PutJMS();
final TestRunner runnerPut = TestRunners.newTestRunner(putJMS);
runnerPut.setProperty(JmsProperties.JMS_PROVIDER, TEST_PROVIDER);
runnerPut.setProperty(JmsProperties.URL, TEST_URL);
runnerPut.setProperty(JmsProperties.DESTINATION_TYPE, TEST_DEST_TYPE);
runnerPut.setProperty(JmsProperties.DESTINATION_NAME, TEST_DEST_NAME + testQueueSuffix());
final Queue<WrappedMessageProducer> wrappedMessageProducerQueue = (Queue) spy(new LinkedBlockingQueue<>());
injectFieldValue(PutJMS.class, putJMS, "producerQueue", wrappedMessageProducerQueue);
final WrappedMessageProducer wrappedProducer = JmsFactory.createMessageProducer(runnerPut.getProcessContext(), true);
wrappedMessageProducerQueue.offer(wrappedProducer);
assertNotNull(wrappedMessageProducerQueue.peek());
putJMS.cleanupResources();
assertNull(wrappedMessageProducerQueue.peek());
}
Aggregations