2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021, 2023 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;
29 import jakarta.jms.JMSException;
30 import jakarta.jms.ObjectMessage;
31 import java.io.ByteArrayOutputStream;
32 import java.io.PrintStream;
33 import java.util.List;
34 import org.apache.activemq.command.ActiveMQObjectMessage;
35 import org.apache.commons.lang3.RandomStringUtils;
36 import org.junit.After;
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.onap.policy.apex.service.engine.event.ApexEvent;
40 import org.onap.policy.apex.service.engine.event.ApexEventException;
41 import org.onap.policy.apex.service.engine.event.ApexEventRuntimeException;
42 import org.onap.policy.apex.service.engine.event.impl.apexprotocolplugin.ApexEventProtocolParameters;
44 public class Apex2JmsObjectEventConverterTest {
45 private Apex2JmsObjectEventConverter converter;
46 private final PrintStream orgOutBuffer = System.out;
47 private ByteArrayOutputStream testOutStream;
50 public void setUp() throws Exception {
51 converter = new Apex2JmsObjectEventConverter();
52 testOutStream = new ByteArrayOutputStream();
53 System.setOut(new PrintStream(testOutStream));
57 public void tearDown() {
58 System.setOut(orgOutBuffer);
62 public void initNull() {
63 assertThatThrownBy(() -> converter.init(null))
64 .isInstanceOf(NullPointerException.class);
68 public void initWrongClass() {
69 converter.init(new ApexEventProtocolParameters());
70 final String actual = testOutStream.toString();
71 assertThat(actual).contains("specified Event Protocol Parameters properties of typ");
72 assertNull(converter.getEventProtocolParameters());
77 final JmsObjectEventProtocolParameters parameters = new JmsObjectEventProtocolParameters();
78 converter.init(parameters);
79 final JmsObjectEventProtocolParameters actual = converter.getEventProtocolParameters();
80 assertSame(parameters, actual);
84 public void toApexEventNull() {
85 final JmsObjectEventProtocolParameters parameters = new JmsObjectEventProtocolParameters();
86 converter.init(parameters);
87 final String eventName = RandomStringUtils.randomAlphabetic(4);
88 assertThatThrownBy(() -> converter.toApexEvent(eventName, null))
89 .isInstanceOf(ApexEventRuntimeException.class);
93 public void toApexEventObject() {
94 final JmsObjectEventProtocolParameters parameters = new JmsObjectEventProtocolParameters();
95 converter.init(parameters);
96 final String eventName = RandomStringUtils.randomAlphabetic(4);
97 assertThatThrownBy(() -> converter.toApexEvent(eventName, new Object()))
98 .isInstanceOf(ApexEventRuntimeException.class);
102 public void toApexEventNoParams() {
103 final String eventName = RandomStringUtils.randomAlphabetic(4);
104 ObjectMessage object = new ActiveMQObjectMessage();
105 assertThatThrownBy(() -> converter.toApexEvent(eventName, object))
106 .isInstanceOf(ApexEventRuntimeException.class);
110 public void toApexEventIncomingObjectIsNull() {
111 final JmsObjectEventProtocolParameters parameters = new JmsObjectEventProtocolParameters();
113 converter.init(parameters);
114 final String eventName = RandomStringUtils.randomAlphabetic(4);
115 ObjectMessage object = new ActiveMQObjectMessage();
116 assertThatThrownBy(() -> converter.toApexEvent(eventName, object))
117 .isInstanceOf(NullPointerException.class);
121 public void toApexEvent() throws ApexEventException, JMSException {
122 final JmsObjectEventProtocolParameters parameters = new JmsObjectEventProtocolParameters();
124 converter.init(parameters);
125 final String eventName = RandomStringUtils.randomAlphabetic(4);
126 final ObjectMessage object = new ActiveMQObjectMessage();
127 final String value = RandomStringUtils.randomAlphabetic(3);
128 object.setObject(value);
130 // Prepare expected object
131 final ApexEvent expectedEvent = new ApexEvent("String" + parameters.getIncomingEventSuffix(),
132 parameters.getIncomingEventVersion(),
134 parameters.getIncomingEventSource(),
135 parameters.getIncomingEventTarget());
136 // Overwrite executionId to match executionId of actual
137 expectedEvent.setExecutionId(1);
138 final Object[] expected = {expectedEvent};
141 final List<ApexEvent> actual = converter.toApexEvent(eventName, object);
142 // Overwrite executionId to match executionId of expected
143 actual.get(0).setExecutionId(1);
144 assertArrayEquals(expected, actual.toArray());
148 public void fromApexEventNull() {
149 assertThatThrownBy(() -> converter.fromApexEvent(null))
150 .isInstanceOf(ApexEventException.class);
154 public void fromApexEventEmptyEvent() throws ApexEventException {
155 final ApexEvent apexEvent = new ApexEvent(
156 "a" + RandomStringUtils.randomAlphabetic(3),
157 "a" + RandomStringUtils.randomAlphabetic(3),
158 "a" + RandomStringUtils.randomAlphabetic(3),
161 assertThatThrownBy(() -> converter.fromApexEvent(apexEvent))
162 .isInstanceOf(ApexEventException.class);
166 public void fromApexEventMultipleEvents() throws ApexEventException {
167 final ApexEvent apexEvent = new ApexEvent(
168 "a" + RandomStringUtils.randomAlphabetic(3),
169 "a" + RandomStringUtils.randomAlphabetic(4),
170 "a" + RandomStringUtils.randomAlphabetic(5),
173 apexEvent.put(RandomStringUtils.randomAlphabetic(2), new Object());
174 apexEvent.put(RandomStringUtils.randomAlphabetic(6), new Object());
175 assertThatThrownBy(() -> converter.fromApexEvent(apexEvent)).isInstanceOf(ApexEventException.class);
179 public void fromApexEventSingleEvent() throws ApexEventException {
180 final ApexEvent apexEvent = new ApexEvent(
181 "a" + RandomStringUtils.randomAlphabetic(3),
182 "a" + RandomStringUtils.randomAlphabetic(3),
183 "a" + RandomStringUtils.randomAlphabetic(3),
187 final Object expected = new Object();
188 apexEvent.put(RandomStringUtils.randomAlphabetic(2), expected);
190 final Object actual = converter.fromApexEvent(apexEvent);
192 assertSame(expected, actual);