diff options
4 files changed, 880 insertions, 65 deletions
diff --git a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-jms/src/main/java/org/onap/policy/apex/plugins/event/carrier/jms/ApexJmsConsumer.java b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-jms/src/main/java/org/onap/policy/apex/plugins/event/carrier/jms/ApexJmsConsumer.java index 538b63546..4bad4cb7a 100644 --- a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-jms/src/main/java/org/onap/policy/apex/plugins/event/carrier/jms/ApexJmsConsumer.java +++ b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-jms/src/main/java/org/onap/policy/apex/plugins/event/carrier/jms/ApexJmsConsumer.java @@ -30,6 +30,7 @@ import javax.jms.MessageListener; import javax.jms.Session; import javax.jms.Topic; import javax.naming.InitialContext; +import javax.naming.NamingException; import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities; import org.onap.policy.apex.service.engine.event.ApexEventException; import org.onap.policy.apex.service.engine.event.ApexEventReceiver; @@ -77,10 +78,10 @@ public class ApexJmsConsumer extends ApexPluginsEventConsumer implements Message jmsConsumerProperties = (JmsCarrierTechnologyParameters) consumerParameters.getCarrierTechnologyParameters(); // Look up the JMS connection factory - InitialContext jmsContext = null; - ConnectionFactory connectionFactory = null; + InitialContext jmsContext; + ConnectionFactory connectionFactory; try { - jmsContext = new InitialContext(jmsConsumerProperties.getJmsConsumerProperties()); + jmsContext = getInitialContext(); connectionFactory = (ConnectionFactory) jmsContext.lookup(jmsConsumerProperties.getConnectionFactory()); // Check if we actually got a connection factory @@ -124,6 +125,17 @@ public class ApexJmsConsumer extends ApexPluginsEventConsumer implements Message } /** + * Construct InitialContext. This function should not be run directly. + * Package-private access is set for testing purposes only. + * + * @return InitialContext + * @throws NamingException if a naming exception is encountered + */ + InitialContext getInitialContext() throws NamingException { + return new InitialContext(jmsConsumerProperties.getJmsConsumerProperties()); + } + + /** * {@inheritDoc}. */ @Override diff --git a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-jms/src/main/java/org/onap/policy/apex/plugins/event/carrier/jms/ApexJmsProducer.java b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-jms/src/main/java/org/onap/policy/apex/plugins/event/carrier/jms/ApexJmsProducer.java index 4e5c7a31c..44ed810fd 100644 --- a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-jms/src/main/java/org/onap/policy/apex/plugins/event/carrier/jms/ApexJmsProducer.java +++ b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-jms/src/main/java/org/onap/policy/apex/plugins/event/carrier/jms/ApexJmsProducer.java @@ -1,7 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2019-2020 Nordix Foundation. + * Modifications Copyright (C) 2019-2021 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -32,6 +32,7 @@ import javax.jms.MessageProducer; import javax.jms.Session; import javax.jms.Topic; import javax.naming.InitialContext; +import javax.naming.NamingException; import org.onap.policy.apex.service.engine.event.ApexEventException; import org.onap.policy.apex.service.engine.event.ApexEventProducer; import org.onap.policy.apex.service.engine.event.ApexEventRuntimeException; @@ -72,7 +73,8 @@ public class ApexJmsProducer implements ApexEventProducer { private String name = null; // The peer references for this event handler - private Map<EventHandlerPeeredMode, PeeredReference> peerReferenceMap = new EnumMap<>(EventHandlerPeeredMode.class); + private final Map<EventHandlerPeeredMode, PeeredReference> peerReferenceMap = + new EnumMap<>(EventHandlerPeeredMode.class); /** * {@inheritDoc}. @@ -91,10 +93,10 @@ public class ApexJmsProducer implements ApexEventProducer { jmsProducerProperties = (JmsCarrierTechnologyParameters) producerParameters.getCarrierTechnologyParameters(); // Look up the JMS connection factory - InitialContext jmsContext = null; - ConnectionFactory connectionFactory = null; + InitialContext jmsContext; + ConnectionFactory connectionFactory; try { - jmsContext = new InitialContext(jmsProducerProperties.getJmsProducerProperties()); + jmsContext = getInitialContext(); connectionFactory = (ConnectionFactory) jmsContext.lookup(jmsProducerProperties.getConnectionFactory()); // Check if we actually got a connection factory @@ -159,6 +161,17 @@ public class ApexJmsProducer implements ApexEventProducer { } /** + * Construct InitialContext. This function should not be run directly. + * Package-private access is set for testing purposes only. + * + * @return InitialContext + * @throws NamingException if a naming exception is encountered + */ + public InitialContext getInitialContext() throws NamingException { + return new InitialContext(jmsProducerProperties.getJmsProducerProperties()); + } + + /** * {@inheritDoc}. */ @Override @@ -204,7 +217,7 @@ public class ApexJmsProducer implements ApexEventProducer { } // The JMS message to send is constructed using the JMS session - Message jmsMessage = null; + Message jmsMessage; // Check the type of JMS message to send if (jmsProducerProperties.isObjectMessageSending()) { diff --git a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-jms/src/test/java/org/onap/policy/apex/plugins/event/carrier/jms/ApexJmsConsumerTest.java b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-jms/src/test/java/org/onap/policy/apex/plugins/event/carrier/jms/ApexJmsConsumerTest.java index 8591d0470..e3ead320f 100644 --- a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-jms/src/test/java/org/onap/policy/apex/plugins/event/carrier/jms/ApexJmsConsumerTest.java +++ b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-jms/src/test/java/org/onap/policy/apex/plugins/event/carrier/jms/ApexJmsConsumerTest.java @@ -1,7 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2019 Samsung. All rights reserved. - * Modifications Copyright (C) 2019-2020 Nordix Foundation. + * Modifications Copyright (C) 2019-2021 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -26,10 +26,18 @@ import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; +import java.util.Properties; +import javax.jms.Connection; +import javax.jms.ConnectionFactory; +import javax.jms.JMSException; import javax.jms.Message; -import javax.jms.Session; +import javax.jms.Topic; +import javax.naming.InitialContext; +import javax.naming.NamingException; import org.junit.Before; import org.junit.Test; +import org.mockito.ArgumentMatchers; +import org.mockito.Mockito; import org.onap.policy.apex.service.engine.event.ApexEventException; import org.onap.policy.apex.service.engine.event.ApexEventProducer; import org.onap.policy.apex.service.engine.event.ApexEventReceiver; @@ -41,41 +49,150 @@ import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerPeeredMo public class ApexJmsConsumerTest { - ApexJmsConsumer apexJmsConsumer = null; - EventHandlerParameters consumerParameters = null; - ApexEventReceiver incomingEventReceiver = null; - ApexEventProducer apexJmsProducer = null; - Session jmsSession = null; - JmsCarrierTechnologyParameters jmsCarrierTechnologyParameters = null; + private static final String CONSUMER_NAME = "TestApexJmsConsumer"; + private ApexJmsConsumer apexJmsConsumer = null; + private EventHandlerParameters consumerParameters = null; + private ApexEventReceiver incomingEventReceiver = null; + private ApexEventProducer apexJmsProducer = null; + private JmsCarrierTechnologyParameters jmsCarrierTechnologyParameters = null; /** * Set up testing. - * - * @throws Exception on test set up errors. */ @Before - public void setUp() throws Exception { - apexJmsConsumer = new ApexJmsConsumer(); + public void setUp() { + apexJmsConsumer = Mockito.spy(new ApexJmsConsumer()); consumerParameters = new EventHandlerParameters(); apexJmsProducer = new ApexJmsProducer(); } @Test - public void testInitWithNonJmsCarrierTechnologyParameters() throws ApexEventException { - consumerParameters.setCarrierTechnologyParameters(new CarrierTechnologyParameters() {}); - assertThatThrownBy(() -> apexJmsConsumer.init("TestApexJmsConsumer", consumerParameters, incomingEventReceiver)) + public void testInitWithNonJmsCarrierTechnologyParameters() { + consumerParameters.setCarrierTechnologyParameters(new CarrierTechnologyParameters() { + }); + assertThatThrownBy( + () -> apexJmsConsumer.init(CONSUMER_NAME, consumerParameters, incomingEventReceiver) + ) .isInstanceOf(ApexEventException.class); } @Test - public void testInitWithJmsCarrierTechnologyParameters() throws ApexEventException { + public void testInitWithJmsCarrierTechnologyParameters() { jmsCarrierTechnologyParameters = new JmsCarrierTechnologyParameters(); consumerParameters.setCarrierTechnologyParameters(jmsCarrierTechnologyParameters); - assertThatThrownBy(() -> apexJmsConsumer.init("TestApexJmsConsumer", consumerParameters, incomingEventReceiver)) + assertThatThrownBy( + () -> apexJmsConsumer.init(CONSUMER_NAME, consumerParameters, incomingEventReceiver) + ) .isInstanceOf(ApexEventException.class); } @Test + public void testInitNoConnectionFactory() throws NamingException { + jmsCarrierTechnologyParameters = new JmsCarrierTechnologyParameters(); + consumerParameters.setCarrierTechnologyParameters(jmsCarrierTechnologyParameters); + + final InitialContext context = Mockito.mock(InitialContext.class); + Mockito.doReturn(null).when(context).lookup(jmsCarrierTechnologyParameters.getConnectionFactory()); + Mockito.doReturn(context).when(apexJmsConsumer).getInitialContext(); + + assertThatThrownBy( + () -> apexJmsConsumer.init(CONSUMER_NAME, consumerParameters, incomingEventReceiver) + ) + .isInstanceOf(ApexEventException.class); + } + + @Test + public void testInitNoConsumerTopic() throws NamingException { + jmsCarrierTechnologyParameters = new JmsCarrierTechnologyParameters(); + consumerParameters.setCarrierTechnologyParameters(jmsCarrierTechnologyParameters); + + final InitialContext context = Mockito.mock(InitialContext.class); + final ConnectionFactory connectionFactory = Mockito.mock(ConnectionFactory.class); + + Mockito.doReturn(connectionFactory).when(context).lookup(jmsCarrierTechnologyParameters.getConnectionFactory()); + Mockito.doReturn(null).when(context).lookup(jmsCarrierTechnologyParameters.getConsumerTopic()); + Mockito.doReturn(context).when(apexJmsConsumer).getInitialContext(); + + assertThatThrownBy( + () -> apexJmsConsumer.init(CONSUMER_NAME, consumerParameters, incomingEventReceiver) + ) + .isInstanceOf(ApexEventException.class); + } + + @Test + public void testInitNoConnection() throws NamingException, JMSException { + jmsCarrierTechnologyParameters = new JmsCarrierTechnologyParameters(); + consumerParameters.setCarrierTechnologyParameters(jmsCarrierTechnologyParameters); + + final InitialContext context = Mockito.mock(InitialContext.class); + final ConnectionFactory connectionFactory = Mockito.mock(ConnectionFactory.class); + final Topic topic = Mockito.mock(Topic.class); + + Mockito.doReturn(connectionFactory).when(context).lookup(jmsCarrierTechnologyParameters.getConnectionFactory()); + Mockito.doReturn(topic).when(context).lookup(jmsCarrierTechnologyParameters.getConsumerTopic()); + Mockito.doThrow(JMSException.class) + .when(connectionFactory) + .createConnection(jmsCarrierTechnologyParameters.getSecurityPrincipal(), + jmsCarrierTechnologyParameters.getSecurityCredentials()); + + Mockito.doReturn(context).when(apexJmsConsumer).getInitialContext(); + + assertThatThrownBy( + () -> apexJmsConsumer.init(CONSUMER_NAME, consumerParameters, incomingEventReceiver) + ) + .isInstanceOf(ApexEventException.class); + } + + @Test + public void testInitConnectionError() throws NamingException, JMSException { + jmsCarrierTechnologyParameters = new JmsCarrierTechnologyParameters(); + consumerParameters.setCarrierTechnologyParameters(jmsCarrierTechnologyParameters); + + final InitialContext context = Mockito.mock(InitialContext.class); + final ConnectionFactory connectionFactory = Mockito.mock(ConnectionFactory.class); + final Topic topic = Mockito.mock(Topic.class); + final Connection connection = Mockito.mock(Connection.class); + + Mockito.doReturn(connectionFactory).when(context).lookup(jmsCarrierTechnologyParameters.getConnectionFactory()); + Mockito.doReturn(topic).when(context).lookup(jmsCarrierTechnologyParameters.getConsumerTopic()); + Mockito.doReturn(connection) + .when(connectionFactory) + .createConnection(jmsCarrierTechnologyParameters.getSecurityPrincipal(), + jmsCarrierTechnologyParameters.getSecurityCredentials()); + Mockito.doThrow(JMSException.class).when(connection).start(); + Mockito.doReturn(context).when(apexJmsConsumer).getInitialContext(); + + assertThatThrownBy( + () -> apexJmsConsumer.init(CONSUMER_NAME, consumerParameters, incomingEventReceiver) + ) + .isInstanceOf(ApexEventException.class); + } + + @Test + public void testInit() throws NamingException, JMSException, ApexEventException { + jmsCarrierTechnologyParameters = new JmsCarrierTechnologyParameters(); + consumerParameters.setCarrierTechnologyParameters(jmsCarrierTechnologyParameters); + + final InitialContext context = Mockito.mock(InitialContext.class); + final ConnectionFactory connectionFactory = Mockito.mock(ConnectionFactory.class); + final Topic topic = Mockito.mock(Topic.class); + final Connection connection = Mockito.mock(Connection.class); + + Mockito.doReturn(connectionFactory).when(context).lookup(jmsCarrierTechnologyParameters.getConnectionFactory()); + Mockito.doReturn(topic).when(context).lookup(jmsCarrierTechnologyParameters.getConsumerTopic()); + Mockito.doReturn(connection) + .when(connectionFactory) + .createConnection(jmsCarrierTechnologyParameters.getSecurityPrincipal(), + jmsCarrierTechnologyParameters.getSecurityCredentials()); + Mockito.doNothing().when(connection).start(); + Mockito.doReturn(context).when(apexJmsConsumer).getInitialContext(); + + apexJmsConsumer.init(CONSUMER_NAME, consumerParameters, incomingEventReceiver); + + Mockito.verify(connection, Mockito.times(1)).start(); + } + + @Test public void testStart() { assertThatCode(apexJmsConsumer::start).doesNotThrowAnyException(); } @@ -93,7 +210,7 @@ public class ApexJmsConsumerTest { @Test public void testSetPeeredReference() { PeeredReference peeredReference = new PeeredReference(EventHandlerPeeredMode.REQUESTOR, - apexJmsConsumer, apexJmsProducer); + apexJmsConsumer, apexJmsProducer); apexJmsConsumer.setPeeredReference(EventHandlerPeeredMode.REQUESTOR, peeredReference); assertNotNull(apexJmsConsumer.getPeeredReference(EventHandlerPeeredMode.REQUESTOR)); } @@ -104,14 +221,107 @@ public class ApexJmsConsumerTest { } @Test - public void testOnMessage() { + public void testOnMessageUninitialized() { Message jmsMessage = null; assertThatThrownBy(() -> apexJmsConsumer.onMessage(jmsMessage)) .isInstanceOf(ApexEventRuntimeException.class); } @Test - public void testStop() { + public void testOnMessage() throws JMSException, NamingException, ApexEventException { + // prepare ApexJmsConsumer + jmsCarrierTechnologyParameters = new JmsCarrierTechnologyParameters(); + consumerParameters.setCarrierTechnologyParameters(jmsCarrierTechnologyParameters); + + final InitialContext context = Mockito.mock(InitialContext.class); + final ConnectionFactory connectionFactory = Mockito.mock(ConnectionFactory.class); + final Topic topic = Mockito.mock(Topic.class); + final Connection connection = Mockito.mock(Connection.class); + incomingEventReceiver = Mockito.mock(ApexEventReceiver.class); + + + Mockito.doReturn(connectionFactory).when(context).lookup(jmsCarrierTechnologyParameters.getConnectionFactory()); + Mockito.doReturn(topic).when(context).lookup(jmsCarrierTechnologyParameters.getConsumerTopic()); + Mockito.doReturn(connection) + .when(connectionFactory) + .createConnection(jmsCarrierTechnologyParameters.getSecurityPrincipal(), + jmsCarrierTechnologyParameters.getSecurityCredentials()); + Mockito.doNothing().when(connection).start(); + Mockito.doReturn(context).when(apexJmsConsumer).getInitialContext(); + + apexJmsConsumer.init(CONSUMER_NAME, consumerParameters, incomingEventReceiver); + + final Message message = Mockito.mock(Message.class); + + apexJmsConsumer.onMessage(message); + Mockito + .verify(incomingEventReceiver, Mockito.times(1)) + .receiveEvent(ArgumentMatchers.any(Properties.class), ArgumentMatchers.eq(message)); + } + + @Test + public void testConnectionError() throws NamingException, JMSException, ApexEventException { + // prepare ApexJmsConsumer + jmsCarrierTechnologyParameters = new JmsCarrierTechnologyParameters(); + consumerParameters.setCarrierTechnologyParameters(jmsCarrierTechnologyParameters); + + final InitialContext context = Mockito.mock(InitialContext.class); + final ConnectionFactory connectionFactory = Mockito.mock(ConnectionFactory.class); + final Topic topic = Mockito.mock(Topic.class); + final Connection connection = Mockito.mock(Connection.class); + + Mockito.doReturn(connectionFactory).when(context).lookup(jmsCarrierTechnologyParameters.getConnectionFactory()); + Mockito.doReturn(topic).when(context).lookup(jmsCarrierTechnologyParameters.getConsumerTopic()); + Mockito.doReturn(connection) + .when(connectionFactory) + .createConnection(jmsCarrierTechnologyParameters.getSecurityPrincipal(), + jmsCarrierTechnologyParameters.getSecurityCredentials()); + Mockito.doNothing().when(connection).start(); + Mockito.doReturn(context).when(apexJmsConsumer).getInitialContext(); + + apexJmsConsumer.init(CONSUMER_NAME, consumerParameters, incomingEventReceiver); + + Mockito.doThrow(JMSException.class).when(connection).close(); + + // do tests + apexJmsConsumer.start(); + assertThatCode(() -> apexJmsConsumer.stop()).doesNotThrowAnyException(); + Mockito.verify(connection, Mockito.times(1)).close(); + } + + @Test + public void testStop() throws NamingException, JMSException, ApexEventException { + // prepare ApexJmsConsumer + jmsCarrierTechnologyParameters = new JmsCarrierTechnologyParameters(); + consumerParameters.setCarrierTechnologyParameters(jmsCarrierTechnologyParameters); + + final InitialContext context = Mockito.mock(InitialContext.class); + final ConnectionFactory connectionFactory = Mockito.mock(ConnectionFactory.class); + final Topic topic = Mockito.mock(Topic.class); + final Connection connection = Mockito.mock(Connection.class); + + Mockito.doReturn(connectionFactory).when(context).lookup(jmsCarrierTechnologyParameters.getConnectionFactory()); + Mockito.doReturn(topic).when(context).lookup(jmsCarrierTechnologyParameters.getConsumerTopic()); + Mockito.doReturn(connection) + .when(connectionFactory) + .createConnection(jmsCarrierTechnologyParameters.getSecurityPrincipal(), + jmsCarrierTechnologyParameters.getSecurityCredentials()); + Mockito.doNothing().when(connection).start(); + Mockito.doReturn(context).when(apexJmsConsumer).getInitialContext(); + + apexJmsConsumer.init(CONSUMER_NAME, consumerParameters, incomingEventReceiver); + + Mockito.doNothing().when(connection).close(); + + apexJmsConsumer.start(); + + // do tests + apexJmsConsumer.stop(); + Mockito.verify(connection, Mockito.times(1)).close(); + } + + @Test + public void testStopNoJmsProperties() { assertThatThrownBy(apexJmsConsumer::stop).isInstanceOf(NullPointerException.class); } } diff --git a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-jms/src/test/java/org/onap/policy/apex/plugins/event/carrier/jms/ApexJmsProducerTest.java b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-jms/src/test/java/org/onap/policy/apex/plugins/event/carrier/jms/ApexJmsProducerTest.java index 09a87f804..708f29f4f 100644 --- a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-jms/src/test/java/org/onap/policy/apex/plugins/event/carrier/jms/ApexJmsProducerTest.java +++ b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-jms/src/test/java/org/onap/policy/apex/plugins/event/carrier/jms/ApexJmsProducerTest.java @@ -21,18 +21,33 @@ package org.onap.policy.apex.plugins.event.carrier.jms; +import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatCode; import static org.assertj.core.api.Assertions.assertThatThrownBy; -import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertSame; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.util.Random; +import javax.jms.Connection; +import javax.jms.ConnectionFactory; +import javax.jms.JMSException; +import javax.jms.Message; +import javax.jms.MessageProducer; +import javax.jms.ObjectMessage; import javax.jms.Session; +import javax.jms.TextMessage; +import javax.jms.Topic; +import javax.naming.InitialContext; +import javax.naming.NamingException; +import org.apache.commons.lang3.RandomStringUtils; +import org.junit.After; import org.junit.Before; import org.junit.Test; +import org.mockito.Mockito; import org.onap.policy.apex.service.engine.event.ApexEvent; import org.onap.policy.apex.service.engine.event.ApexEventException; -import org.onap.policy.apex.service.engine.event.ApexEventProducer; -import org.onap.policy.apex.service.engine.event.ApexEventReceiver; import org.onap.policy.apex.service.engine.event.ApexEventRuntimeException; import org.onap.policy.apex.service.engine.event.PeeredReference; import org.onap.policy.apex.service.engine.event.SynchronousEventCache; @@ -42,43 +57,204 @@ import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerPeeredMo public class ApexJmsProducerTest { - ApexJmsConsumer apexJmsConsumer = null; - EventHandlerParameters producerParameters = null; - ApexEventReceiver incomingEventReceiver = null; - ApexEventProducer apexJmsProducer = null; - Session jmsSession = null; - JmsCarrierTechnologyParameters jmsCarrierTechnologyParameters = null; - SynchronousEventCache synchronousEventCache = null; + private static final String PRODUCER_NAME = "TestApexJmsProducer"; + private ApexJmsConsumer apexJmsConsumer; + private EventHandlerParameters producerParameters; + private ApexJmsProducer apexJmsProducer; + private JmsCarrierTechnologyParameters jmsCarrierTechnologyParameters; private static final long DEFAULT_SYNCHRONOUS_EVENT_TIMEOUT = 1000; + private final Random random = new Random(); + private final PrintStream out = System.out; /** * Set up testing. * - * @throws Exception on test set up errors. */ @Before - public void setUp() throws Exception { + public void setUp() { apexJmsConsumer = new ApexJmsConsumer(); producerParameters = new EventHandlerParameters(); - apexJmsProducer = new ApexJmsProducer(); + apexJmsProducer = Mockito.spy(new ApexJmsProducer()); + } + + @After + public void tearDown() { + // restore system.out + System.setOut(out); } @Test public void testInitWithNonJmsCarrierTechnologyParameters() { - producerParameters.setCarrierTechnologyParameters(new CarrierTechnologyParameters() {}); - assertThatThrownBy(() -> apexJmsProducer.init("TestApexJmsProducer", producerParameters)) + producerParameters.setCarrierTechnologyParameters(new CarrierTechnologyParameters() { + }); + assertThatThrownBy(() -> apexJmsProducer.init(PRODUCER_NAME, producerParameters)) + .isInstanceOf(ApexEventException.class); + } + + @Test + public void testInitWithJmsCarrierTechnologyParameters() { + jmsCarrierTechnologyParameters = new JmsCarrierTechnologyParameters(); + producerParameters.setCarrierTechnologyParameters(jmsCarrierTechnologyParameters); + assertThatThrownBy(() -> apexJmsProducer.init(PRODUCER_NAME, producerParameters)) + .isInstanceOf(ApexEventException.class); + } + + @Test + public void testInitWithoutConnectionFactory() throws NamingException { + jmsCarrierTechnologyParameters = new JmsCarrierTechnologyParameters(); + producerParameters.setCarrierTechnologyParameters(jmsCarrierTechnologyParameters); + final String producerName = RandomStringUtils.randomAlphabetic(5); + + InitialContext context = Mockito.mock(InitialContext.class); + Mockito.doReturn(context).when(apexJmsProducer).getInitialContext(); + Mockito.doReturn(null).when(context).lookup(jmsCarrierTechnologyParameters.getConnectionFactory()); + + assertThatThrownBy(() -> apexJmsProducer.init(producerName, producerParameters)) + .isInstanceOf(ApexEventException.class); + } + + @Test + public void testInitWithoutTopic() throws NamingException { + jmsCarrierTechnologyParameters = new JmsCarrierTechnologyParameters(); + producerParameters.setCarrierTechnologyParameters(jmsCarrierTechnologyParameters); + final String producerName = RandomStringUtils.randomAlphabetic(5); + + final InitialContext context = Mockito.mock(InitialContext.class); + final ConnectionFactory connectionFactory = Mockito.mock(ConnectionFactory.class); + + Mockito.doReturn(context).when(apexJmsProducer).getInitialContext(); + Mockito.doReturn(connectionFactory).when(context).lookup(jmsCarrierTechnologyParameters.getConnectionFactory()); + Mockito.doReturn(null).when(context).lookup(jmsCarrierTechnologyParameters.getProducerTopic()); + + assertThatThrownBy(() -> apexJmsProducer.init(producerName, producerParameters)) + .isInstanceOf(ApexEventException.class); + } + + @Test + public void testInitConnectionCreateError() throws NamingException, JMSException { + jmsCarrierTechnologyParameters = new JmsCarrierTechnologyParameters(); + producerParameters.setCarrierTechnologyParameters(jmsCarrierTechnologyParameters); + final String producerName = RandomStringUtils.randomAlphabetic(5); + + final InitialContext context = Mockito.mock(InitialContext.class); + final ConnectionFactory connectionFactory = Mockito.mock(ConnectionFactory.class); + final Topic topic = Mockito.mock(Topic.class); + + Mockito.doReturn(context).when(apexJmsProducer).getInitialContext(); + Mockito.doReturn(connectionFactory).when(context).lookup(jmsCarrierTechnologyParameters.getConnectionFactory()); + Mockito.doReturn(topic).when(context).lookup(jmsCarrierTechnologyParameters.getProducerTopic()); + Mockito.doThrow(JMSException.class).when(connectionFactory) + .createConnection(jmsCarrierTechnologyParameters.getSecurityPrincipal(), + jmsCarrierTechnologyParameters.getSecurityCredentials()); + + assertThatThrownBy(() -> apexJmsProducer.init(producerName, producerParameters)) + .isInstanceOf(ApexEventException.class); + } + + @Test + public void testInitConnectionStartError() throws NamingException, JMSException { + jmsCarrierTechnologyParameters = new JmsCarrierTechnologyParameters(); + producerParameters.setCarrierTechnologyParameters(jmsCarrierTechnologyParameters); + final String producerName = RandomStringUtils.randomAlphabetic(5); + + final InitialContext context = Mockito.mock(InitialContext.class); + final ConnectionFactory connectionFactory = Mockito.mock(ConnectionFactory.class); + final Topic topic = Mockito.mock(Topic.class); + final Connection connection = Mockito.mock(Connection.class); + + Mockito.doReturn(context).when(apexJmsProducer).getInitialContext(); + Mockito.doReturn(connectionFactory).when(context).lookup(jmsCarrierTechnologyParameters.getConnectionFactory()); + Mockito.doReturn(topic).when(context).lookup(jmsCarrierTechnologyParameters.getProducerTopic()); + Mockito.doReturn(connection) + .when(connectionFactory) + .createConnection(jmsCarrierTechnologyParameters.getSecurityPrincipal(), + jmsCarrierTechnologyParameters.getSecurityCredentials()); + Mockito.doThrow(JMSException.class).when(connection).start(); + + assertThatThrownBy(() -> apexJmsProducer.init(producerName, producerParameters)) + .isInstanceOf(ApexEventException.class); + } + + @Test + public void testInitSessionCreateError() throws NamingException, JMSException { + jmsCarrierTechnologyParameters = new JmsCarrierTechnologyParameters(); + producerParameters.setCarrierTechnologyParameters(jmsCarrierTechnologyParameters); + final String producerName = RandomStringUtils.randomAlphabetic(5); + + final InitialContext context = Mockito.mock(InitialContext.class); + final ConnectionFactory connectionFactory = Mockito.mock(ConnectionFactory.class); + final Topic topic = Mockito.mock(Topic.class); + final Connection connection = Mockito.mock(Connection.class); + + Mockito.doReturn(context).when(apexJmsProducer).getInitialContext(); + Mockito.doReturn(connectionFactory).when(context).lookup(jmsCarrierTechnologyParameters.getConnectionFactory()); + Mockito.doReturn(topic).when(context).lookup(jmsCarrierTechnologyParameters.getProducerTopic()); + Mockito.doReturn(connection) + .when(connectionFactory) + .createConnection(jmsCarrierTechnologyParameters.getSecurityPrincipal(), + jmsCarrierTechnologyParameters.getSecurityCredentials()); + Mockito.doNothing().when(connection).start(); + Mockito.doThrow(JMSException.class).when(connection).createSession(false, Session.AUTO_ACKNOWLEDGE); + + assertThatThrownBy(() -> apexJmsProducer.init(producerName, producerParameters)) .isInstanceOf(ApexEventException.class); } @Test - public void testInitWithJmsCarrierTechnologyParameters() throws ApexEventException { + public void testInitProducerCreateError() throws NamingException, JMSException { jmsCarrierTechnologyParameters = new JmsCarrierTechnologyParameters(); producerParameters.setCarrierTechnologyParameters(jmsCarrierTechnologyParameters); - assertThatThrownBy(() -> apexJmsProducer.init("TestApexJmsProducer", producerParameters)) + final String producerName = RandomStringUtils.randomAlphabetic(5); + + final InitialContext context = Mockito.mock(InitialContext.class); + final ConnectionFactory connectionFactory = Mockito.mock(ConnectionFactory.class); + final Topic topic = Mockito.mock(Topic.class); + final Connection connection = Mockito.mock(Connection.class); + final Session session = Mockito.mock(Session.class); + + Mockito.doReturn(context).when(apexJmsProducer).getInitialContext(); + Mockito.doReturn(connectionFactory).when(context).lookup(jmsCarrierTechnologyParameters.getConnectionFactory()); + Mockito.doReturn(topic).when(context).lookup(jmsCarrierTechnologyParameters.getProducerTopic()); + Mockito.doReturn(connection) + .when(connectionFactory) + .createConnection(jmsCarrierTechnologyParameters.getSecurityPrincipal(), + jmsCarrierTechnologyParameters.getSecurityCredentials()); + Mockito.doNothing().when(connection).start(); + Mockito.doReturn(session).when(connection).createSession(false, Session.AUTO_ACKNOWLEDGE); + Mockito.doThrow(JMSException.class).when(session).createProducer(topic); + assertThatThrownBy(() -> apexJmsProducer.init(producerName, producerParameters)) .isInstanceOf(ApexEventException.class); } @Test + public void testInit() throws NamingException, JMSException { + jmsCarrierTechnologyParameters = new JmsCarrierTechnologyParameters(); + producerParameters.setCarrierTechnologyParameters(jmsCarrierTechnologyParameters); + final String producerName = RandomStringUtils.randomAlphabetic(5); + + final InitialContext context = Mockito.mock(InitialContext.class); + final ConnectionFactory connectionFactory = Mockito.mock(ConnectionFactory.class); + final Topic topic = Mockito.mock(Topic.class); + final Connection connection = Mockito.mock(Connection.class); + final Session session = Mockito.mock(Session.class); + final MessageProducer messageProducer = Mockito.mock(MessageProducer.class); + + Mockito.doReturn(context).when(apexJmsProducer).getInitialContext(); + Mockito.doReturn(connectionFactory).when(context).lookup(jmsCarrierTechnologyParameters.getConnectionFactory()); + Mockito.doReturn(topic).when(context).lookup(jmsCarrierTechnologyParameters.getProducerTopic()); + Mockito.doReturn(connection) + .when(connectionFactory) + .createConnection(jmsCarrierTechnologyParameters.getSecurityPrincipal(), + jmsCarrierTechnologyParameters.getSecurityCredentials()); + Mockito.doNothing().when(connection).start(); + Mockito.doReturn(session).when(connection).createSession(false, Session.AUTO_ACKNOWLEDGE); + Mockito.doReturn(messageProducer).when(session).createProducer(topic); + + assertThatCode(() -> apexJmsProducer.init(producerName, producerParameters)) + .doesNotThrowAnyException(); + } + + @Test public void testGetName() { assertNull(apexJmsProducer.getName()); } @@ -91,40 +267,444 @@ public class ApexJmsProducerTest { @Test public void testSetPeeredReference() { PeeredReference peeredReference = new PeeredReference(EventHandlerPeeredMode.REQUESTOR, - apexJmsConsumer, apexJmsProducer); + apexJmsConsumer, apexJmsProducer); apexJmsProducer.setPeeredReference(EventHandlerPeeredMode.REQUESTOR, peeredReference); - assertNotNull(apexJmsProducer.getPeeredReference(EventHandlerPeeredMode.REQUESTOR)); + final PeeredReference actual = apexJmsProducer.getPeeredReference(EventHandlerPeeredMode.REQUESTOR); + assertSame(peeredReference, actual); + } + + @Test + public void testSendEventNotSerializable() { + producerParameters.setCarrierTechnologyParameters(new JmsCarrierTechnologyParameters()); + Object object = new Object(); + final long executionId = random.nextLong(); + assertThatThrownBy(() -> apexJmsProducer.sendEvent(executionId, null, PRODUCER_NAME, object)) + .isInstanceOf(ApexEventRuntimeException.class); } @Test - public void testSendEvent() throws ApexEventException { - producerParameters.setCarrierTechnologyParameters(new JmsCarrierTechnologyParameters() {}); - synchronousEventCache = new SynchronousEventCache(EventHandlerPeeredMode.SYNCHRONOUS, - apexJmsConsumer, apexJmsProducer, DEFAULT_SYNCHRONOUS_EVENT_TIMEOUT); + public void testSendEventRemoveCache() { + producerParameters.setCarrierTechnologyParameters(new JmsCarrierTechnologyParameters()); + final SynchronousEventCache synchronousEventCache = + Mockito.spy(new SynchronousEventCache(EventHandlerPeeredMode.SYNCHRONOUS, + apexJmsConsumer, apexJmsProducer, DEFAULT_SYNCHRONOUS_EVENT_TIMEOUT)); apexJmsProducer.setPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS, - synchronousEventCache); + synchronousEventCache); + + Object object = new Object(); + final long executionId = random.nextLong(); + + assertThatThrownBy(() -> apexJmsProducer.sendEvent(executionId, null, PRODUCER_NAME, object)) + .isInstanceOf(ApexEventRuntimeException.class); + Mockito.verify(synchronousEventCache, Mockito.times(1)).removeCachedEventToApexIfExists(executionId); + } + + @Test + public void testSendEventCreateObjectMessageError() throws ApexEventException, NamingException, JMSException { + // Prepare ApexJmsProducer + jmsCarrierTechnologyParameters = new JmsCarrierTechnologyParameters(); + producerParameters.setCarrierTechnologyParameters(jmsCarrierTechnologyParameters); + + final InitialContext context = Mockito.mock(InitialContext.class); + final ConnectionFactory connectionFactory = Mockito.mock(ConnectionFactory.class); + final Topic topic = Mockito.mock(Topic.class); + final Connection connection = Mockito.mock(Connection.class); + final Session session = Mockito.mock(Session.class); + final MessageProducer messageProducer = Mockito.mock(MessageProducer.class); + + Mockito.doReturn(context).when(apexJmsProducer).getInitialContext(); + Mockito.doReturn(connectionFactory).when(context).lookup(jmsCarrierTechnologyParameters.getConnectionFactory()); + Mockito.doReturn(topic).when(context).lookup(jmsCarrierTechnologyParameters.getProducerTopic()); + Mockito.doReturn(connection) + .when(connectionFactory) + .createConnection(jmsCarrierTechnologyParameters.getSecurityPrincipal(), + jmsCarrierTechnologyParameters.getSecurityCredentials()); + Mockito.doNothing().when(connection).start(); + Mockito.doReturn(session).when(connection).createSession(false, Session.AUTO_ACKNOWLEDGE); + Mockito.doReturn(messageProducer).when(session).createProducer(topic); + + apexJmsProducer.init(RandomStringUtils.random(5), producerParameters); + + // Prepare sendEvent + ApexEvent apexEvent = new ApexEvent("testEvent", "testVersion", "testNameSpace", - "testSource", "testTarget"); - assertThatThrownBy(() -> apexJmsProducer.sendEvent(1000L, null, "TestApexJmsProducer", apexEvent)) - .isInstanceOf(NullPointerException.class); + "testSource", "testTarget"); + Mockito.doThrow(JMSException.class).when(session).createObjectMessage(apexEvent); + + final long executionId = random.nextLong(); + + + assertThatThrownBy(() -> apexJmsProducer.sendEvent(executionId, null, PRODUCER_NAME, apexEvent)) + .isInstanceOf(ApexEventRuntimeException.class); } @Test - public void testSendEventWithNonSerializableObject() throws ApexEventException { - producerParameters.setCarrierTechnologyParameters(new JmsCarrierTechnologyParameters() {}); - synchronousEventCache = new SynchronousEventCache(EventHandlerPeeredMode.SYNCHRONOUS, - apexJmsConsumer, apexJmsProducer, DEFAULT_SYNCHRONOUS_EVENT_TIMEOUT); - apexJmsProducer.setPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS, - synchronousEventCache); + public void testSendEventCreateObjectMessageSendError() throws ApexEventException, NamingException, JMSException { + // Prepare ApexJmsProducer + jmsCarrierTechnologyParameters = new JmsCarrierTechnologyParameters(); + producerParameters.setCarrierTechnologyParameters(jmsCarrierTechnologyParameters); + + final InitialContext context = Mockito.mock(InitialContext.class); + final ConnectionFactory connectionFactory = Mockito.mock(ConnectionFactory.class); + final Topic topic = Mockito.mock(Topic.class); + final Connection connection = Mockito.mock(Connection.class); + final Session session = Mockito.mock(Session.class); + final MessageProducer messageProducer = Mockito.mock(MessageProducer.class); + + Mockito.doReturn(context).when(apexJmsProducer).getInitialContext(); + Mockito.doReturn(connectionFactory).when(context).lookup(jmsCarrierTechnologyParameters.getConnectionFactory()); + Mockito.doReturn(topic).when(context).lookup(jmsCarrierTechnologyParameters.getProducerTopic()); + Mockito.doReturn(connection) + .when(connectionFactory) + .createConnection(jmsCarrierTechnologyParameters.getSecurityPrincipal(), + jmsCarrierTechnologyParameters.getSecurityCredentials()); + Mockito.doNothing().when(connection).start(); + Mockito.doReturn(session).when(connection).createSession(false, Session.AUTO_ACKNOWLEDGE); + Mockito.doReturn(messageProducer).when(session).createProducer(topic); + + apexJmsProducer.init(RandomStringUtils.random(5), producerParameters); + + // Prepare sendEvent + final Message message = Mockito.mock(ObjectMessage.class); + ApexEvent apexEvent = new ApexEvent("testEvent", "testVersion", "testNameSpace", + "testSource", "testTarget"); + Mockito.doReturn(message).when(session).createObjectMessage(apexEvent); + Mockito.doThrow(JMSException.class).when(messageProducer).send(message); + + final long executionId = random.nextLong(); + + + assertThatThrownBy( + () -> apexJmsProducer + .sendEvent(executionId, null, PRODUCER_NAME, apexEvent) + ) + .isInstanceOf(ApexEventRuntimeException.class); + } + + @Test + public void testSendEventCreateObjectMessageSend() throws ApexEventException, NamingException, JMSException { + // Prepare ApexJmsProducer + jmsCarrierTechnologyParameters = new JmsCarrierTechnologyParameters(); + producerParameters.setCarrierTechnologyParameters(jmsCarrierTechnologyParameters); + + final InitialContext context = Mockito.mock(InitialContext.class); + final ConnectionFactory connectionFactory = Mockito.mock(ConnectionFactory.class); + final Topic topic = Mockito.mock(Topic.class); + final Connection connection = Mockito.mock(Connection.class); + final Session session = Mockito.mock(Session.class); + final MessageProducer messageProducer = Mockito.mock(MessageProducer.class); + + Mockito.doReturn(context).when(apexJmsProducer).getInitialContext(); + Mockito.doReturn(connectionFactory).when(context).lookup(jmsCarrierTechnologyParameters.getConnectionFactory()); + Mockito.doReturn(topic).when(context).lookup(jmsCarrierTechnologyParameters.getProducerTopic()); + Mockito.doReturn(connection) + .when(connectionFactory) + .createConnection(jmsCarrierTechnologyParameters.getSecurityPrincipal(), + jmsCarrierTechnologyParameters.getSecurityCredentials()); + Mockito.doNothing().when(connection).start(); + Mockito.doReturn(session).when(connection).createSession(false, Session.AUTO_ACKNOWLEDGE); + Mockito.doReturn(messageProducer).when(session).createProducer(topic); + + apexJmsProducer.init(RandomStringUtils.random(5), producerParameters); + + // Prepare sendEvent + final Message message = Mockito.mock(ObjectMessage.class); + ApexEvent apexEvent = new ApexEvent("testEvent", "testVersion", "testNameSpace", + "testSource", "testTarget"); + Mockito.doReturn(message).when(session).createObjectMessage(apexEvent); + Mockito.doNothing().when(messageProducer).send(message); + + final long executionId = random.nextLong(); + + + assertThatCode( + () -> apexJmsProducer + .sendEvent(executionId, null, PRODUCER_NAME, apexEvent) + ) + .doesNotThrowAnyException(); + } + + @Test + public void testSendEventCreateTextMessageError() throws ApexEventException, NamingException, JMSException { + // Prepare ApexJmsProducer + jmsCarrierTechnologyParameters = new JmsCarrierTechnologyParameters(); + jmsCarrierTechnologyParameters.setObjectMessageSending(false); + producerParameters.setCarrierTechnologyParameters(jmsCarrierTechnologyParameters); + + final InitialContext context = Mockito.mock(InitialContext.class); + final ConnectionFactory connectionFactory = Mockito.mock(ConnectionFactory.class); + final Topic topic = Mockito.mock(Topic.class); + final Connection connection = Mockito.mock(Connection.class); + final Session session = Mockito.mock(Session.class); + final MessageProducer messageProducer = Mockito.mock(MessageProducer.class); + + Mockito.doReturn(context).when(apexJmsProducer).getInitialContext(); + Mockito.doReturn(connectionFactory).when(context).lookup(jmsCarrierTechnologyParameters.getConnectionFactory()); + Mockito.doReturn(topic).when(context).lookup(jmsCarrierTechnologyParameters.getProducerTopic()); + Mockito.doReturn(connection) + .when(connectionFactory) + .createConnection(jmsCarrierTechnologyParameters.getSecurityPrincipal(), + jmsCarrierTechnologyParameters.getSecurityCredentials()); + Mockito.doNothing().when(connection).start(); + Mockito.doReturn(session).when(connection).createSession(false, Session.AUTO_ACKNOWLEDGE); + Mockito.doReturn(messageProducer).when(session).createProducer(topic); + apexJmsProducer.init(RandomStringUtils.random(5), producerParameters); + + // Prepare sendEvent + ApexEvent apexEvent = new ApexEvent("testEvent", "testVersion", "testNameSpace", + "testSource", "testTarget"); + + Mockito.doThrow(JMSException.class).when(session).createTextMessage(apexEvent.toString()); - ApexJmsProducerTest producerTest = new ApexJmsProducerTest(); + final long executionId = random.nextLong(); - assertThatThrownBy(() -> apexJmsProducer.sendEvent(-1L, null, "TestApexJmsProducer", producerTest)) + assertThatThrownBy( + () -> apexJmsProducer + .sendEvent(executionId, null, PRODUCER_NAME, apexEvent) + ) .isInstanceOf(ApexEventRuntimeException.class); } @Test - public void testStop() { + public void testSendEventCreateTextMessageSendError() throws ApexEventException, NamingException, JMSException { + // Prepare ApexJmsProducer + jmsCarrierTechnologyParameters = new JmsCarrierTechnologyParameters(); + jmsCarrierTechnologyParameters.setObjectMessageSending(false); + producerParameters.setCarrierTechnologyParameters(jmsCarrierTechnologyParameters); + + final InitialContext context = Mockito.mock(InitialContext.class); + final ConnectionFactory connectionFactory = Mockito.mock(ConnectionFactory.class); + final Topic topic = Mockito.mock(Topic.class); + final Connection connection = Mockito.mock(Connection.class); + final Session session = Mockito.mock(Session.class); + final MessageProducer messageProducer = Mockito.mock(MessageProducer.class); + + Mockito.doReturn(context).when(apexJmsProducer).getInitialContext(); + Mockito.doReturn(connectionFactory).when(context).lookup(jmsCarrierTechnologyParameters.getConnectionFactory()); + Mockito.doReturn(topic).when(context).lookup(jmsCarrierTechnologyParameters.getProducerTopic()); + Mockito.doReturn(connection) + .when(connectionFactory) + .createConnection(jmsCarrierTechnologyParameters.getSecurityPrincipal(), + jmsCarrierTechnologyParameters.getSecurityCredentials()); + Mockito.doNothing().when(connection).start(); + Mockito.doReturn(session).when(connection).createSession(false, Session.AUTO_ACKNOWLEDGE); + Mockito.doReturn(messageProducer).when(session).createProducer(topic); + + apexJmsProducer.init(RandomStringUtils.random(5), producerParameters); + + // Prepare sendEvent + final Message message = Mockito.mock(TextMessage.class); + ApexEvent apexEvent = new ApexEvent("testEvent", "testVersion", "testNameSpace", + "testSource", "testTarget"); + Mockito.doReturn(message).when(session).createTextMessage(apexEvent.toString()); + Mockito.doThrow(JMSException.class).when(messageProducer).send(message); + + final long executionId = random.nextLong(); + + + assertThatThrownBy( + () -> apexJmsProducer + .sendEvent(executionId, null, PRODUCER_NAME, apexEvent) + ) + .isInstanceOf(ApexEventRuntimeException.class); + } + + @Test + public void testSendEventCreateTextMessageSend() throws ApexEventException, NamingException, JMSException { + // Prepare ApexJmsProducer + jmsCarrierTechnologyParameters = new JmsCarrierTechnologyParameters(); + jmsCarrierTechnologyParameters.setObjectMessageSending(false); + producerParameters.setCarrierTechnologyParameters(jmsCarrierTechnologyParameters); + + final InitialContext context = Mockito.mock(InitialContext.class); + final ConnectionFactory connectionFactory = Mockito.mock(ConnectionFactory.class); + final Topic topic = Mockito.mock(Topic.class); + final Connection connection = Mockito.mock(Connection.class); + final Session session = Mockito.mock(Session.class); + final MessageProducer messageProducer = Mockito.mock(MessageProducer.class); + + Mockito.doReturn(context).when(apexJmsProducer).getInitialContext(); + Mockito.doReturn(connectionFactory).when(context).lookup(jmsCarrierTechnologyParameters.getConnectionFactory()); + Mockito.doReturn(topic).when(context).lookup(jmsCarrierTechnologyParameters.getProducerTopic()); + Mockito.doReturn(connection) + .when(connectionFactory) + .createConnection(jmsCarrierTechnologyParameters.getSecurityPrincipal(), + jmsCarrierTechnologyParameters.getSecurityCredentials()); + Mockito.doNothing().when(connection).start(); + Mockito.doReturn(session).when(connection).createSession(false, Session.AUTO_ACKNOWLEDGE); + Mockito.doReturn(messageProducer).when(session).createProducer(topic); + + apexJmsProducer.init(RandomStringUtils.random(5), producerParameters); + + // Prepare sendEvent + final Message message = Mockito.mock(TextMessage.class); + ApexEvent apexEvent = new ApexEvent("testEvent", "testVersion", "testNameSpace", + "testSource", "testTarget"); + Mockito.doReturn(message).when(session).createTextMessage(apexEvent.toString()); + Mockito.doNothing().when(messageProducer).send(message); + + final long executionId = random.nextLong(); + + + assertThatCode( + () -> apexJmsProducer.sendEvent(executionId, null, PRODUCER_NAME, apexEvent) + ) + .doesNotThrowAnyException(); + } + + @Test + public void testStopProducerException() throws NamingException, ApexEventException, JMSException { + // Prepare ApexJmsProducer + jmsCarrierTechnologyParameters = new JmsCarrierTechnologyParameters(); + jmsCarrierTechnologyParameters.setObjectMessageSending(false); + producerParameters.setCarrierTechnologyParameters(jmsCarrierTechnologyParameters); + + final InitialContext context = Mockito.mock(InitialContext.class); + final ConnectionFactory connectionFactory = Mockito.mock(ConnectionFactory.class); + final Topic topic = Mockito.mock(Topic.class); + final Connection connection = Mockito.mock(Connection.class); + final Session session = Mockito.mock(Session.class); + final MessageProducer messageProducer = Mockito.mock(MessageProducer.class); + + Mockito.doReturn(context).when(apexJmsProducer).getInitialContext(); + Mockito.doReturn(connectionFactory).when(context).lookup(jmsCarrierTechnologyParameters.getConnectionFactory()); + Mockito.doReturn(topic).when(context).lookup(jmsCarrierTechnologyParameters.getProducerTopic()); + Mockito.doReturn(connection) + .when(connectionFactory) + .createConnection(jmsCarrierTechnologyParameters.getSecurityPrincipal(), + jmsCarrierTechnologyParameters.getSecurityCredentials()); + Mockito.doNothing().when(connection).start(); + Mockito.doReturn(session).when(connection).createSession(false, Session.AUTO_ACKNOWLEDGE); + Mockito.doReturn(messageProducer).when(session).createProducer(topic); + + apexJmsProducer.init(RandomStringUtils.random(5), producerParameters); + + // Prepare stop mock + Mockito.doThrow(JMSException.class).when(messageProducer).close(); + + // Prepare system.out + final ByteArrayOutputStream testBuffer = new ByteArrayOutputStream(); + System.setOut(new PrintStream(testBuffer)); + + // do the test + assertThatCode(apexJmsProducer::stop).doesNotThrowAnyException(); + assertThat(testBuffer.toString()).contains("failed to close JMS message producer"); + } + + @Test + public void testStopCloseSessionException() throws NamingException, ApexEventException, JMSException { + // Prepare ApexJmsProducer + jmsCarrierTechnologyParameters = new JmsCarrierTechnologyParameters(); + jmsCarrierTechnologyParameters.setObjectMessageSending(false); + producerParameters.setCarrierTechnologyParameters(jmsCarrierTechnologyParameters); + + final InitialContext context = Mockito.mock(InitialContext.class); + final ConnectionFactory connectionFactory = Mockito.mock(ConnectionFactory.class); + final Topic topic = Mockito.mock(Topic.class); + final Connection connection = Mockito.mock(Connection.class); + final Session session = Mockito.mock(Session.class); + final MessageProducer messageProducer = Mockito.mock(MessageProducer.class); + + Mockito.doReturn(context).when(apexJmsProducer).getInitialContext(); + Mockito.doReturn(connectionFactory).when(context).lookup(jmsCarrierTechnologyParameters.getConnectionFactory()); + Mockito.doReturn(topic).when(context).lookup(jmsCarrierTechnologyParameters.getProducerTopic()); + Mockito.doReturn(connection) + .when(connectionFactory) + .createConnection(jmsCarrierTechnologyParameters.getSecurityPrincipal(), + jmsCarrierTechnologyParameters.getSecurityCredentials()); + Mockito.doNothing().when(connection).start(); + Mockito.doReturn(session).when(connection).createSession(false, Session.AUTO_ACKNOWLEDGE); + Mockito.doReturn(messageProducer).when(session).createProducer(topic); + + apexJmsProducer.init(RandomStringUtils.random(5), producerParameters); + + // Prepare stop mocks + Mockito.doNothing().when(messageProducer).close(); + Mockito.doThrow(JMSException.class).when(session).close(); + + // Prepare system.out + final ByteArrayOutputStream testBuffer = new ByteArrayOutputStream(); + System.setOut(new PrintStream(testBuffer)); + + // do the test + assertThatCode(apexJmsProducer::stop).doesNotThrowAnyException(); + assertThat(testBuffer.toString()).contains("failed to close the JMS session"); + } + + @Test + public void testStopCloseConnectionException() throws NamingException, ApexEventException, JMSException { + // Prepare ApexJmsProducer + jmsCarrierTechnologyParameters = new JmsCarrierTechnologyParameters(); + jmsCarrierTechnologyParameters.setObjectMessageSending(false); + producerParameters.setCarrierTechnologyParameters(jmsCarrierTechnologyParameters); + + final InitialContext context = Mockito.mock(InitialContext.class); + final ConnectionFactory connectionFactory = Mockito.mock(ConnectionFactory.class); + final Topic topic = Mockito.mock(Topic.class); + final Connection connection = Mockito.mock(Connection.class); + final Session session = Mockito.mock(Session.class); + final MessageProducer messageProducer = Mockito.mock(MessageProducer.class); + + Mockito.doReturn(context).when(apexJmsProducer).getInitialContext(); + Mockito.doReturn(connectionFactory).when(context).lookup(jmsCarrierTechnologyParameters.getConnectionFactory()); + Mockito.doReturn(topic).when(context).lookup(jmsCarrierTechnologyParameters.getProducerTopic()); + Mockito.doReturn(connection) + .when(connectionFactory) + .createConnection(jmsCarrierTechnologyParameters.getSecurityPrincipal(), + jmsCarrierTechnologyParameters.getSecurityCredentials()); + Mockito.doNothing().when(connection).start(); + Mockito.doReturn(session).when(connection).createSession(false, Session.AUTO_ACKNOWLEDGE); + Mockito.doReturn(messageProducer).when(session).createProducer(topic); + + apexJmsProducer.init(RandomStringUtils.random(5), producerParameters); + + // Prepare stop mocks + Mockito.doNothing().when(messageProducer).close(); + Mockito.doNothing().when(session).close(); + Mockito.doThrow(JMSException.class).when(connection).close(); + + // Prepare system.out + final ByteArrayOutputStream testBuffer = new ByteArrayOutputStream(); + System.setOut(new PrintStream(testBuffer)); + + // do the test + assertThatCode(apexJmsProducer::stop).doesNotThrowAnyException(); + assertThat(testBuffer.toString()).contains("close of connection"); + } + + @Test + public void testStop() throws NamingException, JMSException, ApexEventException { + // Prepare ApexJmsProducer + jmsCarrierTechnologyParameters = new JmsCarrierTechnologyParameters(); + jmsCarrierTechnologyParameters.setObjectMessageSending(false); + producerParameters.setCarrierTechnologyParameters(jmsCarrierTechnologyParameters); + + final InitialContext context = Mockito.mock(InitialContext.class); + final ConnectionFactory connectionFactory = Mockito.mock(ConnectionFactory.class); + final Topic topic = Mockito.mock(Topic.class); + final Connection connection = Mockito.mock(Connection.class); + final Session session = Mockito.mock(Session.class); + final MessageProducer messageProducer = Mockito.mock(MessageProducer.class); + + Mockito.doReturn(context).when(apexJmsProducer).getInitialContext(); + Mockito.doReturn(connectionFactory).when(context).lookup(jmsCarrierTechnologyParameters.getConnectionFactory()); + Mockito.doReturn(topic).when(context).lookup(jmsCarrierTechnologyParameters.getProducerTopic()); + Mockito.doReturn(connection) + .when(connectionFactory) + .createConnection(jmsCarrierTechnologyParameters.getSecurityPrincipal(), + jmsCarrierTechnologyParameters.getSecurityCredentials()); + Mockito.doNothing().when(connection).start(); + Mockito.doReturn(session).when(connection).createSession(false, Session.AUTO_ACKNOWLEDGE); + Mockito.doReturn(messageProducer).when(session).createProducer(topic); + + apexJmsProducer.init(RandomStringUtils.random(5), producerParameters); + + // Prepare stop mocks + Mockito.doNothing().when(messageProducer).close(); + Mockito.doNothing().when(session).close(); + Mockito.doNothing().when(connection).close(); + + // do the test assertThatCode(apexJmsProducer::stop).doesNotThrowAnyException(); } } |