Java Code Examples for org.springframework.jms.core.JmsTemplate#receive()

The following examples show how to use org.springframework.jms.core.JmsTemplate#receive() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JMSPublisherConsumerIT.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void validateBytesConvertedToBytesMessageOnSend() throws Exception {
    final String destinationName = "validateBytesConvertedToBytesMessageOnSend";
    JmsTemplate jmsTemplate = CommonTest.buildJmsTemplateForDestination(false);

    try {
        JMSPublisher publisher = new JMSPublisher((CachingConnectionFactory) jmsTemplate.getConnectionFactory(), jmsTemplate, mock(ComponentLog.class));
        publisher.publish(destinationName, "hellomq".getBytes());

        Message receivedMessage = jmsTemplate.receive(destinationName);
        assertTrue(receivedMessage instanceof BytesMessage);
        byte[] bytes = new byte[7];
        ((BytesMessage) receivedMessage).readBytes(bytes);
        assertEquals("hellomq", new String(bytes));
    } finally {
        ((CachingConnectionFactory) jmsTemplate.getConnectionFactory()).destroy();
    }
}
 
Example 2
Source File: JMSPublisherConsumerTest.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void validateBytesConvertedToBytesMessageOnSend() throws Exception {
    final String destinationName = "testQueue";
    JmsTemplate jmsTemplate = CommonTest.buildJmsTemplateForDestination(false);

    JMSPublisher publisher = new JMSPublisher(jmsTemplate, mock(ComponentLog.class));
    publisher.publish(destinationName, "hellomq".getBytes());

    Message receivedMessage = jmsTemplate.receive(destinationName);
    assertTrue(receivedMessage instanceof BytesMessage);
    byte[] bytes = new byte[7];
    ((BytesMessage) receivedMessage).readBytes(bytes);
    assertEquals("hellomq", new String(bytes));

    ((CachingConnectionFactory) jmsTemplate.getConnectionFactory()).destroy();
}
 
Example 3
Source File: JMSPublisherConsumerTest.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void validateJmsHeadersAndPropertiesAreTransferredFromFFAttributes() throws Exception {
    final String destinationName = "testQueue";
    JmsTemplate jmsTemplate = CommonTest.buildJmsTemplateForDestination(false);

    JMSPublisher publisher = new JMSPublisher(jmsTemplate, mock(ComponentLog.class));
    Map<String, String> flowFileAttributes = new HashMap<>();
    flowFileAttributes.put("foo", "foo");
    flowFileAttributes.put(JmsHeaders.REPLY_TO, "myTopic");
    publisher.publish(destinationName, "hellomq".getBytes(), flowFileAttributes);

    Message receivedMessage = jmsTemplate.receive(destinationName);
    assertTrue(receivedMessage instanceof BytesMessage);
    assertEquals("foo", receivedMessage.getStringProperty("foo"));
    assertTrue(receivedMessage.getJMSReplyTo() instanceof Topic);
    assertEquals("myTopic", ((Topic) receivedMessage.getJMSReplyTo()).getTopicName());

    ((CachingConnectionFactory) jmsTemplate.getConnectionFactory()).destroy();
}
 
Example 4
Source File: TagValuePublisherTest.java    From c2mon with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Listens for 1s for updates on the tag topic.
 */
private Thread startListenerThreadForTransferTag(final Tag tag) {
  // start listener in separate thread (to catch update to topic)
  Thread listenerThread = new Thread(new Runnable() {

    @Override
    public void run() {
      try {
        JmsTemplate template = new JmsTemplate(connectionFactory);
        template.setReceiveTimeout(5000);
        synchronized (updateLock) {
          Message message = template.receive(new ActiveMQTopic("c2mon.client.tag." + ((DataTag) tag).getProcessId()));
          updateFromConfig = TransferTagSerializer.fromJson(((TextMessage) message).getText(), TransferTagImpl.class);
        }
      } catch (Exception e) {
        synchronized (updateLock) {
          updateFromConfig = null;
        }
      }
    }
  });
  listenerThread.start();
  return listenerThread;
}
 
Example 5
Source File: TagValuePublisherTest.java    From c2mon with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Listens for 1s for updates on the tag topic.
 */
private Thread startListenerThread(final Tag tag) {
  // start listener in separate thread (to catch update to topic)
  Thread listenerThread = new Thread(new Runnable() {

    @Override
    public void run() {
      try {
        JmsTemplate template = new JmsTemplate(connectionFactory);
        template.setReceiveTimeout(1000);
        synchronized (updateLock) {
          Message message = template.receive(new ActiveMQTopic("c2mon.client.tag." + ((DataTag) tag).getProcessId()));
          update = TransferTagSerializer.fromJson(((TextMessage) message).getText(), TransferTagValueImpl.class);
        }
      } catch (Exception e) {
        synchronized (updateLock) {
          update = null;
        }
      }
    }
  });
  listenerThread.start();
  return listenerThread;
}
 
Example 6
Source File: JMSPublisherConsumerTest.java    From solace-integration-guides with Apache License 2.0 6 votes vote down vote up
@Test
public void validateBytesConvertedToBytesMessageOnSendOverJNDI() throws Exception {
    final String destinationName = "testQueue";
    JmsTemplate jmsTemplate = CommonTest.buildJmsJndiTemplateForDestination(false);

    JMSPublisher publisher = new JMSPublisher(jmsTemplate, mock(ComponentLog.class));
    publisher.publish(destinationName, "hellomq".getBytes());

    Message receivedMessage = jmsTemplate.receive(destinationName);
    assertTrue(receivedMessage instanceof BytesMessage);
    byte[] bytes = new byte[7];
    ((BytesMessage) receivedMessage).readBytes(bytes);
    assertEquals("hellomq", new String(bytes));

    ((CachingConnectionFactory) jmsTemplate.getConnectionFactory()).destroy();
}
 
Example 7
Source File: JMSPublisherConsumerTest.java    From solace-integration-guides with Apache License 2.0 6 votes vote down vote up
@Test
public void validateJmsHeadersAndPropertiesAreTransferredFromFFAttributesOverJNDI() throws Exception {
    final String destinationName = "testQueue";
    JmsTemplate jmsTemplate = CommonTest.buildJmsJndiTemplateForDestination(false);

    JMSPublisher publisher = new JMSPublisher(jmsTemplate, mock(ComponentLog.class));
    Map<String, String> flowFileAttributes = new HashMap<>();
    flowFileAttributes.put("foo", "foo");
    flowFileAttributes.put(JmsHeaders.REPLY_TO, "myTopic");
    publisher.publish(destinationName, "hellomq".getBytes(), flowFileAttributes);

    Message receivedMessage = jmsTemplate.receive(destinationName);
    assertTrue(receivedMessage instanceof BytesMessage);
    assertEquals("foo", receivedMessage.getStringProperty("foo"));
    assertTrue(receivedMessage.getJMSReplyTo() instanceof Topic);
    assertEquals("myTopic", ((Topic) receivedMessage.getJMSReplyTo()).getTopicName());

    ((CachingConnectionFactory) jmsTemplate.getConnectionFactory()).destroy();
}
 
Example 8
Source File: ActiveMQConnectionTest.java    From syndesis with Apache License 2.0 6 votes vote down vote up
@Test
public void connectionTest() {
    final String message = UUID.randomUUID().toString();
    final SjmsComponent sjms1 = context.getComponent("sjms-sjms-0-0", SjmsComponent.class);
    final SjmsComponent sjms2 = context.getComponent("sjms-sjms-0-1", SjmsComponent.class);

    Assertions.assertThat(sjms1).isNotEqualTo(sjms2);

    JmsTemplate subTemplate = new JmsTemplate(new ActiveMQConnectionFactory("vm://localhost?broker.persistent=false&broker.brokerName=sub"));
    subTemplate.send("sub-" + testName.getMethodName(), session -> session.createTextMessage(message));

    JmsTemplate pubTemplate = new JmsTemplate(new ActiveMQConnectionFactory("vm://localhost?broker.persistent=false&broker.brokerName=pub"));
    Object answer = pubTemplate.receive("pub-" + testName.getMethodName());

    Assertions.assertThat(answer).isInstanceOf(TextMessage.class);
    Assertions.assertThat(answer).hasFieldOrPropertyWithValue("text", message);
}
 
Example 9
Source File: PublishJMSIT.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 10000)
public void validatePublishTextMessage() throws Exception {
    ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory("vm://localhost?broker.persistent=false");

    final String destinationName = "validatePublishTextMessage";
    PublishJMS pubProc = new PublishJMS();
    TestRunner runner = TestRunners.newTestRunner(pubProc);
    JMSConnectionFactoryProviderDefinition cs = mock(JMSConnectionFactoryProviderDefinition.class);
    when(cs.getIdentifier()).thenReturn("cfProvider");
    when(cs.getConnectionFactory()).thenReturn(cf);

    runner.addControllerService("cfProvider", cs);
    runner.enableControllerService(cs);

    runner.setProperty(PublishJMS.CF_SERVICE, "cfProvider");
    runner.setProperty(PublishJMS.DESTINATION, destinationName);
    runner.setProperty(PublishJMS.MESSAGE_BODY, "text");

    Map<String, String> attributes = new HashMap<>();
    attributes.put("foo", "foo");
    attributes.put(JmsHeaders.REPLY_TO, "cooQueue");
    runner.enqueue("Hey dude!".getBytes(), attributes);
    runner.run(1, false);

    final MockFlowFile successFF = runner.getFlowFilesForRelationship(PublishJMS.REL_SUCCESS).get(0);
    assertNotNull(successFF);

    JmsTemplate jmst = new JmsTemplate(cf);
    Message message = jmst.receive(destinationName);
    assertTrue(message instanceof TextMessage);
    TextMessage textMessage = (TextMessage) message;

    byte[] messageBytes = MessageBodyToBytesConverter.toBytes(textMessage);
    assertEquals("Hey dude!", new String(messageBytes));
    assertEquals("cooQueue", ((Queue) message.getJMSReplyTo()).getQueueName());
    assertEquals("foo", message.getStringProperty("foo"));

    runner.run(1, true, false); // Run once just so that we can trigger the shutdown of the Connection Factory
}
 
Example 10
Source File: ConsumeJMSIT.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void testUnsupportedMessage() throws Exception {
    JmsTemplate jmsTemplate = CommonTest.buildJmsTemplateForDestination(false);
    try {
        ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory("vm://localhost?broker.persistent=false");

        JMSPublisher sender = new JMSPublisher((CachingConnectionFactory) jmsTemplate.getConnectionFactory(), jmsTemplate, mock(ComponentLog.class));

        sender.jmsTemplate.send("testMapMessage", __ -> createUnsupportedMessage("unsupportedMessagePropertyKey", "unsupportedMessagePropertyValue"));

        TestRunner runner = TestRunners.newTestRunner(new ConsumeJMS());
        JMSConnectionFactoryProviderDefinition cs = mock(JMSConnectionFactoryProviderDefinition.class);
        when(cs.getIdentifier()).thenReturn("cfProvider");
        when(cs.getConnectionFactory()).thenReturn(jmsTemplate.getConnectionFactory());
        runner.addControllerService("cfProvider", cs);
        runner.enableControllerService(cs);

        runner.setProperty(PublishJMS.CF_SERVICE, "cfProvider");
        runner.setProperty(ConsumeJMS.DESTINATION, "testMapMessage");
        runner.setProperty(ConsumeJMS.ERROR_QUEUE, "errorQueue");
        runner.setProperty(ConsumeJMS.DESTINATION_TYPE, ConsumeJMS.QUEUE);
        runner.run(1, false);

        JmsTemplate jmst = new JmsTemplate(cf);
        Message message = jmst.receive("errorQueue");

        assertNotNull(message);
        assertEquals(message.getStringProperty("unsupportedMessagePropertyKey"), "unsupportedMessagePropertyValue");
    } finally {
        ((CachingConnectionFactory) jmsTemplate.getConnectionFactory()).destroy();
    }
}
 
Example 11
Source File: ActiveMQSharedConnectionTest.java    From syndesis with Apache License 2.0 5 votes vote down vote up
@Test
public void sharedConnectionTest() {
    final String message = UUID.randomUUID().toString();
    final SjmsComponent sjms1 = context.getComponent("sjms-sjms-0-0", SjmsComponent.class);
    final SjmsComponent sjms2 = context.getComponent("sjms-sjms-0-1", SjmsComponent.class);

    Assertions.assertThat(sjms1).isEqualTo(sjms2);

    JmsTemplate template = new JmsTemplate(broker.createConnectionFactory());
    template.send("sub-" + testName.getMethodName(), session -> session.createTextMessage(message));
    Object answer = template.receive("pub-" + testName.getMethodName());

    Assertions.assertThat(answer).isInstanceOf(TextMessage.class);
    Assertions.assertThat(answer).hasFieldOrPropertyWithValue("text", message);
}
 
Example 12
Source File: PublishJMSTest.java    From solace-integration-guides with Apache License 2.0 5 votes vote down vote up
@Test
public void validateSuccessfulPublishAndTransferToSuccessWithELOverJNDI() throws Exception {
    ActiveMQConnectionFactory cf = (ActiveMQConnectionFactory) CommonTest.buildJmsJndiConnectionFactory();

    final String destinationNameExpression = "${foo}Queue";
    final String destinationName = "fooQueue";
    PublishJMS pubProc = new PublishJMS();
    TestRunner runner = TestRunners.newTestRunner(pubProc);
    JMSConnectionFactoryProviderDefinition cs = mock(JMSConnectionFactoryProviderDefinition.class);
    when(cs.getIdentifier()).thenReturn("cfProvider");
    when(cs.getConnectionFactory()).thenReturn(cf);

    runner.addControllerService("cfProvider", cs);
    runner.enableControllerService(cs);

    runner.setProperty(PublishJMS.CF_SERVICE, "cfProvider");
    runner.setProperty(PublishJMS.DESTINATION, destinationNameExpression);

    Map<String, String> attributes = new HashMap<>();
    attributes.put("foo", "foo");
    attributes.put(JmsHeaders.REPLY_TO, "cooQueue");
    runner.enqueue("Hey dude!".getBytes(), attributes);
    runner.run(1, false);

    final MockFlowFile successFF = runner.getFlowFilesForRelationship(PublishJMS.REL_SUCCESS).get(0);
    assertNotNull(successFF);

    JmsTemplate jmst = new JmsTemplate(cf);
    BytesMessage message = (BytesMessage) jmst.receive(destinationName);

    byte[] messageBytes = MessageBodyToBytesConverter.toBytes(message);
    assertEquals("Hey dude!", new String(messageBytes));
    assertEquals("cooQueue", ((Queue) message.getJMSReplyTo()).getQueueName());
    assertEquals("foo", message.getStringProperty("foo"));
}
 
Example 13
Source File: PublishJMSIT.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 10000)
public void validateSuccessfulPublishAndTransferToSuccessWithEL() throws Exception {
    ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory("vm://localhost?broker.persistent=false");

    final String destinationNameExpression = "${foo}Queue";
    final String destinationName = "fooQueue";
    PublishJMS pubProc = new PublishJMS();
    TestRunner runner = TestRunners.newTestRunner(pubProc);
    JMSConnectionFactoryProviderDefinition cs = mock(JMSConnectionFactoryProviderDefinition.class);
    when(cs.getIdentifier()).thenReturn("cfProvider");
    when(cs.getConnectionFactory()).thenReturn(cf);

    runner.addControllerService("cfProvider", cs);
    runner.enableControllerService(cs);

    runner.setProperty(PublishJMS.CF_SERVICE, "cfProvider");
    runner.setProperty(PublishJMS.DESTINATION, destinationNameExpression);

    Map<String, String> attributes = new HashMap<>();
    attributes.put("foo", "foo");
    attributes.put(JmsHeaders.REPLY_TO, "cooQueue");
    runner.enqueue("Hey dude!".getBytes(), attributes);
    runner.run(1, false); // Run once but don't shut down because we want the Connection Factory left in tact so that we can use it.

    final MockFlowFile successFF = runner.getFlowFilesForRelationship(PublishJMS.REL_SUCCESS).get(0);
    assertNotNull(successFF);

    JmsTemplate jmst = new JmsTemplate(cf);
    BytesMessage message = (BytesMessage) jmst.receive(destinationName);

    byte[] messageBytes = MessageBodyToBytesConverter.toBytes(message);
    assertEquals("Hey dude!", new String(messageBytes));
    assertEquals("cooQueue", ((Queue) message.getJMSReplyTo()).getQueueName());
    assertEquals("foo", message.getStringProperty("foo"));

    runner.run(1, true, false); // Run once just so that we can trigger the shutdown of the Connection Factory
}
 
Example 14
Source File: JMSPublisherConsumerIT.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void validateJmsHeadersAndPropertiesAreTransferredFromFFAttributes() throws Exception {
    final String destinationName = "validateJmsHeadersAndPropertiesAreTransferredFromFFAttributes";
    JmsTemplate jmsTemplate = CommonTest.buildJmsTemplateForDestination(false);

    try {
        JMSPublisher publisher = new JMSPublisher((CachingConnectionFactory) jmsTemplate.getConnectionFactory(), jmsTemplate, mock(ComponentLog.class));
        Map<String, String> flowFileAttributes = new HashMap<>();
        flowFileAttributes.put("foo", "foo");
        flowFileAttributes.put("hyphen-property", "value");
        flowFileAttributes.put("fullstop.property", "value");
        flowFileAttributes.put(JmsHeaders.REPLY_TO, "myTopic");
        flowFileAttributes.put(JmsHeaders.DELIVERY_MODE, "1");
        flowFileAttributes.put(JmsHeaders.PRIORITY, "1");
        flowFileAttributes.put(JmsHeaders.EXPIRATION, "never"); // value expected to be integer, make sure non-integer doesn't cause problems
        publisher.publish(destinationName, "hellomq".getBytes(), flowFileAttributes);

        Message receivedMessage = jmsTemplate.receive(destinationName);
        assertTrue(receivedMessage instanceof BytesMessage);
        assertEquals("foo", receivedMessage.getStringProperty("foo"));
        assertTrue(receivedMessage.propertyExists("hyphen-property"));
        assertTrue(receivedMessage.propertyExists("fullstop.property"));
        assertTrue(receivedMessage.getJMSReplyTo() instanceof Topic);
        assertEquals(1, receivedMessage.getJMSDeliveryMode());
        assertEquals(1, receivedMessage.getJMSPriority());
        assertEquals("myTopic", ((Topic) receivedMessage.getJMSReplyTo()).getTopicName());

    } finally {
        ((CachingConnectionFactory) jmsTemplate.getConnectionFactory()).destroy();
    }
}
 
Example 15
Source File: PublishJMSTest.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void validateSuccessfulPublishAndTransferToSuccessWithEL() throws Exception {
    ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory("vm://localhost?broker.persistent=false");

    final String destinationNameExpression = "${foo}Queue";
    final String destinationName = "fooQueue";
    PublishJMS pubProc = new PublishJMS();
    TestRunner runner = TestRunners.newTestRunner(pubProc);
    JMSConnectionFactoryProviderDefinition cs = mock(JMSConnectionFactoryProviderDefinition.class);
    when(cs.getIdentifier()).thenReturn("cfProvider");
    when(cs.getConnectionFactory()).thenReturn(cf);

    runner.addControllerService("cfProvider", cs);
    runner.enableControllerService(cs);

    runner.setProperty(PublishJMS.CF_SERVICE, "cfProvider");
    runner.setProperty(PublishJMS.DESTINATION, destinationNameExpression);

    Map<String, String> attributes = new HashMap<>();
    attributes.put("foo", "foo");
    attributes.put(JmsHeaders.REPLY_TO, "cooQueue");
    runner.enqueue("Hey dude!".getBytes(), attributes);
    runner.run(1, false);

    final MockFlowFile successFF = runner.getFlowFilesForRelationship(PublishJMS.REL_SUCCESS).get(0);
    assertNotNull(successFF);

    JmsTemplate jmst = new JmsTemplate(cf);
    BytesMessage message = (BytesMessage) jmst.receive(destinationName);

    byte[] messageBytes = MessageBodyToBytesConverter.toBytes(message);
    assertEquals("Hey dude!", new String(messageBytes));
    assertEquals("cooQueue", ((Queue) message.getJMSReplyTo()).getQueueName());
    assertEquals("foo", message.getStringProperty("foo"));
}
 
Example 16
Source File: PublishJMSTest.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void validateSuccessfulPublishAndTransferToSuccess() throws Exception {
    ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory("vm://localhost?broker.persistent=false");

    final String destinationName = "fooQueue";
    PublishJMS pubProc = new PublishJMS();
    TestRunner runner = TestRunners.newTestRunner(pubProc);
    JMSConnectionFactoryProviderDefinition cs = mock(JMSConnectionFactoryProviderDefinition.class);
    when(cs.getIdentifier()).thenReturn("cfProvider");
    when(cs.getConnectionFactory()).thenReturn(cf);

    runner.addControllerService("cfProvider", cs);
    runner.enableControllerService(cs);

    runner.setProperty(PublishJMS.CF_SERVICE, "cfProvider");
    runner.setProperty(PublishJMS.DESTINATION, destinationName);

    Map<String, String> attributes = new HashMap<>();
    attributes.put("foo", "foo");
    attributes.put(JmsHeaders.REPLY_TO, "cooQueue");
    runner.enqueue("Hey dude!".getBytes(), attributes);
    runner.run(1, false);

    final MockFlowFile successFF = runner.getFlowFilesForRelationship(PublishJMS.REL_SUCCESS).get(0);
    assertNotNull(successFF);

    JmsTemplate jmst = new JmsTemplate(cf);
    BytesMessage message = (BytesMessage) jmst.receive(destinationName);

    byte[] messageBytes = MessageBodyToBytesConverter.toBytes(message);
    assertEquals("Hey dude!", new String(messageBytes));
    assertEquals("cooQueue", ((Queue) message.getJMSReplyTo()).getQueueName());
    assertEquals("foo", message.getStringProperty("foo"));
}
 
Example 17
Source File: PublishJMSIT.java    From nifi with Apache License 2.0 4 votes vote down vote up
@Test(timeout = 10000)
public void validateRegexAndIllegalHeaders() throws Exception {
    ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory("vm://localhost?broker.persistent=false");

    final String destinationName = "validatePublishTextMessage";
    PublishJMS pubProc = new PublishJMS();
    TestRunner runner = TestRunners.newTestRunner(pubProc);
    JMSConnectionFactoryProviderDefinition cs = mock(JMSConnectionFactoryProviderDefinition.class);
    when(cs.getIdentifier()).thenReturn("cfProvider");
    when(cs.getConnectionFactory()).thenReturn(cf);

    runner.addControllerService("cfProvider", cs);
    runner.enableControllerService(cs);

    runner.setProperty(PublishJMS.CF_SERVICE, "cfProvider");
    runner.setProperty(PublishJMS.DESTINATION, destinationName);
    runner.setProperty(PublishJMS.MESSAGE_BODY, "text");
    runner.setProperty(PublishJMS.ATTRIBUTES_AS_HEADERS_REGEX, "^((?!bar).)*$");
    runner.setProperty(PublishJMS.ALLOW_ILLEGAL_HEADER_CHARS, "true");

    Map<String, String> attributes = new HashMap<>();
    attributes.put("foo", "foo");
    attributes.put("bar", "bar");
    attributes.put("test-header-with-hyphen", "value");
    attributes.put(JmsHeaders.REPLY_TO, "cooQueue");
    runner.enqueue("Hey dude!".getBytes(), attributes);
    runner.run(1, false);

    final MockFlowFile successFF = runner.getFlowFilesForRelationship(PublishJMS.REL_SUCCESS).get(0);
    assertNotNull(successFF);

    JmsTemplate jmst = new JmsTemplate(cf);
    Message message = jmst.receive(destinationName);
    assertTrue(message instanceof TextMessage);
    TextMessage textMessage = (TextMessage) message;

    byte[] messageBytes = MessageBodyToBytesConverter.toBytes(textMessage);
    assertEquals("Hey dude!", new String(messageBytes));
    assertEquals("cooQueue", ((Queue) message.getJMSReplyTo()).getQueueName());
    assertEquals("foo", message.getStringProperty("foo"));
    assertEquals("value", message.getStringProperty("test-header-with-hyphen"));
    assertNull(message.getStringProperty("bar"));

    runner.run(1, true, false); // Run once just so that we can trigger the shutdown of the Connection Factory
}
 
Example 18
Source File: PublishJMSIT.java    From nifi with Apache License 2.0 4 votes vote down vote up
@Test(timeout = 10000)
public void validateSuccessfulPublishAndTransferToSuccess() throws Exception {
    ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory("vm://localhost?broker.persistent=false");

    final String destinationName = "validateSuccessfulPublishAndTransferToSuccess";
    PublishJMS pubProc = new PublishJMS();
    TestRunner runner = TestRunners.newTestRunner(pubProc);
    JMSConnectionFactoryProviderDefinition cs = mock(JMSConnectionFactoryProviderDefinition.class);
    when(cs.getIdentifier()).thenReturn("cfProvider");
    when(cs.getConnectionFactory()).thenReturn(cf);

    runner.addControllerService("cfProvider", cs);
    runner.enableControllerService(cs);

    runner.setProperty(PublishJMS.CF_SERVICE, "cfProvider");
    runner.setProperty(PublishJMS.DESTINATION, destinationName);

    Map<String, String> attributes = new HashMap<>();
    attributes.put("foo", "foo");
    attributes.put(JmsHeaders.REPLY_TO, "cooQueue");
    attributes.put("test-attribute.type", "allowed1");
    attributes.put("test.attribute.type", "allowed2");
    attributes.put("test-attribute", "notAllowed1");
    attributes.put("jms.source.destination", "notAllowed2");
    runner.enqueue("Hey dude!".getBytes(), attributes);
    runner.run(1, false); // Run once but don't shut down because we want the Connection Factory left in tact so that we can use it.

    final MockFlowFile successFF = runner.getFlowFilesForRelationship(PublishJMS.REL_SUCCESS).get(0);
    assertNotNull(successFF);

    JmsTemplate jmst = new JmsTemplate(cf);
    BytesMessage message = (BytesMessage) jmst.receive(destinationName);

    byte[] messageBytes = MessageBodyToBytesConverter.toBytes(message);
    assertEquals("Hey dude!", new String(messageBytes));
    assertEquals("cooQueue", ((Queue) message.getJMSReplyTo()).getQueueName());
    assertEquals("foo", message.getStringProperty("foo"));
    assertEquals("allowed1", message.getStringProperty("test-attribute.type"));
    assertEquals("allowed2", message.getStringProperty("test.attribute.type"));
    assertNull(message.getStringProperty("test-attribute"));
    assertNull(message.getStringProperty("jms.source.destination"));

    runner.run(1, true, false); // Run once just so that we can trigger the shutdown of the Connection Factory
}
 
Example 19
Source File: PublishJMSIT.java    From nifi with Apache License 2.0 4 votes vote down vote up
@Test(timeout = 10000)
public void validatePublishPropertyTypes() throws Exception {
    ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory("vm://localhost?broker.persistent=false");

    final String destinationName = "validatePublishPropertyTypes";
    PublishJMS pubProc = new PublishJMS();
    TestRunner runner = TestRunners.newTestRunner(pubProc);
    JMSConnectionFactoryProviderDefinition cs = mock(JMSConnectionFactoryProviderDefinition.class);
    when(cs.getIdentifier()).thenReturn("cfProvider");
    when(cs.getConnectionFactory()).thenReturn(cf);

    runner.addControllerService("cfProvider", cs);
    runner.enableControllerService(cs);

    runner.setProperty(PublishJMS.CF_SERVICE, "cfProvider");
    runner.setProperty(PublishJMS.DESTINATION, destinationName);

    Map<String, String> attributes = new HashMap<>();
    attributes.put("foo", "foo");
    attributes.put("myboolean", "true");
    attributes.put("myboolean.type", "boolean");
    attributes.put("mybyte", "127");
    attributes.put("mybyte.type", "byte");
    attributes.put("myshort", "16384");
    attributes.put("myshort.type", "short");
    attributes.put("myinteger", "1544000");
    attributes.put("myinteger.type", "INTEGER"); // test upper case
    attributes.put("mylong", "9876543210");
    attributes.put("mylong.type", "long");
    attributes.put("myfloat", "3.14");
    attributes.put("myfloat.type", "float");
    attributes.put("mydouble", "3.14159265359");
    attributes.put("mydouble.type", "double");
    attributes.put("badtype", "3.14");
    attributes.put("badtype.type", "pi"); // pi not recognized as a type, so send as String
    attributes.put("badint", "3.14"); // value is not an integer
    attributes.put("badint.type", "integer");

    runner.enqueue("Hey dude!".getBytes(), attributes);
    runner.run(1, false); // Run once but don't shut down because we want the Connection Factory left intact so that we can use it.

    final MockFlowFile successFF = runner.getFlowFilesForRelationship(PublishJMS.REL_SUCCESS).get(0);
    assertNotNull(successFF);

    JmsTemplate jmst = new JmsTemplate(cf);
    BytesMessage message = (BytesMessage) jmst.receive(destinationName);

    byte[] messageBytes = MessageBodyToBytesConverter.toBytes(message);
    assertEquals("Hey dude!", new String(messageBytes));
    assertEquals(true, message.getObjectProperty("foo") instanceof String);
    assertEquals("foo", message.getStringProperty("foo"));
    assertEquals(true, message.getObjectProperty("myboolean") instanceof Boolean);
    assertEquals(true, message.getBooleanProperty("myboolean"));
    assertEquals(true, message.getObjectProperty("mybyte") instanceof Byte);
    assertEquals(127, message.getByteProperty("mybyte"));
    assertEquals(true, message.getObjectProperty("myshort") instanceof Short);
    assertEquals(16384, message.getShortProperty("myshort"));
    assertEquals(true, message.getObjectProperty("myinteger") instanceof Integer);
    assertEquals(1544000, message.getIntProperty("myinteger"));
    assertEquals(true, message.getObjectProperty("mylong") instanceof Long);
    assertEquals(9876543210L, message.getLongProperty("mylong"));
    assertEquals(true, message.getObjectProperty("myfloat") instanceof Float);
    assertEquals(3.14F, message.getFloatProperty("myfloat"), 0.001F);
    assertEquals(true, message.getObjectProperty("mydouble") instanceof Double);
    assertEquals(3.14159265359D, message.getDoubleProperty("mydouble"), 0.00000000001D);
    assertEquals(true, message.getObjectProperty("badtype") instanceof String);
    assertEquals("3.14", message.getStringProperty("badtype"));
    assertFalse(message.propertyExists("badint"));

    runner.run(1, true, false); // Run once just so that we can trigger the shutdown of the Connection Factory
}
 
Example 20
Source File: ArquillianTest.java    From kubernetes-integration-test with Apache License 2.0 4 votes vote down vote up
@Test
public void testSucc() throws Exception {

    log.info("Test resources are in namespace:"+session.getNamespace());

    //OpenShift client - here for example
    log.info("OpenShift - getMasterUrl: {}",  oc.getMasterUrl());
    log.info("OpenShift - getApiVersion: {}",  oc.getApiVersion());
    log.info("OpenShift - currentUser: {}",  oc.currentUser().getMetadata().getName());
    log.info("OpenShift - getNamespace: {}",  oc.getNamespace());
    log.info("OpenShift - getConfiguration: {}",  oc.getConfiguration());
    log.info("OpenShift - getClass: {}",  oc.getClass());

    //Kubernetes client - here for example
    log.info("Kubernetes - getMasterUrl: {}",  client.getMasterUrl());
    log.info("Kubernetes - getApiVersion: {}",  client.getApiVersion());
    log.info("Kubernetes - getConfiguration: {}",  client.getConfiguration());
    log.info("Kubernetes - getClass: {}",  client.getClass());

    //Service in the current namespace
    oc.services().list().getItems().stream()
            .map(s->s.getMetadata().getNamespace()+" - "+s.getMetadata().getName())
            .forEach(s->log.info("Service: {}",s));

    /*************
     * Start test
     *************/
    //The port-foward url has http schema, but it's actually the amq 61616 port
    String brokerUrl = "tcp://"+ amqsvcUrl.getHost()+":"+ amqsvcUrl.getPort();
    log.info("brokerUrl: {}",brokerUrl);
    ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("test","secret",brokerUrl);
    JmsTemplate jmsTemplate = new JmsTemplate(connectionFactory);

    //Send test message and receive outcome
    jmsTemplate.convertAndSend("user.in", "{\"email\":\"[email protected]\"}");
    TextMessage message = (TextMessage) jmsTemplate.receive("user.out");
    String response = message.getText();

    log.info("Response: {}",response);

    //Asserts
    assertEquals("[email protected]", JsonPath.read(response, "$.email"));
    assertEquals("5551234567", JsonPath.read(response, "$.phone"));
    assertEquals("Test State", JsonPath.read(response, "$.address.state"));
    assertEquals("Test City", JsonPath.read(response, "$.address.city"));
    assertEquals("1 Test St", JsonPath.read(response, "$.address.address"));
    assertEquals("T001", JsonPath.read(response, "$.address.zip"));

}