1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
|
/*-
* ============LICENSE_START=======================================================
* Copyright (C) 2018 Ericsson. All rights reserved.
* Modifications Copyright (C) 2020 Nordix Foundation
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
* ============LICENSE_END=========================================================
*/
package org.onap.policy.apex.plugins.event.protocol.yaml;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import java.io.IOException;
import java.util.List;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.onap.policy.apex.context.impl.schema.java.JavaSchemaHelperParameters;
import org.onap.policy.apex.context.parameters.SchemaParameters;
import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey;
import org.onap.policy.apex.model.basicmodel.service.ModelService;
import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema;
import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas;
import org.onap.policy.apex.model.eventmodel.concepts.AxEvent;
import org.onap.policy.apex.model.eventmodel.concepts.AxEvents;
import org.onap.policy.apex.model.eventmodel.concepts.AxField;
import org.onap.policy.apex.service.engine.event.ApexEvent;
import org.onap.policy.apex.service.engine.event.ApexEventException;
import org.onap.policy.common.parameters.ParameterService;
/**
* The Class TestYamlPluginStability.
*/
public class YamlPluginStabilityTest {
static AxEvent testEvent;
/**
* Register test events and schemas.
*
* @throws IOException Signals that an I/O exception has occurred.
*/
@BeforeClass
public static void registerTestEventsAndSchemas() throws IOException {
SchemaParameters schemaParameters = new SchemaParameters();
schemaParameters.getSchemaHelperParameterMap().put("JAVA", new JavaSchemaHelperParameters());
ParameterService.register(schemaParameters);
AxContextSchemas schemas = new AxContextSchemas();
AxContextSchema simpleIntSchema = new AxContextSchema(new AxArtifactKey("SimpleIntSchema", "0.0.1"), "JAVA",
"java.lang.Integer");
schemas.getSchemasMap().put(simpleIntSchema.getKey(), simpleIntSchema);
AxContextSchema simpleDoubleSchema = new AxContextSchema(new AxArtifactKey("SimpleDoubleSchema", "0.0.1"),
"JAVA", "java.lang.Double");
schemas.getSchemasMap().put(simpleDoubleSchema.getKey(), simpleDoubleSchema);
AxContextSchema simpleStringSchema = new AxContextSchema(new AxArtifactKey("SimpleStringSchema", "0.0.1"),
"JAVA", "java.lang.String");
schemas.getSchemasMap().put(simpleStringSchema.getKey(), simpleStringSchema);
ModelService.registerModel(AxContextSchemas.class, schemas);
testEvent = new AxEvent(new AxArtifactKey("TestEvent", "0.0.1"));
testEvent.setNameSpace("org.onap.policy.apex.plugins.event.protocol.yaml");
AxField teField0 = new AxField(new AxReferenceKey(testEvent.getKey(), "intValue"), simpleIntSchema.getKey());
testEvent.getParameterMap().put("intValue", teField0);
AxField teField1 = new AxField(new AxReferenceKey(testEvent.getKey(), "doubleValue"),
simpleDoubleSchema.getKey());
testEvent.getParameterMap().put("doubleValue", teField1);
AxField teField2 = new AxField(new AxReferenceKey(testEvent.getKey(), "stringValue"),
simpleStringSchema.getKey(), true);
testEvent.getParameterMap().put("stringValue", teField2);
AxEvents events = new AxEvents();
events.getEventMap().put(testEvent.getKey(), testEvent);
ModelService.registerModel(AxEvents.class, events);
}
/**
* Unregister test events and schemas.
*/
@AfterClass
public static void unregisterTestEventsAndSchemas() {
ModelService.clear();
ParameterService.clear();
}
/**
* Test stability.
*
* @throws ApexEventException the apex event exception
*/
@Test
public void testStability() throws ApexEventException {
Apex2YamlEventConverter converter = new Apex2YamlEventConverter();
assertThatThrownBy(() -> converter.init(null))
.hasMessage("specified consumer properties are not applicable to the YAML event protocol");
YamlEventProtocolParameters pars = new YamlEventProtocolParameters();
converter.init(pars);
assertThatThrownBy(() -> converter.toApexEvent("NonExistantEvent", ""))
.hasMessageContaining("Failed to unmarshal YAML event")
.getCause().hasMessageStartingWith("an event definition for an event named \"NonExistantEvent\"");
assertThatThrownBy(() -> converter.toApexEvent("TestEvent", null))
.hasMessage("event processing failed, event is null");
assertThatThrownBy(() -> converter.toApexEvent("TestEvent", 1))
.hasMessage("error converting event \"1\" to a string");
assertThatThrownBy(() -> converter.toApexEvent("TestEvent", ""))
.getCause().hasMessageContaining("Field \"doubleValue\" is missing");
assertThatThrownBy(() -> converter.fromApexEvent(null))
.hasMessage("event processing failed, Apex event is null");
ApexEvent apexEvent = new ApexEvent(testEvent.getKey().getName(), testEvent.getKey().getVersion(),
testEvent.getNameSpace(), testEvent.getSource(), testEvent.getTarget());
apexEvent.put("doubleValue", 123.45);
apexEvent.put("intValue", 123);
apexEvent.put("stringValue", "123.45");
apexEvent.setExceptionMessage("my wonderful exception message");
String yamlString = (String) converter.fromApexEvent(apexEvent);
assertTrue(yamlString.contains("my wonderful exception message"));
apexEvent.remove("intValue");
assertThatThrownBy(() -> converter.fromApexEvent(apexEvent))
.hasMessageContaining("error parsing TestEvent:0.0.1 event to Json. Field \"intValue\" is missing");
assertThatThrownBy(() -> converter.toApexEvent(null, ""))
.hasMessageStartingWith("Failed to unmarshal YAML event")
.getCause().hasMessageStartingWith("event received without mandatory parameter \"name\"");
pars.setNameAlias("TheNameField");
assertThatThrownBy(() -> converter.toApexEvent(null, ""))
.hasMessageStartingWith("Failed to unmarshal YAML event")
.getCause().hasMessageStartingWith("event received without mandatory parameter \"name\"");
apexEvent.put("intValue", 123);
apexEvent.remove("stringValue");
yamlString = (String) converter.fromApexEvent(apexEvent);
apexEvent.put("stringValue", "123.45");
String yamlInputString = "doubleValue: 123.45\n" + "intValue: 123";
List<ApexEvent> eventList = converter.toApexEvent("TestEvent", yamlInputString);
assertEquals(123.45, eventList.get(0).get("doubleValue"));
yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: null";
eventList = converter.toApexEvent("TestEvent", yamlInputString);
assertEquals(null, eventList.get(0).get("stringValue"));
yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: TestEvent";
pars.setNameAlias("stringValue");
eventList = converter.toApexEvent(null, yamlInputString);
assertEquals("TestEvent", eventList.get(0).get("stringValue"));
yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: SomeOtherEvent";
eventList = converter.toApexEvent("TestEvent", yamlInputString);
assertEquals("SomeOtherEvent", eventList.get(0).get("stringValue"));
yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: 0.0.1";
pars.setNameAlias(null);
pars.setVersionAlias("stringValue");
eventList = converter.toApexEvent("TestEvent", yamlInputString);
assertEquals("0.0.1", eventList.get(0).get("stringValue"));
yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: org.some.other.namespace";
pars.setVersionAlias(null);
pars.setNameSpaceAlias("stringValue");
final String yamlInputStringCopy = yamlInputString;
assertThatThrownBy(() -> converter.toApexEvent("TestEvent", yamlInputStringCopy))
.hasMessageStartingWith("Failed to unmarshal YAML event")
.getCause().hasMessageStartingWith("namespace \"org.some.other.namespace\" on event");
yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n"
+ "stringValue: org.onap.policy.apex.plugins.event.protocol.yaml";
eventList = converter.toApexEvent("TestEvent", yamlInputString);
assertEquals("org.onap.policy.apex.plugins.event.protocol.yaml", eventList.get(0).getNameSpace());
yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: MySource";
pars.setNameSpaceAlias(null);
pars.setSourceAlias("stringValue");
eventList = converter.toApexEvent("TestEvent", yamlInputString);
assertEquals("MySource", eventList.get(0).getSource());
yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: MyTarget";
pars.setSourceAlias(null);
pars.setTargetAlias("stringValue");
eventList = converter.toApexEvent("TestEvent", yamlInputString);
assertEquals("MyTarget", eventList.get(0).getTarget());
pars.setTargetAlias(null);
yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: MyString";
pars.setSourceAlias(null);
pars.setTargetAlias("intValue");
assertThatThrownBy(() -> converter.toApexEvent("TestEvent", yamlInputStringCopy))
.hasMessageStartingWith("Failed to unmarshal YAML event")
.getCause().hasMessageStartingWith("field \"target\" with type \"java.lang.Integer\"");
pars.setTargetAlias(null);
assertThatThrownBy(() -> converter.toApexEvent("TestEvent", "doubleValue: 123.45\n" + "intValue: ~\n"
+ "stringValue: MyString")).getCause().hasMessageStartingWith("mandatory field \"intValue\" is missing");
}
}
|