6e346a2a0a5ef8c7625e91355d9f5bc61b466ff3
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2020,2023 Nordix Foundation
5  *  Modifications Copyright (C) 2022 Bell Canada. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.apex.service.engine.event;
24
25 import static org.assertj.core.api.Assertions.assertThatThrownBy;
26
27 import org.junit.Test;
28 import org.onap.policy.apex.service.engine.event.impl.jsonprotocolplugin.Apex2JsonEventConverter;
29 import org.onap.policy.apex.service.engine.event.impl.jsonprotocolplugin.JsonEventProtocolParameters;
30 import org.onap.policy.apex.service.parameters.eventprotocol.EventProtocolParameters;
31
32 /**
33  * Test the JSON event converter corner cases.
34  *
35  */
36 public class JsonEventConverterTest {
37     @SuppressWarnings("deprecation")
38     @Test
39     public void testJsonEventConverter() {
40         Apex2JsonEventConverter converter = new Apex2JsonEventConverter();
41
42         assertThatThrownBy(() -> converter.init(null))
43             .hasMessage("specified consumer properties are not applicable to the JSON event protocol");
44         assertThatThrownBy(() -> converter.init(new EventProtocolParameters() {}))
45             .hasMessage("specified consumer properties are not applicable to the JSON event protocol");
46         JsonEventProtocolParameters pars = new JsonEventProtocolParameters();
47         converter.init(pars);
48
49         assertThatThrownBy(() -> converter.toApexEvent(null, null))
50             .hasMessage("event processing failed, event is null");
51         assertThatThrownBy(() -> converter.toApexEvent(null, 1))
52             .hasMessage("error converting event \"1\" to a string");
53         assertThatThrownBy(() -> converter.toApexEvent(null, "[{\"aKey\": 1},{\"aKey\": 2}]"))
54             .hasMessageStartingWith("Failed to unmarshal JSON event")
55             .getCause().hasMessageStartingWith("event received without mandatory parameter \"name\" "
56                             + "on configuration or on event");
57         assertThatThrownBy(() -> converter.toApexEvent(null, "[1,2,3]"))
58             .hasMessageStartingWith("Failed to unmarshal JSON event")
59             .getCause().hasMessageStartingWith("incoming event ([1,2,3]) is a JSON object array "
60                     + "containing an invalid object 1.0");
61         assertThatThrownBy(() -> converter.fromApexEvent(null))
62             .hasMessage("event processing failed, Apex event is null");
63         assertThatThrownBy(() -> converter.fromApexEvent(new ApexEvent("Event", "0.0.1", "a.name.space",
64                 "here", "there", "")))
65             .hasMessage("Model for org.onap.policy.apex.model.eventmodel.concepts.AxEvents not found in model service");
66     }
67 }