diff options
Diffstat (limited to 'plugins')
11 files changed, 1317 insertions, 70 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(); } } diff --git a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-kafka/src/main/java/org/onap/policy/apex/plugins/event/carrier/kafka/KafkaCarrierTechnologyParameters.java b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-kafka/src/main/java/org/onap/policy/apex/plugins/event/carrier/kafka/KafkaCarrierTechnologyParameters.java index b37455d6e..eb1f15c93 100644 --- a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-kafka/src/main/java/org/onap/policy/apex/plugins/event/carrier/kafka/KafkaCarrierTechnologyParameters.java +++ b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-kafka/src/main/java/org/onap/policy/apex/plugins/event/carrier/kafka/KafkaCarrierTechnologyParameters.java @@ -2,6 +2,7 @@ * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. * Modifications Copyright (C) 2019 Nordix Foundation. + * Modifications Copyright (C) 2021 Bell Canada. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -281,9 +282,10 @@ public class KafkaCarrierTechnologyParameters extends CarrierTechnologyParameter ENTRY + i + " invalid, key is null or blank"); } - if (StringUtils.isBlank(kafkaProperties[i][1])) { + // the value of a property has to be specified as empty in some cases, but should never be null. + if (null == kafkaProperties[i][1]) { result.setResult(KAFKA_PROPERTIES, ValidationStatus.INVALID, - ENTRY + i + " invalid, value is null or blank"); + ENTRY + i + " invalid, value is null"); } } } diff --git a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-kafka/src/test/java/org/onap/policy/apex/plugins/event/carrier/kafka/KafkaCarrierTechnologyParametersTest.java b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-kafka/src/test/java/org/onap/policy/apex/plugins/event/carrier/kafka/KafkaCarrierTechnologyParametersTest.java index 9287f52ae..14f13bc97 100644 --- a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-kafka/src/test/java/org/onap/policy/apex/plugins/event/carrier/kafka/KafkaCarrierTechnologyParametersTest.java +++ b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-kafka/src/test/java/org/onap/policy/apex/plugins/event/carrier/kafka/KafkaCarrierTechnologyParametersTest.java @@ -2,6 +2,7 @@ * ============LICENSE_START======================================================= * Copyright (C) 2019 Samsung. All rights reserved. * Modifications Copyright (C) 2019 Nordix Foundation. + * Modifications Copyright (C) 2021 Bell Canada. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -275,6 +276,18 @@ public class KafkaCarrierTechnologyParametersTest { kafkaCarrierTechnologyParameters.setKafkaProperties(kafkaProperties2); assertFalse(kafkaCarrierTechnologyParameters.validate().isValid()); + + // @formatter:off + String[][] kafkaPropertiesWithEmptyValue = { + { + "Property1", "" + } + }; + // @formatter:on + + kafkaCarrierTechnologyParameters.setKafkaProperties(kafkaPropertiesWithEmptyValue); + assertTrue(kafkaCarrierTechnologyParameters.validate().isValid()); + kafkaCarrierTechnologyParameters.setKafkaProperties(origKafkaProperties); assertTrue(kafkaCarrierTechnologyParameters.validate().isValid()); diff --git a/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-jms/pom.xml b/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-jms/pom.xml index d9f800e12..6e562a812 100644 --- a/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-jms/pom.xml +++ b/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-jms/pom.xml @@ -1,6 +1,7 @@ <!-- ============LICENSE_START======================================================= Copyright (C) 2018 Ericsson. All rights reserved. + Modifications Copyright (C) 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. @@ -17,8 +18,17 @@ SPDX-License-Identifier: Apache-2.0 ============LICENSE_END========================================================= --> -<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> + <dependencies> + <dependency> + <groupId>org.apache.activemq</groupId> + <artifactId>activemq-client</artifactId> + <version>5.16.1</version> + <scope>test</scope> + </dependency> + </dependencies> <parent> <groupId>org.onap.policy.apex-pdp.plugins.plugins-event.plugins-event-protocol</groupId> <artifactId>plugins-event-protocol</artifactId> @@ -27,5 +37,6 @@ <artifactId>plugins-event-protocol-jms</artifactId> <name>${project.artifactId}</name> - <description>[${project.parent.artifactId}] Plugins for handling events that are being transported as JMS messages</description> + <description>[${project.parent.artifactId}] Plugins for handling events that are being transported as JMS messages + </description> </project>
\ No newline at end of file diff --git a/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-jms/src/main/java/org/onap/policy/apex/plugins/event/protocol/jms/Apex2JmsObjectEventConverter.java b/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-jms/src/main/java/org/onap/policy/apex/plugins/event/protocol/jms/Apex2JmsObjectEventConverter.java index 89e2e8110..924765dc5 100644 --- a/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-jms/src/main/java/org/onap/policy/apex/plugins/event/protocol/jms/Apex2JmsObjectEventConverter.java +++ b/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-jms/src/main/java/org/onap/policy/apex/plugins/event/protocol/jms/Apex2JmsObjectEventConverter.java @@ -1,7 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2019 Nordix Foundation. + * Modifications Copyright (C) 2019-2021 Nordix Foundation. * Modifications Copyright (C) 2021 Bell Canada. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -141,4 +141,13 @@ public final class Apex2JmsObjectEventConverter implements ApexEventProtocolConv // Return the single object from the Apex event message return apexEvent.values().iterator().next(); } + + /** + * Returns eventProtocolParameters field. Getter used only for testing + * + * @return eventProtocolParameters + */ + JmsObjectEventProtocolParameters getEventProtocolParameters() { + return eventProtocolParameters; + } } diff --git a/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-jms/src/main/test/org/onap/policy/apex/plugins/event/protocol/jms/Apex2JmsObjectEventConverterTest.java b/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-jms/src/main/test/org/onap/policy/apex/plugins/event/protocol/jms/Apex2JmsObjectEventConverterTest.java new file mode 100644 index 000000000..70c17c34e --- /dev/null +++ b/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-jms/src/main/test/org/onap/policy/apex/plugins/event/protocol/jms/Apex2JmsObjectEventConverterTest.java @@ -0,0 +1,195 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.apex.plugins.event.protocol.jms; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertSame; + + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.util.List; +import javax.jms.JMSException; +import javax.jms.ObjectMessage; +import org.apache.activemq.command.ActiveMQObjectMessage; +import org.apache.commons.lang3.RandomStringUtils; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +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.ApexEventRuntimeException; +import org.onap.policy.apex.service.engine.event.impl.apexprotocolplugin.ApexEventProtocolParameters; + +public class Apex2JmsObjectEventConverterTest { + private Apex2JmsObjectEventConverter converter; + private final PrintStream orgOutBuffer = System.out; + private ByteArrayOutputStream testOutStream; + + @Before + public void setUp() throws Exception { + converter = new Apex2JmsObjectEventConverter(); + testOutStream = new ByteArrayOutputStream(); + System.setOut(new PrintStream(testOutStream)); + } + + @After + public void tearDown() { + System.setOut(orgOutBuffer); + } + + @Test + public void initNull() { + assertThatThrownBy(() -> converter.init(null)) + .isInstanceOf(NullPointerException.class); + } + + @Test + public void initWrongClass() { + converter.init(new ApexEventProtocolParameters()); + final String actual = testOutStream.toString(); + assertThat(actual).contains("specified Event Protocol Parameters properties of typ"); + assertNull(converter.getEventProtocolParameters()); + } + + @Test + public void init() { + final JmsObjectEventProtocolParameters parameters = new JmsObjectEventProtocolParameters(); + converter.init(parameters); + final JmsObjectEventProtocolParameters actual = converter.getEventProtocolParameters(); + assertSame(parameters, actual); + } + + @Test + public void toApexEventNull() { + final JmsObjectEventProtocolParameters parameters = new JmsObjectEventProtocolParameters(); + converter.init(parameters); + final String eventName = RandomStringUtils.randomAlphabetic(4); + assertThatThrownBy(() -> converter.toApexEvent(eventName, null)) + .isInstanceOf(ApexEventRuntimeException.class); + } + + @Test + public void toApexEventObject() { + final JmsObjectEventProtocolParameters parameters = new JmsObjectEventProtocolParameters(); + converter.init(parameters); + final String eventName = RandomStringUtils.randomAlphabetic(4); + assertThatThrownBy(() -> converter.toApexEvent(eventName, new Object())) + .isInstanceOf(ApexEventRuntimeException.class); + } + + @Test + public void toApexEventNoParams() { + final String eventName = RandomStringUtils.randomAlphabetic(4); + ObjectMessage object = new ActiveMQObjectMessage(); + assertThatThrownBy(() -> converter.toApexEvent(eventName, object)) + .isInstanceOf(ApexEventRuntimeException.class); + } + + @Test + public void toApexEventIncomingObjectIsNull() { + final JmsObjectEventProtocolParameters parameters = new JmsObjectEventProtocolParameters(); + + converter.init(parameters); + final String eventName = RandomStringUtils.randomAlphabetic(4); + ObjectMessage object = new ActiveMQObjectMessage(); + assertThatThrownBy(() -> converter.toApexEvent(eventName, object)) + .isInstanceOf(NullPointerException.class); + } + + @Test + public void toApexEvent() throws ApexEventException, JMSException { + final JmsObjectEventProtocolParameters parameters = new JmsObjectEventProtocolParameters(); + + converter.init(parameters); + final String eventName = RandomStringUtils.randomAlphabetic(4); + final ObjectMessage object = new ActiveMQObjectMessage(); + final String value = RandomStringUtils.randomAlphabetic(3); + object.setObject(value); + + // Prepare expected object + final ApexEvent expectedEvent = new ApexEvent("String" + parameters.getIncomingEventSuffix(), + parameters.getIncomingEventVersion(), + "java.lang", + parameters.getIncomingEventSource(), + parameters.getIncomingEventTarget()); + // Overwrite executionId to match executionId of actual + expectedEvent.setExecutionId(1); + final Object[] expected = {expectedEvent}; + + // Run tested method + final List<ApexEvent> actual = converter.toApexEvent(eventName, object); + // Overwrite executionId to match executionId of expected + actual.get(0).setExecutionId(1); + assertArrayEquals(expected, actual.toArray()); + } + + @Test + public void fromApexEventNull() { + assertThatThrownBy(() -> converter.fromApexEvent(null)) + .isInstanceOf(ApexEventException.class); + } + + @Test + public void fromApexEventEmptyEvent() throws ApexEventException { + final ApexEvent apexEvent = new ApexEvent( + "a" + RandomStringUtils.randomAlphabetic(3), + "a" + RandomStringUtils.randomAlphabetic(3), + "a" + RandomStringUtils.randomAlphabetic(3), + "", + ""); + assertThatThrownBy(() -> converter.fromApexEvent(apexEvent)) + .isInstanceOf(ApexEventException.class); + } + + @Test + public void fromApexEventMultipleEvents() throws ApexEventException { + final ApexEvent apexEvent = new ApexEvent( + "a" + RandomStringUtils.randomAlphabetic(3), + "a" + RandomStringUtils.randomAlphabetic(4), + "a" + RandomStringUtils.randomAlphabetic(5), + "", + ""); + apexEvent.put(RandomStringUtils.randomAlphabetic(2), new Object()); + apexEvent.put(RandomStringUtils.randomAlphabetic(6), new Object()); + assertThatThrownBy(() -> converter.fromApexEvent(apexEvent)).isInstanceOf(ApexEventException.class); + } + + @Test + public void fromApexEventSingleEvent() throws ApexEventException { + final ApexEvent apexEvent = new ApexEvent( + "a" + RandomStringUtils.randomAlphabetic(3), + "a" + RandomStringUtils.randomAlphabetic(3), + "a" + RandomStringUtils.randomAlphabetic(3), + "", + ""); + + final Object expected = new Object(); + apexEvent.put(RandomStringUtils.randomAlphabetic(2), expected); + + final Object actual = converter.fromApexEvent(apexEvent); + + assertSame(expected, actual); + } +} diff --git a/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-jms/src/main/test/org/onap/policy/apex/plugins/event/protocol/jms/Apex2JmsTextEventConverterTest.java b/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-jms/src/main/test/org/onap/policy/apex/plugins/event/protocol/jms/Apex2JmsTextEventConverterTest.java new file mode 100644 index 000000000..9f68b49df --- /dev/null +++ b/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-jms/src/main/test/org/onap/policy/apex/plugins/event/protocol/jms/Apex2JmsTextEventConverterTest.java @@ -0,0 +1,149 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.apex.plugins.event.protocol.jms; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; + + +import java.util.List; +import org.apache.commons.lang3.RandomStringUtils; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.onap.policy.apex.context.parameters.ContextParameterConstants; +import org.onap.policy.apex.context.parameters.SchemaParameters; +import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; +import org.onap.policy.apex.model.basicmodel.service.ModelService; +import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema; +import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas; +import org.onap.policy.apex.model.eventmodel.concepts.AxEvent; +import org.onap.policy.apex.model.eventmodel.concepts.AxEvents; +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.ApexEventRuntimeException; +import org.onap.policy.apex.service.engine.event.impl.filecarrierplugin.consumer.TextBlock; +import org.onap.policy.apex.service.engine.event.impl.jsonprotocolplugin.JsonEventProtocolParameters; +import org.onap.policy.common.parameters.ParameterService; + +public class Apex2JmsTextEventConverterTest { + private Apex2JmsTextEventConverter converter; + + @Before + public void setUp() { + converter = new Apex2JmsTextEventConverter(); + ModelService.registerModel(AxContextSchemas.class, new AxContextSchemas()); + ModelService.registerModel(AxEvents.class, new AxEvents()); + ParameterService.register(new SchemaParameters()); + } + + @After + public void tearDown() { + ModelService.deregisterModel(AxContextSchema.class); + ModelService.deregisterModel(AxEvents.class); + ParameterService.deregister(ContextParameterConstants.SCHEMA_GROUP_NAME); + } + + @Test + public void toApexEventNull() { + final String eventName = RandomStringUtils.randomAlphabetic(4); + assertThatThrownBy(() -> converter.toApexEvent(eventName, null)) + .isInstanceOf(ApexEventRuntimeException.class); + } + + @Test + public void toApexEventObject() { + final String eventName = RandomStringUtils.randomAlphabetic(4); + assertThatThrownBy(() -> converter.toApexEvent(eventName, new Object())) + .isInstanceOf(ApexEventRuntimeException.class); + } + + @Test + public void toApexEventJsonString() throws ApexEventException { + final String eventName = RandomStringUtils.randomAlphabetic(4); + final String eventVersion = "0.0.1"; + final String source = RandomStringUtils.randomAlphabetic(5); + final String target = RandomStringUtils.randomAlphabetic(6); + final String nameSpace = "a.name.space"; + + // Prepare Json String to be translated into ApexEvent + final TextBlock object = + new TextBlock(false, "{\"name\": \"" + eventName + "\", \"version\":\"" + eventVersion + "\"}"); + + // Prepare Model service + final AxArtifactKey eventKey = new AxArtifactKey(eventName + ":" + eventVersion); + final AxEvent axEvent = new AxEvent(eventKey, nameSpace, source, target); + ModelService.getModel(AxEvents.class).getEventMap().put(eventKey, axEvent); + + // prepare converter + converter.init(new JsonEventProtocolParameters()); + + // execute test + final List<ApexEvent> apexEvents = converter.toApexEvent(eventName, object); + + final ApexEvent expectedEvent = new ApexEvent(eventName, eventVersion, nameSpace, source, target); + + // Reset executionId + expectedEvent.setExecutionId(0); + for (ApexEvent event : apexEvents) { + event.setExecutionId(0); + } + Object[] expected = {expectedEvent}; + + assertArrayEquals(expected, apexEvents.toArray()); + } + + @Test + public void fromApexNull() { + assertThatThrownBy(() -> converter.fromApexEvent(null)).isInstanceOf(ApexEventException.class); + } + + @Test + public void fromApex() throws ApexEventException { + final String name = RandomStringUtils.randomAlphabetic(4); + final String version = "0.2.3"; + final String nameSpace = "a.name.space"; + final String source = RandomStringUtils.randomAlphabetic(6); + final String target = RandomStringUtils.randomAlphabetic(7); + + final String expected = "{\n" + + " \"name\": \"" + name + "\",\n" + + " \"version\": \"" + version + "\",\n" + + " \"nameSpace\": \"" + nameSpace + "\",\n" + + " \"source\": \"" + source + "\",\n" + + " \"target\": \"" + target + "\"\n" + + "}"; + + // Prepare Model service + final AxArtifactKey eventKey = new AxArtifactKey(name + ":" + version); + final AxEvent axEvent = new AxEvent(eventKey, nameSpace, source, target); + ModelService.getModel(AxEvents.class).getEventMap().put(eventKey, axEvent); + + converter.init(new JsonEventProtocolParameters()); + + final ApexEvent apexEvent = new ApexEvent(name, version, nameSpace, source, target); + final Object actual = converter.fromApexEvent(apexEvent); + + assertEquals(expected, actual); + } + +} diff --git a/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-jms/src/main/test/org/onap/policy/apex/plugins/event/protocol/jms/JmsObjectEventProtocolParametersTest.java b/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-jms/src/main/test/org/onap/policy/apex/plugins/event/protocol/jms/JmsObjectEventProtocolParametersTest.java new file mode 100644 index 000000000..8baa9d5b1 --- /dev/null +++ b/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-jms/src/main/test/org/onap/policy/apex/plugins/event/protocol/jms/JmsObjectEventProtocolParametersTest.java @@ -0,0 +1,53 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.apex.plugins.event.protocol.jms; + +import static org.junit.Assert.assertEquals; + + +import org.junit.Test; + +public class JmsObjectEventProtocolParametersTest { + + @Test + public void getIncomingEventVersion() { + final JmsObjectEventProtocolParameters jmsObjectEventProtocolParameters = + new JmsObjectEventProtocolParameters(); + final String actual = jmsObjectEventProtocolParameters.getIncomingEventVersion(); + assertEquals("1.0.0", actual); + } + + @Test + public void getIncomingEventSource() { + final JmsObjectEventProtocolParameters jmsObjectEventProtocolParameters = + new JmsObjectEventProtocolParameters(); + final String actual = jmsObjectEventProtocolParameters.getIncomingEventSource(); + assertEquals("JMS", actual); + } + + @Test + public void getIncomingEventTarget() { + final JmsObjectEventProtocolParameters jmsObjectEventProtocolParameters = + new JmsObjectEventProtocolParameters(); + final String actual = jmsObjectEventProtocolParameters.getIncomingEventTarget(); + assertEquals("Apex", actual); + } +} |