2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2020 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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.apex.service.engine.event;
25 import static org.assertj.core.api.Assertions.assertThatThrownBy;
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;
33 * Test the JSON event converter corner cases.
36 public class JsonEventConverterTest {
38 public void testJsonEventConverter() {
39 Apex2JsonEventConverter converter = new Apex2JsonEventConverter();
41 assertThatThrownBy(() -> converter.init(null))
42 .hasMessage("specified consumer properties are not applicable to the JSON event protocol");
43 assertThatThrownBy(() -> converter.init(new EventProtocolParameters() {}))
44 .hasMessage("specified consumer properties are not applicable to the JSON event protocol");
45 JsonEventProtocolParameters pars = new JsonEventProtocolParameters();
48 assertThatThrownBy(() -> converter.toApexEvent(null, null))
49 .hasMessage("event processing failed, event is null");
50 assertThatThrownBy(() -> converter.toApexEvent(null, 1))
51 .hasMessage("error converting event \"1\" to a string");
52 assertThatThrownBy(() -> converter.toApexEvent(null, "[{\"aKey\": 1},{\"aKey\": 2}]"))
53 .hasMessageStartingWith("Failed to unmarshal JSON event")
54 .getCause().hasMessageStartingWith("event received without mandatory parameter \"name\" "
55 + "on configuration or on event");
56 assertThatThrownBy(() -> converter.toApexEvent(null, "[1,2,3]"))
57 .hasMessageStartingWith("Failed to unmarshal JSON event")
58 .getCause().hasMessageStartingWith("incoming event ([1,2,3]) is a JSON object array "
59 + "containing an invalid object 1.0");
60 assertThatThrownBy(() -> converter.fromApexEvent(null))
61 .hasMessage("event processing failed, Apex event is null");
62 assertThatThrownBy(() -> converter.fromApexEvent(new ApexEvent("Event", "0.0.1", "a.name.space",
63 "here", "there", "")))
64 .hasMessage("Model for org.onap.policy.apex.model.eventmodel.concepts.AxEvents not found in model service");