diff options
Diffstat (limited to 'plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-yaml/src')
4 files changed, 109 insertions, 81 deletions
diff --git a/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-yaml/src/main/java/org/onap/policy/apex/plugins/event/protocol/yaml/Apex2YamlEventConverter.java b/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-yaml/src/main/java/org/onap/policy/apex/plugins/event/protocol/yaml/Apex2YamlEventConverter.java index f81c3a914..4bf10e4ae 100644 --- a/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-yaml/src/main/java/org/onap/policy/apex/plugins/event/protocol/yaml/Apex2YamlEventConverter.java +++ b/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-yaml/src/main/java/org/onap/policy/apex/plugins/event/protocol/yaml/Apex2YamlEventConverter.java @@ -38,9 +38,8 @@ import org.onap.policy.apex.service.engine.event.ApexEventRuntimeException; import org.onap.policy.apex.service.parameters.eventprotocol.EventProtocolParameters; import org.slf4j.ext.XLogger; import org.slf4j.ext.XLoggerFactory; -import org.yaml.snakeyaml.Yaml; - import org.yaml.snakeyaml.DumperOptions.FlowStyle; +import org.yaml.snakeyaml.Yaml; /** * The Class Apex2YamlEventConverter converts {@link ApexEvent} instances to and from YAML string representations of @@ -104,11 +103,10 @@ public class Apex2YamlEventConverter implements ApexEventProtocolConverter { // If the incoming YAML did not create a map it is a primitive type or a collection so we // convert it into a map for processing Map<?, ?> yamlMap; - if (yamlObject != null && yamlObject instanceof Map) { + if (yamlObject instanceof Map) { // We already have a map so just cast the object yamlMap = (Map<?, ?>) yamlObject; - } - else { + } else { // Create a single entry map, new map creation and assignment is to avoid a // type checking warning LinkedHashMap<String, Object> newYamlMap = new LinkedHashMap<>(); @@ -173,7 +171,7 @@ public class Apex2YamlEventConverter implements ApexEventProtocolConverter { continue; } - yamlMap.put(fieldName, apexEvent.get(fieldName)); + yamlMap.put(fieldName, apexEvent.get(fieldName)); } // Use Snake YAML to convert the APEX event to YAML @@ -187,11 +185,9 @@ public class Apex2YamlEventConverter implements ApexEventProtocolConverter { * @param eventName the name of the event * @param yamlMap the YAML map that holds the event * @return the apex event that we have converted the JSON object into - * @throws ApexEventException - * thrown on unmarshaling exceptions + * @throws ApexEventException thrown on unmarshaling exceptions */ - private ApexEvent yamlMap2ApexEvent(final String eventName, final Map<?, ?> yamlMap) - throws ApexEventException { + private ApexEvent yamlMap2ApexEvent(final String eventName, final Map<?, ?> yamlMap) throws ApexEventException { // Process the mandatory Apex header final ApexEvent apexEvent = processApexEventHeader(eventName, yamlMap); @@ -236,16 +232,10 @@ public class Apex2YamlEventConverter implements ApexEventProtocolConverter { * @throws ApexEventRuntimeException the apex event runtime exception * @throws ApexEventException on invalid events with missing header fields */ - private ApexEvent processApexEventHeader(final String eventName, final Map<?, ?> yamlMap) + private ApexEvent processApexEventHeader(final String eventName, final Map<?, ?> yamlMap) throws ApexEventException { - // Get the event header fields - // @formatter:off - String name = getYamlStringField(yamlMap, ApexEvent.NAME_HEADER_FIELD, yamlPars.getNameAlias(), ApexEvent.NAME_REGEXP, false); - String version = getYamlStringField(yamlMap, ApexEvent.VERSION_HEADER_FIELD, yamlPars.getVersionAlias(), ApexEvent.VERSION_REGEXP, false); - String namespace = getYamlStringField(yamlMap, ApexEvent.NAMESPACE_HEADER_FIELD, yamlPars.getNameSpaceAlias(), ApexEvent.NAMESPACE_REGEXP, false); - String source = getYamlStringField(yamlMap, ApexEvent.SOURCE_HEADER_FIELD, yamlPars.getSourceAlias(), ApexEvent.SOURCE_REGEXP, false); - String target = getYamlStringField(yamlMap, ApexEvent.TARGET_HEADER_FIELD, yamlPars.getTargetAlias(), ApexEvent.TARGET_REGEXP, false); - // @formatter:on + String name = getYamlStringField(yamlMap, ApexEvent.NAME_HEADER_FIELD, yamlPars.getNameAlias(), + ApexEvent.NAME_REGEXP, false); // Check that an event name has been specified if (name == null && eventName == null) { @@ -256,14 +246,16 @@ public class Apex2YamlEventConverter implements ApexEventProtocolConverter { // Check if an event name was specified on the event parameters if (eventName != null) { if (name != null && !eventName.equals(name)) { - LOGGER.warn("The incoming event name \"{}\" does not match the configured event name \"{}\", using configured event name", - name, eventName); + LOGGER.warn("The incoming event name \"{}\" does not match the configured event name \"{}\", " + + "using configured event name", name, eventName); } name = eventName; } // Now, find the event definition in the model service. If version is null, the newest event // definition in the model service is used + String version = getYamlStringField(yamlMap, ApexEvent.VERSION_HEADER_FIELD, yamlPars.getVersionAlias(), + ApexEvent.VERSION_REGEXP, false); final AxEvent eventDefinition = ModelService.getModel(AxEvents.class).get(name, version); if (eventDefinition == null) { throw new ApexEventRuntimeException("an event definition for an event named \"" + name @@ -276,6 +268,8 @@ public class Apex2YamlEventConverter implements ApexEventProtocolConverter { } // Check the name space is OK if it is defined, if not, use the name space from the model + String namespace = getYamlStringField(yamlMap, ApexEvent.NAMESPACE_HEADER_FIELD, yamlPars.getNameSpaceAlias(), + ApexEvent.NAMESPACE_REGEXP, false); if (namespace != null) { if (!namespace.equals(eventDefinition.getNameSpace())) { throw new ApexEventRuntimeException("namespace \"" + namespace + "\" on event \"" + name @@ -287,11 +281,15 @@ public class Apex2YamlEventConverter implements ApexEventProtocolConverter { } // For source, use the defined source only if the source is not found on the incoming event + String source = getYamlStringField(yamlMap, ApexEvent.SOURCE_HEADER_FIELD, yamlPars.getSourceAlias(), + ApexEvent.SOURCE_REGEXP, false); if (source == null) { source = eventDefinition.getSource(); } // For target, use the defined source only if the source is not found on the incoming event + String target = getYamlStringField(yamlMap, ApexEvent.TARGET_HEADER_FIELD, yamlPars.getTargetAlias(), + ApexEvent.TARGET_REGEXP, false); if (target == null) { target = eventDefinition.getTarget(); } @@ -302,22 +300,16 @@ public class Apex2YamlEventConverter implements ApexEventProtocolConverter { /** * This method gets an event string field from a JSON object. * - * @param yamlMap - * the YAML containing the YAML representation of the incoming event - * @param fieldName - * the field name to find in the event - * @param fieldAlias - * the alias for the field to find in the event, overrides the field name if it is not null - * @param fieldRE - * the regular expression to check the field against for validity - * @param mandatory - * true if the field is mandatory + * @param yamlMap the YAML containing the YAML representation of the incoming event + * @param fieldName the field name to find in the event + * @param fieldAlias the alias for the field to find in the event, overrides the field name if it is not null + * @param fieldRegexp the regular expression to check the field against for validity + * @param mandatory true if the field is mandatory * @return the value of the field in the JSON object or null if the field is optional - * @throws ApexEventRuntimeException - * the apex event runtime exception + * @throws ApexEventRuntimeException the apex event runtime exception */ private String getYamlStringField(final Map<?, ?> yamlMap, final String fieldName, final String fieldAlias, - final String fieldRE, final boolean mandatory) { + final String fieldRegexp, final boolean mandatory) { // Get the YAML field for the string field final Object yamlField = getYamlField(yamlMap, fieldName, fieldAlias, mandatory); @@ -335,12 +327,12 @@ public class Apex2YamlEventConverter implements ApexEventProtocolConverter { final String fieldValueString = (String) yamlField; // Is regular expression checking required - if (fieldRE == null) { + if (fieldRegexp == null) { return fieldValueString; } // Check the event field against its regular expression - if (!fieldValueString.matches(fieldRE)) { + if (!fieldValueString.matches(fieldRegexp)) { throw new ApexEventRuntimeException( "field \"" + fieldName + "\" with value \"" + fieldValueString + "\" is invalid"); } @@ -351,17 +343,12 @@ public class Apex2YamlEventConverter implements ApexEventProtocolConverter { /** * This method gets an event field from a YAML object. * - * @param yamlMap - * the YAML containing the YAML representation of the incoming event - * @param fieldName - * the field name to find in the event - * @param fieldAlias - * the alias for the field to find in the event, overrides the field name if it is not null - * @param mandatory - * true if the field is mandatory + * @param yamlMap the YAML containing the YAML representation of the incoming event + * @param fieldName the field name to find in the event + * @param fieldAlias the alias for the field to find in the event, overrides the field name if it is not null + * @param mandatory true if the field is mandatory * @return the value of the field in the YAML object or null if the field is optional - * @throws ApexEventRuntimeException - * the apex event runtime exception + * @throws ApexEventRuntimeException the apex event runtime exception */ private Object getYamlField(final Map<?, ?> yamlMap, final String fieldName, final String fieldAlias, final boolean mandatory) { diff --git a/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-yaml/src/main/java/org/onap/policy/apex/plugins/event/protocol/yaml/YamlEventProtocolParameters.java b/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-yaml/src/main/java/org/onap/policy/apex/plugins/event/protocol/yaml/YamlEventProtocolParameters.java index 861e9cd8f..09a7f54aa 100644 --- a/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-yaml/src/main/java/org/onap/policy/apex/plugins/event/protocol/yaml/YamlEventProtocolParameters.java +++ b/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-yaml/src/main/java/org/onap/policy/apex/plugins/event/protocol/yaml/YamlEventProtocolParameters.java @@ -25,7 +25,7 @@ import org.onap.policy.apex.service.parameters.eventprotocol.EventProtocolTextTo /** * Event protocol parameters for YAML as an event protocol. * - * The parameters for this plugin are: + * <p>The parameters for this plugin are: * <ol> * <li>nameAlias: The field in a YAML event to use as an alias for the event name. This parameter is * optional. diff --git a/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-yaml/src/test/java/org/onap/policy/apex/plugins/event/protocol/yaml/TestYamlEventProtocol.java b/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-yaml/src/test/java/org/onap/policy/apex/plugins/event/protocol/yaml/TestYamlEventProtocol.java index c5f6cb781..1d2a1d9f7 100644 --- a/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-yaml/src/test/java/org/onap/policy/apex/plugins/event/protocol/yaml/TestYamlEventProtocol.java +++ b/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-yaml/src/test/java/org/onap/policy/apex/plugins/event/protocol/yaml/TestYamlEventProtocol.java @@ -49,7 +49,16 @@ import org.onap.policy.apex.service.engine.event.impl.filecarrierplugin.consumer import org.onap.policy.apex.service.engine.event.impl.filecarrierplugin.consumer.TextBlock; import org.onap.policy.common.parameters.ParameterService; +/** + * The Class TestYamlEventProtocol. + */ public class TestYamlEventProtocol { + + /** + * 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(); @@ -62,20 +71,20 @@ public class TestYamlEventProtocol { "java.lang.Integer"); schemas.getSchemasMap().put(simpleIntSchema.getKey(), simpleIntSchema); - AxContextSchema simpleDoubleSchema = new AxContextSchema(new AxArtifactKey("SimpleDoubleSchema", "0.0.1"), "JAVA", - "java.lang.Double"); + 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"); + AxContextSchema simpleStringSchema = new AxContextSchema(new AxArtifactKey("SimpleStringSchema", "0.0.1"), + "JAVA", "java.lang.String"); schemas.getSchemasMap().put(simpleStringSchema.getKey(), simpleStringSchema); AxContextSchema arrayListSchema = new AxContextSchema(new AxArtifactKey("ArrayListSchema", "0.0.1"), "JAVA", "java.util.ArrayList"); schemas.getSchemasMap().put(arrayListSchema.getKey(), arrayListSchema); - AxContextSchema linkedHashMapSchema = new AxContextSchema(new AxArtifactKey("LinkedHashMapSchema", "0.0.1"), "JAVA", - "java.util.LinkedHashMap"); + AxContextSchema linkedHashMapSchema = new AxContextSchema(new AxArtifactKey("LinkedHashMapSchema", "0.0.1"), + "JAVA", "java.util.LinkedHashMap"); schemas.getSchemasMap().put(linkedHashMapSchema.getKey(), linkedHashMapSchema); ModelService.registerModel(AxContextSchemas.class, schemas); @@ -105,11 +114,9 @@ public class TestYamlEventProtocol { AxEvent testEvent3 = new AxEvent(new AxArtifactKey("TestEvent3", "0.0.1")); testEvent3.setNameSpace("org.onap.policy.apex.plugins.event.protocol.yaml"); - AxField te3Field0 = new AxField(new AxReferenceKey(testEvent3.getKey(), "american"), - arrayListSchema.getKey()); + AxField te3Field0 = new AxField(new AxReferenceKey(testEvent3.getKey(), "american"), arrayListSchema.getKey()); testEvent3.getParameterMap().put("american", te3Field0); - AxField te3Field1 = new AxField(new AxReferenceKey(testEvent3.getKey(), "national"), - arrayListSchema.getKey()); + AxField te3Field1 = new AxField(new AxReferenceKey(testEvent3.getKey(), "national"), arrayListSchema.getKey()); testEvent3.getParameterMap().put("national", te3Field1); events.getEventMap().put(testEvent3.getKey(), testEvent3); @@ -139,24 +146,19 @@ public class TestYamlEventProtocol { AxEvent testEvent7 = new AxEvent(new AxArtifactKey("TestEvent7", "0.0.1")); testEvent7.setNameSpace("org.onap.policy.apex.plugins.event.protocol.yaml"); - AxField te7Field0 = new AxField(new AxReferenceKey(testEvent7.getKey(), "time"), - simpleIntSchema.getKey()); + AxField te7Field0 = new AxField(new AxReferenceKey(testEvent7.getKey(), "time"), simpleIntSchema.getKey()); testEvent7.getParameterMap().put("time", te7Field0); - AxField te7Field1 = new AxField(new AxReferenceKey(testEvent7.getKey(), "player"), - simpleStringSchema.getKey()); + AxField te7Field1 = new AxField(new AxReferenceKey(testEvent7.getKey(), "player"), simpleStringSchema.getKey()); testEvent7.getParameterMap().put("player", te7Field1); - AxField te7Field2 = new AxField(new AxReferenceKey(testEvent7.getKey(), "action"), - simpleStringSchema.getKey()); + AxField te7Field2 = new AxField(new AxReferenceKey(testEvent7.getKey(), "action"), simpleStringSchema.getKey()); testEvent7.getParameterMap().put("action", te7Field2); events.getEventMap().put(testEvent7.getKey(), testEvent7); AxEvent testEvent8 = new AxEvent(new AxArtifactKey("TestEvent8", "0.0.1")); testEvent8.setNameSpace("org.onap.policy.apex.plugins.event.protocol.yaml"); - AxField te8Field0 = new AxField(new AxReferenceKey(testEvent8.getKey(), "hr"), - arrayListSchema.getKey()); + AxField te8Field0 = new AxField(new AxReferenceKey(testEvent8.getKey(), "hr"), arrayListSchema.getKey()); testEvent8.getParameterMap().put("hr", te8Field0); - AxField te8Field1 = new AxField(new AxReferenceKey(testEvent8.getKey(), "rbi"), - arrayListSchema.getKey()); + AxField te8Field1 = new AxField(new AxReferenceKey(testEvent8.getKey(), "rbi"), arrayListSchema.getKey()); testEvent8.getParameterMap().put("rbi", te8Field1); events.getEventMap().put(testEvent8.getKey(), testEvent8); @@ -196,12 +198,21 @@ public class TestYamlEventProtocol { ModelService.registerModel(AxEvents.class, events); } + /** + * Unregister test events and schemas. + */ @AfterClass public static void unregisterTestEventsAndSchemas() { ModelService.clear(); ParameterService.clear(); } + /** + * Test yaml processing. + * + * @throws ApexEventException the apex event exception + * @throws IOException Signals that an I/O exception has occurred. + */ @Test public void testYamlProcessing() throws ApexEventException, IOException { try { @@ -227,8 +238,18 @@ public class TestYamlEventProtocol { testYamlDecodeEncode("TestEvent11", 1, 4, "TOSCA0"); } - private void testYamlDecodeEncode(final String eventName, final int eventCount, final int parCount, final String fileName) - throws ApexEventException, IOException { + /** + * Test yaml decode encode. + * + * @param eventName the event name + * @param eventCount the event count + * @param parCount the par count + * @param fileName the file name + * @throws ApexEventException the apex event exception + * @throws IOException Signals that an I/O exception has occurred. + */ + private void testYamlDecodeEncode(final String eventName, final int eventCount, final int parCount, + final String fileName) throws ApexEventException, IOException { YamlEventProtocolParameters parameters = new YamlEventProtocolParameters(); parameters.setDelimiterAtStart(false); @@ -239,16 +260,17 @@ public class TestYamlEventProtocol { FileInputStream fileInputStream = new FileInputStream(new File(filePath)); HeaderDelimitedTextBlockReader reader = new HeaderDelimitedTextBlockReader(parameters); reader.init(fileInputStream); - + List<ApexEvent> eventList = new ArrayList<>(); - + TextBlock textBlock; do { - textBlock = reader.readTextBlock(); - - eventList.addAll(converter.toApexEvent(eventName, textBlock.getText())); - } while (!textBlock.isEndOfText()); - + textBlock = reader.readTextBlock(); + + eventList.addAll(converter.toApexEvent(eventName, textBlock.getText())); + } + while (!textBlock.isEndOfText()); + fileInputStream.close(); assertEquals(eventCount, eventList.size()); @@ -257,7 +279,8 @@ public class TestYamlEventProtocol { assertEquals(parCount, eventList.get(0).size()); String eventYaml = (String) converter.fromApexEvent(eventList.get(eventNo)); - String expectedYaml = TextFileUtils.getTextFileAsString("src/test/resources/yaml_out/" + fileName + '_' + eventNo + ".yaml"); + String expectedYaml = TextFileUtils + .getTextFileAsString("src/test/resources/yaml_out/" + fileName + '_' + eventNo + ".yaml"); assertEquals(expectedYaml.replaceAll("\\s*", ""), eventYaml.replaceAll("\\s*", "")); } } diff --git a/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-yaml/src/test/java/org/onap/policy/apex/plugins/event/protocol/yaml/TestYamlPluginStability.java b/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-yaml/src/test/java/org/onap/policy/apex/plugins/event/protocol/yaml/TestYamlPluginStability.java index 57b4b72d6..d033de56d 100644 --- a/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-yaml/src/test/java/org/onap/policy/apex/plugins/event/protocol/yaml/TestYamlPluginStability.java +++ b/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-yaml/src/test/java/org/onap/policy/apex/plugins/event/protocol/yaml/TestYamlPluginStability.java @@ -17,6 +17,7 @@ * SPDX-License-Identifier: Apache-2.0 * ============LICENSE_END========================================================= */ + package org.onap.policy.apex.plugins.event.protocol.yaml; import static org.junit.Assert.assertEquals; @@ -44,9 +45,17 @@ import org.onap.policy.apex.service.engine.event.ApexEventException; import org.onap.policy.apex.service.engine.event.ApexEventRuntimeException; import org.onap.policy.common.parameters.ParameterService; +/** + * The Class TestYamlPluginStability. + */ public class TestYamlPluginStability { 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(); @@ -69,8 +78,6 @@ public class TestYamlPluginStability { ModelService.registerModel(AxContextSchemas.class, schemas); - AxEvents events = new AxEvents(); - 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()); @@ -81,17 +88,27 @@ public class TestYamlPluginStability { 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(); @@ -220,7 +237,8 @@ public class TestYamlPluginStability { e.getMessage().substring(0, 77)); } - yamlInputString = "doubleValue: 123.45\n" + "intValue: 123\n" + "stringValue: org.onap.policy.apex.plugins.event.protocol.yaml"; + 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()); @@ -249,7 +267,7 @@ public class TestYamlPluginStability { } pars.setTargetAlias(null); - yamlInputString = "doubleValue: 123.45\n" + "intValue: ~\n"+ "stringValue: MyString"; + yamlInputString = "doubleValue: 123.45\n" + "intValue: ~\n" + "stringValue: MyString"; try { converter.toApexEvent("TestEvent", yamlInputString); fail("this test should throw an exception"); |