Esempio n. 1
0
  @Test
  public void validateSuccessfulConsumeAndTransferToSuccess() throws Exception {
    JmsTemplate jmsTemplate = CommonTest.buildJmsTemplateForDestination("cooQueue", false);
    JMSPublisher sender = new JMSPublisher(jmsTemplate, mock(ComponentLog.class));
    sender.publish("Hey dude!".getBytes());
    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, "cooQueue");
    runner.setProperty(ConsumeJMS.DESTINATION_TYPE, ConsumeJMS.QUEUE);
    runner.run(1, false);
    //
    final MockFlowFile successFF =
        runner.getFlowFilesForRelationship(PublishJMS.REL_SUCCESS).get(0);
    assertNotNull(successFF);
    assertEquals("cooQueue", successFF.getAttributes().get(JmsHeaders.DESTINATION));
    successFF.assertContentEquals("Hey dude!".getBytes());

    ((CachingConnectionFactory) jmsTemplate.getConnectionFactory()).destroy();
  }
 @Test
 public void testDefaultJmsConfiguration() {
   load(TestConfiguration.class);
   ActiveMQConnectionFactory connectionFactory =
       this.context.getBean(ActiveMQConnectionFactory.class);
   JmsTemplate jmsTemplate = this.context.getBean(JmsTemplate.class);
   JmsMessagingTemplate messagingTemplate = this.context.getBean(JmsMessagingTemplate.class);
   assertThat(connectionFactory).isEqualTo(jmsTemplate.getConnectionFactory());
   assertThat(messagingTemplate.getJmsTemplate()).isEqualTo(jmsTemplate);
   assertThat(((ActiveMQConnectionFactory) jmsTemplate.getConnectionFactory()).getBrokerURL())
       .isEqualTo(ACTIVEMQ_EMBEDDED_URL);
   assertThat(this.context.containsBean("jmsListenerContainerFactory")).isTrue();
 }
  public void start() throws JMSException {
    String selector = "next = '" + myId + "'";

    try {
      ConnectionFactory factory = template.getConnectionFactory();
      final Connection c = connection = factory.createConnection();

      // we might be a reusable connection in spring
      // so lets only set the client ID once if its not set
      synchronized (c) {
        if (c.getClientID() == null) {
          c.setClientID(myId);
        }
      }

      connection.start();

      session = connection.createSession(true, Session.CLIENT_ACKNOWLEDGE);
      consumer = session.createConsumer(destination, selector, false);
      consumer.setMessageListener(this);
    } catch (JMSException ex) {
      LOG.error("", ex);
      throw ex;
    }
  }
 /**
  * Failure test for the method <code>handleMessage</code>.<br>
  * when ExpirationTime not valid date time value
  *
  * @throws Exception to JUnit.
  */
 @Test(expected = IllegalArgumentException.class)
 public void test_handleMessageFail2() throws Exception {
   clearDB();
   loadData();
   User user = getTestUser();
   user.setRole(entityManager.find(Role.class, "1"));
   user = userService.create(user);
   entityManager.flush();
   User user1 = getTestUser();
   user1.setRole(entityManager.find(Role.class, "1"));
   user1 = userService.create(user1);
   entityManager.flush();
   Connection connection = jmsTemplate.getConnectionFactory().createConnection();
   try {
     Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
     TextMessage message = session.createTextMessage("");
     Document doc = getDataRequestDocument(request);
     doc.getElementsByTagName("ExpirationTime").item(0).setTextContent("invalida date");
     XPathFactory xpathFactory = XPathFactory.newInstance();
     XPath xpath = xpathFactory.newXPath();
     VelocityContext templateContext = new VelocityContext();
     instance.handleMessage(message, user1, doc, xpath, templateContext);
   } finally {
     connection.close();
   }
 }
 /**
  * Failure test for the method <code>handleMessage</code>.<br>
  * when request partner id not exist
  *
  * @throws Exception to JUnit.
  */
 @Test(expected = EntityNotFoundException.class)
 public void test_handleMessageFail1() throws Exception {
   clearDB();
   loadData();
   User user = getTestUser();
   user.setRole(entityManager.find(Role.class, "1"));
   user = userService.create(user);
   entityManager.flush();
   User user1 = getTestUser();
   user1.setRole(entityManager.find(Role.class, "1"));
   user1 = userService.create(user1);
   entityManager.flush();
   Connection connection = jmsTemplate.getConnectionFactory().createConnection();
   try {
     Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
     TextMessage message = session.createTextMessage("");
     request.setRequestedPartners(Arrays.asList("not exist Id"));
     Document doc = getDataRequestDocument(request);
     XPathFactory xpathFactory = XPathFactory.newInstance();
     XPath xpath = xpathFactory.newXPath();
     VelocityContext templateContext = new VelocityContext();
     instance.handleMessage(message, user1, doc, xpath, templateContext);
   } finally {
     connection.close();
   }
 }
 /**
  * Accuracy test for the method <code>handleMessage</code>.<br>
  * The result should be correct when CacheSafe element exist.
  *
  * @throws Exception to JUnit.
  */
 @Test
 public void test_handleMessage3() throws Exception {
   clearDB();
   loadData();
   User user = getTestUser();
   user.setRole(entityManager.find(Role.class, "1"));
   user = userService.create(user);
   entityManager.flush();
   User user1 = getTestUser();
   user1.setRole(entityManager.find(Role.class, "1"));
   user1 = userService.create(user1);
   entityManager.flush();
   Connection connection = jmsTemplate.getConnectionFactory().createConnection();
   try {
     Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
     TextMessage message = session.createTextMessage("");
     Document doc = getDataRequestDocument(request);
     // remove cache safe element
     doc.getDocumentElement().removeChild(doc.getElementsByTagName("CacheSafe").item(0));
     XPathFactory xpathFactory = XPathFactory.newInstance();
     XPath xpath = xpathFactory.newXPath();
     VelocityContext templateContext = new VelocityContext();
     String id = request.getId();
     entityManager.clear();
     DataRequest retrievedRequest = entityManager.find(DataRequest.class, id);
     assertNull("'handleMessage' should be correct.", retrievedRequest);
     instance.handleMessage(message, user1, doc, xpath, templateContext);
     entityManager.flush();
     entityManager.clear();
     assertEquals(
         "'handleMessage' should be correct.",
         request.getId(),
         (String) templateContext.get("requestId"));
     retrievedRequest = entityManager.find(DataRequest.class, id);
     assertNotNull("'handleMessage' should be correct.", retrievedRequest);
     assertEquals(
         "'handleMessage' should be correct.",
         request.getOriginalRequesterId(),
         retrievedRequest.getOriginalRequesterId());
     assertEquals(
         "'handleMessage' should be correct.", user1.getId(), retrievedRequest.getRequesterId());
     assertEquals(
         "'handleMessage' should be correct.", request.getQuery(), retrievedRequest.getQuery());
     assertEquals(
         "'handleMessage' should be correct.",
         request.getRequestedPartners().size(),
         retrievedRequest.getRequestedPartners().size());
     // isCacheSafe become false
     assertTrue("'handleMessage' should be correct.", request.isCacheSafe());
     assertFalse("'handleMessage' should be correct.", retrievedRequest.isCacheSafe());
     assertNotNull("'handleMessage' should be correct.", retrievedRequest.getExpirationTime());
   } finally {
     connection.close();
   }
 }
 @Test
 public void testActiveMQOverriddenPoolAndStandalone() {
   load(TestConfiguration.class, "spring.activemq.pooled:true", "spring.activemq.inMemory:false");
   JmsTemplate jmsTemplate = this.context.getBean(JmsTemplate.class);
   PooledConnectionFactory pool = this.context.getBean(PooledConnectionFactory.class);
   assertThat(jmsTemplate).isNotNull();
   assertThat(pool).isNotNull();
   assertThat(pool).isEqualTo(jmsTemplate.getConnectionFactory());
   ActiveMQConnectionFactory factory = (ActiveMQConnectionFactory) pool.getConnectionFactory();
   assertThat(factory.getBrokerURL()).isEqualTo(ACTIVEMQ_NETWORK_URL);
 }
 @Test
 public void testPubSubDomainOverride() {
   load(TestConfiguration.class, "spring.jms.pubSubDomain:false");
   JmsTemplate jmsTemplate = this.context.getBean(JmsTemplate.class);
   ActiveMQConnectionFactory connectionFactory =
       this.context.getBean(ActiveMQConnectionFactory.class);
   assertThat(jmsTemplate).isNotNull();
   assertThat(jmsTemplate.isPubSubDomain()).isFalse();
   assertThat(connectionFactory).isNotNull();
   assertThat(connectionFactory).isEqualTo(jmsTemplate.getConnectionFactory());
 }
 @Test
 public void testActiveMQOverriddenRemoteHost() {
   load(TestConfiguration.class, "spring.activemq.brokerUrl:tcp://remote-host:10000");
   JmsTemplate jmsTemplate = this.context.getBean(JmsTemplate.class);
   ActiveMQConnectionFactory connectionFactory =
       this.context.getBean(ActiveMQConnectionFactory.class);
   assertThat(jmsTemplate).isNotNull();
   assertThat(connectionFactory).isNotNull();
   assertThat(connectionFactory).isEqualTo(jmsTemplate.getConnectionFactory());
   assertThat(((ActiveMQConnectionFactory) jmsTemplate.getConnectionFactory()).getBrokerURL())
       .isEqualTo("tcp://remote-host:10000");
 }
 @Test
 public void testActiveMQOverriddenStandalone() {
   load(TestConfiguration.class, "spring.activemq.inMemory:false");
   JmsTemplate jmsTemplate = this.context.getBean(JmsTemplate.class);
   ActiveMQConnectionFactory connectionFactory =
       this.context.getBean(ActiveMQConnectionFactory.class);
   assertThat(jmsTemplate).isNotNull();
   assertThat(connectionFactory).isNotNull();
   assertThat(connectionFactory).isEqualTo(jmsTemplate.getConnectionFactory());
   assertThat(((ActiveMQConnectionFactory) jmsTemplate.getConnectionFactory()).getBrokerURL())
       .isEqualTo(ACTIVEMQ_NETWORK_URL);
 }
 @Test
 public void testActiveMQOverriddenPoolAndRemoteServer() {
   load(
       TestConfiguration.class,
       "spring.activemq.pooled:true",
       "spring.activemq.brokerUrl:tcp://remote-host:10000");
   JmsTemplate jmsTemplate = this.context.getBean(JmsTemplate.class);
   PooledConnectionFactory pool = this.context.getBean(PooledConnectionFactory.class);
   assertThat(jmsTemplate).isNotNull();
   assertThat(pool).isNotNull();
   assertThat(pool).isEqualTo(jmsTemplate.getConnectionFactory());
   ActiveMQConnectionFactory factory = (ActiveMQConnectionFactory) pool.getConnectionFactory();
   assertThat(factory.getBrokerURL()).isEqualTo("tcp://remote-host:10000");
 }