Search in sources :

Example 6 with WrappedMessageProducer

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());
}
Also used : HashMap(java.util.HashMap) TestRunner(org.apache.nifi.util.TestRunner) Connection(javax.jms.Connection) 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) Session(javax.jms.Session) Test(org.junit.Test)

Example 7 with WrappedMessageProducer

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();
}
Also used : MockFlowFile(org.apache.nifi.util.MockFlowFile) Message(javax.jms.Message) WrappedMessageProducer(org.apache.nifi.processors.standard.util.WrappedMessageProducer) TestRunner(org.apache.nifi.util.TestRunner) Relationship(org.apache.nifi.processor.Relationship) MessageProducer(javax.jms.MessageProducer) WrappedMessageProducer(org.apache.nifi.processors.standard.util.WrappedMessageProducer) Session(javax.jms.Session) Test(org.junit.Test)

Example 8 with WrappedMessageProducer

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();
}
Also used : WrappedMessageProducer(org.apache.nifi.processors.standard.util.WrappedMessageProducer) TestRunner(org.apache.nifi.util.TestRunner) MapMessage(javax.jms.MapMessage) WrappedMessageProducer(org.apache.nifi.processors.standard.util.WrappedMessageProducer) MessageProducer(javax.jms.MessageProducer) Session(javax.jms.Session)

Example 9 with WrappedMessageProducer

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();
}
Also used : MockFlowFile(org.apache.nifi.util.MockFlowFile) StreamMessage(javax.jms.StreamMessage) MapMessage(javax.jms.MapMessage) ObjectMessage(javax.jms.ObjectMessage) BytesMessage(javax.jms.BytesMessage) Message(javax.jms.Message) WrappedMessageProducer(org.apache.nifi.processors.standard.util.WrappedMessageProducer) TestRunner(org.apache.nifi.util.TestRunner) Relationship(org.apache.nifi.processor.Relationship) WrappedMessageProducer(org.apache.nifi.processors.standard.util.WrappedMessageProducer) MessageProducer(javax.jms.MessageProducer) Session(javax.jms.Session) Test(org.junit.Test)

Example 10 with WrappedMessageProducer

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());
}
Also used : WrappedMessageProducer(org.apache.nifi.processors.standard.util.WrappedMessageProducer) TestRunner(org.apache.nifi.util.TestRunner) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) 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