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