aboutsummaryrefslogtreecommitdiffstats
path: root/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/impl/apexprotocolplugin/Apex2ApexEventConverterTest.java
blob: 04e87ee42f7c8937586c3a5b486781a46a7ad252 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2021  Nordix Foundation
 *  ================================================================================
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 *  SPDX-License-Identifier: Apache-2.0
 *  ============LICENSE_END=========================================================
 */

package org.onap.policy.apex.service.engine.event.impl.apexprotocolplugin;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatNoException;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertSame;

import java.util.List;
import org.apache.commons.lang3.RandomStringUtils;
import org.junit.Before;
import org.junit.Test;
import org.onap.policy.apex.service.engine.event.ApexEvent;
import org.onap.policy.apex.service.engine.event.ApexEventException;
import org.onap.policy.apex.service.engine.event.ApexEventList;
import org.onap.policy.apex.service.engine.event.ApexEventRuntimeException;

public class Apex2ApexEventConverterTest {
    private Apex2ApexEventConverter converter;

    @Before
    public void setUp() throws Exception {
        converter = new Apex2ApexEventConverter();
    }

    @Test
    public void initWithNull() {
        assertThatThrownBy(() -> converter.init(null))
            .isInstanceOf(ApexEventRuntimeException.class);
    }

    @Test
    public void init() {
        assertThatNoException()
            .isThrownBy(() -> converter.init(new ApexEventProtocolParameters()));
    }

    @Test
    public void toApexEventWithNull() {
        final String eventName = RandomStringUtils.randomAlphanumeric(5);
        assertThatThrownBy(() -> converter.toApexEvent(eventName, null))
            .isInstanceOf(ApexEventException.class);
    }

    @Test
    public void toApexEventWithNonApexEvent() {
        final String eventName = RandomStringUtils.randomAlphanumeric(5);
        assertThatThrownBy(() -> converter.toApexEvent(eventName, new Object()))
            .isInstanceOf(ApexEventException.class);
    }

    @Test
    public void toApexEmptyEvent() throws ApexEventException {
        final String eventName = RandomStringUtils.randomAlphanumeric(4);
        final String name = RandomStringUtils.randomAlphanumeric(5);
        final String version = RandomStringUtils.randomAlphanumeric(6);
        final String nameSpace = "a" + RandomStringUtils.randomAlphanumeric(7);
        final String source = RandomStringUtils.randomAlphanumeric(8);
        final String target = RandomStringUtils.randomAlphanumeric(9);

        final ApexEvent event = new ApexEvent(name, version, nameSpace, source, target);
        final List<ApexEvent> result = converter.toApexEvent(eventName, event);
        assertThat(result).isEmpty();
    }

    @Test
    public void toApexEventWithApexAndOtherFields() throws ApexEventException {
        final String eventName = RandomStringUtils.randomAlphanumeric(4);
        final String name1 = RandomStringUtils.randomAlphanumeric(5);
        final String version1 = RandomStringUtils.randomAlphanumeric(6);
        final String nameSpace1 = "a" + RandomStringUtils.randomAlphanumeric(7);
        final String source1 = RandomStringUtils.randomAlphanumeric(8);
        final String target1 = RandomStringUtils.randomAlphanumeric(9);

        final ApexEvent event = new ApexEvent(name1, version1, nameSpace1, source1, target1);

        final String key = RandomStringUtils.randomAlphabetic(3);
        event.put(key, new Object());
        final List<ApexEvent> result = converter.toApexEvent(eventName, event);
        Object[] expected = {event};
        assertArrayEquals(expected, result.toArray());
    }

    @Test
    public void toApexEventWithApexAndList() throws ApexEventException {
        final String eventName = RandomStringUtils.randomAlphanumeric(4);
        final String name1 = RandomStringUtils.randomAlphanumeric(5);
        final String version1 = RandomStringUtils.randomAlphanumeric(6);
        final String nameSpace1 = "a" + RandomStringUtils.randomAlphanumeric(7);
        final String source1 = RandomStringUtils.randomAlphanumeric(8);
        final String target1 = RandomStringUtils.randomAlphanumeric(9);

        final ApexEvent event = new ApexEvent(name1, version1, nameSpace1, source1, target1);

        final ApexEventList eventList = new ApexEventList();
        eventList.add(event);

        final String name2 = RandomStringUtils.randomAlphanumeric(15);
        final String version2 = RandomStringUtils.randomAlphanumeric(16);
        final String nameSpace2 = "b" + RandomStringUtils.randomAlphanumeric(17);
        final String source2 = RandomStringUtils.randomAlphanumeric(18);
        final String target2 = RandomStringUtils.randomAlphanumeric(19);

        final ApexEvent parentEvent = new ApexEvent(name2, version2, nameSpace2, source2, target2);
        final String key = RandomStringUtils.randomAlphabetic(3);
        parentEvent.put(key, eventList);
        final List<ApexEvent> result = converter.toApexEvent(eventName, parentEvent);
        Object[] expected = {event};
        assertArrayEquals(expected, result.toArray());
    }

    @Test
    public void toApexEventWithApexAndListAndOtherFields() throws ApexEventException {
        final String eventName = RandomStringUtils.randomAlphanumeric(4);
        final String name1 = RandomStringUtils.randomAlphanumeric(5);
        final String version1 = RandomStringUtils.randomAlphanumeric(6);
        final String nameSpace1 = "a" + RandomStringUtils.randomAlphanumeric(7);
        final String source1 = RandomStringUtils.randomAlphanumeric(8);
        final String target1 = RandomStringUtils.randomAlphanumeric(9);

        final ApexEvent event = new ApexEvent(name1, version1, nameSpace1, source1, target1);

        final ApexEventList eventList = new ApexEventList();
        eventList.add(event);

        final String name2 = RandomStringUtils.randomAlphanumeric(15);
        final String version2 = RandomStringUtils.randomAlphanumeric(16);
        final String nameSpace2 = "b" + RandomStringUtils.randomAlphanumeric(17);
        final String source2 = RandomStringUtils.randomAlphanumeric(18);
        final String target2 = RandomStringUtils.randomAlphanumeric(19);

        final ApexEvent parentEvent = new ApexEvent(name2, version2, nameSpace2, source2, target2);
        final String key1 = RandomStringUtils.randomAlphabetic(3);
        final String key2 = RandomStringUtils.randomAlphabetic(2);
        parentEvent.put(key1, eventList);
        parentEvent.put(key2, new Object());
        assertThatThrownBy(() -> converter.toApexEvent(eventName, parentEvent))
            .isInstanceOf(ApexEventException.class);
    }

    @Test
    public void fromApexEventNull() {
        assertThatThrownBy(() -> converter.fromApexEvent(null))
            .isInstanceOf(ApexEventException.class);
    }

    @Test
    public void fromApexEvent() throws ApexEventException {
        final String name1 = RandomStringUtils.randomAlphanumeric(5);
        final String version1 = RandomStringUtils.randomAlphanumeric(6);
        final String nameSpace1 = "a" + RandomStringUtils.randomAlphanumeric(7);
        final String source1 = RandomStringUtils.randomAlphanumeric(8);
        final String target1 = RandomStringUtils.randomAlphanumeric(9);

        final ApexEvent event = new ApexEvent(name1, version1, nameSpace1, source1, target1);

        final Object actual = converter.fromApexEvent(event);
        assertSame(event, actual);
    }

}