99b2654ab05b55d6c6d8a10e05e095103e22cdad
[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  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  * 
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  * 
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  * 
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.service.engine.event;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.fail;
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         try {
41             converter.init(null);
42             fail("test should throw an exception");
43         } catch (Exception ie) {
44             assertEquals("specified consumer properties are not applicable to the JSON event protocol",
45                             ie.getMessage());
46         }
47
48         try {
49             converter.init(new EventProtocolParameters() {
50             });
51             fail("test should throw an exception");
52         } catch (Exception ie) {
53             assertEquals("specified consumer properties are not applicable to the JSON event protocol",
54                             ie.getMessage());
55         }
56
57         JsonEventProtocolParameters pars = new JsonEventProtocolParameters();
58         converter.init(pars);
59
60         try {
61             converter.toApexEvent(null, null);
62             fail("test should throw an exception");
63         } catch (Exception tae) {
64             assertEquals("event processing failed, event is null", tae.getMessage());
65         }
66
67         try {
68             converter.toApexEvent(null, 1);
69             fail("test should throw an exception");
70         } catch (Exception tae) {
71             assertEquals("error converting event \"1\" to a string", tae.getMessage());
72         }
73
74         try {
75             converter.toApexEvent(null, "[{\"aKey\": 1},{\"aKey\": 2}]");
76             fail("test should throw an exception");
77         } catch (Exception tae) {
78             assertEquals("Failed to unmarshal JSON event: event received without mandatory parameter \"name\" "
79                             + "on configuration or on event, event=[{\"aKey\": 1},{\"aKey\": 2}]", tae.getMessage());
80         }
81
82         try {
83             converter.toApexEvent(null, "[1,2,3]");
84             fail("test should throw an exception");
85         } catch (Exception tae) {
86             assertEquals("Failed to unmarshal JSON event: incoming event ([1,2,3]) is a JSON object array "
87                             + "containing an invalid object 1.0, event=[1,2,3]", tae.getMessage());
88         }
89
90         try {
91             converter.fromApexEvent(null);
92             fail("test should throw an exception");
93         } catch (Exception tae) {
94             assertEquals("event processing failed, Apex event is null", tae.getMessage());
95         }
96
97         try {
98             converter.fromApexEvent(new ApexEvent("Event", "0.0.1", "a.name.space", "here", "there"));
99             fail("test should throw an exception");
100         } catch (Exception tae) {
101             assertEquals("Model for org.onap.policy.apex.model.eventmodel.concepts.AxEvents not found in model service",
102                             tae.getMessage());
103         }
104     }
105 }