701326f712a2904ab3a046e6098f2b15db01b924
[policy/apex-pdp.git] /
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.plugins.event.protocol.jms;
22
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;
28
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;
43
44 public class Apex2JmsObjectEventConverterTest {
45     private Apex2JmsObjectEventConverter converter;
46     private final PrintStream orgOutBuffer = System.out;
47     private ByteArrayOutputStream testOutStream;
48
49     @Before
50     public void setUp() throws Exception {
51         converter = new Apex2JmsObjectEventConverter();
52         testOutStream = new ByteArrayOutputStream();
53         System.setOut(new PrintStream(testOutStream));
54     }
55
56     @After
57     public void tearDown() {
58         System.setOut(orgOutBuffer);
59     }
60
61     @Test
62     public void initNull() {
63         assertThatThrownBy(() -> converter.init(null))
64             .isInstanceOf(NullPointerException.class);
65     }
66
67     @Test
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());
73     }
74
75     @Test
76     public void init() {
77         final JmsObjectEventProtocolParameters parameters = new JmsObjectEventProtocolParameters();
78         converter.init(parameters);
79         final JmsObjectEventProtocolParameters actual = converter.getEventProtocolParameters();
80         assertSame(parameters, actual);
81     }
82
83     @Test
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);
90     }
91
92     @Test
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);
99     }
100
101     @Test
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);
107     }
108
109     @Test
110     public void toApexEventIncomingObjectIsNull() {
111         final JmsObjectEventProtocolParameters parameters = new JmsObjectEventProtocolParameters();
112
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);
118     }
119
120     @Test
121     public void toApexEvent() throws ApexEventException, JMSException {
122         final JmsObjectEventProtocolParameters parameters = new JmsObjectEventProtocolParameters();
123
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);
129
130         // Prepare expected object
131         final ApexEvent expectedEvent = new ApexEvent("String" + parameters.getIncomingEventSuffix(),
132             parameters.getIncomingEventVersion(),
133             "java.lang",
134             parameters.getIncomingEventSource(),
135             parameters.getIncomingEventTarget());
136         // Overwrite executionId to match executionId of actual
137         expectedEvent.setExecutionId(1);
138         final Object[] expected = {expectedEvent};
139
140         // Run tested method
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());
145     }
146
147     @Test
148     public void fromApexEventNull() {
149         assertThatThrownBy(() -> converter.fromApexEvent(null))
150             .isInstanceOf(ApexEventException.class);
151     }
152
153     @Test
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),
159             "",
160             "");
161         assertThatThrownBy(() -> converter.fromApexEvent(apexEvent))
162             .isInstanceOf(ApexEventException.class);
163     }
164
165     @Test
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),
171             "",
172             "");
173         apexEvent.put(RandomStringUtils.randomAlphabetic(2), new Object());
174         apexEvent.put(RandomStringUtils.randomAlphabetic(6), new Object());
175         assertThatThrownBy(() -> converter.fromApexEvent(apexEvent)).isInstanceOf(ApexEventException.class);
176     }
177
178     @Test
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),
184             "",
185             "");
186
187         final Object expected = new Object();
188         apexEvent.put(RandomStringUtils.randomAlphabetic(2), expected);
189
190         final Object actual = converter.fromApexEvent(apexEvent);
191
192         assertSame(expected, actual);
193     }
194 }