471f904250c3e014759de05282a9695615d0e084
[policy/apex-pdp.git] /
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 TestJsonEventConverter {
36
37     @Test
38     public void testJsonEventConverter() {
39         Apex2JsonEventConverter converter = new Apex2JsonEventConverter();
40
41         try {
42             converter.init(null);
43             fail("test should throw an exception");
44         } catch (Exception ie) {
45             assertEquals("specified consumer properties are not applicable to the JSON event protocol",
46                             ie.getMessage());
47         }
48
49         try {
50             converter.init(new EventProtocolParameters() {
51             });
52             fail("test should throw an exception");
53         } catch (Exception ie) {
54             assertEquals("specified consumer properties are not applicable to the JSON event protocol",
55                             ie.getMessage());
56         }
57
58         JsonEventProtocolParameters pars = new JsonEventProtocolParameters();
59         converter.init(pars);
60
61         try {
62             converter.toApexEvent(null, null);
63             fail("test should throw an exception");
64         } catch (Exception tae) {
65             assertEquals("event processing failed, event is null", tae.getMessage());
66         }
67
68         try {
69             converter.toApexEvent(null, 1);
70             fail("test should throw an exception");
71         } catch (Exception tae) {
72             assertEquals("error converting event \"1\" to a string", tae.getMessage());
73         }
74
75         try {
76             converter.toApexEvent(null, "[{\"aKey\": 1},{\"aKey\": 2}]");
77             fail("test should throw an exception");
78         } catch (Exception tae) {
79             assertEquals("Failed to unmarshal JSON event: incoming event ([{\"aKey\": 1},{\"aKey\": 2}]) "
80                             + "is a JSON object array containing an invalid object "
81                             + "{aKey=1.0}, event=[{\"aKey\": 1},{\"aKey\": 2}]", tae.getMessage());
82         }
83
84         try {
85             converter.toApexEvent(null, "[1,2,3]");
86             fail("test should throw an exception");
87         } catch (Exception tae) {
88             assertEquals("Failed to unmarshal JSON event: incoming event ([1,2,3]) is a JSON object array "
89                             + "containing an invalid object 1.0, event=[1,2,3]", tae.getMessage());
90         }
91
92         try {
93             converter.fromApexEvent(null);
94             fail("test should throw an exception");
95         } catch (Exception tae) {
96             assertEquals("event processing failed, Apex event is null", tae.getMessage());
97         }
98
99         try {
100             converter.fromApexEvent(new ApexEvent("Event", "0.0.1", "a.name.space", "here", "there"));
101             fail("test should throw an exception");
102         } catch (Exception tae) {
103             assertEquals("Model for org.onap.policy.apex.model.eventmodel.concepts.AxEvents not found in model service",
104                             tae.getMessage());
105         }
106     }
107 }