2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021, 2023-2024 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.jupiter.api.Assertions.assertArrayEquals;
26 import static org.junit.jupiter.api.Assertions.assertNull;
27 import static org.junit.jupiter.api.Assertions.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.jupiter.api.AfterEach;
37 import org.junit.jupiter.api.BeforeEach;
38 import org.junit.jupiter.api.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 class Apex2JmsObjectEventConverterTest {
45 private Apex2JmsObjectEventConverter converter;
46 private final PrintStream orgOutBuffer = System.out;
47 private ByteArrayOutputStream testOutStream;
51 converter = new Apex2JmsObjectEventConverter();
52 testOutStream = new ByteArrayOutputStream();
53 System.setOut(new PrintStream(testOutStream));
58 System.setOut(orgOutBuffer);
63 assertThatThrownBy(() -> converter.init(null))
64 .isInstanceOf(NullPointerException.class);
68 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 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 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 void toApexEventNoParams() {
103 final String eventName = RandomStringUtils.randomAlphabetic(4);
104 ObjectMessage object = (ObjectMessage) new ActiveMQObjectMessage();
105 assertThatThrownBy(() -> converter.toApexEvent(eventName, object))
106 .isInstanceOf(ApexEventRuntimeException.class);
110 void toApexEventIncomingObjectIsNull() {
111 final JmsObjectEventProtocolParameters parameters = new JmsObjectEventProtocolParameters();
113 converter.init(parameters);
114 final String eventName = RandomStringUtils.randomAlphabetic(4);
115 ObjectMessage object = (ObjectMessage) new ActiveMQObjectMessage();
116 assertThatThrownBy(() -> converter.toApexEvent(eventName, object))
117 .isInstanceOf(NullPointerException.class);
121 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 = (ObjectMessage) 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 void fromApexEventNull() {
149 assertThatThrownBy(() -> converter.fromApexEvent(null)).isInstanceOf(ApexEventException.class);
153 void fromApexEventEmptyEvent() throws ApexEventException {
154 final ApexEvent apexEvent = new ApexEvent(
155 "a" + RandomStringUtils.randomAlphabetic(3),
156 "a" + RandomStringUtils.randomAlphabetic(3),
157 "a" + RandomStringUtils.randomAlphabetic(3),
160 assertThatThrownBy(() -> converter.fromApexEvent(apexEvent))
161 .isInstanceOf(ApexEventException.class);
165 void fromApexEventMultipleEvents() throws ApexEventException {
166 final ApexEvent apexEvent = new ApexEvent(
167 "a" + RandomStringUtils.randomAlphabetic(3),
168 "a" + RandomStringUtils.randomAlphabetic(4),
169 "a" + RandomStringUtils.randomAlphabetic(5),
172 apexEvent.put(RandomStringUtils.randomAlphabetic(2), new Object());
173 apexEvent.put(RandomStringUtils.randomAlphabetic(6), new Object());
174 assertThatThrownBy(() -> converter.fromApexEvent(apexEvent)).isInstanceOf(ApexEventException.class);
178 void fromApexEventSingleEvent() throws ApexEventException {
179 final ApexEvent apexEvent = new ApexEvent(
180 "a" + RandomStringUtils.randomAlphabetic(3),
181 "a" + RandomStringUtils.randomAlphabetic(3),
182 "a" + RandomStringUtils.randomAlphabetic(3),
186 final Object expected = new Object();
187 apexEvent.put(RandomStringUtils.randomAlphabetic(2), expected);
189 final Object actual = converter.fromApexEvent(apexEvent);
191 assertSame(expected, actual);