Replace try/catch with assertj
[policy/apex-pdp.git] / services / services-engine / src / test / java / org / onap / policy / apex / service / engine / event / JsonEventConverterTest.java
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             .hasMessage("Failed to unmarshal JSON event: event received without mandatory parameter \"name\" "
53                             + "on configuration or on event, event=[{\"aKey\": 1},{\"aKey\": 2}]");
54         assertThatThrownBy(() -> converter.toApexEvent(null, "[1,2,3]"))
55             .hasMessage("Failed to unmarshal JSON event: incoming event ([1,2,3]) is a JSON object array "
56                     + "containing an invalid object 1.0, event=[1,2,3]");
57         assertThatThrownBy(() -> converter.fromApexEvent(null))
58             .hasMessage("event processing failed, Apex event is null");
59         assertThatThrownBy(() -> converter.fromApexEvent(new ApexEvent("Event", "0.0.1", "a.name.space",
60                 "here", "there")))
61             .hasMessage("Model for org.onap.policy.apex.model.eventmodel.concepts.AxEvents not found in model service");
62     }
63 }