diff options
Diffstat (limited to 'services/services-engine/src/test/java/org')
17 files changed, 3284 insertions, 0 deletions
diff --git a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/JSONEventGenerator.java b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/JSONEventGenerator.java new file mode 100644 index 000000000..4da8c4201 --- /dev/null +++ b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/JSONEventGenerator.java @@ -0,0 +1,442 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-2018 Ericsson. All rights reserved. + * ================================================================================ + * 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.service.engine.event; + +import java.util.Random; + +/** + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public class JSONEventGenerator { + private static int nextEventNo = 0; + + public static String jsonEvents(final int eventCount) { + final StringBuilder builder = new StringBuilder(); + + for (int i = 0; i < eventCount; i++) { + if (i > 0) { + builder.append("\n"); + } + builder.append(jsonEvent()); + } + + return builder.toString(); + } + + public static String jsonEvent() { + final Random rand = new Random(); + + final StringBuilder builder = new StringBuilder(); + + int nextEventNo = rand.nextInt(2); + final String eventName = (nextEventNo == 0 ? "Event0000" : "Event0100"); + final int nextMatchCase = rand.nextInt(4); + final float nextTestTemperature = rand.nextFloat() * 10000; + + builder.append("{\n"); + builder.append(" \"nameSpace\": \"org.onap.policy.apex.sample.events\",\n"); + builder.append(" \"name\": \"" + eventName + "\",\n"); + builder.append(" \"version\": \"0.0.1\",\n"); + builder.append(" \"source\": \"test\",\n"); + builder.append(" \"target\": \"apex\",\n"); + builder.append(" \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n"); + builder.append(" \"TestMatchCase\": " + nextMatchCase + ",\n"); + builder.append(" \"TestTimestamp\": " + System.currentTimeMillis() + ",\n"); + builder.append(" \"TestTemperature\": " + nextTestTemperature + "\n"); + builder.append("}"); + + return builder.toString(); + } + + public static String jsonEventNoName() { + final Random rand = new Random(); + + final StringBuilder builder = new StringBuilder(); + + int nextEventNo = rand.nextInt(2); + final String eventName = (nextEventNo == 0 ? "Event0000" : "Event0100"); + final int nextMatchCase = rand.nextInt(4); + final float nextTestTemperature = rand.nextFloat() * 10000; + + builder.append("{\n"); + builder.append(" \"nameSpace\": \"org.onap.policy.apex.sample.events\",\n"); + builder.append(" \"namez\": \"" + eventName + "\",\n"); + builder.append(" \"version\": \"0.0.1\",\n"); + builder.append(" \"source\": \"test\",\n"); + builder.append(" \"target\": \"apex\",\n"); + builder.append(" \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n"); + builder.append(" \"TestMatchCase\": " + nextMatchCase + ",\n"); + builder.append(" \"TestTimestamp\": " + System.currentTimeMillis() + ",\n"); + builder.append(" \"TestTemperature\": " + nextTestTemperature + "\n"); + builder.append("}"); + + return builder.toString(); + } + + public static String jsonEventBadName() { + final Random rand = new Random(); + + final StringBuilder builder = new StringBuilder(); + + int nextEventNo = rand.nextInt(2); + final int nextMatchCase = rand.nextInt(4); + final float nextTestTemperature = rand.nextFloat() * 10000; + + builder.append("{\n"); + builder.append(" \"nameSpace\": \"org.onap.policy.apex.sample.events\",\n"); + builder.append(" \"name\": \"%%%%\",\n"); + builder.append(" \"version\": \"0.0.1\",\n"); + builder.append(" \"source\": \"test\",\n"); + builder.append(" \"target\": \"apex\",\n"); + builder.append(" \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n"); + builder.append(" \"TestMatchCase\": " + nextMatchCase + ",\n"); + builder.append(" \"TestTimestamp\": " + System.currentTimeMillis() + ",\n"); + builder.append(" \"TestTemperature\": " + nextTestTemperature + "\n"); + builder.append("}"); + + return builder.toString(); + } + + public static String jsonEventNoExName() { + final Random rand = new Random(); + + final StringBuilder builder = new StringBuilder(); + + int nextEventNo = rand.nextInt(2); + final int nextMatchCase = rand.nextInt(4); + final float nextTestTemperature = rand.nextFloat() * 10000; + + builder.append("{\n"); + builder.append(" \"nameSpace\": \"org.onap.policy.apex.sample.events\",\n"); + builder.append(" \"name\": \"I_DONT_EXIST\",\n"); + builder.append(" \"source\": \"test\",\n"); + builder.append(" \"target\": \"apex\",\n"); + builder.append(" \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n"); + builder.append(" \"TestMatchCase\": " + nextMatchCase + ",\n"); + builder.append(" \"TestTimestamp\": " + System.currentTimeMillis() + ",\n"); + builder.append(" \"TestTemperature\": " + nextTestTemperature + "\n"); + builder.append("}"); + + return builder.toString(); + } + + public static String jsonEventNoVersion() { + final Random rand = new Random(); + + final StringBuilder builder = new StringBuilder(); + + int nextEventNo = rand.nextInt(2); + final String eventName = (nextEventNo == 0 ? "Event0000" : "Event0100"); + final int nextMatchCase = rand.nextInt(4); + final float nextTestTemperature = rand.nextFloat() * 10000; + + builder.append("{\n"); + builder.append(" \"nameSpace\": \"org.onap.policy.apex.sample.events\",\n"); + builder.append(" \"name\": \"" + eventName + "\",\n"); + builder.append(" \"versiion\": \"0.0.1\",\n"); + builder.append(" \"source\": \"test\",\n"); + builder.append(" \"target\": \"apex\",\n"); + builder.append(" \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n"); + builder.append(" \"TestMatchCase\": " + nextMatchCase + ",\n"); + builder.append(" \"TestTimestamp\": " + System.currentTimeMillis() + ",\n"); + builder.append(" \"TestTemperature\": " + nextTestTemperature + "\n"); + builder.append("}"); + + return builder.toString(); + } + + public static String jsonEventBadVersion() { + final Random rand = new Random(); + + final StringBuilder builder = new StringBuilder(); + + int nextEventNo = rand.nextInt(2); + final String eventName = (nextEventNo == 0 ? "Event0000" : "Event0100"); + final int nextMatchCase = rand.nextInt(4); + final float nextTestTemperature = rand.nextFloat() * 10000; + + builder.append("{\n"); + builder.append(" \"nameSpace\": \"org.onap.policy.apex.sample.events\",\n"); + builder.append(" \"name\": \"" + eventName + "\",\n"); + builder.append(" \"version\": \"#####\",\n"); + builder.append(" \"source\": \"test\",\n"); + builder.append(" \"target\": \"apex\",\n"); + builder.append(" \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n"); + builder.append(" \"TestMatchCase\": " + nextMatchCase + ",\n"); + builder.append(" \"TestTimestamp\": " + System.currentTimeMillis() + ",\n"); + builder.append(" \"TestTemperature\": " + nextTestTemperature + "\n"); + builder.append("}"); + + return builder.toString(); + } + + public static String jsonEventNoExVersion() { + final Random rand = new Random(); + + final StringBuilder builder = new StringBuilder(); + + int nextEventNo = rand.nextInt(2); + final int nextMatchCase = rand.nextInt(4); + final float nextTestTemperature = rand.nextFloat() * 10000; + + builder.append("{\n"); + builder.append(" \"nameSpace\": \"org.onap.policy.apex.sample.events\",\n"); + builder.append(" \"name\": \"Event0000\",\n"); + builder.append(" \"version\": \"1.2.3\",\n"); + builder.append(" \"source\": \"test\",\n"); + builder.append(" \"target\": \"apex\",\n"); + builder.append(" \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n"); + builder.append(" \"TestMatchCase\": " + nextMatchCase + ",\n"); + builder.append(" \"TestTimestamp\": " + System.currentTimeMillis() + ",\n"); + builder.append(" \"TestTemperature\": " + nextTestTemperature + "\n"); + builder.append("}"); + + return builder.toString(); + } + + public static String jsonEventNoNamespace() { + final Random rand = new Random(); + + final StringBuilder builder = new StringBuilder(); + + int nextEventNo = rand.nextInt(2); + final String eventName = (nextEventNo == 0 ? "Event0000" : "Event0100"); + final int nextMatchCase = rand.nextInt(4); + final float nextTestTemperature = rand.nextFloat() * 10000; + + builder.append("{\n"); + builder.append(" \"nameSpacee\": \"org.onap.policy.apex.sample.events\",\n"); + builder.append(" \"name\": \"" + eventName + "\",\n"); + builder.append(" \"version\": \"0.0.1\",\n"); + builder.append(" \"source\": \"test\",\n"); + builder.append(" \"target\": \"apex\",\n"); + builder.append(" \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n"); + builder.append(" \"TestMatchCase\": " + nextMatchCase + ",\n"); + builder.append(" \"TestTimestamp\": " + System.currentTimeMillis() + ",\n"); + builder.append(" \"TestTemperature\": " + nextTestTemperature + "\n"); + builder.append("}"); + + return builder.toString(); + } + + public static String jsonEventBadNamespace() { + final Random rand = new Random(); + + final StringBuilder builder = new StringBuilder(); + + int nextEventNo = rand.nextInt(2); + final String eventName = (nextEventNo == 0 ? "Event0000" : "Event0100"); + final int nextMatchCase = rand.nextInt(4); + final float nextTestTemperature = rand.nextFloat() * 10000; + + builder.append("{\n"); + builder.append(" \"nameSpace\": \"hello.&&&&\",\n"); + builder.append(" \"name\": \"" + eventName + "\",\n"); + builder.append(" \"version\": \"0.0.1\",\n"); + builder.append(" \"source\": \"test\",\n"); + builder.append(" \"target\": \"apex\",\n"); + builder.append(" \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n"); + builder.append(" \"TestMatchCase\": " + nextMatchCase + ",\n"); + builder.append(" \"TestTimestamp\": " + System.currentTimeMillis() + ",\n"); + builder.append(" \"TestTemperature\": " + nextTestTemperature + "\n"); + builder.append("}"); + + return builder.toString(); + } + + public static String jsonEventNoExNamespace() { + final Random rand = new Random(); + + final StringBuilder builder = new StringBuilder(); + + int nextEventNo = rand.nextInt(2); + final int nextMatchCase = rand.nextInt(4); + final float nextTestTemperature = rand.nextFloat() * 10000; + + builder.append("{\n"); + builder.append(" \"nameSpace\": \"pie.in.the.sky\",\n"); + builder.append(" \"name\": \"Event0000\",\n"); + builder.append(" \"version\": \"0.0.1\",\n"); + builder.append(" \"source\": \"test\",\n"); + builder.append(" \"target\": \"apex\",\n"); + builder.append(" \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n"); + builder.append(" \"TestMatchCase\": " + nextMatchCase + ",\n"); + builder.append(" \"TestTimestamp\": " + System.currentTimeMillis() + ",\n"); + builder.append(" \"TestTemperature\": " + nextTestTemperature + "\n"); + builder.append("}"); + + return builder.toString(); + } + + public static String jsonEventNoSource() { + final Random rand = new Random(); + + final StringBuilder builder = new StringBuilder(); + + int nextEventNo = rand.nextInt(2); + final String eventName = (nextEventNo == 0 ? "Event0000" : "Event0100"); + final int nextMatchCase = rand.nextInt(4); + final float nextTestTemperature = rand.nextFloat() * 10000; + + builder.append("{\n"); + builder.append(" \"nameSpace\": \"org.onap.policy.apex.sample.events\",\n"); + builder.append(" \"name\": \"" + eventName + "\",\n"); + builder.append(" \"version\": \"0.0.1\",\n"); + builder.append(" \"sourcee\": \"test\",\n"); + builder.append(" \"target\": \"apex\",\n"); + builder.append(" \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n"); + builder.append(" \"TestMatchCase\": " + nextMatchCase + ",\n"); + builder.append(" \"TestTimestamp\": " + System.currentTimeMillis() + ",\n"); + builder.append(" \"TestTemperature\": " + nextTestTemperature + "\n"); + builder.append("}"); + + return builder.toString(); + } + + public static String jsonEventBadSource() { + final Random rand = new Random(); + + final StringBuilder builder = new StringBuilder(); + + int nextEventNo = rand.nextInt(2); + final String eventName = (nextEventNo == 0 ? "Event0000" : "Event0100"); + final int nextMatchCase = rand.nextInt(4); + final float nextTestTemperature = rand.nextFloat() * 10000; + + builder.append("{\n"); + builder.append(" \"nameSpace\": \"org.onap.policy.apex.sample.events\",\n"); + builder.append(" \"name\": \"" + eventName + "\",\n"); + builder.append(" \"version\": \"0.0.1\",\n"); + builder.append(" \"source\": \"%!@**@!\",\n"); + builder.append(" \"target\": \"apex\",\n"); + builder.append(" \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n"); + builder.append(" \"TestMatchCase\": " + nextMatchCase + ",\n"); + builder.append(" \"TestTimestamp\": " + System.currentTimeMillis() + ",\n"); + builder.append(" \"TestTemperature\": " + nextTestTemperature + "\n"); + builder.append("}"); + + return builder.toString(); + } + + public static String jsonEventNoTarget() { + final Random rand = new Random(); + + final StringBuilder builder = new StringBuilder(); + + int nextEventNo = rand.nextInt(2); + final String eventName = (nextEventNo == 0 ? "Event0000" : "Event0100"); + final int nextMatchCase = rand.nextInt(4); + final float nextTestTemperature = rand.nextFloat() * 10000; + + builder.append("{\n"); + builder.append(" \"nameSpace\": \"org.onap.policy.apex.sample.events\",\n"); + builder.append(" \"name\": \"" + eventName + "\",\n"); + builder.append(" \"version\": \"0.0.1\",\n"); + builder.append(" \"source\": \"test\",\n"); + builder.append(" \"targett\": \"apex\",\n"); + builder.append(" \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n"); + builder.append(" \"TestMatchCase\": " + nextMatchCase + ",\n"); + builder.append(" \"TestTimestamp\": " + System.currentTimeMillis() + ",\n"); + builder.append(" \"TestTemperature\": " + nextTestTemperature + "\n"); + builder.append("}"); + + return builder.toString(); + } + + public static String jsonEventBadTarget() { + final Random rand = new Random(); + + final StringBuilder builder = new StringBuilder(); + + int nextEventNo = rand.nextInt(2); + final String eventName = (nextEventNo == 0 ? "Event0000" : "Event0100"); + final int nextMatchCase = rand.nextInt(4); + final float nextTestTemperature = rand.nextFloat() * 10000; + + builder.append("{\n"); + builder.append(" \"nameSpace\": \"org.onap.policy.apex.sample.events\",\n"); + builder.append(" \"name\": \"" + eventName + "\",\n"); + builder.append(" \"version\": \"0.0.1\",\n"); + builder.append(" \"source\": \"test\",\n"); + builder.append(" \"target\": \"KNIO(*S)A(S)D\",\n"); + builder.append(" \"TestSlogan\": \"Test slogan for External Event" + (nextEventNo++) + "\",\n"); + builder.append(" \"TestMatchCase\": " + nextMatchCase + ",\n"); + builder.append(" \"TestTimestamp\": " + System.currentTimeMillis() + ",\n"); + builder.append(" \"TestTemperature\": " + nextTestTemperature + "\n"); + builder.append("}"); + + return builder.toString(); + } + + public static String jsonEventMissingFields() { + final StringBuilder builder = new StringBuilder(); + + builder.append("{\n"); + builder.append(" \"nameSpace\": \"org.onap.policy.apex.sample.events\",\n"); + builder.append(" \"name\": \"Event0000\",\n"); + builder.append(" \"version\": \"0.0.1\",\n"); + builder.append(" \"source\": \"test\",\n"); + builder.append(" \"target\": \"apex\"\n"); + builder.append("}"); + + return builder.toString(); + } + + public static String jsonEventNullFields() { + final StringBuilder builder = new StringBuilder(); + + builder.append("{\n"); + builder.append(" \"nameSpace\": \"org.onap.policy.apex.sample.events\",\n"); + builder.append(" \"name\": \"Event0000\",\n"); + builder.append(" \"version\": \"0.0.1\",\n"); + builder.append(" \"source\": \"test\",\n"); + builder.append(" \"target\": \"Apex\",\n"); + builder.append(" \"TestSlogan\": null,\n"); + builder.append(" \"TestMatchCase\": -1,\n"); + builder.append(" \"TestTimestamp\": -1,\n"); + builder.append(" \"TestTemperature\": -1.0\n"); + builder.append("}"); + + return builder.toString(); + } + + public static void main(final String[] args) { + if (args.length != 1) { + System.err.println("usage EventGenerator #events"); + return; + } + + int eventCount = 0; + try { + eventCount = Integer.parseInt(args[0]); + } catch (final Exception e) { + System.err.println("usage EventGenerator #events"); + e.printStackTrace(); + return; + } + + System.out.println(jsonEvents(eventCount)); + } + + public static int getNextEventNo() { + return nextEventNo; + } +} diff --git a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/TestJSONEventHandler.java b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/TestJSONEventHandler.java new file mode 100644 index 000000000..be61b165e --- /dev/null +++ b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/TestJSONEventHandler.java @@ -0,0 +1,316 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-2018 Ericsson. All rights reserved. + * ================================================================================ + * 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.service.engine.event; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.util.Date; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.onap.policy.apex.context.parameters.SchemaParameters; +import org.onap.policy.apex.model.basicmodel.concepts.ApexException; +import org.onap.policy.apex.model.basicmodel.handling.ApexModelException; +import org.onap.policy.apex.model.basicmodel.handling.ApexModelReader; +import org.onap.policy.apex.model.basicmodel.service.ModelService; +import org.onap.policy.apex.model.eventmodel.concepts.AxEvent; +import org.onap.policy.apex.model.eventmodel.concepts.AxEvents; +import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel; +import org.onap.policy.apex.model.utilities.TextFileUtils; +import org.onap.policy.apex.service.engine.event.impl.jsonprotocolplugin.Apex2JSONEventConverter; +import org.onap.policy.apex.service.engine.event.impl.jsonprotocolplugin.JSONEventProtocolParameters; +import org.slf4j.ext.XLogger; +import org.slf4j.ext.XLoggerFactory; + +/** + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public class TestJSONEventHandler { + private static final XLogger logger = XLoggerFactory.getXLogger(TestJSONEventHandler.class); + + @BeforeClass + public static void setupEventModel() throws IOException, ApexModelException { + final String policyModelString = + TextFileUtils.getTextFileAsString("src/test/resources/policymodels/SamplePolicyModelMVEL.json"); + final ApexModelReader<AxPolicyModel> modelReader = new ApexModelReader<AxPolicyModel>(AxPolicyModel.class); + final AxPolicyModel apexPolicyModel = modelReader.read(new ByteArrayInputStream(policyModelString.getBytes())); + + // Set up the models in the model service + apexPolicyModel.register(); + } + + @Before + public void initializeDefaultSchemaParameters() { + new SchemaParameters(); + } + + @Test + public void testJSONtoApexEvent() throws ApexException { + try { + final Apex2JSONEventConverter jsonEventConverter = new Apex2JSONEventConverter(); + assertNotNull(jsonEventConverter); + jsonEventConverter.init(new JSONEventProtocolParameters()); + + final String apexEventJSONStringIn = JSONEventGenerator.jsonEvent(); + + logger.debug("input event\n" + apexEventJSONStringIn); + + final List<ApexEvent> apexEventList = jsonEventConverter.toApexEvent(null, apexEventJSONStringIn); + for (final ApexEvent apexEvent : apexEventList) { + assertNotNull(apexEvent); + + logger.debug(apexEvent.toString()); + + assertTrue(apexEvent.getName().equals("Event0000") || apexEvent.getName().equals("Event0100")); + assertTrue(apexEvent.getVersion().equals("0.0.1")); + assertTrue(apexEvent.getNameSpace().equals("org.onap.policy.apex.sample.events")); + assertTrue(apexEvent.getSource().equals("test")); + assertTrue(apexEvent.getTarget().equals("apex")); + assertTrue(apexEvent.get("TestSlogan").toString().startsWith("Test slogan for External Event")); + + final Object testMatchCaseSelected = apexEvent.get("TestMatchCaseSelected"); + assertTrue(testMatchCaseSelected == null); + } + } catch (final Exception e) { + e.printStackTrace(); + throw new ApexException("Exception reading Apex event JSON file", e); + } + } + + @Test + public void testJSONtoApexBadEvent() throws ApexException { + try { + final Apex2JSONEventConverter jsonEventConverter = new Apex2JSONEventConverter(); + assertNotNull(jsonEventConverter); + jsonEventConverter.init(new JSONEventProtocolParameters()); + + String apexEventJSONStringIn = null; + + try { + apexEventJSONStringIn = JSONEventGenerator.jsonEventNoName(); + jsonEventConverter.toApexEvent(null, apexEventJSONStringIn); + fail("Test should throw an exception here"); + } catch (final ApexEventException e) { + assertEquals("Failed to unmarshal JSON event: event received without mandatory parameter \"name\" ", + e.getMessage().substring(0, 82)); + } + + try { + apexEventJSONStringIn = JSONEventGenerator.jsonEventBadName(); + jsonEventConverter.toApexEvent(null, apexEventJSONStringIn); + fail("Test should throw an exception here"); + } catch (final ApexEventException e) { + assertTrue(e.getMessage() + .startsWith("Failed to unmarshal JSON event: field \"name\" with value \"%%%%\" is invalid")); + } + + try { + apexEventJSONStringIn = JSONEventGenerator.jsonEventNoExName(); + jsonEventConverter.toApexEvent(null, apexEventJSONStringIn); + fail("Test should throw an exception here"); + } catch (final ApexEventException e) { + assertEquals("Failed to unmarshal JSON event: an event definition for an event named \"I_DONT_EXI", + e.getMessage().substring(0, 82)); + } + + apexEventJSONStringIn = JSONEventGenerator.jsonEventNoVersion(); + ApexEvent event = jsonEventConverter.toApexEvent(null, apexEventJSONStringIn).get(0); + assertEquals("0.0.1", event.getVersion()); + + try { + apexEventJSONStringIn = JSONEventGenerator.jsonEventBadVersion(); + jsonEventConverter.toApexEvent(null, apexEventJSONStringIn); + fail("Test should throw an exception here"); + } catch (final ApexEventException e) { + assertTrue(e.getMessage().startsWith( + "Failed to unmarshal JSON event: field \"version\" with value \"#####\" is invalid")); + } + + try { + apexEventJSONStringIn = JSONEventGenerator.jsonEventNoExVersion(); + jsonEventConverter.toApexEvent(null, apexEventJSONStringIn); + fail("Test should throw an exception here"); + } catch (final ApexEventException e) { + assertTrue(e.getMessage().startsWith( + "Failed to unmarshal JSON event: an event definition for an event named \"Event0000\" with version \"1.2.3\" not found in Apex model")); + } + + apexEventJSONStringIn = JSONEventGenerator.jsonEventNoNamespace(); + event = jsonEventConverter.toApexEvent(null, apexEventJSONStringIn).get(0); + assertEquals("org.onap.policy.apex.sample.events", event.getNameSpace()); + + try { + apexEventJSONStringIn = JSONEventGenerator.jsonEventBadNamespace(); + jsonEventConverter.toApexEvent(null, apexEventJSONStringIn); + fail("Test should throw an exception here"); + } catch (final ApexEventException e) { + assertTrue(e.getMessage().startsWith( + "Failed to unmarshal JSON event: field \"nameSpace\" with value \"hello.&&&&\" is invalid")); + } + + try { + apexEventJSONStringIn = JSONEventGenerator.jsonEventNoExNamespace(); + jsonEventConverter.toApexEvent(null, apexEventJSONStringIn); + fail("Test should throw an exception here"); + } catch (final ApexEventException e) { + assertTrue(e.getMessage().startsWith( + "Failed to unmarshal JSON event: namespace \"pie.in.the.sky\" on event \"Event0000\" " + + "does not match namespace \"org.onap.policy.apex.sample.events\" for that event in the Apex model")); + } + + apexEventJSONStringIn = JSONEventGenerator.jsonEventNoSource(); + event = jsonEventConverter.toApexEvent(null, apexEventJSONStringIn).get(0); + assertEquals("Outside", event.getSource()); + + try { + apexEventJSONStringIn = JSONEventGenerator.jsonEventBadSource(); + jsonEventConverter.toApexEvent(null, apexEventJSONStringIn); + fail("Test should throw an exception here"); + } catch (final ApexEventException e) { + assertTrue(e.getMessage().startsWith( + "Failed to unmarshal JSON event: field \"source\" with value \"%!@**@!\" is invalid")); + } + + apexEventJSONStringIn = JSONEventGenerator.jsonEventNoTarget(); + event = jsonEventConverter.toApexEvent(null, apexEventJSONStringIn).get(0); + assertEquals("Match", event.getTarget()); + + try { + apexEventJSONStringIn = JSONEventGenerator.jsonEventBadTarget(); + jsonEventConverter.toApexEvent(null, apexEventJSONStringIn); + fail("Test should throw an exception here"); + } catch (final ApexEventException e) { + assertTrue(e.getMessage().startsWith( + "Failed to unmarshal JSON event: field \"target\" with value \"KNIO(*S)A(S)D\" is invalid")); + } + + try { + apexEventJSONStringIn = JSONEventGenerator.jsonEventMissingFields(); + jsonEventConverter.toApexEvent(null, apexEventJSONStringIn); + fail("Test should throw an exception here"); + } catch (final ApexEventException e) { + assertTrue(e.getMessage().startsWith("Failed to unmarshal JSON event: error parsing Event0000:0.0.1 " + + "event from Json. Field \"TestMatchCase\" is missing, but is mandatory.")); + } + + apexEventJSONStringIn = JSONEventGenerator.jsonEventNullFields(); + event = jsonEventConverter.toApexEvent(null, apexEventJSONStringIn).get(0); + assertEquals(event.get("TestSlogan"), null); + assertEquals(event.get("TestMatchCase"), (byte) -1); + assertEquals(event.get("TestTimestamp"), (long) -1); + assertEquals(event.get("TestTemperature"), -1.0); + + // Set the missing fields as optional in the model + final AxEvent eventDefinition = ModelService.getModel(AxEvents.class).get("Event0000"); + eventDefinition.getParameterMap().get("TestSlogan").setOptional(true); + eventDefinition.getParameterMap().get("TestMatchCase").setOptional(true); + eventDefinition.getParameterMap().get("TestTimestamp").setOptional(true); + eventDefinition.getParameterMap().get("TestTemperature").setOptional(true); + + apexEventJSONStringIn = JSONEventGenerator.jsonEventMissingFields(); + event = jsonEventConverter.toApexEvent(null, apexEventJSONStringIn).get(0); + assertEquals(null, event.get("TestSlogan")); + assertEquals(null, event.get("TestMatchCase")); + assertEquals(null, event.get("TestTimestamp")); + assertEquals(null, event.get("TestTemperature")); + } catch (final Exception e) { + e.printStackTrace(); + throw new ApexException("Exception reading Apex event JSON file", e); + } + } + + @Test + public void testApexEventToJSON() throws ApexException { + try { + final Apex2JSONEventConverter jsonEventConverter = new Apex2JSONEventConverter(); + assertNotNull(jsonEventConverter); + + final Date event0000StartTime = new Date(); + final Map<String, Object> event0000DataMap = new HashMap<String, Object>(); + event0000DataMap.put("TestSlogan", "This is a test slogan"); + event0000DataMap.put("TestMatchCase", 12345); + event0000DataMap.put("TestTimestamp", event0000StartTime.getTime()); + event0000DataMap.put("TestTemperature", 34.5445667); + + final ApexEvent apexEvent0000 = + new ApexEvent("Event0000", "0.0.1", "org.onap.policy.apex.sample.events", "test", "apex"); + apexEvent0000.putAll(event0000DataMap); + + final String apexEvent0000JSONString = (String) jsonEventConverter.fromApexEvent(apexEvent0000); + + logger.debug(apexEvent0000JSONString); + + assertTrue(apexEvent0000JSONString.contains("\"name\": \"Event0000\"")); + assertTrue(apexEvent0000JSONString.contains("\"version\": \"0.0.1\"")); + assertTrue(apexEvent0000JSONString.contains("\"nameSpace\": \"org.onap.policy.apex.sample.events\"")); + assertTrue(apexEvent0000JSONString.contains("\"source\": \"test\"")); + assertTrue(apexEvent0000JSONString.contains("\"target\": \"apex\"")); + assertTrue(apexEvent0000JSONString.contains("\"TestSlogan\": \"This is a test slogan\"")); + assertTrue(apexEvent0000JSONString.contains("\"TestMatchCase\": 12345")); + assertTrue(apexEvent0000JSONString.contains("\"TestTimestamp\": " + event0000StartTime.getTime())); + assertTrue(apexEvent0000JSONString.contains("\"TestTemperature\": 34.5445667")); + + final Date event0004StartTime = new Date(1434363272000L); + final Map<String, Object> event0004DataMap = new HashMap<String, Object>(); + event0004DataMap.put("TestSlogan", "Test slogan for External Event"); + event0004DataMap.put("TestMatchCase", new Integer(2)); + event0004DataMap.put("TestTimestamp", new Long(event0004StartTime.getTime())); + event0004DataMap.put("TestTemperature", new Double(1064.43)); + event0004DataMap.put("TestMatchCaseSelected", new Integer(2)); + event0004DataMap.put("TestMatchStateTime", new Long(1434370506078L)); + event0004DataMap.put("TestEstablishCaseSelected", new Integer(0)); + event0004DataMap.put("TestEstablishStateTime", new Long(1434370506085L)); + event0004DataMap.put("TestDecideCaseSelected", new Integer(3)); + event0004DataMap.put("TestDecideStateTime", new Long(1434370506092L)); + event0004DataMap.put("TestActCaseSelected", new Integer(2)); + event0004DataMap.put("TestActStateTime", new Long(1434370506095L)); + + final ApexEvent apexEvent0004 = + new ApexEvent("Event0004", "0.0.1", "org.onap.policy.apex.sample.events", "test", "apex"); + apexEvent0004.putAll(event0004DataMap); + + final String apexEvent0004JSONString = (String) jsonEventConverter.fromApexEvent(apexEvent0004); + + logger.debug(apexEvent0004JSONString); + + assertTrue(apexEvent0004JSONString.contains("\"name\": \"Event0004\"")); + assertTrue(apexEvent0004JSONString.contains("\"version\": \"0.0.1\"")); + assertTrue(apexEvent0004JSONString.contains("\"nameSpace\": \"org.onap.policy.apex.sample.events\"")); + assertTrue(apexEvent0004JSONString.contains("\"source\": \"test\"")); + assertTrue(apexEvent0004JSONString.contains("\"target\": \"apex\"")); + assertTrue(apexEvent0004JSONString.contains("\"TestSlogan\": \"Test slogan for External Event\"")); + assertTrue(apexEvent0004JSONString.contains("1434370506078")); + assertTrue(apexEvent0004JSONString.contains("1064.43")); + } catch (final Exception e) { + e.printStackTrace(); + throw new ApexException("Exception reading Apex event JSON file", e); + } + } +} diff --git a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/TestJSONTaggedEventConsumer.java b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/TestJSONTaggedEventConsumer.java new file mode 100644 index 000000000..b19cceec9 --- /dev/null +++ b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/TestJSONTaggedEventConsumer.java @@ -0,0 +1,130 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-2018 Ericsson. All rights reserved. + * ================================================================================ + * 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.service.engine.event; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; + +import org.junit.Test; +import org.onap.policy.apex.service.engine.event.impl.filecarrierplugin.consumer.CharacterDelimitedTextBlockReader; +import org.onap.policy.apex.service.engine.event.impl.filecarrierplugin.consumer.TextBlock; + +/** + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public class TestJSONTaggedEventConsumer { + + @Test + public void testGarbageText() throws IOException { + final InputStream jsonInputStream = new ByteArrayInputStream("hello there".getBytes()); + + final CharacterDelimitedTextBlockReader taggedReader = new CharacterDelimitedTextBlockReader('{', '}'); + taggedReader.init(jsonInputStream); + + final TextBlock textBlock = taggedReader.readTextBlock(); + assertNull(textBlock.getText()); + assertTrue(textBlock.isEndOfText()); + } + + @Test + public void testPartialEvent() throws IOException { + final InputStream jsonInputStream = new ByteArrayInputStream("\"TestTimestamp\": 1469781869268}".getBytes()); + + final CharacterDelimitedTextBlockReader taggedReader = new CharacterDelimitedTextBlockReader('{', '}'); + taggedReader.init(jsonInputStream); + + final TextBlock textBlock = taggedReader.readTextBlock(); + assertNull(textBlock.getText()); + assertTrue(textBlock.isEndOfText()); + } + + @Test + public void testFullEvent() throws IOException { + final InputStream jsonInputStream = new ByteArrayInputStream("{TestTimestamp\": 1469781869268}".getBytes()); + + final CharacterDelimitedTextBlockReader taggedReader = new CharacterDelimitedTextBlockReader('{', '}'); + taggedReader.init(jsonInputStream); + + TextBlock textBlock = taggedReader.readTextBlock(); + assertEquals(textBlock.getText(), "{TestTimestamp\": 1469781869268}"); + + textBlock = taggedReader.readTextBlock(); + assertNull(textBlock.getText()); + assertTrue(textBlock.isEndOfText()); + } + + @Test + public void testFullEventGarbageBefore() throws IOException { + final InputStream jsonInputStream = + new ByteArrayInputStream("Garbage{TestTimestamp\": 1469781869268}".getBytes()); + + final CharacterDelimitedTextBlockReader taggedReader = new CharacterDelimitedTextBlockReader('{', '}'); + taggedReader.init(jsonInputStream); + + TextBlock textBlock = taggedReader.readTextBlock(); + assertEquals(textBlock.getText(), "{TestTimestamp\": 1469781869268}"); + assertFalse(textBlock.isEndOfText()); + + textBlock = taggedReader.readTextBlock(); + assertNull(textBlock.getText()); + assertTrue(textBlock.isEndOfText()); + } + + @Test + public void testFullEventGarbageBeforeAfter() throws IOException { + final InputStream jsonInputStream = + new ByteArrayInputStream("Garbage{TestTimestamp\": 1469781869268}Rubbish".getBytes()); + + final CharacterDelimitedTextBlockReader taggedReader = new CharacterDelimitedTextBlockReader('{', '}'); + taggedReader.init(jsonInputStream); + + TextBlock textBlock = taggedReader.readTextBlock(); + assertEquals(textBlock.getText(), "{TestTimestamp\": 1469781869268}"); + assertFalse(textBlock.isEndOfText()); + + textBlock = taggedReader.readTextBlock(); + assertNull(textBlock.getText()); + assertTrue(textBlock.isEndOfText()); + } + + @Test + public void testFullEventGarbageAfter() throws IOException { + final InputStream jsonInputStream = + new ByteArrayInputStream("{TestTimestamp\": 1469781869268}Rubbish".getBytes()); + + final CharacterDelimitedTextBlockReader taggedReader = new CharacterDelimitedTextBlockReader('{', '}'); + taggedReader.init(jsonInputStream); + + TextBlock textBlock = taggedReader.readTextBlock(); + assertEquals(textBlock.getText(), "{TestTimestamp\": 1469781869268}"); + assertFalse(textBlock.isEndOfText()); + + textBlock = taggedReader.readTextBlock(); + assertNull(textBlock.getText()); + assertTrue(textBlock.isEndOfText()); + } +} diff --git a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/TestPluginFactories.java b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/TestPluginFactories.java new file mode 100644 index 000000000..5d77bfd2a --- /dev/null +++ b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/TestPluginFactories.java @@ -0,0 +1,62 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-2018 Ericsson. All rights reserved. + * ================================================================================ + * 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.service.engine.event; + +import static org.junit.Assert.assertNotNull; + +import java.util.Map.Entry; + +import org.junit.Test; +import org.onap.policy.apex.service.engine.event.impl.EventConsumerFactory; +import org.onap.policy.apex.service.engine.event.impl.EventProducerFactory; +import org.onap.policy.apex.service.engine.main.ApexCommandLineArguments; + +import org.onap.policy.apex.service.parameters.ApexParameterException; +import org.onap.policy.apex.service.parameters.ApexParameterHandler; +import org.onap.policy.apex.service.parameters.ApexParameters; +import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters; + +/** + * @author Liam Fallon (liam.fallon@ericsson.com) + * @author John Keeney (john.keeney@ericsson.com) + */ +public class TestPluginFactories { + + @Test + public void testEventConsumerFactory() throws ApexEventException, ApexParameterException { + final String[] args = {"-c", "src/test/resources/parameters/factoryGoodParams.json"}; + final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); + + final ApexParameters parameters = new ApexParameterHandler().getParameters(arguments); + + for (final Entry<String, EventHandlerParameters> ce : parameters.getEventInputParameters().entrySet()) { + final ApexEventConsumer consumer = new EventConsumerFactory().createConsumer( + parameters.getEngineServiceParameters().getName() + "_consumer_" + ce.getKey(), ce.getValue()); + assertNotNull(consumer); + } + + for (final Entry<String, EventHandlerParameters> pe : parameters.getEventOutputParameters().entrySet()) { + final ApexEventProducer producer = new EventProducerFactory().createProducer( + parameters.getEngineServiceParameters().getName() + "_producer_" + pe.getKey(), pe.getValue()); + assertNotNull(producer); + } + } +} diff --git a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/main/TestApexCommandLineArguments.java b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/main/TestApexCommandLineArguments.java new file mode 100644 index 000000000..247f9b935 --- /dev/null +++ b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/main/TestApexCommandLineArguments.java @@ -0,0 +1,186 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-2018 Ericsson. All rights reserved. + * ================================================================================ + * 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.service.engine.main; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import org.junit.Test; +import org.onap.policy.apex.model.basicmodel.concepts.ApexException; + +/** + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public class TestApexCommandLineArguments { + + @Test + public void testCommandLineArguments() { + final ApexCommandLineArguments apexArguments = new ApexCommandLineArguments(); + + final String[] args00 = {""}; + try { + apexArguments.parse(args00); + apexArguments.validate(); + fail("Test should throw an exception here"); + } catch (final ApexException e) { + assertEquals("Apex configuration file was not specified as an argument", e.getMessage()); + } + + final String[] args01 = {"-h"}; + try { + final String result = apexArguments.parse(args01); + assertTrue(result.startsWith("usage: org.onap.policy.apex.service.engine.main.ApexMain [options...]")); + } catch (final ApexException e) { + e.printStackTrace(); + fail("Test should not throw an exception"); + } + + final String[] args02 = {"-v"}; + try { + final String result = apexArguments.parse(args02); + assertTrue(result.startsWith("Apex Adaptive Policy Engine")); + } catch (final ApexException e) { + e.printStackTrace(); + fail("Test should not throw an exception"); + } + + final String[] args03 = {"-v", "-h"}; + try { + final String result = apexArguments.parse(args03); + assertTrue(result.startsWith("usage: org.onap.policy.apex.service.engine.main.ApexMain [options...]")); + } catch (final ApexException e) { + e.printStackTrace(); + fail("Test should not throw an exception"); + } + + final String[] args04 = {"-h", "-v"}; + try { + final String result = apexArguments.parse(args04); + assertTrue(result.startsWith("usage: org.onap.policy.apex.service.engine.main.ApexMain [options...]")); + } catch (final ApexException e) { + e.printStackTrace(); + fail("Test should not throw an exception"); + } + + final String[] args05 = {"-a"}; + try { + apexArguments.parse(args05); + } catch (final ApexException e) { + assertEquals("invalid command line arguments specified : Unrecognized option: -a", e.getMessage()); + } + + final String[] args06 = {"-c", "hello", "-m", "goodbye", "-h", "-v"}; + try { + final String result = apexArguments.parse(args06); + assertTrue(result.startsWith("usage: org.onap.policy.apex.service.engine.main.ApexMain [options...]")); + } catch (final ApexException e) { + assertEquals("invalid command line arguments specified : Unrecognized option: -a", e.getMessage()); + } + + final String[] args07 = {"-c", "hello", "-m", "goodbye", "-h", "aaa"}; + try { + final String result = apexArguments.parse(args07); + assertTrue(result.startsWith("usage: org.onap.policy.apex.service.engine.main.ApexMain [options...]")); + } catch (final ApexException e) { + assertEquals("too many command line arguments specified : [-c, hello, -m, goodbye, -h, aaa]", + e.getMessage()); + } + } + + @Test + public void testCommandLineFileParameters() { + final ApexCommandLineArguments apexArguments = new ApexCommandLineArguments(); + + final String[] args00 = {"-c", "zooby"}; + try { + apexArguments.parse(args00); + apexArguments.validate(); + fail("Test should throw an exception here"); + } catch (final ApexException e) { + assertEquals("Apex configuration file \"zooby\" does not exist", e.getMessage()); + } + + final String[] args01 = {"-c"}; + try { + apexArguments.parse(args01); + apexArguments.validate(); + fail("Test should throw an exception here"); + } catch (final ApexException e) { + assertEquals("invalid command line arguments specified : Missing argument for option: c", e.getMessage()); + } + + final String[] args02 = {"-c", "src/test/resources/parameters/goodParams.json"}; + try { + apexArguments.parse(args02); + apexArguments.validate(); + } catch (final ApexException e) { + e.printStackTrace(); + fail("Test should not throw an exception"); + } + + final String[] args03 = {"-c", "src/test/resources/parameters/goodParams.json", "-m", "zooby"}; + try { + apexArguments.parse(args03); + apexArguments.validate(); + fail("Test should throw an exception here"); + } catch (final ApexException e) { + assertEquals("Apex model file \"zooby\" does not exist", e.getMessage()); + } + + final String[] args04 = {"-m"}; + try { + apexArguments.parse(args04); + apexArguments.validate(); + fail("Test should throw an exception here"); + } catch (final ApexException e) { + assertEquals("invalid command line arguments specified : Missing argument for option: m", e.getMessage()); + } + + final String[] args05 = {"-c", "src/test/resources/parameters/goodParams.json", "-m"}; + try { + apexArguments.parse(args05); + apexArguments.validate(); + fail("Test should throw an exception here"); + } catch (final ApexException e) { + assertEquals("invalid command line arguments specified : Missing argument for option: m", e.getMessage()); + } + + final String[] args06 = {"-c", "src/test/resources/parameters/goodParams.json", "-m", + "src/test/resources/main/DummyModelFile.json"}; + try { + apexArguments.parse(args06); + apexArguments.validate(); + } catch (final ApexException e) { + e.printStackTrace(); + fail("Test should not throw an exception"); + } + + final String[] args07 = {"-c", "parameters/goodParams.json", "-m", "main/DummyModelFile.json"}; + try { + apexArguments.parse(args07); + apexArguments.validate(); + } catch (final ApexException e) { + e.printStackTrace(); + fail("Test should not throw an exception"); + } + } +} diff --git a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/parameters/ContextParameterTests.java b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/parameters/ContextParameterTests.java new file mode 100644 index 000000000..33bcd0d5f --- /dev/null +++ b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/parameters/ContextParameterTests.java @@ -0,0 +1,276 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-2018 Ericsson. All rights reserved. + * ================================================================================ + * 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.service.engine.parameters; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + +import org.junit.Test; +import org.onap.policy.apex.service.engine.main.ApexCommandLineArguments; +import org.onap.policy.apex.service.engine.parameters.dummyclasses.SuperDooperDistributorParameters; +import org.onap.policy.apex.service.parameters.ApexParameterException; +import org.onap.policy.apex.service.parameters.ApexParameterHandler; +import org.onap.policy.apex.service.parameters.ApexParameters; + +/** + * Test for an empty parameter file + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public class ContextParameterTests { + + @Test + public void noParamsTest() { + final String[] args = {"-c", "src/test/resources/parameters/serviceContextNoParams.json"}; + final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); + + try { + new ApexParameterHandler().getParameters(arguments); + fail("This test should throw an exception"); + } catch (final ApexParameterException e) { + assertEquals( + "error reading parameters from \"src/test/resources/parameters/serviceContextNoParams.json\"\n" + + "(ApexParameterRuntimeException):could not find field \"parameterClassName\" in \"contextParameters\" entry", + e.getMessage()); + } + } + + @Test + public void badParamsTest() { + final String[] args = {"-c", "src/test/resources/parameters/serviceContextBadParams.json"}; + final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); + + try { + new ApexParameterHandler().getParameters(arguments); + fail("This test should throw an exception"); + } catch (final ApexParameterException e) { + assertEquals( + "error reading parameters from \"src/test/resources/parameters/serviceContextBadParams.json\"\n" + + "(ApexParameterRuntimeException):failed to deserialize the parameters for \"contextParameters\" to parameter class \"hello\"\n" + + "java.lang.ClassNotFoundException: hello", + e.getMessage()); + } + } + + @Test + public void badPluginParamNameTest() { + final String[] args = {"-c", "src/test/resources/parameters/serviceContextBadPluginNameParams.json"}; + final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); + + try { + new ApexParameterHandler().getParameters(arguments); + fail("This test should throw an exception"); + } catch (final ApexParameterException e) { + assertEquals( + "error reading parameters from \"src/test/resources/parameters/serviceContextBadPluginNameParams.json\"\n" + + "(ApexParameterRuntimeException):could not find field \"parameterClassName\" in \"contextParameters\" entry", + e.getMessage()); + } + } + + @Test + public void badClassParamTest() { + final String[] args = {"-c", "src/test/resources/parameters/serviceContextBadClassParams.json"}; + final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); + + try { + new ApexParameterHandler().getParameters(arguments); + fail("This test should throw an exception"); + } catch (final ApexParameterException e) { + assertEquals( + "error reading parameters from \"src/test/resources/parameters/serviceContextBadClassParams.json\"\n" + + "(ApexParameterRuntimeException):failed to deserialize the parameters for \"contextParameters\" to parameter class \"java.lang.Integer\"\n" + + "com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected NUMBER but was BEGIN_OBJECT at path $", + e.getMessage()); + } + } + + @Test + public void badPluginClassTest() { + final String[] args = {"-c", "src/test/resources/parameters/serviceContextBadPluginClassParams.json"}; + final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); + + try { + new ApexParameterHandler().getParameters(arguments); + fail("This test should throw an exception"); + } catch (final ApexParameterException e) { + assertEquals( + "error reading parameters from \"src/test/resources/parameters/serviceContextBadPluginClassParams.json\"\n" + + "(ClassCastException):org.onap.policy.apex.service.engine.parameters.dummyclasses.SuperDooperExecutorParameters cannot be cast to org.onap.policy.apex.context.parameters.ContextParameters", + e.getMessage()); + } + } + + @Test + public void okFlushParamTest() { + final String[] args = {"-c", "src/test/resources/parameters/serviceContextOKFlushParams.json"}; + final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); + + try { + final ApexParameters parameters = new ApexParameterHandler().getParameters(arguments); + assertEquals("org.onap.policy.apex.context.parameters.ContextParameters", parameters + .getEngineServiceParameters().getEngineParameters().getContextParameters().getParameterClassName()); + assertEquals(123456, parameters.getEngineServiceParameters().getEngineParameters().getContextParameters() + .getPersistorParameters().getFlushPeriod()); + } catch (final ApexParameterException e) { + fail("This test should not throw any exception: " + e.getMessage()); + } + } + + @Test + public void okDefaultParamTest() { + final String[] args = {"-c", "src/test/resources/parameters/serviceContextOKDefaultParams.json"}; + final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); + + try { + final ApexParameters parameters = new ApexParameterHandler().getParameters(arguments); + assertEquals("org.onap.policy.apex.context.parameters.ContextParameters", parameters + .getEngineServiceParameters().getEngineParameters().getContextParameters().getParameterClassName()); + assertEquals(300000, parameters.getEngineServiceParameters().getEngineParameters().getContextParameters() + .getPersistorParameters().getFlushPeriod()); + } catch (final ApexParameterException e) { + fail("This test should not throw any exception: " + e.getMessage()); + } + } + + @Test + public void okDistParamTest() { + final String[] args = {"-c", "src/test/resources/parameters/serviceContextOKDistParams.json"}; + final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); + + try { + final ApexParameters parameters = new ApexParameterHandler().getParameters(arguments); + assertEquals("org.onap.policy.apex.context.parameters.ContextParameters", parameters + .getEngineServiceParameters().getEngineParameters().getContextParameters().getParameterClassName()); + assertEquals("org.onap.policy.apex.context.parameters.DistributorParameters", + parameters.getEngineServiceParameters().getEngineParameters().getContextParameters() + .getDistributorParameters().getParameterClassName()); + } catch (final ApexParameterException e) { + fail("This test should not throw any exception: " + e.getMessage()); + } + } + + @Test + public void okFullDefaultParamTest() { + final String[] args = {"-c", "src/test/resources/parameters/goodParams.json"}; + final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); + + try { + final ApexParameters parameters = new ApexParameterHandler().getParameters(arguments); + assertEquals("org.onap.policy.apex.context.parameters.ContextParameters", parameters + .getEngineServiceParameters().getEngineParameters().getContextParameters().getParameterClassName()); + assertEquals("org.onap.policy.apex.context.parameters.DistributorParameters", + parameters.getEngineServiceParameters().getEngineParameters().getContextParameters() + .getDistributorParameters().getParameterClassName()); + assertEquals("org.onap.policy.apex.context.parameters.LockManagerParameters", + parameters.getEngineServiceParameters().getEngineParameters().getContextParameters() + .getLockManagerParameters().getParameterClassName()); + assertEquals("org.onap.policy.apex.context.parameters.PersistorParameters", + parameters.getEngineServiceParameters().getEngineParameters().getContextParameters() + .getPersistorParameters().getParameterClassName()); + assertEquals(300000, parameters.getEngineServiceParameters().getEngineParameters().getContextParameters() + .getPersistorParameters().getFlushPeriod()); + } catch (final ApexParameterException e) { + fail("This test should not throw any exception: " + e.getMessage()); + } + } + + @Test + public void okFullParamTest() { + final String[] args = {"-c", "src/test/resources/parameters/serviceContextOKFullParams.json"}; + final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); + + try { + final ApexParameters parameters = new ApexParameterHandler().getParameters(arguments); + assertEquals("org.onap.policy.apex.context.parameters.ContextParameters", parameters + .getEngineServiceParameters().getEngineParameters().getContextParameters().getParameterClassName()); + assertEquals("org.onap.policy.apex.context.parameters.LockManagerParameters", + parameters.getEngineServiceParameters().getEngineParameters().getContextParameters() + .getLockManagerParameters().getParameterClassName()); + assertEquals("org.onap.policy.apex.context.parameters.PersistorParameters", + parameters.getEngineServiceParameters().getEngineParameters().getContextParameters() + .getPersistorParameters().getParameterClassName()); + assertEquals(123456, parameters.getEngineServiceParameters().getEngineParameters().getContextParameters() + .getPersistorParameters().getFlushPeriod()); + + final SuperDooperDistributorParameters infinispanParameters = + (SuperDooperDistributorParameters) parameters.getEngineServiceParameters().getEngineParameters() + .getContextParameters().getDistributorParameters(); + assertEquals("org.onap.policy.apex.service.engine.parameters.dummyclasses.SuperDooperDistributorParameters", + infinispanParameters.getParameterClassName()); + assertEquals("my/lovely/configFile.xml", infinispanParameters.getConfigFile()); + assertEquals("holy/stone.xml", infinispanParameters.getJgroupsFile()); + assertEquals(false, infinispanParameters.preferIPv4Stack()); + assertEquals("fatherted", infinispanParameters.getjGroupsBindAddress()); + + } catch (final ApexParameterException e) { + fail("This test should not throw any exception: " + e.getMessage()); + } + } + + @Test + public void badClassDistParamTest() { + final String[] args = {"-c", "src/test/resources/parameters/serviceContextBadClassDistParams.json"}; + final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); + + try { + new ApexParameterHandler().getParameters(arguments); + fail("This test should throw an exception"); + } catch (final ApexParameterException e) { + assertEquals( + "error reading parameters from \"src/test/resources/parameters/serviceContextBadClassDistParams.json\"\n" + + "(ClassCastException):org.onap.policy.apex.context.parameters.ContextParameters cannot be cast to org.onap.policy.apex.context.parameters.DistributorParameters", + e.getMessage()); + } + } + + @Test + public void badClassLockParamTest() { + final String[] args = {"-c", "src/test/resources/parameters/serviceContextBadClassLockParams.json"}; + final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); + + try { + new ApexParameterHandler().getParameters(arguments); + fail("This test should throw an exception"); + } catch (final ApexParameterException e) { + assertEquals( + "error reading parameters from \"src/test/resources/parameters/serviceContextBadClassLockParams.json\"\n" + + "(ClassCastException):org.onap.policy.apex.context.parameters.ContextParameters cannot be cast to org.onap.policy.apex.context.parameters.LockManagerParameters", + e.getMessage()); + } + } + + @Test + public void badClassPersistParamTest() { + final String[] args = {"-c", "src/test/resources/parameters/serviceContextBadClassPersistParams.json"}; + final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); + + try { + new ApexParameterHandler().getParameters(arguments); + fail("This test should throw an exception"); + } catch (final ApexParameterException e) { + assertEquals( + "error reading parameters from \"src/test/resources/parameters/serviceContextBadClassPersistParams.json\"\n" + + "(ClassCastException):org.onap.policy.apex.context.parameters.ContextParameters cannot be cast to org.onap.policy.apex.context.parameters.PersistorParameters", + e.getMessage()); + } + } +} diff --git a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/parameters/ExecutorParameterTests.java b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/parameters/ExecutorParameterTests.java new file mode 100644 index 000000000..b14cc22be --- /dev/null +++ b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/parameters/ExecutorParameterTests.java @@ -0,0 +1,184 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-2018 Ericsson. All rights reserved. + * ================================================================================ + * 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.service.engine.parameters; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + +import org.junit.Test; +import org.onap.policy.apex.service.engine.main.ApexCommandLineArguments; +import org.onap.policy.apex.service.parameters.ApexParameterException; +import org.onap.policy.apex.service.parameters.ApexParameterHandler; +import org.onap.policy.apex.service.parameters.ApexParameters; + +/** + * Test for an empty parameter file + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public class ExecutorParameterTests { + + @Test + public void noParamsTest() { + final String[] args = {"-c", "src/test/resources/parameters/serviceExecutorNoParams.json"}; + final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); + + try { + final ApexParameters parameters = new ApexParameterHandler().getParameters(arguments); + assertEquals(0, + parameters.getEngineServiceParameters().getEngineParameters().getExecutorParameterMap().size()); + } catch (final ApexParameterException e) { + fail("This test should not throw any exception: " + e.getMessage()); + } + } + + @Test + public void badParamsTest() { + final String[] args = {"-c", "src/test/resources/parameters/serviceExecutorBadParams.json"}; + final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); + + try { + new ApexParameterHandler().getParameters(arguments); + fail("This test should throw an exception"); + } catch (final ApexParameterException e) { + assertEquals( + "error reading parameters from \"src/test/resources/parameters/serviceExecutorBadParams.json\"\n" + + "(ApexParameterRuntimeException):value of \"executorParameters:ZOOBY\" entry is not a parameter JSON object", + e.getMessage()); + } + } + + @Test + public void noExecutorParamsTest() { + final String[] args = {"-c", "src/test/resources/parameters/serviceExecutorNoExecutorParams.json"}; + final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); + + try { + new ApexParameterHandler().getParameters(arguments); + fail("This test should throw an exception"); + } catch (final ApexParameterException e) { + assertEquals( + "error reading parameters from \"src/test/resources/parameters/serviceExecutorNoExecutorParams.json\"\n" + + "(ApexParameterRuntimeException):no \"executorParameters\" entry found in parameters, at least one executor parameter entry must be specified", + e.getMessage()); + } + } + + @Test + public void emptyParamsTest() { + final String[] args = {"-c", "src/test/resources/parameters/serviceExecutorEmptyParams.json"}; + final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); + + try { + new ApexParameterHandler().getParameters(arguments); + fail("This test should throw an exception"); + } catch (final ApexParameterException e) { + assertEquals( + "error reading parameters from \"src/test/resources/parameters/serviceExecutorEmptyParams.json\"\n" + + "(ApexParameterRuntimeException):could not find field \"parameterClassName\" in \"executorParameters:ZOOBY\" entry", + e.getMessage()); + } + } + + @Test + public void badPluginParamNameTest() { + final String[] args = {"-c", "src/test/resources/parameters/serviceExecutorBadPluginNameParams.json"}; + final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); + + try { + new ApexParameterHandler().getParameters(arguments); + fail("This test should throw an exception"); + } catch (final ApexParameterException e) { + assertEquals( + "error reading parameters from \"src/test/resources/parameters/serviceExecutorBadPluginNameParams.json\"\n" + + "(ApexParameterRuntimeException):could not find field \"parameterClassName\" in \"executorParameters:ZOOBY\" entry", + e.getMessage()); + } + } + + @Test + public void badPluginParamObjectTest() { + final String[] args = {"-c", "src/test/resources/parameters/serviceExecutorBadPluginValueObjectParams.json"}; + final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); + + try { + new ApexParameterHandler().getParameters(arguments); + fail("This test should throw an exception"); + } catch (final ApexParameterException e) { + assertEquals( + "error reading parameters from \"src/test/resources/parameters/serviceExecutorBadPluginValueObjectParams.json\"\n" + + "(ApexParameterRuntimeException):value for field \"parameterClassName\" in \"executorParameters:LOOBY\" entry is not a plain string", + e.getMessage()); + } + } + + @Test + public void badPluginParamBlankTest() { + final String[] args = {"-c", "src/test/resources/parameters/serviceExecutorBadPluginValueBlankParams.json"}; + final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); + + try { + new ApexParameterHandler().getParameters(arguments); + fail("This test should throw an exception"); + } catch (final ApexParameterException e) { + assertEquals( + "error reading parameters from \"src/test/resources/parameters/serviceExecutorBadPluginValueBlankParams.json\"\n" + + "(ApexParameterRuntimeException):value for field \"parameterClassName\" in \"executorParameters:LOOBY\" entry is not specified or is blank", + e.getMessage()); + } + } + + + @Test + public void badPluginParamValueTest() { + final String[] args = {"-c", "src/test/resources/parameters/serviceExecutorBadPluginValueParams.json"}; + final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); + + try { + new ApexParameterHandler().getParameters(arguments); + fail("This test should throw an exception"); + } catch (final ApexParameterException e) { + assertEquals( + "error reading parameters from \"src/test/resources/parameters/serviceExecutorBadPluginValueParams.json\"\n" + + "(ApexParameterRuntimeException):failed to deserialize the parameters for \"executorParameters:LOOBY\" to parameter class \"helloworld\"\n" + + "java.lang.ClassNotFoundException: helloworld", + e.getMessage()); + } + } + + @Test + public void goodParametersTest() { + final String[] args = {"-c", "src/test/resources/parameters/goodParams.json"}; + final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); + + try { + final ApexParameters parameters = new ApexParameterHandler().getParameters(arguments); + + assertEquals("MyApexEngine", parameters.getEngineServiceParameters().getName()); + assertEquals("0.0.1", parameters.getEngineServiceParameters().getVersion()); + assertEquals(45, parameters.getEngineServiceParameters().getId()); + assertEquals(19, parameters.getEngineServiceParameters().getInstanceCount()); + assertEquals(65522, parameters.getEngineServiceParameters().getDeploymentPort()); + } catch (final ApexParameterException e) { + fail("This test should not throw any exception: " + e.getMessage()); + } + } +} diff --git a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/parameters/ParameterTests.java b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/parameters/ParameterTests.java new file mode 100644 index 000000000..6ea8d5924 --- /dev/null +++ b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/parameters/ParameterTests.java @@ -0,0 +1,239 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-2018 Ericsson. All rights reserved. + * ================================================================================ + * 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.service.engine.parameters; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.util.Arrays; + +import org.junit.Test; +import org.onap.policy.apex.service.engine.event.impl.filecarrierplugin.consumer.ApexFileEventConsumer; +import org.onap.policy.apex.service.engine.event.impl.filecarrierplugin.producer.ApexFileEventProducer; +import org.onap.policy.apex.service.engine.main.ApexCommandLineArguments; +import org.onap.policy.apex.service.engine.parameters.dummyclasses.SuperDooperCarrierTechnologyParameters; +import org.onap.policy.apex.service.engine.parameters.dummyclasses.SuperDooperEventProducer; +import org.onap.policy.apex.service.engine.parameters.dummyclasses.SuperDooperEventSubscriber; +import org.onap.policy.apex.service.parameters.ApexParameterException; +import org.onap.policy.apex.service.parameters.ApexParameterHandler; +import org.onap.policy.apex.service.parameters.ApexParameters; +import org.onap.policy.apex.service.parameters.carriertechnology.CarrierTechnologyParameters; +import org.onap.policy.apex.service.parameters.eventprotocol.EventProtocolParameters; + +/** + * Test for an empty parameter file + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public class ParameterTests { + @Test + public void invalidParametersNoFileTest() throws ApexParameterException { + final String[] args = {"-c", "src/test/resources/parameters/invalidNoFile.json"}; + final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); + + try { + new ApexParameterHandler().getParameters(arguments); + fail("This test should throw an exception"); + } catch (final ApexParameterException e) { + assertTrue(e.getMessage().startsWith("error reading parameters from \"src")); + assertTrue(e.getMessage().contains("FileNotFoundException")); + } + } + + @Test + public void invalidParametersEmptyTest() { + final String[] args = {"-c", "src/test/resources/parameters/empty.json"}; + final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); + + try { + new ApexParameterHandler().getParameters(arguments); + fail("This test should throw an exception"); + } catch (final ApexParameterException e) { + assertTrue(e.getMessage() + .startsWith("validation error(s) on parameters from \"src/test/resources/parameters/empty.json\"")); + } + } + + @Test + public void invalidParametersNoParamsTest() { + final String[] args = {"-c", "src/test/resources/parameters/noParams.json"}; + final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); + + try { + new ApexParameterHandler().getParameters(arguments); + fail("This test should throw an exception"); + } catch (final ApexParameterException e) { + assertEquals("validation error(s) on parameters from \"src/test/resources/parameters/noParams.json\"\n" + + "Apex parameters invalid\n" + " engine service parameters are not specified\n" + + " at least one event output and one event input must be specified", e.getMessage()); + } + } + + @Test + public void invalidParametersBlankParamsTest() { + final String[] args = {"-c", "src/test/resources/parameters/blankParams.json"}; + final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); + + try { + new ApexParameterHandler().getParameters(arguments); + fail("This test should throw an exception"); + } catch (final ApexParameterException e) { + + assertEquals("validation error(s) on parameters from \"src/test/resources/parameters/blankParams.json\"\n" + + "Apex parameters invalid\n" + " engine service parameters invalid\n" + + " id not specified or specified value [-1] invalid, must be specified as id >= 0\n" + + " at least one event output and one event input must be specified", e.getMessage()); + } + } + + @Test + public void invalidParametersTest() { + final String[] args = {"-c", "src/test/resources/parameters/badParams.json"}; + final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); + + try { + new ApexParameterHandler().getParameters(arguments); + fail("This test should throw an exception"); + } catch (final ApexParameterException e) { + assertEquals( + "validation error(s) on parameters from \"src/test/resources/parameters/badParams.json\"\n" + + "Apex parameters invalid\n" + " engine service parameters invalid\n" + + " name [hello there] and/or version [PA1] invalid\n" + + " parameter \"name\": value \"hello there\", does not match regular expression \"[A-Za-z0-9\\-_\\.]+\"\n" + + " id not specified or specified value [-45] invalid, must be specified as id >= 0\n" + + " instanceCount [-345] invalid, must be specified as instanceCount >= 1\n" + + " deploymentPort [65536] invalid, must be specified as 1024 <= port <= 65535\n" + + " policyModelFileName [/some/file/name.xml] not found or is not a plain file\n" + + " event input (TheFileConsumer1) parameters invalid\n" + + " fileName not specified or is blank or null, it must be specified as a valid file location\n" + + " event output (FirstProducer) parameters invalid\n" + + " fileName not specified or is blank or null, it must be specified as a valid file location", + e.getMessage()); + } + } + + @Test + public void goodParametersTest() { + final String[] args = {"-c", "src/test/resources/parameters/goodParams.json"}; + final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); + + try { + final ApexParameters parameters = new ApexParameterHandler().getParameters(arguments); + + assertEquals(2, parameters.getEventInputParameters().size()); + assertEquals(2, parameters.getEventOutputParameters().size()); + + assertTrue(parameters.getEventOutputParameters().containsKey("FirstProducer")); + assertTrue(parameters.getEventOutputParameters().containsKey("MyOtherProducer")); + assertEquals("FILE", parameters.getEventOutputParameters().get("FirstProducer") + .getCarrierTechnologyParameters().getLabel()); + assertEquals("FILE", parameters.getEventOutputParameters().get("MyOtherProducer") + .getCarrierTechnologyParameters().getLabel()); + assertEquals(ApexFileEventProducer.class.getCanonicalName(), parameters.getEventOutputParameters() + .get("MyOtherProducer").getCarrierTechnologyParameters().getEventProducerPluginClass()); + assertEquals(ApexFileEventConsumer.class.getCanonicalName(), parameters.getEventOutputParameters() + .get("MyOtherProducer").getCarrierTechnologyParameters().getEventConsumerPluginClass()); + assertEquals("JSON", + parameters.getEventOutputParameters().get("FirstProducer").getEventProtocolParameters().getLabel()); + assertEquals("JSON", parameters.getEventOutputParameters().get("MyOtherProducer") + .getEventProtocolParameters().getLabel()); + + assertTrue(parameters.getEventInputParameters().containsKey("TheFileConsumer1")); + assertTrue(parameters.getEventInputParameters().containsKey("MySuperDooperConsumer1")); + assertEquals("FILE", parameters.getEventInputParameters().get("TheFileConsumer1") + .getCarrierTechnologyParameters().getLabel()); + assertEquals("SUPER_DOOPER", parameters.getEventInputParameters().get("MySuperDooperConsumer1") + .getCarrierTechnologyParameters().getLabel()); + assertEquals("JSON", parameters.getEventInputParameters().get("TheFileConsumer1") + .getEventProtocolParameters().getLabel()); + assertEquals("SUPER_TOK_DEL", parameters.getEventInputParameters().get("MySuperDooperConsumer1") + .getEventProtocolParameters().getLabel()); + assertEquals(ApexFileEventProducer.class.getCanonicalName(), parameters.getEventInputParameters() + .get("TheFileConsumer1").getCarrierTechnologyParameters().getEventProducerPluginClass()); + assertEquals(ApexFileEventConsumer.class.getCanonicalName(), parameters.getEventInputParameters() + .get("TheFileConsumer1").getCarrierTechnologyParameters().getEventConsumerPluginClass()); + assertEquals(SuperDooperEventProducer.class.getCanonicalName(), parameters.getEventInputParameters() + .get("MySuperDooperConsumer1").getCarrierTechnologyParameters().getEventProducerPluginClass()); + assertEquals(SuperDooperEventSubscriber.class.getCanonicalName(), parameters.getEventInputParameters() + .get("MySuperDooperConsumer1").getCarrierTechnologyParameters().getEventConsumerPluginClass()); + } catch (final ApexParameterException e) { + fail("This test should not throw an exception"); + } + } + + @Test + public void superDooperParametersTest() { + final String[] args = {"-c", "src/test/resources/parameters/superDooperParams.json"}; + final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); + + try { + final ApexParameters parameters = new ApexParameterHandler().getParameters(arguments); + + assertEquals("MyApexEngine", parameters.getEngineServiceParameters().getName()); + assertEquals("0.0.1", parameters.getEngineServiceParameters().getVersion()); + assertEquals(45, parameters.getEngineServiceParameters().getId()); + assertEquals(345, parameters.getEngineServiceParameters().getInstanceCount()); + assertEquals(65522, parameters.getEngineServiceParameters().getDeploymentPort()); + + final CarrierTechnologyParameters prodCT = + parameters.getEventOutputParameters().get("FirstProducer").getCarrierTechnologyParameters(); + final EventProtocolParameters prodEP = + parameters.getEventOutputParameters().get("FirstProducer").getEventProtocolParameters(); + final CarrierTechnologyParameters consCT = + parameters.getEventInputParameters().get("MySuperDooperConsumer1").getCarrierTechnologyParameters(); + final EventProtocolParameters consEP = + parameters.getEventInputParameters().get("MySuperDooperConsumer1").getEventProtocolParameters(); + + assertEquals("SUPER_DOOPER", prodCT.getLabel()); + assertEquals("SUPER_TOK_DEL", prodEP.getLabel()); + assertEquals("SUPER_DOOPER", consCT.getLabel()); + assertEquals("JSON", consEP.getLabel()); + + assertTrue(prodCT instanceof SuperDooperCarrierTechnologyParameters); + + final SuperDooperCarrierTechnologyParameters superDooperParameters = + (SuperDooperCarrierTechnologyParameters) prodCT; + assertEquals("somehost:12345", superDooperParameters.getBootstrapServers()); + assertEquals("0", superDooperParameters.getAcks()); + assertEquals(25, superDooperParameters.getRetries()); + assertEquals(98765, superDooperParameters.getBatchSize()); + assertEquals(21, superDooperParameters.getLingerTime()); + assertEquals(50505050, superDooperParameters.getBufferMemory()); + assertEquals("first-group-id", superDooperParameters.getGroupId()); + assertFalse(superDooperParameters.isEnableAutoCommit()); + assertEquals(441, superDooperParameters.getAutoCommitTime()); + assertEquals(987, superDooperParameters.getSessionTimeout()); + assertEquals("producer-out", superDooperParameters.getProducerTopic()); + assertEquals(101, superDooperParameters.getConsumerPollTime()); + assertEquals("some.key.serailizer", superDooperParameters.getKeySerializer()); + assertEquals("some.value.serailizer", superDooperParameters.getValueSerializer()); + assertEquals("some.key.deserailizer", superDooperParameters.getKeyDeserializer()); + assertEquals("some.value.deserailizer", superDooperParameters.getValueDeserializer()); + + final String[] consumerTopics = {"consumer-out-0", "consumer-out-1", "consumer-out-2"}; + assertEquals(Arrays.asList(consumerTopics), superDooperParameters.getConsumerTopicList()); + } catch (final ApexParameterException e) { + fail("This test should not throw an exception"); + } + } +} diff --git a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/parameters/ProducerConsumerTests.java b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/parameters/ProducerConsumerTests.java new file mode 100644 index 000000000..dad989913 --- /dev/null +++ b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/parameters/ProducerConsumerTests.java @@ -0,0 +1,266 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-2018 Ericsson. All rights reserved. + * ================================================================================ + * 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.service.engine.parameters; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + +import org.junit.Test; +import org.onap.policy.apex.service.engine.event.impl.filecarrierplugin.FILECarrierTechnologyParameters; +import org.onap.policy.apex.service.engine.main.ApexCommandLineArguments; +import org.onap.policy.apex.service.parameters.ApexParameterException; +import org.onap.policy.apex.service.parameters.ApexParameterHandler; +import org.onap.policy.apex.service.parameters.ApexParameters; + +/** + * Test for an empty parameter file + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public class ProducerConsumerTests { + @Test + public void goodParametersTest() { + final String[] args = {"-c", "src/test/resources/parameters/goodParams.json"}; + final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); + + try { + final ApexParameters parameters = new ApexParameterHandler().getParameters(arguments); + + assertEquals("MyApexEngine", parameters.getEngineServiceParameters().getName()); + assertEquals("0.0.1", parameters.getEngineServiceParameters().getVersion()); + assertEquals(45, parameters.getEngineServiceParameters().getId()); + assertEquals(19, parameters.getEngineServiceParameters().getInstanceCount()); + assertEquals(65522, parameters.getEngineServiceParameters().getDeploymentPort()); + assertEquals("FILE", parameters.getEventOutputParameters().get("FirstProducer") + .getCarrierTechnologyParameters().getLabel()); + assertEquals("JSON", + parameters.getEventOutputParameters().get("FirstProducer").getEventProtocolParameters().getLabel()); + assertEquals("FILE", parameters.getEventOutputParameters().get("MyOtherProducer") + .getCarrierTechnologyParameters().getLabel()); + assertEquals("JSON", parameters.getEventOutputParameters().get("MyOtherProducer") + .getEventProtocolParameters().getLabel()); + assertEquals("FILE", parameters.getEventInputParameters().get("TheFileConsumer1") + .getCarrierTechnologyParameters().getLabel()); + assertEquals("JSON", parameters.getEventInputParameters().get("TheFileConsumer1") + .getEventProtocolParameters().getLabel()); + assertEquals("SUPER_DOOPER", parameters.getEventInputParameters().get("MySuperDooperConsumer1") + .getCarrierTechnologyParameters().getLabel()); + assertEquals("SUPER_TOK_DEL", parameters.getEventInputParameters().get("MySuperDooperConsumer1") + .getEventProtocolParameters().getLabel()); + } catch (final ApexParameterException e) { + fail("This test should not throw an exception"); + } + } + + @Test + public void noCarrierTechnology() { + final String[] args = {"-c", "src/test/resources/parameters/prodConsNoCT.json"}; + final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); + + try { + new ApexParameterHandler().getParameters(arguments); + fail("This test should throw an exception"); + } catch (final ApexParameterException e) { + assertEquals("validation error(s) on parameters from \"src/test/resources/parameters/prodConsNoCT.json\"\n" + + "Apex parameters invalid\n" + " event input (aConsumer) parameters invalid\n" + + " event handler carrierTechnologyParameters not specified or blank", e.getMessage()); + } + } + + @Test + public void noEventProcol() { + final String[] args = {"-c", "src/test/resources/parameters/prodConsNoEP.json"}; + final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); + + try { + new ApexParameterHandler().getParameters(arguments); + fail("This test should throw an exception"); + } catch (final ApexParameterException e) { + assertEquals("validation error(s) on parameters from \"src/test/resources/parameters/prodConsNoEP.json\"\n" + + "Apex parameters invalid\n" + " event input (aConsumer) parameters invalid\n" + + " fileName not specified or is blank or null, it must be specified as a valid file location\n" + + " event output (aProducer) parameters invalid\n" + + " event handler eventProtocolParameters not specified or blank", e.getMessage()); + } + } + + @Test + public void noCarrierTechnologyParClass() { + final String[] args = {"-c", "src/test/resources/parameters/prodConsNoCTParClass.json"}; + final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); + + try { + new ApexParameterHandler().getParameters(arguments); + fail("This test should throw an exception"); + } catch (final ApexParameterException e) { + assertEquals( + "error reading parameters from \"src/test/resources/parameters/prodConsNoCTParClass.json\"\n" + + "(ApexParameterRuntimeException):carrier technology \"SUPER_DOOPER\" does not match plugin \"FILE\" in " + + "\"com.ericsson.apex.service.engine.event.impl.filecarrierplugin.FILECarrierTechnologyParameters\", " + + "specify correct carrier technology parameter plugin in parameter \"parameterClassName\"", + e.getMessage()); + } + } + + @Test + public void mismatchCarrierTechnologyParClass() { + final String[] args = {"-c", "src/test/resources/parameters/prodConsMismatchCTParClass.json"}; + final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); + + try { + new ApexParameterHandler().getParameters(arguments); + fail("This test should throw an exception"); + } catch (final ApexParameterException e) { + assertEquals( + "error reading parameters from \"src/test/resources/parameters/prodConsMismatchCTParClass.json\"\n" + + "(ApexParameterRuntimeException):carrier technology \"SUPER_LOOPER\" does not match plugin \"SUPER_DOOPER\" in " + + "\"com.ericsson.apex.service.engine.parameters.dummyclasses.SuperDooperCarrierTechnologyParameters\", " + + "specify correct carrier technology parameter plugin in parameter \"parameterClassName\"", + e.getMessage()); + } + } + + @Test + public void wrongTypeCarrierTechnologyParClass() { + final String[] args = {"-c", "src/test/resources/parameters/prodConsWrongTypeCTParClass.json"}; + final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); + + try { + new ApexParameterHandler().getParameters(arguments); + fail("This test should throw an exception"); + } catch (final ApexParameterException e) { + assertEquals( + "error reading parameters from \"src/test/resources/parameters/prodConsWrongTypeCTParClass.json\"\n" + + "(ApexParameterRuntimeException):could not create default parameters for carrier technology \"SUPER_DOOPER\"\n" + + "com.ericsson.apex.service.engine.parameters.dummyclasses.SuperTokenDelimitedEventProtocolParameters " + + "cannot be cast to com.ericsson.apex.service.parameters.carriertechnology.CarrierTechnologyParameters", + e.getMessage()); + } + } + + @Test + public void okFileNameCarrierTechnology() { + final String[] args = {"-c", "src/test/resources/parameters/prodConsOKFileName.json"}; + final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); + + try { + final ApexParameters parameters = new ApexParameterHandler().getParameters(arguments); + final FILECarrierTechnologyParameters fileParams = (FILECarrierTechnologyParameters) parameters + .getEventOutputParameters().get("aProducer").getCarrierTechnologyParameters(); + assertEquals("/tmp/aaa.json", fileParams.getFileName()); + assertEquals(false, fileParams.isStandardError()); + assertEquals(false, fileParams.isStandardIO()); + assertEquals(false, fileParams.isStreamingMode()); + } catch (final ApexParameterException e) { + fail("This test should not throw an exception"); + } + } + + @Test + public void badFileNameCarrierTechnology() { + final String[] args = {"-c", "src/test/resources/parameters/prodConsBadFileName.json"}; + final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); + + try { + new ApexParameterHandler().getParameters(arguments); + fail("This test should throw an exception"); + } catch (final ApexParameterException e) { + assertEquals( + "validation error(s) on parameters from \"src/test/resources/parameters/prodConsBadFileName.json\"\n" + + "Apex parameters invalid\n" + " event output (aProducer) parameters invalid\n" + + " fileName not specified or is blank or null, it must be specified as a valid file location", + e.getMessage()); + } + } + + + @Test + public void badEventProtocolParClass() { + final String[] args = {"-c", "src/test/resources/parameters/prodConsBadEPParClass.json"}; + final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); + + try { + new ApexParameterHandler().getParameters(arguments); + fail("This test should throw an exception"); + } catch (final ApexParameterException e) { + assertEquals( + "error reading parameters from \"src/test/resources/parameters/prodConsBadEPParClass.json\"\n" + + "(ApexParameterRuntimeException):event protocol \"SUPER_TOK_DEL\" does not match plugin \"JSON\" in " + + "\"com.ericsson.apex.service.engine.event.impl.jsonprotocolplugin.JSONEventProtocolParameters\", " + + "specify correct event protocol parameter plugin in parameter \"parameterClassName\"", + e.getMessage()); + } + } + + @Test + public void noEventProtocolParClass() { + final String[] args = {"-c", "src/test/resources/parameters/prodConsNoEPParClass.json"}; + final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); + + try { + new ApexParameterHandler().getParameters(arguments); + fail("This test should throw an exception"); + } catch (final ApexParameterException e) { + assertEquals( + "error reading parameters from \"src/test/resources/parameters/prodConsNoEPParClass.json\"\n" + + "(ApexParameterRuntimeException):event protocol \"SUPER_TOK_DEL\" does not match plugin \"JSON\" in " + + "\"com.ericsson.apex.service.engine.event.impl.jsonprotocolplugin.JSONEventProtocolParameters\", " + + "specify correct event protocol parameter plugin in parameter \"parameterClassName\"", + e.getMessage()); + } + } + + @Test + public void mismatchEventProtocolParClass() { + final String[] args = {"-c", "src/test/resources/parameters/prodConsMismatchEPParClass.json"}; + final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); + + try { + new ApexParameterHandler().getParameters(arguments); + fail("This test should throw an exception"); + } catch (final ApexParameterException e) { + assertEquals( + "error reading parameters from \"src/test/resources/parameters/prodConsMismatchEPParClass.json\"\n" + + "(ApexParameterRuntimeException):event protocol \"SUPER_TOK_BEL\" does not match plugin \"SUPER_TOK_DEL\" in " + + "\"com.ericsson.apex.service.engine.parameters.dummyclasses.SuperTokenDelimitedEventProtocolParameters\", " + + "specify correct event protocol parameter plugin in parameter \"parameterClassName\"", + e.getMessage()); + } + } + + @Test + public void wrongTypeEventProtocolParClass() { + final String[] args = {"-c", "src/test/resources/parameters/prodConsWrongTypeEPParClass.json"}; + final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); + + try { + new ApexParameterHandler().getParameters(arguments); + fail("This test should throw an exception"); + } catch (final ApexParameterException e) { + assertEquals( + "error reading parameters from \"src/test/resources/parameters/prodConsWrongTypeEPParClass.json\"\n" + + "(ApexParameterRuntimeException):could not create default parameters for event protocol \"SUPER_TOK_DEL\"\n" + + "com.ericsson.apex.service.engine.parameters.dummyclasses.SuperDooperCarrierTechnologyParameters " + + "cannot be cast to com.ericsson.apex.service.parameters.eventprotocol.EventProtocolParameters", + e.getMessage()); + } + } +} diff --git a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/parameters/SyncParameterTests.java b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/parameters/SyncParameterTests.java new file mode 100644 index 000000000..62e27eac9 --- /dev/null +++ b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/parameters/SyncParameterTests.java @@ -0,0 +1,362 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-2018 Ericsson. All rights reserved. + * ================================================================================ + * 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.service.engine.parameters; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.util.Arrays; + +import org.junit.Test; +import org.onap.policy.apex.service.engine.main.ApexCommandLineArguments; +import org.onap.policy.apex.service.engine.parameters.dummyclasses.SuperDooperCarrierTechnologyParameters; +import org.onap.policy.apex.service.engine.parameters.dummyclasses.SuperTokenDelimitedEventProtocolParameters; +import org.onap.policy.apex.service.parameters.ApexParameterException; +import org.onap.policy.apex.service.parameters.ApexParameterHandler; +import org.onap.policy.apex.service.parameters.ApexParameters; +import org.onap.policy.apex.service.parameters.carriertechnology.CarrierTechnologyParameters; +import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerPeeredMode; +import org.onap.policy.apex.service.parameters.eventprotocol.EventProtocolParameters; + +/** + * Test for an empty parameter file + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public class SyncParameterTests { + @Test + public void syncBadNoSyncWithPeer() throws ApexParameterException { + final String[] args = {"-c", "src/test/resources/parameters/syncBadParamsNoSyncWithPeer.json"}; + final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); + + try { + new ApexParameterHandler().getParameters(arguments); + fail("This test should throw an exception"); + } catch (final ApexParameterException e) { + assertEquals( + "validation error(s) on parameters from \"src/test/resources/parameters/syncBadParamsNoSyncWithPeer.json\"\n" + + "Apex parameters invalid\n" + + " parameter \\\"synchronousPeer\\\" is illegal on non synchronous event output \"SyncProducer0\"", + e.getMessage()); + } + } + + @Test + public void syncBadNotSyncWithPeer() throws ApexParameterException { + final String[] args = {"-c", "src/test/resources/parameters/syncBadParamsNotSyncWithPeer.json"}; + final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); + + try { + new ApexParameterHandler().getParameters(arguments); + fail("This test should throw an exception"); + } catch (final ApexParameterException e) { + assertEquals( + "validation error(s) on parameters from \"src/test/resources/parameters/syncBadParamsNotSyncWithPeer.json\"\n" + + "Apex parameters invalid\n" + + " parameter \\\"synchronousPeer\\\" is illegal on non synchronous event output \"SyncProducer0\"", + e.getMessage()); + } + } + + @Test + public void syncBadSyncBadPeers() throws ApexParameterException { + final String[] args = {"-c", "src/test/resources/parameters/syncBadParamsBadPeers.json"}; + final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); + + try { + new ApexParameterHandler().getParameters(arguments); + fail("This test should throw an exception"); + } catch (final ApexParameterException e) { + assertEquals( + "validation error(s) on parameters from \"src/test/resources/parameters/syncBadParamsBadPeers.json\"\n" + + "Apex parameters invalid\n" + + " specified \"synchronousPeer\" parameter value \"SyncConsumer1\" on event input \"SyncConsumer0\" does not exist or is an invalid peer for this event handler\n" + + " specified \"synchronousPeer\" parameter value \"SyncConsumer0\" on event input \"SyncConsumer1\" does not exist or is an invalid peer for this event handler\n" + + " specified \"synchronousPeer\" parameter value \"SyncProducer1\" on event output \"SyncProducer0\" does not exist or is an invalid peer for this event handler\n" + + " specified \"synchronousPeer\" parameter value \"SyncProducer0\" on event output \"SyncProducer1\" does not exist or is an invalid peer for this event handler", + e.getMessage()); + } + } + + @Test + public void syncBadSyncInvalidTimeout() throws ApexParameterException { + final String[] args = {"-c", "src/test/resources/parameters/syncBadParamsInvalidTimeout.json"}; + final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); + + try { + new ApexParameterHandler().getParameters(arguments); + fail("This test should throw an exception"); + } catch (final ApexParameterException e) { + assertEquals( + "validation error(s) on parameters from \"src/test/resources/parameters/syncBadParamsInvalidTimeout.json\"\n" + + "Apex parameters invalid\n" + + " parameter \\\"synchronousTimeout\\\" value \"-1\" is illegal on synchronous event input \"SyncConsumer0\", specify a non-negative timeout value in milliseconds\n" + + " parameter \\\"synchronousTimeout\\\" value \"-99999999\" is illegal on synchronous event input \"SyncConsumer1\", specify a non-negative timeout value in milliseconds\n" + + " parameter \\\"synchronousTimeout\\\" value \"-10\" is illegal on synchronous event output \"SyncProducer0\", specify a non-negative timeout value in milliseconds\n" + + " parameter \\\"synchronousTimeout\\\" value \"-3\" is illegal on synchronous event output \"SyncProducer1\", specify a non-negative timeout value in milliseconds\n" + + " synchronous timeout of event input \"SyncConsumer0\" and event output \"SyncProducer0\" [-1/-10] do not match\n" + + " synchronous timeout of event input \"SyncConsumer1\" and event output \"SyncProducer1\" [-99999999/-3] do not match", + e.getMessage()); + } + } + + @Test + public void syncBadSyncBadTimeout() throws ApexParameterException { + final String[] args = {"-c", "src/test/resources/parameters/syncBadParamsBadTimeout.json"}; + final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); + + try { + new ApexParameterHandler().getParameters(arguments); + fail("This test should throw an exception"); + } catch (final ApexParameterException e) { + assertEquals( + "validation error(s) on parameters from \"src/test/resources/parameters/syncBadParamsBadTimeout.json\"\n" + + "Apex parameters invalid\n" + + " parameter \\\"synchronousTimeout\\\" is illegal on non synchronous event output \"MyOtherProducer\"", + e.getMessage()); + } + } + + @Test + public void syncBadSyncUnpairedTimeout() throws ApexParameterException { + final String[] args = {"-c", "src/test/resources/parameters/syncBadParamsUnpairedTimeout.json"}; + final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); + + try { + new ApexParameterHandler().getParameters(arguments); + fail("This test should throw an exception"); + } catch (final ApexParameterException e) { + assertEquals( + "validation error(s) on parameters from \"src/test/resources/parameters/syncBadParamsUnpairedTimeout.json\"\n" + + "Apex parameters invalid\n" + + " synchronous timeout of event input \"SyncConsumer0\" and event output \"SyncProducer0\" [1/10] do not match\n" + + " synchronous timeout of event input \"SyncConsumer1\" and event output \"SyncProducer1\" [99999999/3] do not match", + e.getMessage()); + } + } + + @Test + public void syncGoodSyncGoodTimeoutProducer() throws ApexParameterException { + final String[] args = {"-c", "src/test/resources/parameters/syncGoodParamsProducerTimeout.json"}; + final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); + + try { + final ApexParameters parameters = new ApexParameterHandler().getParameters(arguments); + assertEquals(12345, parameters.getEventInputParameters().get("SyncConsumer0") + .getPeerTimeout(EventHandlerPeeredMode.SYNCHRONOUS)); + assertEquals(1, parameters.getEventInputParameters().get("SyncConsumer1") + .getPeerTimeout(EventHandlerPeeredMode.SYNCHRONOUS)); + assertEquals(12345, parameters.getEventOutputParameters().get("SyncProducer0") + .getPeerTimeout(EventHandlerPeeredMode.SYNCHRONOUS)); + assertEquals(1, parameters.getEventOutputParameters().get("SyncProducer1") + .getPeerTimeout(EventHandlerPeeredMode.SYNCHRONOUS)); + } catch (final Exception e) { + fail("This test should not throw an exception"); + } + } + + @Test + public void syncGoodSyncGoodTimeoutConsumer() throws ApexParameterException { + final String[] args = {"-c", "src/test/resources/parameters/syncGoodParamsConsumerTimeout.json"}; + final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); + + try { + final ApexParameters parameters = new ApexParameterHandler().getParameters(arguments); + assertEquals(12345, parameters.getEventInputParameters().get("SyncConsumer0") + .getPeerTimeout(EventHandlerPeeredMode.SYNCHRONOUS)); + assertEquals(1, parameters.getEventInputParameters().get("SyncConsumer1") + .getPeerTimeout(EventHandlerPeeredMode.SYNCHRONOUS)); + assertEquals(12345, parameters.getEventOutputParameters().get("SyncProducer0") + .getPeerTimeout(EventHandlerPeeredMode.SYNCHRONOUS)); + assertEquals(1, parameters.getEventOutputParameters().get("SyncProducer1") + .getPeerTimeout(EventHandlerPeeredMode.SYNCHRONOUS)); + } catch (final Exception e) { + fail("This test should not throw an exception"); + } + } + + @Test + public void syncGoodSyncGoodTimeoutBoth() throws ApexParameterException { + final String[] args = {"-c", "src/test/resources/parameters/syncGoodParamsBothTimeout.json"}; + final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); + + try { + final ApexParameters parameters = new ApexParameterHandler().getParameters(arguments); + assertEquals(12345, parameters.getEventInputParameters().get("SyncConsumer0") + .getPeerTimeout(EventHandlerPeeredMode.SYNCHRONOUS)); + assertEquals(1, parameters.getEventInputParameters().get("SyncConsumer1") + .getPeerTimeout(EventHandlerPeeredMode.SYNCHRONOUS)); + assertEquals(12345, parameters.getEventOutputParameters().get("SyncProducer0") + .getPeerTimeout(EventHandlerPeeredMode.SYNCHRONOUS)); + assertEquals(1, parameters.getEventOutputParameters().get("SyncProducer1") + .getPeerTimeout(EventHandlerPeeredMode.SYNCHRONOUS)); + } catch (final Exception e) { + fail("This test should not throw an exception"); + } + } + + @Test + public void syncUnusedConsumerPeers() throws ApexParameterException { + final String[] args = {"-c", "src/test/resources/parameters/syncUnusedConsumerPeers.json"}; + final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); + + try { + new ApexParameterHandler().getParameters(arguments); + fail("This test should throw an exception"); + } catch (final ApexParameterException e) { + assertEquals( + "validation error(s) on parameters from \"src/test/resources/parameters/syncUnusedConsumerPeers.json\"\n" + + "Apex parameters invalid\n" + + " value of parameter \"synchronousPeer\" on event output \"SyncProducer1\" must be unique, it s used on another event output\n" + + "" + + " synchronous peers of event input \"SyncConsumer1\" and event output \"SyncProducer1/SyncConsumer0\" do not match", + e.getMessage()); + } + } + + @Test + public void syncMismatchedPeers() throws ApexParameterException { + final String[] args = {"-c", "src/test/resources/parameters/syncMismatchedPeers.json"}; + final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); + + try { + new ApexParameterHandler().getParameters(arguments); + fail("This test should throw an exception"); + } catch (final ApexParameterException e) { + assertEquals( + "validation error(s) on parameters from \"src/test/resources/parameters/syncMismatchedPeers.json\"\n" + + "Apex parameters invalid\n" + + " synchronous peers of event input \"SyncConsumer0\" and event output \"SyncProducer0/SyncConsumer1\" do not match\n" + + " synchronous peers of event input \"SyncConsumer1\" and event output \"SyncProducer1/SyncConsumer0\" do not match", + e.getMessage()); + } + } + + @Test + public void syncUnusedProducerPeers() throws ApexParameterException { + final String[] args = {"-c", "src/test/resources/parameters/syncUnusedProducerPeers.json"}; + final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); + + try { + new ApexParameterHandler().getParameters(arguments); + fail("This test should throw an exception"); + } catch (final ApexParameterException e) { + assertEquals( + "validation error(s) on parameters from \"src/test/resources/parameters/syncUnusedProducerPeers.json\"\n" + + "Apex parameters invalid\n" + + " value of parameter \"synchronousPeer\" on event input \"SyncConsumer1\" must be unique, it s used on another event input\n" + + " synchronous peers of event input \"SyncConsumer0\" and event output \"SyncProducer1/SyncConsumer1\" do not match", + e.getMessage()); + } + } + + @Test + public void syncMismatchedTimeout() throws ApexParameterException { + final String[] args = {"-c", "src/test/resources/parameters/syncUnusedProducerPeers.json"}; + final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); + + try { + new ApexParameterHandler().getParameters(arguments); + fail("This test should throw an exception"); + } catch (final ApexParameterException e) { + assertEquals( + "validation error(s) on parameters from \"src/test/resources/parameters/syncUnusedProducerPeers.json\"\n" + + "Apex parameters invalid\n" + + " value of parameter \"synchronousPeer\" on event input \"SyncConsumer1\" must be unique, it s used on another event input\n" + + " synchronous peers of event input \"SyncConsumer0\" and event output \"SyncProducer1/SyncConsumer1\" do not match", + e.getMessage()); + } + } + + @Test + public void syncGoodParametersTest() { + final String[] args = {"-c", "src/test/resources/parameters/SyncGoodParams.json"}; + final ApexCommandLineArguments arguments = new ApexCommandLineArguments(args); + + try { + final ApexParameters parameters = new ApexParameterHandler().getParameters(arguments); + + assertEquals("MyApexEngine", parameters.getEngineServiceParameters().getName()); + assertEquals("0.0.1", parameters.getEngineServiceParameters().getVersion()); + assertEquals(45, parameters.getEngineServiceParameters().getId()); + assertEquals(19, parameters.getEngineServiceParameters().getInstanceCount()); + assertEquals(65522, parameters.getEngineServiceParameters().getDeploymentPort()); + + final CarrierTechnologyParameters prodCT0 = + parameters.getEventOutputParameters().get("SyncProducer0").getCarrierTechnologyParameters(); + final EventProtocolParameters prodEP0 = + parameters.getEventOutputParameters().get("SyncProducer0").getEventProtocolParameters(); + final CarrierTechnologyParameters consCT0 = + parameters.getEventInputParameters().get("SyncConsumer0").getCarrierTechnologyParameters(); + final EventProtocolParameters consEP0 = + parameters.getEventInputParameters().get("SyncConsumer0").getEventProtocolParameters(); + final CarrierTechnologyParameters prodCT1 = + parameters.getEventOutputParameters().get("SyncProducer1").getCarrierTechnologyParameters(); + final EventProtocolParameters prodEP1 = + parameters.getEventOutputParameters().get("SyncProducer1").getEventProtocolParameters(); + final CarrierTechnologyParameters consCT1 = + parameters.getEventInputParameters().get("SyncConsumer1").getCarrierTechnologyParameters(); + final EventProtocolParameters consEP1 = + parameters.getEventInputParameters().get("SyncConsumer1").getEventProtocolParameters(); + + assertEquals("FILE", prodCT0.getLabel()); + assertEquals("JSON", prodEP0.getLabel()); + assertEquals("FILE", consCT0.getLabel()); + assertEquals("JSON", consEP0.getLabel()); + assertEquals("FILE", prodCT1.getLabel()); + assertEquals("JSON", prodEP1.getLabel()); + assertEquals("SUPER_DOOPER", consCT1.getLabel()); + assertEquals("SUPER_TOK_DEL", consEP1.getLabel()); + + assertTrue(consCT1 instanceof SuperDooperCarrierTechnologyParameters); + assertTrue(consEP1 instanceof SuperTokenDelimitedEventProtocolParameters); + + final SuperDooperCarrierTechnologyParameters superDooperParameters = + (SuperDooperCarrierTechnologyParameters) consCT1; + assertEquals("localhost:9092", superDooperParameters.getBootstrapServers()); + assertEquals("all", superDooperParameters.getAcks()); + assertEquals(0, superDooperParameters.getRetries()); + assertEquals(16384, superDooperParameters.getBatchSize()); + assertEquals(1, superDooperParameters.getLingerTime()); + assertEquals(33554432, superDooperParameters.getBufferMemory()); + assertEquals("default-group-id", superDooperParameters.getGroupId()); + assertTrue(superDooperParameters.isEnableAutoCommit()); + assertEquals(1000, superDooperParameters.getAutoCommitTime()); + assertEquals(30000, superDooperParameters.getSessionTimeout()); + assertEquals("apex-out", superDooperParameters.getProducerTopic()); + assertEquals(100, superDooperParameters.getConsumerPollTime()); + assertEquals("org.apache.superDooper.common.serialization.StringSerializer", + superDooperParameters.getKeySerializer()); + assertEquals("org.apache.superDooper.common.serialization.StringSerializer", + superDooperParameters.getValueSerializer()); + assertEquals("org.apache.superDooper.common.serialization.StringDeserializer", + superDooperParameters.getKeyDeserializer()); + assertEquals("org.apache.superDooper.common.serialization.StringDeserializer", + superDooperParameters.getValueDeserializer()); + + final String[] consumerTopics = {"apex-in"}; + assertEquals(Arrays.asList(consumerTopics), superDooperParameters.getConsumerTopicList()); + } catch (final ApexParameterException e) { + fail("This test should not throw an exception"); + } + } +} diff --git a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/parameters/dummyclasses/SuperDooperCarrierTechnologyParameters.java b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/parameters/dummyclasses/SuperDooperCarrierTechnologyParameters.java new file mode 100644 index 000000000..ffbd1c0e5 --- /dev/null +++ b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/parameters/dummyclasses/SuperDooperCarrierTechnologyParameters.java @@ -0,0 +1,392 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-2018 Ericsson. All rights reserved. + * ================================================================================ + * 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.service.engine.parameters.dummyclasses; + +import java.util.Arrays; +import java.util.Collection; +import java.util.Properties; + +import org.onap.policy.apex.model.basicmodel.service.ParameterService; +import org.onap.policy.apex.service.parameters.carriertechnology.CarrierTechnologyParameters; + +/** + * Apex parameters for SuperDooper as an event carrier technology. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public class SuperDooperCarrierTechnologyParameters extends CarrierTechnologyParameters { + // Default parameter values + private static final String DEFAULT_ACKS = "all"; + private static final String DEFAULT_BOOTSTRAP_SERVERS = "localhost:9092"; + private static final int DEFAULT_RETRIES = 0; + private static final int DEFAULT_BATCH_SIZE = 16384; + private static final int DEFAULT_LINGER_TIME = 1; + private static final long DEFAULT_BUFFER_MEMORY = 33554432; + private static final String DEFAULT_GROUP_ID = "default-group-id"; + private static final boolean DEFAULT_ENABLE_AUTO_COMMIT = true; + private static final int DEFAULT_AUTO_COMMIT_TIME = 1000; + private static final int DEFAULT_SESSION_TIMEOUT = 30000; + private static final String DEFAULT_PRODUCER_TOPIC = "apex-out"; + private static final int DEFAULT_CONSUMER_POLL_TIME = 100; + private static final String[] DEFAULT_CONSUMER_TOPIC_LIST = {"apex-in"}; + private static final String DEFAULT_KEY_SERIALIZER = "org.apache.superDooper.common.serialization.StringSerializer"; + private static final String DEFAULT_VALUE_SERIALIZER = + "org.apache.superDooper.common.serialization.StringSerializer"; + private static final String DEFAULT_KEY_DESERIALIZER = + "org.apache.superDooper.common.serialization.StringDeserializer"; + private static final String DEFAULT_VALUE_DESERIALIZER = + "org.apache.superDooper.common.serialization.StringDeserializer"; + + // Parameter property map tokens + private static final String PROPERTY_BOOTSTRAP_SERVERS = "bootstrap.servers"; + private static final String PROPERTY_ACKS = "acks"; + private static final String PROPERTY_RETRIES = "retries"; + private static final String PROPERTY_BATCH_SIZE = "batch.size"; + private static final String PROPERTY_LINGER_TIME = "linger.ms"; + private static final String PROPERTY_BUFFER_MEMORY = "buffer.memory"; + private static final String PROPERTY_GROUP_ID = "group.id"; + private static final String PROPERTY_ENABLE_AUTO_COMMIT = "enable.auto.commit"; + private static final String PROPERTY_AUTO_COMMIT_TIME = "auto.commit.interval.ms"; + private static final String PROPERTY_SESSION_TIMEOUT = "session.timeout.ms"; + private static final String PROPERTY_KEY_SERIALIZER = "key.serializer"; + private static final String PROPERTY_VALUE_SERIALIZER = "value.serializer"; + private static final String PROPERTY_KEY_DESERIALIZER = "key.deserializer"; + private static final String PROPERTY_VALUE_DESERIALIZER = "value.deserializer"; + + // superDooper carrier parameters + private final String bootstrapServers = DEFAULT_BOOTSTRAP_SERVERS; + private final String acks = DEFAULT_ACKS; + private final int retries = DEFAULT_RETRIES; + private final int batchSize = DEFAULT_BATCH_SIZE; + private final int lingerTime = DEFAULT_LINGER_TIME; + private final long bufferMemory = DEFAULT_BUFFER_MEMORY; + private final String groupId = DEFAULT_GROUP_ID; + private final boolean enableAutoCommit = DEFAULT_ENABLE_AUTO_COMMIT; + private final int autoCommitTime = DEFAULT_AUTO_COMMIT_TIME; + private final int sessionTimeout = DEFAULT_SESSION_TIMEOUT; + private final String producerTopic = DEFAULT_PRODUCER_TOPIC; + private final int consumerPollTime = DEFAULT_CONSUMER_POLL_TIME; + private final String[] consumerTopicList = DEFAULT_CONSUMER_TOPIC_LIST; + private final String keySerializer = DEFAULT_KEY_SERIALIZER; + private final String valueSerializer = DEFAULT_VALUE_SERIALIZER; + private final String keyDeserializer = DEFAULT_KEY_DESERIALIZER; + private final String valueDeserializer = DEFAULT_VALUE_DESERIALIZER; + + /** + * Constructor to create a file carrier technology parameters instance and register the instance + * with the parameter service. + */ + public SuperDooperCarrierTechnologyParameters() { + super(SuperDooperCarrierTechnologyParameters.class.getCanonicalName()); + ParameterService.registerParameters(SuperDooperCarrierTechnologyParameters.class, this); + + // Set the carrier technology properties for the FILE carrier technology + this.setLabel("SUPER_DOOPER"); + this.setEventProducerPluginClass( + "org.onap.policy.apex.service.engine.parameters.dummyclasses.SuperDooperEventProducer"); + this.setEventConsumerPluginClass( + "org.onap.policy.apex.service.engine.parameters.dummyclasses.SuperDooperEventSubscriber"); + } + + /** + * Gets the superDooper producer properties. + * + * @return the superDooper producer properties + */ + public Properties getSuperDooperProducerProperties() { + final Properties superDooperProperties = new Properties(); + + superDooperProperties.put(PROPERTY_BOOTSTRAP_SERVERS, bootstrapServers); + superDooperProperties.put(PROPERTY_ACKS, acks); + superDooperProperties.put(PROPERTY_RETRIES, retries); + superDooperProperties.put(PROPERTY_BATCH_SIZE, batchSize); + superDooperProperties.put(PROPERTY_LINGER_TIME, lingerTime); + superDooperProperties.put(PROPERTY_BUFFER_MEMORY, bufferMemory); + superDooperProperties.put(PROPERTY_KEY_SERIALIZER, keySerializer); + superDooperProperties.put(PROPERTY_VALUE_SERIALIZER, valueSerializer); + + return superDooperProperties; + } + + /** + * Gets the superDooper consumer properties. + * + * @return the superDooper consumer properties + */ + public Properties getSuperDooperConsumerProperties() { + final Properties superDooperProperties = new Properties(); + + superDooperProperties.put(PROPERTY_BOOTSTRAP_SERVERS, bootstrapServers); + superDooperProperties.put(PROPERTY_GROUP_ID, groupId); + superDooperProperties.put(PROPERTY_ENABLE_AUTO_COMMIT, enableAutoCommit); + superDooperProperties.put(PROPERTY_AUTO_COMMIT_TIME, autoCommitTime); + superDooperProperties.put(PROPERTY_SESSION_TIMEOUT, sessionTimeout); + superDooperProperties.put(PROPERTY_KEY_DESERIALIZER, keyDeserializer); + superDooperProperties.put(PROPERTY_VALUE_DESERIALIZER, valueDeserializer); + + return superDooperProperties; + } + + /** + * Gets the bootstrap servers. + * + * @return the bootstrap servers + */ + public String getBootstrapServers() { + return bootstrapServers; + } + + /** + * Gets the acks. + * + * @return the acks + */ + public String getAcks() { + return acks; + } + + /** + * Gets the retries. + * + * @return the retries + */ + public int getRetries() { + return retries; + } + + /** + * Gets the batch size. + * + * @return the batch size + */ + public int getBatchSize() { + return batchSize; + } + + /** + * Gets the linger time. + * + * @return the linger time + */ + public int getLingerTime() { + return lingerTime; + } + + /** + * Gets the buffer memory. + * + * @return the buffer memory + */ + public long getBufferMemory() { + return bufferMemory; + } + + /** + * Gets the group id. + * + * @return the group id + */ + public String getGroupId() { + return groupId; + } + + /** + * Checks if is enable auto commit. + * + * @return true, if checks if is enable auto commit + */ + public boolean isEnableAutoCommit() { + return enableAutoCommit; + } + + /** + * Gets the auto commit time. + * + * @return the auto commit time + */ + public int getAutoCommitTime() { + return autoCommitTime; + } + + /** + * Gets the session timeout. + * + * @return the session timeout + */ + public int getSessionTimeout() { + return sessionTimeout; + } + + /** + * Gets the producer topic. + * + * @return the producer topic + */ + public String getProducerTopic() { + return producerTopic; + } + + /** + * Gets the consumer poll time. + * + * @return the consumer poll time + */ + public long getConsumerPollTime() { + return consumerPollTime; + } + + /** + * Gets the consumer topic list. + * + * @return the consumer topic list + */ + public Collection<String> getConsumerTopicList() { + return Arrays.asList(consumerTopicList); + } + + /** + * Gets the key serializer. + * + * @return the key serializer + */ + public String getKeySerializer() { + return keySerializer; + } + + /** + * Gets the value serializer. + * + * @return the value serializer + */ + public String getValueSerializer() { + return valueSerializer; + } + + /** + * Gets the key deserializer. + * + * @return the key deserializer + */ + public String getKeyDeserializer() { + return keyDeserializer; + } + + /** + * Gets the value deserializer. + * + * @return the value deserializer + */ + public String getValueDeserializer() { + return valueDeserializer; + } + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.apps.uservice.parameters.ApexParameterValidator#validate() + */ + @Override + public String validate() { + final StringBuilder errorMessageBuilder = new StringBuilder(); + + errorMessageBuilder.append(super.validate()); + + if (bootstrapServers == null || bootstrapServers.trim().length() == 0) { + errorMessageBuilder + .append(" bootstrapServers not specified, must be specified as a string of form host:port\n"); + } + + if (acks == null || acks.trim().length() == 0) { + errorMessageBuilder.append(" acks not specified, must be specified as a string with values [0|1|all]\n"); + } + + if (retries < 0) { + errorMessageBuilder.append(" retries [" + retries + "] invalid, must be specified as retries >= 0\n"); + } + + if (batchSize < 0) { + errorMessageBuilder + .append(" batchSize [" + batchSize + "] invalid, must be specified as batchSize >= 0\n"); + } + + if (lingerTime < 0) { + errorMessageBuilder + .append(" lingerTime [" + lingerTime + "] invalid, must be specified as lingerTime >= 0\n"); + } + + if (bufferMemory < 0) { + errorMessageBuilder + .append(" bufferMemory [" + bufferMemory + "] invalid, must be specified as bufferMemory >= 0\n"); + } + + if (groupId == null || groupId.trim().length() == 0) { + errorMessageBuilder.append(" groupId not specified, must be specified as a string\n"); + } + + if (autoCommitTime < 0) { + errorMessageBuilder.append( + " autoCommitTime [" + autoCommitTime + "] invalid, must be specified as autoCommitTime >= 0\n"); + } + + if (sessionTimeout < 0) { + errorMessageBuilder.append( + " sessionTimeout [" + sessionTimeout + "] invalid, must be specified as sessionTimeout >= 0\n"); + } + + if (producerTopic == null || producerTopic.trim().length() == 0) { + errorMessageBuilder.append(" producerTopic not specified, must be specified as a string\n"); + } + + if (consumerPollTime < 0) { + errorMessageBuilder.append(" consumerPollTime [" + consumerPollTime + + "] invalid, must be specified as consumerPollTime >= 0\n"); + } + + if (consumerTopicList == null || consumerTopicList.length == 0) { + errorMessageBuilder.append(" consumerTopicList not specified, must be specified as a list of strings\n"); + } + + for (final String consumerTopic : consumerTopicList) { + if (consumerTopic == null || consumerTopic.trim().length() == 0) { + errorMessageBuilder.append(" invalid consumer topic \"" + consumerTopic + + "\" specified on consumerTopicList, consumer topics must be specified as strings\n"); + } + } + + if (keySerializer == null || keySerializer.trim().length() == 0) { + errorMessageBuilder.append(" keySerializer not specified, must be specified as a string\n"); + } + + if (valueSerializer == null || valueSerializer.trim().length() == 0) { + errorMessageBuilder.append(" valueSerializer not specified, must be specified as a string\n"); + } + + if (keyDeserializer == null || keyDeserializer.trim().length() == 0) { + errorMessageBuilder.append(" keyDeserializer not specified, must be specified as a string\n"); + } + + if (valueDeserializer == null || valueDeserializer.trim().length() == 0) { + errorMessageBuilder.append(" valueDeserializer not specified, must be specified as a string\n"); + } + + return errorMessageBuilder.toString(); + } +} diff --git a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/parameters/dummyclasses/SuperDooperDistributorParameters.java b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/parameters/dummyclasses/SuperDooperDistributorParameters.java new file mode 100644 index 000000000..6d7c6ff7e --- /dev/null +++ b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/parameters/dummyclasses/SuperDooperDistributorParameters.java @@ -0,0 +1,89 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-2018 Ericsson. All rights reserved. + * ================================================================================ + * 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.service.engine.parameters.dummyclasses; + +import org.onap.policy.apex.context.parameters.DistributorParameters; +import org.onap.policy.apex.model.basicmodel.service.ParameterService; + +/** + * Distributor parameters for the Super Dooper Distributor + * + * @author Liam Fallon (liam.fallon@ericsson.com) + * @version + */ +public class SuperDooperDistributorParameters extends DistributorParameters { + // Constants for SuperDooper configuration file locations + public static final String DEFAULT_SUPER_DOOPER_DISTRIBUTION_CONFIG_FILE = "superDooper/superDooper.xml"; + public static final String DEFAULT_SUPER_DOOPER_DISTRIBUTION_JGROUPS_FILE = + "superDooper/jgroups-superDooper-apex.xml"; + public static final boolean DEFAULT_SUPER_DOOPER_JAVA_NET_PREFER_IPV4_STACK = true; + public static final String DEFAULT_INFINSPAN_JGROUPS_BIND_ADDRESS = "localhost"; + + // SuperDooper configuration file names + private String configFile = DEFAULT_SUPER_DOOPER_DISTRIBUTION_CONFIG_FILE; + private String jgroupsFile = DEFAULT_SUPER_DOOPER_DISTRIBUTION_JGROUPS_FILE; + private boolean preferIPv4Stack = DEFAULT_SUPER_DOOPER_JAVA_NET_PREFER_IPV4_STACK; + private String jGroupsBindAddress = DEFAULT_INFINSPAN_JGROUPS_BIND_ADDRESS; + + public SuperDooperDistributorParameters() { + super(SuperDooperDistributorParameters.class.getCanonicalName()); + ParameterService.registerParameters(SuperDooperDistributorParameters.class, this); + ParameterService.registerParameters(DistributorParameters.class, this); + } + + public String getConfigFile() { + return configFile; + } + + public void setConfigFile(final String configFile) { + this.configFile = configFile; + } + + public String getJgroupsFile() { + return jgroupsFile; + } + + public void setJgroupsFile(final String jgroupsFile) { + this.jgroupsFile = jgroupsFile; + } + + public boolean preferIPv4Stack() { + return preferIPv4Stack; + } + + public void setPreferIPv4Stack(final boolean preferIPv4Stack) { + this.preferIPv4Stack = preferIPv4Stack; + } + + public String getjGroupsBindAddress() { + return jGroupsBindAddress; + } + + public void setjGroupsBindAddress(final String jGroupsBindAddress) { + this.jGroupsBindAddress = jGroupsBindAddress; + } + + @Override + public String toString() { + return "SuperDooperDistributorParameters [configFile=" + configFile + ", jgroupsFile=" + jgroupsFile + + ", preferIPv4Stack=" + preferIPv4Stack + ", jGroupsBindAddress=" + jGroupsBindAddress + "]"; + } +} diff --git a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/parameters/dummyclasses/SuperDooperEventProducer.java b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/parameters/dummyclasses/SuperDooperEventProducer.java new file mode 100644 index 000000000..e405107f0 --- /dev/null +++ b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/parameters/dummyclasses/SuperDooperEventProducer.java @@ -0,0 +1,106 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-2018 Ericsson. All rights reserved. + * ================================================================================ + * 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.service.engine.parameters.dummyclasses; + +import org.onap.policy.apex.service.engine.event.ApexEventException; +import org.onap.policy.apex.service.engine.event.ApexEventProducer; +import org.onap.policy.apex.service.engine.event.PeeredReference; +import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters; +import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerPeeredMode; +import org.slf4j.ext.XLogger; +import org.slf4j.ext.XLoggerFactory; + +/** + * @author John Keeney (john.keeney@ericsson.com) + */ +public class SuperDooperEventProducer implements ApexEventProducer { + + private static final XLogger LOGGER = XLoggerFactory.getXLogger(SuperDooperEventProducer.class); + + private String name; + + public SuperDooperEventProducer() {} + + /* + * (non-Javadoc) + * + * @see com.ericsson.apex.service.engine.event.ApexEventProducer#init(java.lang.String, + * com.ericsson.apex.service.parameters.producer.ProducerParameters) + */ + @Override + public void init(final String name, final EventHandlerParameters producerParameters) throws ApexEventException { + this.name = name; + } + + /* + * (non-Javadoc) + * + * @see com.ericsson.apex.service.engine.event.ApexEventProducer#getName() + */ + @Override + public String getName() { + return name; + } + + /* + * (non-Javadoc) + * + * @see + * com.ericsson.apex.service.engine.event.ApexEventProducer#getPeeredReference(com.ericsson.apex + * .service.parameters.eventhandler.EventHandlerPeeredMode) + */ + @Override + public PeeredReference getPeeredReference(final EventHandlerPeeredMode peeredMode) { + return null; + } + + /* + * (non-Javadoc) + * + * @see + * com.ericsson.apex.service.engine.event.ApexEventProducer#setPeeredReference(com.ericsson.apex + * .service.parameters.eventhandler.EventHandlerPeeredMode, + * com.ericsson.apex.service.engine.event.PeeredReference) + */ + @Override + public void setPeeredReference(final EventHandlerPeeredMode peeredMode, final PeeredReference peeredReference) {} + + + /* + * (non-Javadoc) + * + * @see com.ericsson.apex.service.engine.event.ApexEventProducer#sendEvent(long, + * java.lang.String, java.lang.Object) + */ + @Override + public void sendEvent(final long executionId, final String eventName, final Object event) { + LOGGER.info("Sending Event: " + this.getClass().getCanonicalName() + ":" + this.name + " ... event (" + + eventName + ") : " + event); + } + + /* + * (non-Javadoc) + * + * @see com.ericsson.apex.service.engine.event.ApexEventProducer#stop() + */ + @Override + public void stop() {} +} diff --git a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/parameters/dummyclasses/SuperDooperEventSubscriber.java b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/parameters/dummyclasses/SuperDooperEventSubscriber.java new file mode 100644 index 000000000..c791d31e1 --- /dev/null +++ b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/parameters/dummyclasses/SuperDooperEventSubscriber.java @@ -0,0 +1,82 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-2018 Ericsson. All rights reserved. + * ================================================================================ + * 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.service.engine.parameters.dummyclasses; + +import org.onap.policy.apex.service.engine.event.ApexEventConsumer; +import org.onap.policy.apex.service.engine.event.ApexEventException; +import org.onap.policy.apex.service.engine.event.ApexEventReceiver; +import org.onap.policy.apex.service.engine.event.PeeredReference; +import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters; +import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerPeeredMode; +import org.slf4j.ext.XLogger; +import org.slf4j.ext.XLoggerFactory; + +public class SuperDooperEventSubscriber implements ApexEventConsumer { + + private static final XLogger LOGGER = XLoggerFactory.getXLogger(SuperDooperEventSubscriber.class); + + private String name; + + public SuperDooperEventSubscriber() {} + + @Override + public void init(final String name, final EventHandlerParameters consumerParameters, + final ApexEventReceiver apexEventReceiver) throws ApexEventException { + this.name = name; + LOGGER.info("Initialising Apex Consumer: " + this.getClass().getCanonicalName() + ":" + this.name); + } + + @Override + public String getName() { + return name; + } + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.service.engine.event.ApexEventConsumer#getPeeredReference(org.onap. + * policy.apex .service.parameters.eventhandler.EventHandlerPeeredMode) + */ + @Override + public PeeredReference getPeeredReference(final EventHandlerPeeredMode peeredMode) { + return null; + } + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.service.engine.event.ApexEventConsumer#setPeeredReference(org.onap. + * policy.apex.service.parameters.eventhandler.EventHandlerPeeredMode, + * org.onap.policy.apex.service.engine.event.PeeredReference) + */ + @Override + public void setPeeredReference(final EventHandlerPeeredMode peeredMode, final PeeredReference peeredReference) {} + + @Override + public void start() { + // TODO Auto-generated method stub + + } + + @Override + public void stop() {} + +} diff --git a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/parameters/dummyclasses/SuperDooperExecutorParameters.java b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/parameters/dummyclasses/SuperDooperExecutorParameters.java new file mode 100644 index 000000000..3af772985 --- /dev/null +++ b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/parameters/dummyclasses/SuperDooperExecutorParameters.java @@ -0,0 +1,40 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-2018 Ericsson. All rights reserved. + * ================================================================================ + * 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.service.engine.parameters.dummyclasses; + +import org.onap.policy.apex.core.engine.ExecutorParameters; + +/** + * Default Executor parameters for MVEL + * + * @author Liam Fallon (liam.fallon@ericsson.com) + * @version + */ +public class SuperDooperExecutorParameters extends ExecutorParameters { + public SuperDooperExecutorParameters() { + this.setTaskExecutorPluginClass( + "org.onap.policy.apex.service.engine.parameters.dummyclasses.DummyTaskExecutor"); + this.setTaskSelectionExecutorPluginClass( + "org.onap.policy.apex.service.engine.parameters.dummyclasses.DummyTaskSelectExecutor"); + this.setStateFinalizerExecutorPluginClass( + "org.onap.policy.apex.service.engine.parameters.dummyclasses.DummyStateFinalizerExecutor"); + } +} diff --git a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/parameters/dummyclasses/SuperTokenDelimitedEventConverter.java b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/parameters/dummyclasses/SuperTokenDelimitedEventConverter.java new file mode 100644 index 000000000..e807f7fb5 --- /dev/null +++ b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/parameters/dummyclasses/SuperTokenDelimitedEventConverter.java @@ -0,0 +1,58 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-2018 Ericsson. All rights reserved. + * ================================================================================ + * 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.service.engine.parameters.dummyclasses; + +import java.util.List; + +import org.onap.policy.apex.model.basicmodel.concepts.ApexException; +import org.onap.policy.apex.service.engine.event.ApexEvent; +import org.onap.policy.apex.service.engine.event.ApexEventProtocolConverter; +import org.onap.policy.apex.service.parameters.eventprotocol.EventProtocolParameters; + +public final class SuperTokenDelimitedEventConverter implements ApexEventProtocolConverter { + + /* + * (non-Javadoc) + * + * @see + * org.onap.policy.apex.service.engine.event.ApexEventConverter#toApexEvent(java.lang.String, + * java.lang.Object) + */ + @Override + public List<ApexEvent> toApexEvent(final String eventName, final Object eventOfOtherType) throws ApexException { + return null; + } + + /* + * (non-Javadoc) + * + * @see + * org.onap.policy.apex.service.engine.event.ApexEventConverter#fromApexEvent(org.onap.policy. + * apex.service.engine.event.ApexEvent) + */ + @Override + public String fromApexEvent(final ApexEvent apexEvent) throws ApexException { + return null; + } + + @Override + public void init(final EventProtocolParameters parameters) {} +} diff --git a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/parameters/dummyclasses/SuperTokenDelimitedEventProtocolParameters.java b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/parameters/dummyclasses/SuperTokenDelimitedEventProtocolParameters.java new file mode 100644 index 000000000..99f938e10 --- /dev/null +++ b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/parameters/dummyclasses/SuperTokenDelimitedEventProtocolParameters.java @@ -0,0 +1,54 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-2018 Ericsson. All rights reserved. + * ================================================================================ + * 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.service.engine.parameters.dummyclasses; + +import org.onap.policy.apex.model.basicmodel.service.ParameterService; +import org.onap.policy.apex.service.engine.event.impl.jsonprotocolplugin.JSONEventProtocolParameters; +import org.onap.policy.apex.service.parameters.eventprotocol.EventProtocolTextTokenDelimitedParameters; + +/** + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public class SuperTokenDelimitedEventProtocolParameters extends EventProtocolTextTokenDelimitedParameters { + /** The label of this carrier technology. */ + public static final String SUPER_TOKEN_EVENT_PROTOCOL_LABEL = "SUPER_TOK_DEL"; + + // Constants for text delimiter tokeb + private static final String SUPER_TOKEN_DELIMITER = "SuperToken"; + + /** + * Constructor to create a JSON event protocol parameter instance and register the instance with + * the parameter service. + */ + public SuperTokenDelimitedEventProtocolParameters() { + super(JSONEventProtocolParameters.class.getCanonicalName()); + ParameterService.registerParameters(SuperTokenDelimitedEventProtocolParameters.class, this); + + // Set the event protocol properties for the JSON carrier technology + this.setLabel(SUPER_TOKEN_EVENT_PROTOCOL_LABEL); + + // Set the starting and ending delimiters for text blocks of JSON events + this.setDelimiterToken(SUPER_TOKEN_DELIMITER); + + // Set the event protocol plugin class + this.setEventProtocolPluginClass(SuperTokenDelimitedEventConverter.class.getCanonicalName()); + } +} |