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
 
   9  *      http://www.apache.org/licenses/LICENSE-2.0
 
  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.
 
  17  * SPDX-License-Identifier: Apache-2.0
 
  18  * ============LICENSE_END=========================================================
 
  21 package org.onap.policy.apex.plugins.event.protocol.yaml;
 
  23 import static org.junit.Assert.assertEquals;
 
  24 import static org.junit.Assert.assertTrue;
 
  25 import static org.junit.Assert.fail;
 
  27 import java.io.IOException;
 
  28 import java.util.List;
 
  30 import org.junit.AfterClass;
 
  31 import org.junit.BeforeClass;
 
  32 import org.junit.Test;
 
  33 import org.onap.policy.apex.context.impl.schema.java.JavaSchemaHelperParameters;
 
  34 import org.onap.policy.apex.context.parameters.SchemaParameters;
 
  35 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
 
  36 import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey;
 
  37 import org.onap.policy.apex.model.basicmodel.service.ModelService;
 
  38 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema;
 
  39 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas;
 
  40 import org.onap.policy.apex.model.eventmodel.concepts.AxEvent;
 
  41 import org.onap.policy.apex.model.eventmodel.concepts.AxEvents;
 
  42 import org.onap.policy.apex.model.eventmodel.concepts.AxField;
 
  43 import org.onap.policy.apex.service.engine.event.ApexEvent;
 
  44 import org.onap.policy.apex.service.engine.event.ApexEventException;
 
  45 import org.onap.policy.apex.service.engine.event.ApexEventRuntimeException;
 
  46 import org.onap.policy.common.parameters.ParameterService;
 
  49  * The Class TestYamlPluginStability.
 
  51 public class TestYamlPluginStability {
 
  52     static AxEvent testEvent;
 
  55      * Register test events and schemas.
 
  57      * @throws IOException Signals that an I/O exception has occurred.
 
  60     public static void registerTestEventsAndSchemas() throws IOException {
 
  61         SchemaParameters schemaParameters = new SchemaParameters();
 
  62         schemaParameters.getSchemaHelperParameterMap().put("JAVA", new JavaSchemaHelperParameters());
 
  63         ParameterService.register(schemaParameters);
 
  65         AxContextSchemas schemas = new AxContextSchemas();
 
  67         AxContextSchema simpleIntSchema = new AxContextSchema(new AxArtifactKey("SimpleIntSchema", "0.0.1"), "JAVA",
 
  69         schemas.getSchemasMap().put(simpleIntSchema.getKey(), simpleIntSchema);
 
  71         AxContextSchema simpleDoubleSchema = new AxContextSchema(new AxArtifactKey("SimpleDoubleSchema", "0.0.1"),
 
  72                         "JAVA", "java.lang.Double");
 
  73         schemas.getSchemasMap().put(simpleDoubleSchema.getKey(), simpleDoubleSchema);
 
  75         AxContextSchema simpleStringSchema = new AxContextSchema(new AxArtifactKey("SimpleStringSchema", "0.0.1"),
 
  76                         "JAVA", "java.lang.String");
 
  77         schemas.getSchemasMap().put(simpleStringSchema.getKey(), simpleStringSchema);
 
  79         ModelService.registerModel(AxContextSchemas.class, schemas);
 
  81         testEvent = new AxEvent(new AxArtifactKey("TestEvent", "0.0.1"));
 
  82         testEvent.setNameSpace("org.onap.policy.apex.plugins.event.protocol.yaml");
 
  83         AxField teField0 = new AxField(new AxReferenceKey(testEvent.getKey(), "intValue"), simpleIntSchema.getKey());
 
  84         testEvent.getParameterMap().put("intValue", teField0);
 
  85         AxField teField1 = new AxField(new AxReferenceKey(testEvent.getKey(), "doubleValue"),
 
  86                         simpleDoubleSchema.getKey());
 
  87         testEvent.getParameterMap().put("doubleValue", teField1);
 
  88         AxField teField2 = new AxField(new AxReferenceKey(testEvent.getKey(), "stringValue"),
 
  89                         simpleStringSchema.getKey(), true);
 
  90         testEvent.getParameterMap().put("stringValue", teField2);
 
  92         AxEvents events = new AxEvents();
 
  93         events.getEventMap().put(testEvent.getKey(), testEvent);
 
  95         ModelService.registerModel(AxEvents.class, events);
 
  99      * Unregister test events and schemas.
 
 102     public static void unregisterTestEventsAndSchemas() {
 
 103         ModelService.clear();
 
 104         ParameterService.clear();
 
 110      * @throws ApexEventException the apex event exception
 
 113     public void testStability() throws ApexEventException {
 
 114         Apex2YamlEventConverter converter = new Apex2YamlEventConverter();
 
 117             converter.init(null);
 
 118             fail("this test should throw an exception");
 
 119         } catch (ApexEventRuntimeException e) {
 
 120             assertEquals("specified consumer properties are not applicable to the YAML event protocol", e.getMessage());
 
 123         YamlEventProtocolParameters pars = new YamlEventProtocolParameters();
 
 124         converter.init(pars);
 
 127             converter.toApexEvent("NonExistantEvent", "");
 
 128             fail("this test should throw an exception");
 
 129         } catch (ApexEventException e) {
 
 130             assertEquals("Failed to unmarshal YAML event: an event definition for an event named \"NonExistantEvent\"",
 
 131                             e.getMessage().substring(0, 89));
 
 135             converter.toApexEvent("TestEvent", null);
 
 136             fail("this test should throw an exception");
 
 137         } catch (ApexEventException e) {
 
 138             assertEquals("event processing failed, event is null", e.getMessage());
 
 142             converter.toApexEvent("TestEvent", 1);
 
 143             fail("this test should throw an exception");
 
 144         } catch (ApexEventException e) {
 
 145             assertEquals("error converting event \"1\" to a string", e.getMessage());
 
 149             converter.toApexEvent("TestEvent", "");
 
 150             fail("this test should throw an exception");
 
 151         } catch (ApexEventException e) {
 
 152             assertTrue(e.getMessage().contains("Field \"doubleValue\" is missing"));
 
 156             converter.fromApexEvent(null);
 
 157             fail("this test should throw an exception");
 
 158         } catch (ApexEventException e) {
 
 159             assertEquals("event processing failed, Apex event is null", e.getMessage());
 
 162         ApexEvent apexEvent = new ApexEvent(testEvent.getKey().getName(), testEvent.getKey().getVersion(),
 
 163                         testEvent.getNameSpace(), testEvent.getSource(), testEvent.getTarget());
 
 164         apexEvent.put("doubleValue", 123.45);
 
 165         apexEvent.put("intValue", 123);
 
 166         apexEvent.put("stringValue", "123.45");
 
 168         apexEvent.setExceptionMessage("my wonderful exception message");
 
 169         String yamlString = (String) converter.fromApexEvent(apexEvent);
 
 170         assertTrue(yamlString.contains("my wonderful exception message"));
 
 172         apexEvent.remove("intValue");
 
 174             yamlString = (String) converter.fromApexEvent(apexEvent);
 
 175             fail("this test should throw an exception");
 
 176         } catch (ApexEventRuntimeException e) {
 
 177             assertEquals("error parsing TestEvent:0.0.1 event to Json. Field \"intValue\" is missing",
 
 178                             e.getMessage().substring(0, 72));
 
 182             converter.toApexEvent(null, "");
 
 183             fail("this test should throw an exception");
 
 184         } catch (ApexEventException e) {
 
 185             assertEquals("Failed to unmarshal YAML event: event received without mandatory parameter \"name\"",
 
 186                             e.getMessage().substring(0, 81));
 
 189         pars.setNameAlias("TheNameField");
 
 191             converter.toApexEvent(null, "");
 
 192             fail("this test should throw an exception");
 
 193         } catch (ApexEventException e) {
 
 194             assertEquals("Failed to unmarshal YAML event: event received without mandatory parameter \"name\"",
 
 195                             e.getMessage().substring(0, 81));
 
 198         apexEvent.put("intValue", 123);
 
 200         apexEvent.remove("stringValue");
 
 201         yamlString = (String) converter.fromApexEvent(apexEvent);
 
 202         apexEvent.put("stringValue", "123.45");
 
 204         String yamlInputString = "doubleValue: 123.45\n" + "intValue: 123";
 
 206         List<ApexEvent> eventList = converter.toApexEvent("TestEvent", yamlInputString);
 
 207         assertEquals(123.45, eventList.get(0).get("doubleValue"));
 
 209         yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: null";
 
 211         eventList = converter.toApexEvent("TestEvent", yamlInputString);
 
 212         assertEquals(null, eventList.get(0).get("stringValue"));
 
 214         yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: TestEvent";
 
 215         pars.setNameAlias("stringValue");
 
 216         eventList = converter.toApexEvent(null, yamlInputString);
 
 217         assertEquals("TestEvent", eventList.get(0).get("stringValue"));
 
 219         yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: SomeOtherEvent";
 
 220         eventList = converter.toApexEvent("TestEvent", yamlInputString);
 
 221         assertEquals("SomeOtherEvent", eventList.get(0).get("stringValue"));
 
 223         yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: 0.0.1";
 
 224         pars.setNameAlias(null);
 
 225         pars.setVersionAlias("stringValue");
 
 226         eventList = converter.toApexEvent("TestEvent", yamlInputString);
 
 227         assertEquals("0.0.1", eventList.get(0).get("stringValue"));
 
 229         yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: org.some.other.namespace";
 
 230         pars.setVersionAlias(null);
 
 231         pars.setNameSpaceAlias("stringValue");
 
 233             converter.toApexEvent("TestEvent", yamlInputString);
 
 234             fail("this test should throw an exception");
 
 235         } catch (ApexEventException e) {
 
 236             assertEquals("Failed to unmarshal YAML event: namespace \"org.some.other.namespace\" on event",
 
 237                             e.getMessage().substring(0, 77));
 
 240         yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n"
 
 241                         + "stringValue: org.onap.policy.apex.plugins.event.protocol.yaml";
 
 242         eventList = converter.toApexEvent("TestEvent", yamlInputString);
 
 243         assertEquals("org.onap.policy.apex.plugins.event.protocol.yaml", eventList.get(0).getNameSpace());
 
 245         yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: MySource";
 
 246         pars.setNameSpaceAlias(null);
 
 247         pars.setSourceAlias("stringValue");
 
 248         eventList = converter.toApexEvent("TestEvent", yamlInputString);
 
 249         assertEquals("MySource", eventList.get(0).getSource());
 
 251         yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: MyTarget";
 
 252         pars.setSourceAlias(null);
 
 253         pars.setTargetAlias("stringValue");
 
 254         eventList = converter.toApexEvent("TestEvent", yamlInputString);
 
 255         assertEquals("MyTarget", eventList.get(0).getTarget());
 
 256         pars.setTargetAlias(null);
 
 258         yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: MyString";
 
 259         pars.setSourceAlias(null);
 
 260         pars.setTargetAlias("intValue");
 
 262             converter.toApexEvent("TestEvent", yamlInputString);
 
 263             fail("this test should throw an exception");
 
 264         } catch (ApexEventException e) {
 
 265             assertEquals("Failed to unmarshal YAML event: field \"target\" with type \"java.lang.Integer\"",
 
 266                             e.getMessage().substring(0, 76));
 
 268         pars.setTargetAlias(null);
 
 270         yamlInputString = "doubleValue: 123.45\n" + "intValue: ~\n" + "stringValue: MyString";
 
 272             converter.toApexEvent("TestEvent", yamlInputString);
 
 273             fail("this test should throw an exception");
 
 274         } catch (ApexEventException e) {
 
 275             assertTrue(e.getMessage().contains("mandatory field \"intValue\" is missing"));