70c17c34efc24a7d26d33557f63770f80d26cb7a
[policy/apex-pdp.git] /
1 /*-
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
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
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;
44
45 public class Apex2JmsObjectEventConverterTest {
46     private Apex2JmsObjectEventConverter converter;
47     private final PrintStream orgOutBuffer = System.out;
48     private ByteArrayOutputStream testOutStream;
49
50     @Before
51     public void setUp() throws Exception {
52         converter = new Apex2JmsObjectEventConverter();
53         testOutStream = new ByteArrayOutputStream();
54         System.setOut(new PrintStream(testOutStream));
55     }
56
57     @After
58     public void tearDown() {
59         System.setOut(orgOutBuffer);
60     }
61
62     @Test
63     public void initNull() {
64         assertThatThrownBy(() -> converter.init(null))
65             .isInstanceOf(NullPointerException.class);
66     }
67
68     @Test
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());
74     }
75
76     @Test
77     public void init() {
78         final JmsObjectEventProtocolParameters parameters = new JmsObjectEventProtocolParameters();
79         converter.init(parameters);
80         final JmsObjectEventProtocolParameters actual = converter.getEventProtocolParameters();
81         assertSame(parameters, actual);
82     }
83
84     @Test
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);
91     }
92
93     @Test
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);
100     }
101
102     @Test
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);
108     }
109
110     @Test
111     public void toApexEventIncomingObjectIsNull() {
112         final JmsObjectEventProtocolParameters parameters = new JmsObjectEventProtocolParameters();
113
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);
119     }
120
121     @Test
122     public void toApexEvent() throws ApexEventException, JMSException {
123         final JmsObjectEventProtocolParameters parameters = new JmsObjectEventProtocolParameters();
124
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);
130
131         // Prepare expected object
132         final ApexEvent expectedEvent = new ApexEvent("String" + parameters.getIncomingEventSuffix(),
133             parameters.getIncomingEventVersion(),
134             "java.lang",
135             parameters.getIncomingEventSource(),
136             parameters.getIncomingEventTarget());
137         // Overwrite executionId to match executionId of actual
138         expectedEvent.setExecutionId(1);
139         final Object[] expected = {expectedEvent};
140
141         // Run tested method
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());
146     }
147
148     @Test
149     public void fromApexEventNull() {
150         assertThatThrownBy(() -> converter.fromApexEvent(null))
151             .isInstanceOf(ApexEventException.class);
152     }
153
154     @Test
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),
160             "",
161             "");
162         assertThatThrownBy(() -> converter.fromApexEvent(apexEvent))
163             .isInstanceOf(ApexEventException.class);
164     }
165
166     @Test
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),
172             "",
173             "");
174         apexEvent.put(RandomStringUtils.randomAlphabetic(2), new Object());
175         apexEvent.put(RandomStringUtils.randomAlphabetic(6), new Object());
176         assertThatThrownBy(() -> converter.fromApexEvent(apexEvent)).isInstanceOf(ApexEventException.class);
177     }
178
179     @Test
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),
185             "",
186             "");
187
188         final Object expected = new Object();
189         apexEvent.put(RandomStringUtils.randomAlphabetic(2), expected);
190
191         final Object actual = converter.fromApexEvent(apexEvent);
192
193         assertSame(expected, actual);
194     }
195 }