2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 Nordix Foundation.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.plugins.event.protocol.jms;
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertArrayEquals;
26 import static org.junit.Assert.assertNull;
27 import static org.junit.Assert.assertSame;
30 import java.io.ByteArrayOutputStream;
31 import java.io.PrintStream;
32 import java.util.List;
33 import javax.jms.JMSException;
34 import javax.jms.ObjectMessage;
35 import org.apache.activemq.command.ActiveMQObjectMessage;
36 import org.apache.commons.lang3.RandomStringUtils;
37 import org.junit.After;
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.onap.policy.apex.service.engine.event.ApexEvent;
41 import org.onap.policy.apex.service.engine.event.ApexEventException;
42 import org.onap.policy.apex.service.engine.event.ApexEventRuntimeException;
43 import org.onap.policy.apex.service.engine.event.impl.apexprotocolplugin.ApexEventProtocolParameters;
45 public class Apex2JmsObjectEventConverterTest {
46 private Apex2JmsObjectEventConverter converter;
47 private final PrintStream orgOutBuffer = System.out;
48 private ByteArrayOutputStream testOutStream;
51 public void setUp() throws Exception {
52 converter = new Apex2JmsObjectEventConverter();
53 testOutStream = new ByteArrayOutputStream();
54 System.setOut(new PrintStream(testOutStream));
58 public void tearDown() {
59 System.setOut(orgOutBuffer);
63 public void initNull() {
64 assertThatThrownBy(() -> converter.init(null))
65 .isInstanceOf(NullPointerException.class);
69 public void initWrongClass() {
70 converter.init(new ApexEventProtocolParameters());
71 final String actual = testOutStream.toString();
72 assertThat(actual).contains("specified Event Protocol Parameters properties of typ");
73 assertNull(converter.getEventProtocolParameters());
78 final JmsObjectEventProtocolParameters parameters = new JmsObjectEventProtocolParameters();
79 converter.init(parameters);
80 final JmsObjectEventProtocolParameters actual = converter.getEventProtocolParameters();
81 assertSame(parameters, actual);
85 public void toApexEventNull() {
86 final JmsObjectEventProtocolParameters parameters = new JmsObjectEventProtocolParameters();
87 converter.init(parameters);
88 final String eventName = RandomStringUtils.randomAlphabetic(4);
89 assertThatThrownBy(() -> converter.toApexEvent(eventName, null))
90 .isInstanceOf(ApexEventRuntimeException.class);
94 public void toApexEventObject() {
95 final JmsObjectEventProtocolParameters parameters = new JmsObjectEventProtocolParameters();
96 converter.init(parameters);
97 final String eventName = RandomStringUtils.randomAlphabetic(4);
98 assertThatThrownBy(() -> converter.toApexEvent(eventName, new Object()))
99 .isInstanceOf(ApexEventRuntimeException.class);
103 public void toApexEventNoParams() {
104 final String eventName = RandomStringUtils.randomAlphabetic(4);
105 ObjectMessage object = new ActiveMQObjectMessage();
106 assertThatThrownBy(() -> converter.toApexEvent(eventName, object))
107 .isInstanceOf(ApexEventRuntimeException.class);
111 public void toApexEventIncomingObjectIsNull() {
112 final JmsObjectEventProtocolParameters parameters = new JmsObjectEventProtocolParameters();
114 converter.init(parameters);
115 final String eventName = RandomStringUtils.randomAlphabetic(4);
116 ObjectMessage object = new ActiveMQObjectMessage();
117 assertThatThrownBy(() -> converter.toApexEvent(eventName, object))
118 .isInstanceOf(NullPointerException.class);
122 public void toApexEvent() throws ApexEventException, JMSException {
123 final JmsObjectEventProtocolParameters parameters = new JmsObjectEventProtocolParameters();
125 converter.init(parameters);
126 final String eventName = RandomStringUtils.randomAlphabetic(4);
127 final ObjectMessage object = new ActiveMQObjectMessage();
128 final String value = RandomStringUtils.randomAlphabetic(3);
129 object.setObject(value);
131 // Prepare expected object
132 final ApexEvent expectedEvent = new ApexEvent("String" + parameters.getIncomingEventSuffix(),
133 parameters.getIncomingEventVersion(),
135 parameters.getIncomingEventSource(),
136 parameters.getIncomingEventTarget());
137 // Overwrite executionId to match executionId of actual
138 expectedEvent.setExecutionId(1);
139 final Object[] expected = {expectedEvent};
142 final List<ApexEvent> actual = converter.toApexEvent(eventName, object);
143 // Overwrite executionId to match executionId of expected
144 actual.get(0).setExecutionId(1);
145 assertArrayEquals(expected, actual.toArray());
149 public void fromApexEventNull() {
150 assertThatThrownBy(() -> converter.fromApexEvent(null))
151 .isInstanceOf(ApexEventException.class);
155 public void fromApexEventEmptyEvent() throws ApexEventException {
156 final ApexEvent apexEvent = new ApexEvent(
157 "a" + RandomStringUtils.randomAlphabetic(3),
158 "a" + RandomStringUtils.randomAlphabetic(3),
159 "a" + RandomStringUtils.randomAlphabetic(3),
162 assertThatThrownBy(() -> converter.fromApexEvent(apexEvent))
163 .isInstanceOf(ApexEventException.class);
167 public void fromApexEventMultipleEvents() throws ApexEventException {
168 final ApexEvent apexEvent = new ApexEvent(
169 "a" + RandomStringUtils.randomAlphabetic(3),
170 "a" + RandomStringUtils.randomAlphabetic(4),
171 "a" + RandomStringUtils.randomAlphabetic(5),
174 apexEvent.put(RandomStringUtils.randomAlphabetic(2), new Object());
175 apexEvent.put(RandomStringUtils.randomAlphabetic(6), new Object());
176 assertThatThrownBy(() -> converter.fromApexEvent(apexEvent)).isInstanceOf(ApexEventException.class);
180 public void fromApexEventSingleEvent() throws ApexEventException {
181 final ApexEvent apexEvent = new ApexEvent(
182 "a" + RandomStringUtils.randomAlphabetic(3),
183 "a" + RandomStringUtils.randomAlphabetic(3),
184 "a" + RandomStringUtils.randomAlphabetic(3),
188 final Object expected = new Object();
189 apexEvent.put(RandomStringUtils.randomAlphabetic(2), expected);
191 final Object actual = converter.fromApexEvent(apexEvent);
193 assertSame(expected, actual);