04e87ee42f7c8937586c3a5b486781a46a7ad252
[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  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License.
15  *
16  *  SPDX-License-Identifier: Apache-2.0
17  *  ============LICENSE_END=========================================================
18  */
19
20 package org.onap.policy.apex.service.engine.event.impl.apexprotocolplugin;
21
22 import static org.assertj.core.api.Assertions.assertThat;
23 import static org.assertj.core.api.Assertions.assertThatNoException;
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertArrayEquals;
26 import static org.junit.Assert.assertSame;
27
28 import java.util.List;
29 import org.apache.commons.lang3.RandomStringUtils;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.onap.policy.apex.service.engine.event.ApexEvent;
33 import org.onap.policy.apex.service.engine.event.ApexEventException;
34 import org.onap.policy.apex.service.engine.event.ApexEventList;
35 import org.onap.policy.apex.service.engine.event.ApexEventRuntimeException;
36
37 public class Apex2ApexEventConverterTest {
38     private Apex2ApexEventConverter converter;
39
40     @Before
41     public void setUp() throws Exception {
42         converter = new Apex2ApexEventConverter();
43     }
44
45     @Test
46     public void initWithNull() {
47         assertThatThrownBy(() -> converter.init(null))
48             .isInstanceOf(ApexEventRuntimeException.class);
49     }
50
51     @Test
52     public void init() {
53         assertThatNoException()
54             .isThrownBy(() -> converter.init(new ApexEventProtocolParameters()));
55     }
56
57     @Test
58     public void toApexEventWithNull() {
59         final String eventName = RandomStringUtils.randomAlphanumeric(5);
60         assertThatThrownBy(() -> converter.toApexEvent(eventName, null))
61             .isInstanceOf(ApexEventException.class);
62     }
63
64     @Test
65     public void toApexEventWithNonApexEvent() {
66         final String eventName = RandomStringUtils.randomAlphanumeric(5);
67         assertThatThrownBy(() -> converter.toApexEvent(eventName, new Object()))
68             .isInstanceOf(ApexEventException.class);
69     }
70
71     @Test
72     public void toApexEmptyEvent() throws ApexEventException {
73         final String eventName = RandomStringUtils.randomAlphanumeric(4);
74         final String name = RandomStringUtils.randomAlphanumeric(5);
75         final String version = RandomStringUtils.randomAlphanumeric(6);
76         final String nameSpace = "a" + RandomStringUtils.randomAlphanumeric(7);
77         final String source = RandomStringUtils.randomAlphanumeric(8);
78         final String target = RandomStringUtils.randomAlphanumeric(9);
79
80         final ApexEvent event = new ApexEvent(name, version, nameSpace, source, target);
81         final List<ApexEvent> result = converter.toApexEvent(eventName, event);
82         assertThat(result).isEmpty();
83     }
84
85     @Test
86     public void toApexEventWithApexAndOtherFields() throws ApexEventException {
87         final String eventName = RandomStringUtils.randomAlphanumeric(4);
88         final String name1 = RandomStringUtils.randomAlphanumeric(5);
89         final String version1 = RandomStringUtils.randomAlphanumeric(6);
90         final String nameSpace1 = "a" + RandomStringUtils.randomAlphanumeric(7);
91         final String source1 = RandomStringUtils.randomAlphanumeric(8);
92         final String target1 = RandomStringUtils.randomAlphanumeric(9);
93
94         final ApexEvent event = new ApexEvent(name1, version1, nameSpace1, source1, target1);
95
96         final String key = RandomStringUtils.randomAlphabetic(3);
97         event.put(key, new Object());
98         final List<ApexEvent> result = converter.toApexEvent(eventName, event);
99         Object[] expected = {event};
100         assertArrayEquals(expected, result.toArray());
101     }
102
103     @Test
104     public void toApexEventWithApexAndList() throws ApexEventException {
105         final String eventName = RandomStringUtils.randomAlphanumeric(4);
106         final String name1 = RandomStringUtils.randomAlphanumeric(5);
107         final String version1 = RandomStringUtils.randomAlphanumeric(6);
108         final String nameSpace1 = "a" + RandomStringUtils.randomAlphanumeric(7);
109         final String source1 = RandomStringUtils.randomAlphanumeric(8);
110         final String target1 = RandomStringUtils.randomAlphanumeric(9);
111
112         final ApexEvent event = new ApexEvent(name1, version1, nameSpace1, source1, target1);
113
114         final ApexEventList eventList = new ApexEventList();
115         eventList.add(event);
116
117         final String name2 = RandomStringUtils.randomAlphanumeric(15);
118         final String version2 = RandomStringUtils.randomAlphanumeric(16);
119         final String nameSpace2 = "b" + RandomStringUtils.randomAlphanumeric(17);
120         final String source2 = RandomStringUtils.randomAlphanumeric(18);
121         final String target2 = RandomStringUtils.randomAlphanumeric(19);
122
123         final ApexEvent parentEvent = new ApexEvent(name2, version2, nameSpace2, source2, target2);
124         final String key = RandomStringUtils.randomAlphabetic(3);
125         parentEvent.put(key, eventList);
126         final List<ApexEvent> result = converter.toApexEvent(eventName, parentEvent);
127         Object[] expected = {event};
128         assertArrayEquals(expected, result.toArray());
129     }
130
131     @Test
132     public void toApexEventWithApexAndListAndOtherFields() throws ApexEventException {
133         final String eventName = RandomStringUtils.randomAlphanumeric(4);
134         final String name1 = RandomStringUtils.randomAlphanumeric(5);
135         final String version1 = RandomStringUtils.randomAlphanumeric(6);
136         final String nameSpace1 = "a" + RandomStringUtils.randomAlphanumeric(7);
137         final String source1 = RandomStringUtils.randomAlphanumeric(8);
138         final String target1 = RandomStringUtils.randomAlphanumeric(9);
139
140         final ApexEvent event = new ApexEvent(name1, version1, nameSpace1, source1, target1);
141
142         final ApexEventList eventList = new ApexEventList();
143         eventList.add(event);
144
145         final String name2 = RandomStringUtils.randomAlphanumeric(15);
146         final String version2 = RandomStringUtils.randomAlphanumeric(16);
147         final String nameSpace2 = "b" + RandomStringUtils.randomAlphanumeric(17);
148         final String source2 = RandomStringUtils.randomAlphanumeric(18);
149         final String target2 = RandomStringUtils.randomAlphanumeric(19);
150
151         final ApexEvent parentEvent = new ApexEvent(name2, version2, nameSpace2, source2, target2);
152         final String key1 = RandomStringUtils.randomAlphabetic(3);
153         final String key2 = RandomStringUtils.randomAlphabetic(2);
154         parentEvent.put(key1, eventList);
155         parentEvent.put(key2, new Object());
156         assertThatThrownBy(() -> converter.toApexEvent(eventName, parentEvent))
157             .isInstanceOf(ApexEventException.class);
158     }
159
160     @Test
161     public void fromApexEventNull() {
162         assertThatThrownBy(() -> converter.fromApexEvent(null))
163             .isInstanceOf(ApexEventException.class);
164     }
165
166     @Test
167     public void fromApexEvent() throws ApexEventException {
168         final String name1 = RandomStringUtils.randomAlphanumeric(5);
169         final String version1 = RandomStringUtils.randomAlphanumeric(6);
170         final String nameSpace1 = "a" + RandomStringUtils.randomAlphanumeric(7);
171         final String source1 = RandomStringUtils.randomAlphanumeric(8);
172         final String target1 = RandomStringUtils.randomAlphanumeric(9);
173
174         final ApexEvent event = new ApexEvent(name1, version1, nameSpace1, source1, target1);
175
176         final Object actual = converter.fromApexEvent(event);
177         assertSame(event, actual);
178     }
179
180 }