Search in sources :

Example 11 with WrappedMessageProducer

use of org.apache.nifi.processors.standard.util.WrappedMessageProducer in project nifi by apache.

the class TestPutJMS method testPutSendRoutesToFailure.

@Test
public void testPutSendRoutesToFailure() 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());
    doAnswer(new Answer<WrappedMessageProducer>() {

        @Override
        public WrappedMessageProducer answer(InvocationOnMock invocationOnMock) {
            return wrappedMessageProducer;
        }
    }).when(wrappedMessageProducerQueue).poll();
    assertEquals(wrappedMessageProducer, wrappedMessageProducerQueue.poll());
    doAnswer(new Answer<MessageProducer>() {

        @Override
        public MessageProducer answer(InvocationOnMock invocationOnMock) {
            return messageProducer;
        }
    }).when(wrappedMessageProducer).getProducer();
    doThrow(new JMSException("force send to fail")).when(messageProducer).send(any(Message.class));
    final Map<String, String> attributes = new HashMap<>();
    attributes.put("filename", "file1.txt");
    runnerPut.enqueue("putSendRoutesToFailure".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());
}
Also used : Message(javax.jms.Message) HashMap(java.util.HashMap) TestRunner(org.apache.nifi.util.TestRunner) JMSException(javax.jms.JMSException) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) ProcessContext(org.apache.nifi.processor.ProcessContext) MockFlowFile(org.apache.nifi.util.MockFlowFile) WrappedMessageProducer(org.apache.nifi.processors.standard.util.WrappedMessageProducer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) MessageProducer(javax.jms.MessageProducer) WrappedMessageProducer(org.apache.nifi.processors.standard.util.WrappedMessageProducer) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Queue(java.util.Queue) Test(org.junit.Test)

Aggregations

WrappedMessageProducer (org.apache.nifi.processors.standard.util.WrappedMessageProducer)11 MessageProducer (javax.jms.MessageProducer)9 TestRunner (org.apache.nifi.util.TestRunner)9 Session (javax.jms.Session)8 Test (org.junit.Test)7 MockFlowFile (org.apache.nifi.util.MockFlowFile)6 Message (javax.jms.Message)4 Relationship (org.apache.nifi.processor.Relationship)4 Queue (java.util.Queue)3 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)3 BytesMessage (javax.jms.BytesMessage)3 JMSException (javax.jms.JMSException)3 StreamMessage (javax.jms.StreamMessage)3 HashMap (java.util.HashMap)2 MapMessage (javax.jms.MapMessage)2 ObjectMessage (javax.jms.ObjectMessage)2 ProcessContext (org.apache.nifi.processor.ProcessContext)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1