From 8f142981c3f5fddfd58c2e106ead7b4f5fc93da0 Mon Sep 17 00:00:00 2001 From: Parshad Patel Date: Wed, 26 Dec 2018 14:54:59 +0900 Subject: Rename test classes in apex-pdp/services Make test classes name consitence by adding 'Test' at end of junit test classes and adding 'Support' or 'Dummy' at start of util or dummy type of test classes Issue-ID: POLICY-1263 Change-Id: I951f4e30736144010d69a0fb3b9181f7fee76013 Signed-off-by: Parshad Patel --- .../apex/service/engine/engdep/BadAction.java | 45 --- .../apex/service/engine/engdep/BadMessage.java | 41 --- .../apex/service/engine/engdep/DummyAction.java | 45 +++ .../apex/service/engine/engdep/DummyMessage.java | 41 +++ .../engine/engdep/EngDepMessageListenerTest.java | 6 +- .../engine/event/JsonEventConverterTest.java | 105 +++++++ .../service/engine/event/JsonEventGenerator.java | 346 --------------------- .../engine/event/JsonEventHandlerForPojoTest.java | 247 +++++++++++++++ .../service/engine/event/JsonEventHandlerTest.java | 315 +++++++++++++++++++ .../event/JsonEventProtocolPrametersTest.java | 66 ++++ .../engine/event/JsonTaggedEventConsumerTest.java | 131 ++++++++ .../service/engine/event/PluginFactoriesTest.java | 63 ++++ .../engine/event/SupportJsonEventGenerator.java | 346 +++++++++++++++++++++ .../engine/event/TestJsonEventConverter.java | 105 ------- .../service/engine/event/TestJsonEventHandler.java | 315 ------------------- .../engine/event/TestJsonEventHandlerForPojo.java | 247 --------------- .../event/TestJsonEventProtocolPrameters.java | 66 ---- .../engine/event/TestJsonTaggedEventConsumer.java | 131 -------- .../service/engine/event/TestPluginFactories.java | 63 ---- .../service/engine/event/testpojos/DummyPojo.java | 69 ++++ .../engine/event/testpojos/DummyPojoList.java | 39 +++ .../engine/event/testpojos/DummySubPojo.java | 69 ++++ .../engine/event/testpojos/DummySubSubPojo.java | 57 ++++ .../service/engine/event/testpojos/TestPojo.java | 69 ---- .../engine/event/testpojos/TestPojoList.java | 39 --- .../engine/event/testpojos/TestSubPojo.java | 69 ---- .../engine/event/testpojos/TestSubSubPojo.java | 57 ---- .../engine/main/ApexCommandLineArgumentsTest.java | 262 ++++++++++++++++ .../engine/main/TestApexCommandLineArguments.java | 262 ---------------- .../resources/policymodels/PojoEventModel.apex | 4 +- .../resources/policymodels/PojoEventModel.json | 4 +- 31 files changed, 1862 insertions(+), 1862 deletions(-) delete mode 100644 services/services-engine/src/test/java/org/onap/policy/apex/service/engine/engdep/BadAction.java delete mode 100644 services/services-engine/src/test/java/org/onap/policy/apex/service/engine/engdep/BadMessage.java create mode 100644 services/services-engine/src/test/java/org/onap/policy/apex/service/engine/engdep/DummyAction.java create mode 100644 services/services-engine/src/test/java/org/onap/policy/apex/service/engine/engdep/DummyMessage.java create mode 100644 services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/JsonEventConverterTest.java delete mode 100644 services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/JsonEventGenerator.java create mode 100644 services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/JsonEventHandlerForPojoTest.java create mode 100644 services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/JsonEventHandlerTest.java create mode 100644 services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/JsonEventProtocolPrametersTest.java create mode 100644 services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/JsonTaggedEventConsumerTest.java create mode 100644 services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/PluginFactoriesTest.java create mode 100644 services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/SupportJsonEventGenerator.java delete mode 100644 services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/TestJsonEventConverter.java delete mode 100644 services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/TestJsonEventHandler.java delete mode 100644 services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/TestJsonEventHandlerForPojo.java delete mode 100644 services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/TestJsonEventProtocolPrameters.java delete mode 100644 services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/TestJsonTaggedEventConsumer.java delete mode 100644 services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/TestPluginFactories.java create mode 100644 services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/testpojos/DummyPojo.java create mode 100644 services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/testpojos/DummyPojoList.java create mode 100644 services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/testpojos/DummySubPojo.java create mode 100644 services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/testpojos/DummySubSubPojo.java delete mode 100644 services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/testpojos/TestPojo.java delete mode 100644 services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/testpojos/TestPojoList.java delete mode 100644 services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/testpojos/TestSubPojo.java delete mode 100644 services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/testpojos/TestSubSubPojo.java create mode 100644 services/services-engine/src/test/java/org/onap/policy/apex/service/engine/main/ApexCommandLineArgumentsTest.java delete mode 100644 services/services-engine/src/test/java/org/onap/policy/apex/service/engine/main/TestApexCommandLineArguments.java (limited to 'services') diff --git a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/engdep/BadAction.java b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/engdep/BadAction.java deleted file mode 100644 index 72cdb54b3..000000000 --- a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/engdep/BadAction.java +++ /dev/null @@ -1,45 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 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.engdep; - -import org.apache.commons.lang3.NotImplementedException; -import org.onap.policy.apex.core.protocols.Action; - -/** - * Bad action class. - */ -public class BadAction implements Action { - private static final long serialVersionUID = -6562765120898697138L; - - private String actionString; - - public BadAction(final String actionString) { - this.actionString = actionString; - } - - @Override - public String getActionString() { - if (actionString == "throw exception") { - throw new NotImplementedException("dummy IO excepton"); - } - return actionString; - } -} diff --git a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/engdep/BadMessage.java b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/engdep/BadMessage.java deleted file mode 100644 index acbfed83f..000000000 --- a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/engdep/BadMessage.java +++ /dev/null @@ -1,41 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 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.engdep; - -import org.onap.policy.apex.core.protocols.Action; -import org.onap.policy.apex.core.protocols.Message; -import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; - -/** - * Bad protocol message. - */ -public class BadMessage extends Message { - private static final long serialVersionUID = 3827403727783909797L; - - /** - * Constructor. - * @param action the message action - * @param targetKey the message target key - */ - public BadMessage(Action action, AxArtifactKey targetKey) { - super(action, targetKey); - } -} diff --git a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/engdep/DummyAction.java b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/engdep/DummyAction.java new file mode 100644 index 000000000..6b59b0754 --- /dev/null +++ b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/engdep/DummyAction.java @@ -0,0 +1,45 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 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.engdep; + +import org.apache.commons.lang3.NotImplementedException; +import org.onap.policy.apex.core.protocols.Action; + +/** + * Bad action class. + */ +public class DummyAction implements Action { + private static final long serialVersionUID = -6562765120898697138L; + + private String actionString; + + public DummyAction(final String actionString) { + this.actionString = actionString; + } + + @Override + public String getActionString() { + if (actionString == "throw exception") { + throw new NotImplementedException("dummy IO excepton"); + } + return actionString; + } +} diff --git a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/engdep/DummyMessage.java b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/engdep/DummyMessage.java new file mode 100644 index 000000000..5d1b1ce8e --- /dev/null +++ b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/engdep/DummyMessage.java @@ -0,0 +1,41 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 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.engdep; + +import org.onap.policy.apex.core.protocols.Action; +import org.onap.policy.apex.core.protocols.Message; +import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; + +/** + * Bad protocol message. + */ +public class DummyMessage extends Message { + private static final long serialVersionUID = 3827403727783909797L; + + /** + * Constructor. + * @param action the message action + * @param targetKey the message target key + */ + public DummyMessage(Action action, AxArtifactKey targetKey) { + super(action, targetKey); + } +} diff --git a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/engdep/EngDepMessageListenerTest.java b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/engdep/EngDepMessageListenerTest.java index f7ac43dbc..f755a669c 100644 --- a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/engdep/EngDepMessageListenerTest.java +++ b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/engdep/EngDepMessageListenerTest.java @@ -138,19 +138,19 @@ public class EngDepMessageListenerTest { assertEquals("UpdateModel:0.0.1", dummyEngineService.getUpdateModelKey().getId()); messageList.clear(); - Message badMessage0 = new BadMessage(null, null); + Message badMessage0 = new DummyMessage(null, null); messageList.add(badMessage0); listener.onMessage(new MessageBlock<>(messageList, webSocketMock)); ThreadUtilities.sleep(50); messageList.clear(); - Message badMessage1 = new BadMessage(new BadAction(null), null); + Message badMessage1 = new DummyMessage(new DummyAction(null), null); messageList.add(badMessage1); listener.onMessage(new MessageBlock<>(messageList, webSocketMock)); ThreadUtilities.sleep(50); messageList.clear(); - Message badMessage2 = new BadMessage(new BadAction("throw exception"), null); + Message badMessage2 = new DummyMessage(new DummyAction("throw exception"), null); messageList.add(badMessage2); listener.onMessage(new MessageBlock<>(messageList, webSocketMock)); ThreadUtilities.sleep(50); diff --git a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/JsonEventConverterTest.java b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/JsonEventConverterTest.java new file mode 100644 index 000000000..99b2654ab --- /dev/null +++ b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/JsonEventConverterTest.java @@ -0,0 +1,105 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 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.fail; + +import org.junit.Test; +import org.onap.policy.apex.service.engine.event.impl.jsonprotocolplugin.Apex2JsonEventConverter; +import org.onap.policy.apex.service.engine.event.impl.jsonprotocolplugin.JsonEventProtocolParameters; +import org.onap.policy.apex.service.parameters.eventprotocol.EventProtocolParameters; + +/** + * Test the JSON event converter corner cases. + * + */ +public class JsonEventConverterTest { + @Test + public void testJsonEventConverter() { + Apex2JsonEventConverter converter = new Apex2JsonEventConverter(); + + try { + converter.init(null); + fail("test should throw an exception"); + } catch (Exception ie) { + assertEquals("specified consumer properties are not applicable to the JSON event protocol", + ie.getMessage()); + } + + try { + converter.init(new EventProtocolParameters() { + }); + fail("test should throw an exception"); + } catch (Exception ie) { + assertEquals("specified consumer properties are not applicable to the JSON event protocol", + ie.getMessage()); + } + + JsonEventProtocolParameters pars = new JsonEventProtocolParameters(); + converter.init(pars); + + try { + converter.toApexEvent(null, null); + fail("test should throw an exception"); + } catch (Exception tae) { + assertEquals("event processing failed, event is null", tae.getMessage()); + } + + try { + converter.toApexEvent(null, 1); + fail("test should throw an exception"); + } catch (Exception tae) { + assertEquals("error converting event \"1\" to a string", tae.getMessage()); + } + + try { + converter.toApexEvent(null, "[{\"aKey\": 1},{\"aKey\": 2}]"); + fail("test should throw an exception"); + } catch (Exception tae) { + assertEquals("Failed to unmarshal JSON event: event received without mandatory parameter \"name\" " + + "on configuration or on event, event=[{\"aKey\": 1},{\"aKey\": 2}]", tae.getMessage()); + } + + try { + converter.toApexEvent(null, "[1,2,3]"); + fail("test should throw an exception"); + } catch (Exception tae) { + assertEquals("Failed to unmarshal JSON event: incoming event ([1,2,3]) is a JSON object array " + + "containing an invalid object 1.0, event=[1,2,3]", tae.getMessage()); + } + + try { + converter.fromApexEvent(null); + fail("test should throw an exception"); + } catch (Exception tae) { + assertEquals("event processing failed, Apex event is null", tae.getMessage()); + } + + try { + converter.fromApexEvent(new ApexEvent("Event", "0.0.1", "a.name.space", "here", "there")); + fail("test should throw an exception"); + } catch (Exception tae) { + assertEquals("Model for org.onap.policy.apex.model.eventmodel.concepts.AxEvents not found in model service", + tae.getMessage()); + } + } +} 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 deleted file mode 100644 index c2dbd48d6..000000000 --- a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/JsonEventGenerator.java +++ /dev/null @@ -1,346 +0,0 @@ -/*- - * ============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; - -/** - * This class generates JSON event used for the test cases. - * - * @author Liam Fallon (liam.fallon@ericsson.com) - */ -public class JsonEventGenerator { - /** - * Json event. - * - * @return the string - */ - public static String jsonEvent() { - final StringBuilder builder = new StringBuilder(); - - builder.append("{\n"); - builder.append(" \"nameSpace\": \"org.onap.policy.apex.events\",\n"); - builder.append(" \"name\": \"BasicEvent\",\n"); - builder.append(" \"version\": \"0.0.1\",\n"); - builder.append(" \"source\": \"test\",\n"); - builder.append(" \"target\": \"apex\",\n"); - builder.append(" \"intPar\": 12345\n"); - builder.append("}"); - - return builder.toString(); - } - - /** - * Json event no name. - * - * @return the string - */ - public static String jsonEventNoName() { - final StringBuilder builder = new StringBuilder(); - - builder.append("{\n"); - builder.append(" \"nameSpace\": \"org.onap.policy.apex.events\",\n"); - builder.append(" \"namez\": \"BasicEvent\",\n"); - builder.append(" \"version\": \"0.0.1\",\n"); - builder.append(" \"source\": \"test\",\n"); - builder.append(" \"target\": \"apex\",\n"); - builder.append(" \"intPar\": 12346\n"); - builder.append("}"); - - return builder.toString(); - } - - /** - * Json event bad name. - * - * @return the string - */ - public static String jsonEventBadName() { - final StringBuilder builder = new StringBuilder(); - - builder.append("{\n"); - builder.append(" \"nameSpace\": \"org.onap.policy.apex.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(" \"intPar\": 12347\n"); - builder.append("}"); - - return builder.toString(); - } - - /** - * Json event no ex name. - * - * @return the string - */ - public static String jsonEventNoExName() { - final StringBuilder builder = new StringBuilder(); - - builder.append("{\n"); - builder.append(" \"nameSpace\": \"org.onap.policy.apex.events\",\n"); - builder.append(" \"name\": \"I_DONT_EXIST\",\n"); - builder.append(" \"source\": \"test\",\n"); - builder.append(" \"target\": \"apex\",\n"); - builder.append(" \"intPar\": 12348\n"); - builder.append("}"); - - return builder.toString(); - } - - /** - * Json event no version. - * - * @return the string - */ - public static String jsonEventNoVersion() { - final StringBuilder builder = new StringBuilder(); - - builder.append("{\n"); - builder.append(" \"nameSpace\": \"org.onap.policy.apex.events\",\n"); - builder.append(" \"name\": \"BasicEvent\",\n"); - builder.append(" \"versiion\": \"0.0.1\",\n"); - builder.append(" \"source\": \"test\",\n"); - builder.append(" \"target\": \"apex\",\n"); - builder.append(" \"intPar\": 12349\n"); - builder.append("}"); - - return builder.toString(); - } - - /** - * Json event bad version. - * - * @return the string - */ - public static String jsonEventBadVersion() { - final StringBuilder builder = new StringBuilder(); - - builder.append("{\n"); - builder.append(" \"nameSpace\": \"org.onap.policy.apex.events\",\n"); - builder.append(" \"name\": \"BasicEvent\",\n"); - builder.append(" \"version\": \"#####\",\n"); - builder.append(" \"source\": \"test\",\n"); - builder.append(" \"target\": \"apex\",\n"); - builder.append(" \"intPar\": 12350\n"); - builder.append("}"); - - return builder.toString(); - } - - /** - * Json event no ex version. - * - * @return the string - */ - public static String jsonEventNoExVersion() { - final StringBuilder builder = new StringBuilder(); - - builder.append("{\n"); - builder.append(" \"nameSpace\": \"org.onap.policy.apex.events\",\n"); - builder.append(" \"name\": \"BasicEvent\",\n"); - builder.append(" \"version\": \"1.2.3\",\n"); - builder.append(" \"source\": \"test\",\n"); - builder.append(" \"target\": \"apex\",\n"); - builder.append(" \"intPar\": 12351\n"); - builder.append("}"); - - return builder.toString(); - } - - /** - * Json event no namespace. - * - * @return the string - */ - public static String jsonEventNoNamespace() { - final StringBuilder builder = new StringBuilder(); - - builder.append("{\n"); - builder.append(" \"nameSpacee\": \"org.onap.policy.apex.events\",\n"); - builder.append(" \"name\": \"BasicEvent\",\n"); - builder.append(" \"version\": \"0.0.1\",\n"); - builder.append(" \"source\": \"test\",\n"); - builder.append(" \"target\": \"apex\",\n"); - builder.append(" \"intPar\": 12352\n"); - builder.append("}"); - - return builder.toString(); - } - - /** - * Json event bad namespace. - * - * @return the string - */ - public static String jsonEventBadNamespace() { - final StringBuilder builder = new StringBuilder(); - - builder.append("{\n"); - builder.append(" \"nameSpace\": \"hello.&&&&\",\n"); - builder.append(" \"name\": \"BasicEvent\",\n"); - builder.append(" \"version\": \"0.0.1\",\n"); - builder.append(" \"source\": \"test\",\n"); - builder.append(" \"target\": \"apex\",\n"); - builder.append(" \"intPar\": 12353\n"); - builder.append("}"); - - return builder.toString(); - } - - /** - * Json event no ex namespace. - * - * @return the string - */ - public static String jsonEventNoExNamespace() { - final StringBuilder builder = new StringBuilder(); - - builder.append("{\n"); - builder.append(" \"nameSpace\": \"pie.in.the.sky\",\n"); - builder.append(" \"name\": \"BasicEvent\",\n"); - builder.append(" \"version\": \"0.0.1\",\n"); - builder.append(" \"source\": \"test\",\n"); - builder.append(" \"target\": \"apex\",\n"); - builder.append(" \"intPar\": 12354\n"); - builder.append("}"); - - return builder.toString(); - } - - /** - * Json event no source. - * - * @return the string - */ - public static String jsonEventNoSource() { - final StringBuilder builder = new StringBuilder(); - - builder.append("{\n"); - builder.append(" \"nameSpace\": \"org.onap.policy.apex.events\",\n"); - builder.append(" \"name\": \"BasicEvent\",\n"); - builder.append(" \"version\": \"0.0.1\",\n"); - builder.append(" \"sourcee\": \"test\",\n"); - builder.append(" \"target\": \"apex\",\n"); - builder.append(" \"intPar\": 12355\n"); - builder.append("}"); - - return builder.toString(); - } - - /** - * Json event bad source. - * - * @return the string - */ - public static String jsonEventBadSource() { - final StringBuilder builder = new StringBuilder(); - - builder.append("{\n"); - builder.append(" \"nameSpace\": \"org.onap.policy.apex.events\",\n"); - builder.append(" \"name\": \"BasicEvent\",\n"); - builder.append(" \"version\": \"0.0.1\",\n"); - builder.append(" \"source\": \"%!@**@!\",\n"); - builder.append(" \"target\": \"apex\",\n"); - builder.append(" \"intPar\": 12356\n"); - builder.append("}"); - - return builder.toString(); - } - - /** - * Json event no target. - * - * @return the string - */ - public static String jsonEventNoTarget() { - final StringBuilder builder = new StringBuilder(); - - builder.append("{\n"); - builder.append(" \"nameSpace\": \"org.onap.policy.apex.events\",\n"); - builder.append(" \"name\": \"BasicEvent\",\n"); - builder.append(" \"version\": \"0.0.1\",\n"); - builder.append(" \"source\": \"test\",\n"); - builder.append(" \"targett\": \"apex\",\n"); - builder.append(" \"intPar\": 12357\n"); - builder.append("}"); - - return builder.toString(); - } - - /** - * Json event bad target. - * - * @return the string - */ - public static String jsonEventBadTarget() { - final StringBuilder builder = new StringBuilder(); - - builder.append("{\n"); - builder.append(" \"nameSpace\": \"org.onap.policy.apex.events\",\n"); - builder.append(" \"name\": \"BasicEvent\",\n"); - builder.append(" \"version\": \"0.0.1\",\n"); - builder.append(" \"source\": \"test\",\n"); - builder.append(" \"target\": \"KNIO(*S)A(S)D\",\n"); - builder.append(" \"intPar\": 12358\n"); - builder.append("}"); - - return builder.toString(); - } - - /** - * Json event missing fields. - * - * @return the string - */ - public static String jsonEventMissingFields() { - final StringBuilder builder = new StringBuilder(); - - builder.append("{\n"); - builder.append(" \"nameSpace\": \"org.onap.policy.apex.events\",\n"); - builder.append(" \"name\": \"BasicEvent\",\n"); - builder.append(" \"version\": \"0.0.1\",\n"); - builder.append(" \"source\": \"test\",\n"); - builder.append(" \"target\": \"apex\"\n"); - builder.append("}"); - - return builder.toString(); - } - - /** - * Json event null fields. - * - * @return the string - */ - public static String jsonEventNullFields() { - final StringBuilder builder = new StringBuilder(); - - builder.append("{\n"); - builder.append(" \"nameSpace\": \"org.onap.policy.apex.events\",\n"); - builder.append(" \"name\": \"BasicEvent\",\n"); - builder.append(" \"version\": \"0.0.1\",\n"); - builder.append(" \"source\": \"test\",\n"); - builder.append(" \"target\": \"Apex\",\n"); - builder.append(" \"intPar\": -1\n"); - builder.append("}"); - - return builder.toString(); - } -} diff --git a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/JsonEventHandlerForPojoTest.java b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/JsonEventHandlerForPojoTest.java new file mode 100644 index 000000000..ee15d2745 --- /dev/null +++ b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/JsonEventHandlerForPojoTest.java @@ -0,0 +1,247 @@ +/*- + * ============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.fail; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.util.List; + +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; +import org.onap.policy.apex.context.parameters.ContextParameterConstants; +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.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.onap.policy.apex.service.engine.event.testpojos.DummyPojo; +import org.onap.policy.apex.service.engine.event.testpojos.DummyPojoList; +import org.onap.policy.common.parameters.ParameterService; +import org.slf4j.ext.XLogger; +import org.slf4j.ext.XLoggerFactory; + +/** + * Test JSON Event Handler. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public class JsonEventHandlerForPojoTest { + private static final XLogger logger = XLoggerFactory.getXLogger(JsonEventHandlerForPojoTest.class); + + /** + * Setup event model. + * + * @throws IOException Signals that an I/O exception has occurred. + * @throws ApexModelException the apex model exception + */ + @BeforeClass + public static void setupEventModel() throws IOException, ApexModelException { + final String policyModelString = TextFileUtils + .getTextFileAsString("src/test/resources/policymodels/PojoEventModel.json"); + final ApexModelReader modelReader = new ApexModelReader(AxPolicyModel.class); + modelReader.setValidateFlag(false); + final AxPolicyModel apexPolicyModel = modelReader.read(new ByteArrayInputStream(policyModelString.getBytes())); + + // Set up the models in the model service + apexPolicyModel.register(); + } + + /** + * Initialize default schema parameters. + */ + @BeforeClass + public static void initializeDefaultSchemaParameters() { + ParameterService.clear(); + final SchemaParameters schemaParameters = new SchemaParameters(); + schemaParameters.setName(ContextParameterConstants.SCHEMA_GROUP_NAME); + ParameterService.register(schemaParameters); + } + + /** + * Teardown default schema parameters. + */ + @AfterClass + public static void teardownDefaultSchemaParameters() { + ParameterService.deregister(ContextParameterConstants.SCHEMA_GROUP_NAME); + ModelService.clear(); + } + + /** + * Test POJO to apex event and back. + * + * @throws ApexException the apex exception + * @throws IOException on IO exceptions + */ + @Test + public void testJsonPojoToApexEvent() throws ApexException, IOException { + final Apex2JsonEventConverter jsonEventConverter = new Apex2JsonEventConverter(); + assertNotNull(jsonEventConverter); + + JsonEventProtocolParameters pars = new JsonEventProtocolParameters(); + pars.setPojoField("POJO_PAR"); + jsonEventConverter.init(pars); + + final String apexEventJsonStringIn = TextFileUtils + .getTextFileAsString("src/test/resources/events/TestPojoEvent.json"); + + logger.debug("input event\n" + apexEventJsonStringIn); + + final List apexEventList = jsonEventConverter.toApexEvent("PojoEvent", apexEventJsonStringIn); + assertEquals(1, apexEventList.size()); + final ApexEvent apexEvent = apexEventList.get(0); + assertNotNull(apexEvent); + + logger.debug(apexEvent.toString()); + + assertEquals("PojoEvent", apexEvent.getName()); + assertEquals("0.0.1", apexEvent.getVersion()); + assertEquals("org.onap.policy.apex.service.engine.event.testpojos", apexEvent.getNameSpace()); + assertEquals("Outside", apexEvent.getSource()); + assertEquals("Apex", apexEvent.getTarget()); + + DummyPojo testPojo = (DummyPojo) apexEvent.get("POJO_PAR"); + + assertEquals(1, testPojo.getAnInt()); + assertEquals(2, testPojo.getAnInteger().intValue()); + assertEquals("a string", testPojo.getSomeString()); + + assertEquals(10, testPojo.getTestSubPojo().getAnInt()); + assertEquals(20, testPojo.getTestSubPojo().getAnInteger().intValue()); + assertEquals("a sub string", testPojo.getTestSubPojo().getSomeString()); + + assertEquals(100, testPojo.getTestSubPojo().getTestSubSubPojo().getAnInt()); + assertEquals(200, testPojo.getTestSubPojo().getTestSubSubPojo().getAnInteger().intValue()); + assertEquals("a sub sub string", testPojo.getTestSubPojo().getTestSubSubPojo().getSomeString()); + + String eventBackInJson = (String) jsonEventConverter.fromApexEvent(apexEvent); + assertEquals(apexEventJsonStringIn.replaceAll("\\s+", ""), eventBackInJson.replaceAll("\\s+", "")); + } + + /** + * Test POJO List to apex event and back. + * + * @throws ApexException the apex exception + * @throws IOException on IO exceptions + */ + @Test + public void testJsonPojoListToApexEvent() throws ApexException, IOException { + final Apex2JsonEventConverter jsonEventConverter = new Apex2JsonEventConverter(); + assertNotNull(jsonEventConverter); + + JsonEventProtocolParameters pars = new JsonEventProtocolParameters(); + pars.setPojoField("POJO_LIST_PAR"); + jsonEventConverter.init(pars); + + final String apexEventJsonStringIn = TextFileUtils + .getTextFileAsString("src/test/resources/events/TestPojoListEvent.json"); + + logger.debug("input event\n" + apexEventJsonStringIn); + + final List apexEventList = jsonEventConverter.toApexEvent("PojoListEvent", apexEventJsonStringIn); + assertEquals(1, apexEventList.size()); + final ApexEvent apexEvent = apexEventList.get(0); + assertNotNull(apexEvent); + + logger.debug(apexEvent.toString()); + + assertEquals("PojoListEvent", apexEvent.getName()); + assertEquals("0.0.1", apexEvent.getVersion()); + assertEquals("org.onap.policy.apex.service.engine.event.testpojos", apexEvent.getNameSpace()); + assertEquals("Outside", apexEvent.getSource()); + assertEquals("Apex", apexEvent.getTarget()); + + DummyPojoList testPojoList = (DummyPojoList) apexEvent.get("POJO_LIST_PAR"); + + for (DummyPojo testPojo : testPojoList.getTestPojoList()) { + assertEquals(1, testPojo.getAnInt()); + assertEquals(2, testPojo.getAnInteger().intValue()); + assertEquals("a string", testPojo.getSomeString()); + + assertEquals(10, testPojo.getTestSubPojo().getAnInt()); + assertEquals(20, testPojo.getTestSubPojo().getAnInteger().intValue()); + assertEquals("a sub string", testPojo.getTestSubPojo().getSomeString()); + + assertEquals(100, testPojo.getTestSubPojo().getTestSubSubPojo().getAnInt()); + assertEquals(200, testPojo.getTestSubPojo().getTestSubSubPojo().getAnInteger().intValue()); + assertEquals("a sub sub string", testPojo.getTestSubPojo().getTestSubSubPojo().getSomeString()); + } + String eventBackInJson = (String) jsonEventConverter.fromApexEvent(apexEvent); + assertEquals(apexEventJsonStringIn.replaceAll("\\s+", ""), eventBackInJson.replaceAll("\\s+", "")); + } + + /** + * Test POJO event with bad configurations. + * + * @throws ApexException the apex exception + * @throws IOException on IO exceptions + */ + @Test + public void testJsonBadPojoApexEvent() throws ApexException, IOException { + final Apex2JsonEventConverter jsonEventConverter = new Apex2JsonEventConverter(); + assertNotNull(jsonEventConverter); + + JsonEventProtocolParameters pars = new JsonEventProtocolParameters(); + pars.setPojoField("BAD_POJO_PAR"); + jsonEventConverter.init(pars); + + final String apexEventJsonStringIn = TextFileUtils + .getTextFileAsString("src/test/resources/events/TestPojoEvent.json"); + + logger.debug("input event\n" + apexEventJsonStringIn); + + try { + jsonEventConverter.toApexEvent("PojoEvent", apexEventJsonStringIn); + fail("test should throw an exception"); + } catch (Exception tae) { + assertEquals("Failed to unmarshal JSON event: error parsing PojoEvent:0.0.1 event from Json. " + + "Field BAD_POJO_PAR not found on POJO event definition.", + tae.getMessage().substring(0, 133)); + } + + pars.setPojoField("POJO_PAR"); + try { + jsonEventConverter.toApexEvent("PojoNoFieldEvent", apexEventJsonStringIn); + fail("test should throw an exception"); + } catch (Exception tae) { + assertEquals("Failed to unmarshal JSON event: error parsing PojoNoFieldEvent:0.0.1 event from Json, " + + "Field POJO_PAR not found, no fields defined on event.", + tae.getMessage().substring(0, 139)); + } + + try { + jsonEventConverter.toApexEvent("PojoTooManyFieldsEvent", apexEventJsonStringIn); + fail("test should throw an exception"); + } catch (Exception tae) { + assertEquals("Failed to unmarshal JSON event: error parsing PojoTooManyFieldsEvent:0.0.1 event from Json, " + + "Field POJO_PAR, one and only one field may be defined on a POJO event definition.", + tae.getMessage().substring(0, 173)); + } + } +} diff --git a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/JsonEventHandlerTest.java b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/JsonEventHandlerTest.java new file mode 100644 index 000000000..73dd9bbe0 --- /dev/null +++ b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/JsonEventHandlerTest.java @@ -0,0 +1,315 @@ +/*- + * ============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.assertNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; +import org.onap.policy.apex.context.parameters.ContextParameterConstants; +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.onap.policy.common.parameters.ParameterService; +import org.slf4j.ext.XLogger; +import org.slf4j.ext.XLoggerFactory; + +/** + * Test JSON Event Handler. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public class JsonEventHandlerTest { + private static final XLogger logger = XLoggerFactory.getXLogger(JsonEventHandlerTest.class); + + /** + * Setup event model. + * + * @throws IOException Signals that an I/O exception has occurred. + * @throws ApexModelException the apex model exception + */ + @BeforeClass + public static void setupEventModel() throws IOException, ApexModelException { + final String policyModelString = TextFileUtils + .getTextFileAsString("src/test/resources/policymodels/SmallModel.json"); + final ApexModelReader modelReader = new ApexModelReader(AxPolicyModel.class); + final AxPolicyModel apexPolicyModel = modelReader.read(new ByteArrayInputStream(policyModelString.getBytes())); + + // Set up the models in the model service + apexPolicyModel.register(); + } + + /** + * Initialize default schema parameters. + */ + @BeforeClass + public static void initializeDefaultSchemaParameters() { + ParameterService.clear(); + final SchemaParameters schemaParameters = new SchemaParameters(); + schemaParameters.setName(ContextParameterConstants.SCHEMA_GROUP_NAME); + ParameterService.register(schemaParameters); + } + + /** + * Teardown default schema parameters. + */ + @AfterClass + public static void teardownDefaultSchemaParameters() { + ParameterService.deregister(ContextParameterConstants.SCHEMA_GROUP_NAME); + ModelService.clear(); + } + + /** + * Test JSON to apex event. + * + * @throws ApexException the apex exception + */ + @Test + public void testJsontoApexEvent() throws ApexException { + try { + final Apex2JsonEventConverter jsonEventConverter = new Apex2JsonEventConverter(); + assertNotNull(jsonEventConverter); + jsonEventConverter.init(new JsonEventProtocolParameters()); + + final String apexEventJsonStringIn = SupportJsonEventGenerator.jsonEvent(); + + logger.debug("input event\n" + apexEventJsonStringIn); + + final List apexEventList = jsonEventConverter.toApexEvent(null, apexEventJsonStringIn); + for (final ApexEvent apexEvent : apexEventList) { + assertNotNull(apexEvent); + + logger.debug(apexEvent.toString()); + + assertEquals("BasicEvent", apexEvent.getName()); + assertEquals("0.0.1", apexEvent.getVersion()); + assertEquals("org.onap.policy.apex.events", apexEvent.getNameSpace()); + assertEquals("test", apexEvent.getSource()); + assertEquals("apex", apexEvent.getTarget()); + assertEquals(12345, apexEvent.get("intPar")); + + final Object testMatchCaseSelected = apexEvent.get("TestMatchCaseSelected"); + assertNull(testMatchCaseSelected); + } + } catch (final Exception e) { + e.printStackTrace(); + throw new ApexException("Exception reading Apex event JSON file", e); + } + } + + /** + * Test JSON to apex bad event. + * + * @throws ApexException the apex exception + */ + @Test + public void testJsontoApexBadEvent() throws ApexException { + try { + final Apex2JsonEventConverter jsonEventConverter = new Apex2JsonEventConverter(); + assertNotNull(jsonEventConverter); + jsonEventConverter.init(new JsonEventProtocolParameters()); + + String apexEventJsonStringIn = null; + + try { + apexEventJsonStringIn = SupportJsonEventGenerator.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 = SupportJsonEventGenerator.jsonEventBadName(); + jsonEventConverter.toApexEvent(null, apexEventJsonStringIn); + fail("Test should throw an exception here"); + } catch (final ApexEventException e) { + assertEquals("Failed to unmarshal JSON event: field \"name\" with value \"%%%%\" is invalid", + e.getMessage().substring(0, 73)); + } + + try { + apexEventJsonStringIn = SupportJsonEventGenerator.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 = SupportJsonEventGenerator.jsonEventNoVersion(); + ApexEvent event = jsonEventConverter.toApexEvent(null, apexEventJsonStringIn).get(0); + assertEquals("0.0.1", event.getVersion()); + + try { + apexEventJsonStringIn = SupportJsonEventGenerator.jsonEventBadVersion(); + jsonEventConverter.toApexEvent(null, apexEventJsonStringIn); + fail("Test should throw an exception here"); + } catch (final ApexEventException e) { + assertEquals("Failed to unmarshal JSON event: field \"version\" with value \"#####\" is invalid", + e.getMessage().substring(0, 77)); + } + + try { + apexEventJsonStringIn = SupportJsonEventGenerator.jsonEventNoExVersion(); + 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 " + + "\"BasicEvent\" with version \"1.2.3\" not found in Apex model", + e.getMessage().substring(0, 128)); + } + + apexEventJsonStringIn = SupportJsonEventGenerator.jsonEventNoNamespace(); + event = jsonEventConverter.toApexEvent(null, apexEventJsonStringIn).get(0); + assertEquals("org.onap.policy.apex.events", event.getNameSpace()); + + try { + apexEventJsonStringIn = SupportJsonEventGenerator.jsonEventBadNamespace(); + jsonEventConverter.toApexEvent(null, apexEventJsonStringIn); + fail("Test should throw an exception here"); + } catch (final ApexEventException e) { + assertEquals("Failed to unmarshal JSON event: " + + "field \"nameSpace\" with value \"hello.&&&&\" is invalid", + e.getMessage().substring(0, 84)); + } + + try { + apexEventJsonStringIn = SupportJsonEventGenerator.jsonEventNoExNamespace(); + jsonEventConverter.toApexEvent(null, apexEventJsonStringIn); + fail("Test should throw an exception here"); + } catch (final ApexEventException e) { + assertEquals("Failed to unmarshal JSON event: namespace \"pie.in.the.sky\" " + + "on event \"BasicEvent\" does not" + + " match namespace \"org.onap.policy.apex.events\" " + + "for that event in the Apex model", e.getMessage().substring(0, 168)); + } + + apexEventJsonStringIn = SupportJsonEventGenerator.jsonEventNoSource(); + event = jsonEventConverter.toApexEvent(null, apexEventJsonStringIn).get(0); + assertEquals("source", event.getSource()); + + try { + apexEventJsonStringIn = SupportJsonEventGenerator.jsonEventBadSource(); + jsonEventConverter.toApexEvent(null, apexEventJsonStringIn); + fail("Test should throw an exception here"); + } catch (final ApexEventException e) { + assertEquals("Failed to unmarshal JSON event: field \"source\" with value \"%!@**@!\" is invalid", + e.getMessage().substring(0, 78)); + } + + apexEventJsonStringIn = SupportJsonEventGenerator.jsonEventNoTarget(); + event = jsonEventConverter.toApexEvent(null, apexEventJsonStringIn).get(0); + assertEquals("target", event.getTarget()); + + try { + apexEventJsonStringIn = SupportJsonEventGenerator.jsonEventBadTarget(); + jsonEventConverter.toApexEvent(null, apexEventJsonStringIn); + fail("Test should throw an exception here"); + } catch (final ApexEventException e) { + assertEquals("Failed to unmarshal JSON event: field \"target\" with value \"KNIO(*S)A(S)D\" is invalid", + e.getMessage().substring(0, 84)); + } + + try { + apexEventJsonStringIn = SupportJsonEventGenerator.jsonEventMissingFields(); + jsonEventConverter.toApexEvent(null, apexEventJsonStringIn); + fail("Test should throw an exception here"); + } catch (final ApexEventException e) { + assertEquals("Failed to unmarshal JSON event: error parsing BasicEvent:0.0.1 " + + "event from Json. Field \"intPar\" is missing, but is mandatory.", + e.getMessage().substring(0, 124)); + } + + apexEventJsonStringIn = SupportJsonEventGenerator.jsonEventNullFields(); + event = jsonEventConverter.toApexEvent(null, apexEventJsonStringIn).get(0); + assertEquals(null, event.get("TestSlogan")); + assertEquals(-1, event.get("intPar")); + + // Set the missing fields as optional in the model + final AxEvent eventDefinition = ModelService.getModel(AxEvents.class).get("BasicEvent"); + eventDefinition.getParameterMap().get("intPar").setOptional(true); + + apexEventJsonStringIn = SupportJsonEventGenerator.jsonEventMissingFields(); + event = jsonEventConverter.toApexEvent(null, apexEventJsonStringIn).get(0); + assertEquals(null, event.get("TestSlogan")); + assertEquals(null, event.get("intPar")); + } catch (final Exception e) { + e.printStackTrace(); + throw new ApexException("Exception reading Apex event JSON file", e); + } + } + + /** + * Test apex event to JSON. + * + * @throws ApexException the apex exception + */ + @Test + public void testApexEventToJson() throws ApexException { + try { + final Apex2JsonEventConverter jsonEventConverter = new Apex2JsonEventConverter(); + jsonEventConverter.init(new JsonEventProtocolParameters()); + assertNotNull(jsonEventConverter); + + final Map basicEventMap = new HashMap(); + basicEventMap.put("intPar", 12345); + + final ApexEvent basicEvent = new ApexEvent("BasicEvent", "0.0.1", "org.onap.policy.apex.events", "test", + "apex"); + basicEvent.putAll(basicEventMap); + + final String apexEvent0000JsonString = (String) jsonEventConverter.fromApexEvent(basicEvent); + + logger.debug(apexEvent0000JsonString); + + assertTrue(apexEvent0000JsonString.contains("\"name\": \"BasicEvent\"")); + assertTrue(apexEvent0000JsonString.contains("\"version\": \"0.0.1\"")); + assertTrue(apexEvent0000JsonString.contains("\"nameSpace\": \"org.onap.policy.apex.events\"")); + assertTrue(apexEvent0000JsonString.contains("\"source\": \"test\"")); + assertTrue(apexEvent0000JsonString.contains("\"target\": \"apex\"")); + assertTrue(apexEvent0000JsonString.contains("\"intPar\": 12345")); + } 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/JsonEventProtocolPrametersTest.java b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/JsonEventProtocolPrametersTest.java new file mode 100644 index 000000000..bf19d3f0b --- /dev/null +++ b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/JsonEventProtocolPrametersTest.java @@ -0,0 +1,66 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 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 org.junit.Test; +import org.onap.policy.apex.service.engine.event.impl.jsonprotocolplugin.JsonEventProtocolParameters; + +/** + * Test the JSON event parameters. + * + */ +public class JsonEventProtocolPrametersTest { + + @Test + public void testJsonParameters() { + assertNotNull(new JsonEventProtocolParameters()); + + JsonEventProtocolParameters params = new JsonEventProtocolParameters(); + + params.setLabel("MyLabel"); + assertEquals("MyLabel", params.getLabel()); + assertEquals("MyLabel", params.getName()); + + params.setEventProtocolPluginClass("MyPluginClass"); + assertEquals("MyPluginClass", params.getEventProtocolPluginClass()); + + params.setNameAlias("MyNameAlias"); + assertEquals("MyNameAlias", params.getNameAlias()); + + params.setVersionAlias("MyVersionAlias"); + assertEquals("MyVersionAlias", params.getVersionAlias()); + + params.setNameSpaceAlias("MyNameSpaceAlias"); + assertEquals("MyNameSpaceAlias", params.getNameSpaceAlias()); + + params.setSourceAlias("MySourceAlias"); + assertEquals("MySourceAlias", params.getSourceAlias()); + + params.setTargetAlias("MyTargetAlias"); + assertEquals("MyTargetAlias", params.getTargetAlias()); + + params.setPojoField("MyPojoField"); + assertEquals("MyPojoField", params.getPojoField()); + } +} diff --git a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/JsonTaggedEventConsumerTest.java b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/JsonTaggedEventConsumerTest.java new file mode 100644 index 000000000..f313f1fd4 --- /dev/null +++ b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/JsonTaggedEventConsumerTest.java @@ -0,0 +1,131 @@ +/*- + * ============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; + +/** + * Test JSON Tagged Event Consumer. + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public class JsonTaggedEventConsumerTest { + + @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("{TestTimestamp\": 1469781869268}", textBlock.getText()); + + 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("{TestTimestamp\": 1469781869268}", textBlock.getText()); + 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("{TestTimestamp\": 1469781869268}", textBlock.getText()); + 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("{TestTimestamp\": 1469781869268}", textBlock.getText()); + 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/PluginFactoriesTest.java b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/PluginFactoriesTest.java new file mode 100644 index 000000000..d67087a7d --- /dev/null +++ b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/PluginFactoriesTest.java @@ -0,0 +1,63 @@ +/*- + * ============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.ApexParameterHandler; +import org.onap.policy.apex.service.parameters.ApexParameters; +import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters; +import org.onap.policy.common.parameters.ParameterException; + +/** + * Test Plugin Factories. + * @author Liam Fallon (liam.fallon@ericsson.com) + * @author John Keeney (john.keeney@ericsson.com) + */ +public class PluginFactoriesTest { + + @Test + public void testEventConsumerFactory() throws ApexEventException, ParameterException { + 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 ce : parameters.getEventInputParameters().entrySet()) { + final ApexEventConsumer consumer = new EventConsumerFactory().createConsumer( + parameters.getEngineServiceParameters().getName() + "_consumer_" + ce.getKey(), ce.getValue()); + assertNotNull(consumer); + } + + for (final Entry 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/event/SupportJsonEventGenerator.java b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/SupportJsonEventGenerator.java new file mode 100644 index 000000000..4b022fb55 --- /dev/null +++ b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/SupportJsonEventGenerator.java @@ -0,0 +1,346 @@ +/*- + * ============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; + +/** + * This class generates JSON event used for the test cases. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public class SupportJsonEventGenerator { + /** + * Json event. + * + * @return the string + */ + public static String jsonEvent() { + final StringBuilder builder = new StringBuilder(); + + builder.append("{\n"); + builder.append(" \"nameSpace\": \"org.onap.policy.apex.events\",\n"); + builder.append(" \"name\": \"BasicEvent\",\n"); + builder.append(" \"version\": \"0.0.1\",\n"); + builder.append(" \"source\": \"test\",\n"); + builder.append(" \"target\": \"apex\",\n"); + builder.append(" \"intPar\": 12345\n"); + builder.append("}"); + + return builder.toString(); + } + + /** + * Json event no name. + * + * @return the string + */ + public static String jsonEventNoName() { + final StringBuilder builder = new StringBuilder(); + + builder.append("{\n"); + builder.append(" \"nameSpace\": \"org.onap.policy.apex.events\",\n"); + builder.append(" \"namez\": \"BasicEvent\",\n"); + builder.append(" \"version\": \"0.0.1\",\n"); + builder.append(" \"source\": \"test\",\n"); + builder.append(" \"target\": \"apex\",\n"); + builder.append(" \"intPar\": 12346\n"); + builder.append("}"); + + return builder.toString(); + } + + /** + * Json event bad name. + * + * @return the string + */ + public static String jsonEventBadName() { + final StringBuilder builder = new StringBuilder(); + + builder.append("{\n"); + builder.append(" \"nameSpace\": \"org.onap.policy.apex.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(" \"intPar\": 12347\n"); + builder.append("}"); + + return builder.toString(); + } + + /** + * Json event no ex name. + * + * @return the string + */ + public static String jsonEventNoExName() { + final StringBuilder builder = new StringBuilder(); + + builder.append("{\n"); + builder.append(" \"nameSpace\": \"org.onap.policy.apex.events\",\n"); + builder.append(" \"name\": \"I_DONT_EXIST\",\n"); + builder.append(" \"source\": \"test\",\n"); + builder.append(" \"target\": \"apex\",\n"); + builder.append(" \"intPar\": 12348\n"); + builder.append("}"); + + return builder.toString(); + } + + /** + * Json event no version. + * + * @return the string + */ + public static String jsonEventNoVersion() { + final StringBuilder builder = new StringBuilder(); + + builder.append("{\n"); + builder.append(" \"nameSpace\": \"org.onap.policy.apex.events\",\n"); + builder.append(" \"name\": \"BasicEvent\",\n"); + builder.append(" \"versiion\": \"0.0.1\",\n"); + builder.append(" \"source\": \"test\",\n"); + builder.append(" \"target\": \"apex\",\n"); + builder.append(" \"intPar\": 12349\n"); + builder.append("}"); + + return builder.toString(); + } + + /** + * Json event bad version. + * + * @return the string + */ + public static String jsonEventBadVersion() { + final StringBuilder builder = new StringBuilder(); + + builder.append("{\n"); + builder.append(" \"nameSpace\": \"org.onap.policy.apex.events\",\n"); + builder.append(" \"name\": \"BasicEvent\",\n"); + builder.append(" \"version\": \"#####\",\n"); + builder.append(" \"source\": \"test\",\n"); + builder.append(" \"target\": \"apex\",\n"); + builder.append(" \"intPar\": 12350\n"); + builder.append("}"); + + return builder.toString(); + } + + /** + * Json event no ex version. + * + * @return the string + */ + public static String jsonEventNoExVersion() { + final StringBuilder builder = new StringBuilder(); + + builder.append("{\n"); + builder.append(" \"nameSpace\": \"org.onap.policy.apex.events\",\n"); + builder.append(" \"name\": \"BasicEvent\",\n"); + builder.append(" \"version\": \"1.2.3\",\n"); + builder.append(" \"source\": \"test\",\n"); + builder.append(" \"target\": \"apex\",\n"); + builder.append(" \"intPar\": 12351\n"); + builder.append("}"); + + return builder.toString(); + } + + /** + * Json event no namespace. + * + * @return the string + */ + public static String jsonEventNoNamespace() { + final StringBuilder builder = new StringBuilder(); + + builder.append("{\n"); + builder.append(" \"nameSpacee\": \"org.onap.policy.apex.events\",\n"); + builder.append(" \"name\": \"BasicEvent\",\n"); + builder.append(" \"version\": \"0.0.1\",\n"); + builder.append(" \"source\": \"test\",\n"); + builder.append(" \"target\": \"apex\",\n"); + builder.append(" \"intPar\": 12352\n"); + builder.append("}"); + + return builder.toString(); + } + + /** + * Json event bad namespace. + * + * @return the string + */ + public static String jsonEventBadNamespace() { + final StringBuilder builder = new StringBuilder(); + + builder.append("{\n"); + builder.append(" \"nameSpace\": \"hello.&&&&\",\n"); + builder.append(" \"name\": \"BasicEvent\",\n"); + builder.append(" \"version\": \"0.0.1\",\n"); + builder.append(" \"source\": \"test\",\n"); + builder.append(" \"target\": \"apex\",\n"); + builder.append(" \"intPar\": 12353\n"); + builder.append("}"); + + return builder.toString(); + } + + /** + * Json event no ex namespace. + * + * @return the string + */ + public static String jsonEventNoExNamespace() { + final StringBuilder builder = new StringBuilder(); + + builder.append("{\n"); + builder.append(" \"nameSpace\": \"pie.in.the.sky\",\n"); + builder.append(" \"name\": \"BasicEvent\",\n"); + builder.append(" \"version\": \"0.0.1\",\n"); + builder.append(" \"source\": \"test\",\n"); + builder.append(" \"target\": \"apex\",\n"); + builder.append(" \"intPar\": 12354\n"); + builder.append("}"); + + return builder.toString(); + } + + /** + * Json event no source. + * + * @return the string + */ + public static String jsonEventNoSource() { + final StringBuilder builder = new StringBuilder(); + + builder.append("{\n"); + builder.append(" \"nameSpace\": \"org.onap.policy.apex.events\",\n"); + builder.append(" \"name\": \"BasicEvent\",\n"); + builder.append(" \"version\": \"0.0.1\",\n"); + builder.append(" \"sourcee\": \"test\",\n"); + builder.append(" \"target\": \"apex\",\n"); + builder.append(" \"intPar\": 12355\n"); + builder.append("}"); + + return builder.toString(); + } + + /** + * Json event bad source. + * + * @return the string + */ + public static String jsonEventBadSource() { + final StringBuilder builder = new StringBuilder(); + + builder.append("{\n"); + builder.append(" \"nameSpace\": \"org.onap.policy.apex.events\",\n"); + builder.append(" \"name\": \"BasicEvent\",\n"); + builder.append(" \"version\": \"0.0.1\",\n"); + builder.append(" \"source\": \"%!@**@!\",\n"); + builder.append(" \"target\": \"apex\",\n"); + builder.append(" \"intPar\": 12356\n"); + builder.append("}"); + + return builder.toString(); + } + + /** + * Json event no target. + * + * @return the string + */ + public static String jsonEventNoTarget() { + final StringBuilder builder = new StringBuilder(); + + builder.append("{\n"); + builder.append(" \"nameSpace\": \"org.onap.policy.apex.events\",\n"); + builder.append(" \"name\": \"BasicEvent\",\n"); + builder.append(" \"version\": \"0.0.1\",\n"); + builder.append(" \"source\": \"test\",\n"); + builder.append(" \"targett\": \"apex\",\n"); + builder.append(" \"intPar\": 12357\n"); + builder.append("}"); + + return builder.toString(); + } + + /** + * Json event bad target. + * + * @return the string + */ + public static String jsonEventBadTarget() { + final StringBuilder builder = new StringBuilder(); + + builder.append("{\n"); + builder.append(" \"nameSpace\": \"org.onap.policy.apex.events\",\n"); + builder.append(" \"name\": \"BasicEvent\",\n"); + builder.append(" \"version\": \"0.0.1\",\n"); + builder.append(" \"source\": \"test\",\n"); + builder.append(" \"target\": \"KNIO(*S)A(S)D\",\n"); + builder.append(" \"intPar\": 12358\n"); + builder.append("}"); + + return builder.toString(); + } + + /** + * Json event missing fields. + * + * @return the string + */ + public static String jsonEventMissingFields() { + final StringBuilder builder = new StringBuilder(); + + builder.append("{\n"); + builder.append(" \"nameSpace\": \"org.onap.policy.apex.events\",\n"); + builder.append(" \"name\": \"BasicEvent\",\n"); + builder.append(" \"version\": \"0.0.1\",\n"); + builder.append(" \"source\": \"test\",\n"); + builder.append(" \"target\": \"apex\"\n"); + builder.append("}"); + + return builder.toString(); + } + + /** + * Json event null fields. + * + * @return the string + */ + public static String jsonEventNullFields() { + final StringBuilder builder = new StringBuilder(); + + builder.append("{\n"); + builder.append(" \"nameSpace\": \"org.onap.policy.apex.events\",\n"); + builder.append(" \"name\": \"BasicEvent\",\n"); + builder.append(" \"version\": \"0.0.1\",\n"); + builder.append(" \"source\": \"test\",\n"); + builder.append(" \"target\": \"Apex\",\n"); + builder.append(" \"intPar\": -1\n"); + builder.append("}"); + + return builder.toString(); + } +} diff --git a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/TestJsonEventConverter.java b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/TestJsonEventConverter.java deleted file mode 100644 index 3ccdd5926..000000000 --- a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/TestJsonEventConverter.java +++ /dev/null @@ -1,105 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 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.fail; - -import org.junit.Test; -import org.onap.policy.apex.service.engine.event.impl.jsonprotocolplugin.Apex2JsonEventConverter; -import org.onap.policy.apex.service.engine.event.impl.jsonprotocolplugin.JsonEventProtocolParameters; -import org.onap.policy.apex.service.parameters.eventprotocol.EventProtocolParameters; - -/** - * Test the JSON event converter corner cases. - * - */ -public class TestJsonEventConverter { - @Test - public void testJsonEventConverter() { - Apex2JsonEventConverter converter = new Apex2JsonEventConverter(); - - try { - converter.init(null); - fail("test should throw an exception"); - } catch (Exception ie) { - assertEquals("specified consumer properties are not applicable to the JSON event protocol", - ie.getMessage()); - } - - try { - converter.init(new EventProtocolParameters() { - }); - fail("test should throw an exception"); - } catch (Exception ie) { - assertEquals("specified consumer properties are not applicable to the JSON event protocol", - ie.getMessage()); - } - - JsonEventProtocolParameters pars = new JsonEventProtocolParameters(); - converter.init(pars); - - try { - converter.toApexEvent(null, null); - fail("test should throw an exception"); - } catch (Exception tae) { - assertEquals("event processing failed, event is null", tae.getMessage()); - } - - try { - converter.toApexEvent(null, 1); - fail("test should throw an exception"); - } catch (Exception tae) { - assertEquals("error converting event \"1\" to a string", tae.getMessage()); - } - - try { - converter.toApexEvent(null, "[{\"aKey\": 1},{\"aKey\": 2}]"); - fail("test should throw an exception"); - } catch (Exception tae) { - assertEquals("Failed to unmarshal JSON event: event received without mandatory parameter \"name\" " - + "on configuration or on event, event=[{\"aKey\": 1},{\"aKey\": 2}]", tae.getMessage()); - } - - try { - converter.toApexEvent(null, "[1,2,3]"); - fail("test should throw an exception"); - } catch (Exception tae) { - assertEquals("Failed to unmarshal JSON event: incoming event ([1,2,3]) is a JSON object array " - + "containing an invalid object 1.0, event=[1,2,3]", tae.getMessage()); - } - - try { - converter.fromApexEvent(null); - fail("test should throw an exception"); - } catch (Exception tae) { - assertEquals("event processing failed, Apex event is null", tae.getMessage()); - } - - try { - converter.fromApexEvent(new ApexEvent("Event", "0.0.1", "a.name.space", "here", "there")); - fail("test should throw an exception"); - } catch (Exception tae) { - assertEquals("Model for org.onap.policy.apex.model.eventmodel.concepts.AxEvents not found in model service", - tae.getMessage()); - } - } -} 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 deleted file mode 100644 index fa399e8c0..000000000 --- a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/TestJsonEventHandler.java +++ /dev/null @@ -1,315 +0,0 @@ -/*- - * ============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.assertNull; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; - -import java.io.ByteArrayInputStream; -import java.io.IOException; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import org.junit.AfterClass; -import org.junit.BeforeClass; -import org.junit.Test; -import org.onap.policy.apex.context.parameters.ContextParameterConstants; -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.onap.policy.common.parameters.ParameterService; -import org.slf4j.ext.XLogger; -import org.slf4j.ext.XLoggerFactory; - -/** - * Test JSON Event Handler. - * - * @author Liam Fallon (liam.fallon@ericsson.com) - */ -public class TestJsonEventHandler { - private static final XLogger logger = XLoggerFactory.getXLogger(TestJsonEventHandler.class); - - /** - * Setup event model. - * - * @throws IOException Signals that an I/O exception has occurred. - * @throws ApexModelException the apex model exception - */ - @BeforeClass - public static void setupEventModel() throws IOException, ApexModelException { - final String policyModelString = TextFileUtils - .getTextFileAsString("src/test/resources/policymodels/SmallModel.json"); - final ApexModelReader modelReader = new ApexModelReader(AxPolicyModel.class); - final AxPolicyModel apexPolicyModel = modelReader.read(new ByteArrayInputStream(policyModelString.getBytes())); - - // Set up the models in the model service - apexPolicyModel.register(); - } - - /** - * Initialize default schema parameters. - */ - @BeforeClass - public static void initializeDefaultSchemaParameters() { - ParameterService.clear(); - final SchemaParameters schemaParameters = new SchemaParameters(); - schemaParameters.setName(ContextParameterConstants.SCHEMA_GROUP_NAME); - ParameterService.register(schemaParameters); - } - - /** - * Teardown default schema parameters. - */ - @AfterClass - public static void teardownDefaultSchemaParameters() { - ParameterService.deregister(ContextParameterConstants.SCHEMA_GROUP_NAME); - ModelService.clear(); - } - - /** - * Test JSON to apex event. - * - * @throws ApexException the apex exception - */ - @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 apexEventList = jsonEventConverter.toApexEvent(null, apexEventJsonStringIn); - for (final ApexEvent apexEvent : apexEventList) { - assertNotNull(apexEvent); - - logger.debug(apexEvent.toString()); - - assertEquals("BasicEvent", apexEvent.getName()); - assertEquals("0.0.1", apexEvent.getVersion()); - assertEquals("org.onap.policy.apex.events", apexEvent.getNameSpace()); - assertEquals("test", apexEvent.getSource()); - assertEquals("apex", apexEvent.getTarget()); - assertEquals(12345, apexEvent.get("intPar")); - - final Object testMatchCaseSelected = apexEvent.get("TestMatchCaseSelected"); - assertNull(testMatchCaseSelected); - } - } catch (final Exception e) { - e.printStackTrace(); - throw new ApexException("Exception reading Apex event JSON file", e); - } - } - - /** - * Test JSON to apex bad event. - * - * @throws ApexException the apex exception - */ - @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) { - assertEquals("Failed to unmarshal JSON event: field \"name\" with value \"%%%%\" is invalid", - e.getMessage().substring(0, 73)); - } - - 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) { - assertEquals("Failed to unmarshal JSON event: field \"version\" with value \"#####\" is invalid", - e.getMessage().substring(0, 77)); - } - - try { - apexEventJsonStringIn = JsonEventGenerator.jsonEventNoExVersion(); - 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 " - + "\"BasicEvent\" with version \"1.2.3\" not found in Apex model", - e.getMessage().substring(0, 128)); - } - - apexEventJsonStringIn = JsonEventGenerator.jsonEventNoNamespace(); - event = jsonEventConverter.toApexEvent(null, apexEventJsonStringIn).get(0); - assertEquals("org.onap.policy.apex.events", event.getNameSpace()); - - try { - apexEventJsonStringIn = JsonEventGenerator.jsonEventBadNamespace(); - jsonEventConverter.toApexEvent(null, apexEventJsonStringIn); - fail("Test should throw an exception here"); - } catch (final ApexEventException e) { - assertEquals("Failed to unmarshal JSON event: " - + "field \"nameSpace\" with value \"hello.&&&&\" is invalid", - e.getMessage().substring(0, 84)); - } - - try { - apexEventJsonStringIn = JsonEventGenerator.jsonEventNoExNamespace(); - jsonEventConverter.toApexEvent(null, apexEventJsonStringIn); - fail("Test should throw an exception here"); - } catch (final ApexEventException e) { - assertEquals("Failed to unmarshal JSON event: namespace \"pie.in.the.sky\" " - + "on event \"BasicEvent\" does not" - + " match namespace \"org.onap.policy.apex.events\" " - + "for that event in the Apex model", e.getMessage().substring(0, 168)); - } - - apexEventJsonStringIn = JsonEventGenerator.jsonEventNoSource(); - event = jsonEventConverter.toApexEvent(null, apexEventJsonStringIn).get(0); - assertEquals("source", event.getSource()); - - try { - apexEventJsonStringIn = JsonEventGenerator.jsonEventBadSource(); - jsonEventConverter.toApexEvent(null, apexEventJsonStringIn); - fail("Test should throw an exception here"); - } catch (final ApexEventException e) { - assertEquals("Failed to unmarshal JSON event: field \"source\" with value \"%!@**@!\" is invalid", - e.getMessage().substring(0, 78)); - } - - apexEventJsonStringIn = JsonEventGenerator.jsonEventNoTarget(); - event = jsonEventConverter.toApexEvent(null, apexEventJsonStringIn).get(0); - assertEquals("target", event.getTarget()); - - try { - apexEventJsonStringIn = JsonEventGenerator.jsonEventBadTarget(); - jsonEventConverter.toApexEvent(null, apexEventJsonStringIn); - fail("Test should throw an exception here"); - } catch (final ApexEventException e) { - assertEquals("Failed to unmarshal JSON event: field \"target\" with value \"KNIO(*S)A(S)D\" is invalid", - e.getMessage().substring(0, 84)); - } - - try { - apexEventJsonStringIn = JsonEventGenerator.jsonEventMissingFields(); - jsonEventConverter.toApexEvent(null, apexEventJsonStringIn); - fail("Test should throw an exception here"); - } catch (final ApexEventException e) { - assertEquals("Failed to unmarshal JSON event: error parsing BasicEvent:0.0.1 " - + "event from Json. Field \"intPar\" is missing, but is mandatory.", - e.getMessage().substring(0, 124)); - } - - apexEventJsonStringIn = JsonEventGenerator.jsonEventNullFields(); - event = jsonEventConverter.toApexEvent(null, apexEventJsonStringIn).get(0); - assertEquals(null, event.get("TestSlogan")); - assertEquals(-1, event.get("intPar")); - - // Set the missing fields as optional in the model - final AxEvent eventDefinition = ModelService.getModel(AxEvents.class).get("BasicEvent"); - eventDefinition.getParameterMap().get("intPar").setOptional(true); - - apexEventJsonStringIn = JsonEventGenerator.jsonEventMissingFields(); - event = jsonEventConverter.toApexEvent(null, apexEventJsonStringIn).get(0); - assertEquals(null, event.get("TestSlogan")); - assertEquals(null, event.get("intPar")); - } catch (final Exception e) { - e.printStackTrace(); - throw new ApexException("Exception reading Apex event JSON file", e); - } - } - - /** - * Test apex event to JSON. - * - * @throws ApexException the apex exception - */ - @Test - public void testApexEventToJson() throws ApexException { - try { - final Apex2JsonEventConverter jsonEventConverter = new Apex2JsonEventConverter(); - jsonEventConverter.init(new JsonEventProtocolParameters()); - assertNotNull(jsonEventConverter); - - final Map basicEventMap = new HashMap(); - basicEventMap.put("intPar", 12345); - - final ApexEvent basicEvent = new ApexEvent("BasicEvent", "0.0.1", "org.onap.policy.apex.events", "test", - "apex"); - basicEvent.putAll(basicEventMap); - - final String apexEvent0000JsonString = (String) jsonEventConverter.fromApexEvent(basicEvent); - - logger.debug(apexEvent0000JsonString); - - assertTrue(apexEvent0000JsonString.contains("\"name\": \"BasicEvent\"")); - assertTrue(apexEvent0000JsonString.contains("\"version\": \"0.0.1\"")); - assertTrue(apexEvent0000JsonString.contains("\"nameSpace\": \"org.onap.policy.apex.events\"")); - assertTrue(apexEvent0000JsonString.contains("\"source\": \"test\"")); - assertTrue(apexEvent0000JsonString.contains("\"target\": \"apex\"")); - assertTrue(apexEvent0000JsonString.contains("\"intPar\": 12345")); - } 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/TestJsonEventHandlerForPojo.java b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/TestJsonEventHandlerForPojo.java deleted file mode 100644 index 38f6da1b0..000000000 --- a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/TestJsonEventHandlerForPojo.java +++ /dev/null @@ -1,247 +0,0 @@ -/*- - * ============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.fail; - -import java.io.ByteArrayInputStream; -import java.io.IOException; -import java.util.List; - -import org.junit.AfterClass; -import org.junit.BeforeClass; -import org.junit.Test; -import org.onap.policy.apex.context.parameters.ContextParameterConstants; -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.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.onap.policy.apex.service.engine.event.testpojos.TestPojo; -import org.onap.policy.apex.service.engine.event.testpojos.TestPojoList; -import org.onap.policy.common.parameters.ParameterService; -import org.slf4j.ext.XLogger; -import org.slf4j.ext.XLoggerFactory; - -/** - * Test JSON Event Handler. - * - * @author Liam Fallon (liam.fallon@ericsson.com) - */ -public class TestJsonEventHandlerForPojo { - private static final XLogger logger = XLoggerFactory.getXLogger(TestJsonEventHandlerForPojo.class); - - /** - * Setup event model. - * - * @throws IOException Signals that an I/O exception has occurred. - * @throws ApexModelException the apex model exception - */ - @BeforeClass - public static void setupEventModel() throws IOException, ApexModelException { - final String policyModelString = TextFileUtils - .getTextFileAsString("src/test/resources/policymodels/PojoEventModel.json"); - final ApexModelReader modelReader = new ApexModelReader(AxPolicyModel.class); - modelReader.setValidateFlag(false); - final AxPolicyModel apexPolicyModel = modelReader.read(new ByteArrayInputStream(policyModelString.getBytes())); - - // Set up the models in the model service - apexPolicyModel.register(); - } - - /** - * Initialize default schema parameters. - */ - @BeforeClass - public static void initializeDefaultSchemaParameters() { - ParameterService.clear(); - final SchemaParameters schemaParameters = new SchemaParameters(); - schemaParameters.setName(ContextParameterConstants.SCHEMA_GROUP_NAME); - ParameterService.register(schemaParameters); - } - - /** - * Teardown default schema parameters. - */ - @AfterClass - public static void teardownDefaultSchemaParameters() { - ParameterService.deregister(ContextParameterConstants.SCHEMA_GROUP_NAME); - ModelService.clear(); - } - - /** - * Test POJO to apex event and back. - * - * @throws ApexException the apex exception - * @throws IOException on IO exceptions - */ - @Test - public void testJsonPojoToApexEvent() throws ApexException, IOException { - final Apex2JsonEventConverter jsonEventConverter = new Apex2JsonEventConverter(); - assertNotNull(jsonEventConverter); - - JsonEventProtocolParameters pars = new JsonEventProtocolParameters(); - pars.setPojoField("POJO_PAR"); - jsonEventConverter.init(pars); - - final String apexEventJsonStringIn = TextFileUtils - .getTextFileAsString("src/test/resources/events/TestPojoEvent.json"); - - logger.debug("input event\n" + apexEventJsonStringIn); - - final List apexEventList = jsonEventConverter.toApexEvent("PojoEvent", apexEventJsonStringIn); - assertEquals(1, apexEventList.size()); - final ApexEvent apexEvent = apexEventList.get(0); - assertNotNull(apexEvent); - - logger.debug(apexEvent.toString()); - - assertEquals("PojoEvent", apexEvent.getName()); - assertEquals("0.0.1", apexEvent.getVersion()); - assertEquals("org.onap.policy.apex.service.engine.event.testpojos", apexEvent.getNameSpace()); - assertEquals("Outside", apexEvent.getSource()); - assertEquals("Apex", apexEvent.getTarget()); - - TestPojo testPojo = (TestPojo) apexEvent.get("POJO_PAR"); - - assertEquals(1, testPojo.getAnInt()); - assertEquals(2, testPojo.getAnInteger().intValue()); - assertEquals("a string", testPojo.getSomeString()); - - assertEquals(10, testPojo.getTestSubPojo().getAnInt()); - assertEquals(20, testPojo.getTestSubPojo().getAnInteger().intValue()); - assertEquals("a sub string", testPojo.getTestSubPojo().getSomeString()); - - assertEquals(100, testPojo.getTestSubPojo().getTestSubSubPojo().getAnInt()); - assertEquals(200, testPojo.getTestSubPojo().getTestSubSubPojo().getAnInteger().intValue()); - assertEquals("a sub sub string", testPojo.getTestSubPojo().getTestSubSubPojo().getSomeString()); - - String eventBackInJson = (String) jsonEventConverter.fromApexEvent(apexEvent); - assertEquals(apexEventJsonStringIn.replaceAll("\\s+", ""), eventBackInJson.replaceAll("\\s+", "")); - } - - /** - * Test POJO List to apex event and back. - * - * @throws ApexException the apex exception - * @throws IOException on IO exceptions - */ - @Test - public void testJsonPojoListToApexEvent() throws ApexException, IOException { - final Apex2JsonEventConverter jsonEventConverter = new Apex2JsonEventConverter(); - assertNotNull(jsonEventConverter); - - JsonEventProtocolParameters pars = new JsonEventProtocolParameters(); - pars.setPojoField("POJO_LIST_PAR"); - jsonEventConverter.init(pars); - - final String apexEventJsonStringIn = TextFileUtils - .getTextFileAsString("src/test/resources/events/TestPojoListEvent.json"); - - logger.debug("input event\n" + apexEventJsonStringIn); - - final List apexEventList = jsonEventConverter.toApexEvent("PojoListEvent", apexEventJsonStringIn); - assertEquals(1, apexEventList.size()); - final ApexEvent apexEvent = apexEventList.get(0); - assertNotNull(apexEvent); - - logger.debug(apexEvent.toString()); - - assertEquals("PojoListEvent", apexEvent.getName()); - assertEquals("0.0.1", apexEvent.getVersion()); - assertEquals("org.onap.policy.apex.service.engine.event.testpojos", apexEvent.getNameSpace()); - assertEquals("Outside", apexEvent.getSource()); - assertEquals("Apex", apexEvent.getTarget()); - - TestPojoList testPojoList = (TestPojoList) apexEvent.get("POJO_LIST_PAR"); - - for (TestPojo testPojo : testPojoList.getTestPojoList()) { - assertEquals(1, testPojo.getAnInt()); - assertEquals(2, testPojo.getAnInteger().intValue()); - assertEquals("a string", testPojo.getSomeString()); - - assertEquals(10, testPojo.getTestSubPojo().getAnInt()); - assertEquals(20, testPojo.getTestSubPojo().getAnInteger().intValue()); - assertEquals("a sub string", testPojo.getTestSubPojo().getSomeString()); - - assertEquals(100, testPojo.getTestSubPojo().getTestSubSubPojo().getAnInt()); - assertEquals(200, testPojo.getTestSubPojo().getTestSubSubPojo().getAnInteger().intValue()); - assertEquals("a sub sub string", testPojo.getTestSubPojo().getTestSubSubPojo().getSomeString()); - } - String eventBackInJson = (String) jsonEventConverter.fromApexEvent(apexEvent); - assertEquals(apexEventJsonStringIn.replaceAll("\\s+", ""), eventBackInJson.replaceAll("\\s+", "")); - } - - /** - * Test POJO event with bad configurations. - * - * @throws ApexException the apex exception - * @throws IOException on IO exceptions - */ - @Test - public void testJsonBadPojoApexEvent() throws ApexException, IOException { - final Apex2JsonEventConverter jsonEventConverter = new Apex2JsonEventConverter(); - assertNotNull(jsonEventConverter); - - JsonEventProtocolParameters pars = new JsonEventProtocolParameters(); - pars.setPojoField("BAD_POJO_PAR"); - jsonEventConverter.init(pars); - - final String apexEventJsonStringIn = TextFileUtils - .getTextFileAsString("src/test/resources/events/TestPojoEvent.json"); - - logger.debug("input event\n" + apexEventJsonStringIn); - - try { - jsonEventConverter.toApexEvent("PojoEvent", apexEventJsonStringIn); - fail("test should throw an exception"); - } catch (Exception tae) { - assertEquals("Failed to unmarshal JSON event: error parsing PojoEvent:0.0.1 event from Json. " - + "Field BAD_POJO_PAR not found on POJO event definition.", - tae.getMessage().substring(0, 133)); - } - - pars.setPojoField("POJO_PAR"); - try { - jsonEventConverter.toApexEvent("PojoNoFieldEvent", apexEventJsonStringIn); - fail("test should throw an exception"); - } catch (Exception tae) { - assertEquals("Failed to unmarshal JSON event: error parsing PojoNoFieldEvent:0.0.1 event from Json, " - + "Field POJO_PAR not found, no fields defined on event.", - tae.getMessage().substring(0, 139)); - } - - try { - jsonEventConverter.toApexEvent("PojoTooManyFieldsEvent", apexEventJsonStringIn); - fail("test should throw an exception"); - } catch (Exception tae) { - assertEquals("Failed to unmarshal JSON event: error parsing PojoTooManyFieldsEvent:0.0.1 event from Json, " - + "Field POJO_PAR, one and only one field may be defined on a POJO event definition.", - tae.getMessage().substring(0, 173)); - } - } -} diff --git a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/TestJsonEventProtocolPrameters.java b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/TestJsonEventProtocolPrameters.java deleted file mode 100644 index 139ea6b45..000000000 --- a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/TestJsonEventProtocolPrameters.java +++ /dev/null @@ -1,66 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 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 org.junit.Test; -import org.onap.policy.apex.service.engine.event.impl.jsonprotocolplugin.JsonEventProtocolParameters; - -/** - * Test the JSON event parameters. - * - */ -public class TestJsonEventProtocolPrameters { - - @Test - public void testJsonParameters() { - assertNotNull(new JsonEventProtocolParameters()); - - JsonEventProtocolParameters params = new JsonEventProtocolParameters(); - - params.setLabel("MyLabel"); - assertEquals("MyLabel", params.getLabel()); - assertEquals("MyLabel", params.getName()); - - params.setEventProtocolPluginClass("MyPluginClass"); - assertEquals("MyPluginClass", params.getEventProtocolPluginClass()); - - params.setNameAlias("MyNameAlias"); - assertEquals("MyNameAlias", params.getNameAlias()); - - params.setVersionAlias("MyVersionAlias"); - assertEquals("MyVersionAlias", params.getVersionAlias()); - - params.setNameSpaceAlias("MyNameSpaceAlias"); - assertEquals("MyNameSpaceAlias", params.getNameSpaceAlias()); - - params.setSourceAlias("MySourceAlias"); - assertEquals("MySourceAlias", params.getSourceAlias()); - - params.setTargetAlias("MyTargetAlias"); - assertEquals("MyTargetAlias", params.getTargetAlias()); - - params.setPojoField("MyPojoField"); - assertEquals("MyPojoField", params.getPojoField()); - } -} 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 deleted file mode 100644 index e948ba67a..000000000 --- a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/TestJsonTaggedEventConsumer.java +++ /dev/null @@ -1,131 +0,0 @@ -/*- - * ============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; - -/** - * Test JSON Tagged Event Consumer. - * @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("{TestTimestamp\": 1469781869268}", textBlock.getText()); - - 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("{TestTimestamp\": 1469781869268}", textBlock.getText()); - 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("{TestTimestamp\": 1469781869268}", textBlock.getText()); - 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("{TestTimestamp\": 1469781869268}", textBlock.getText()); - 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 deleted file mode 100644 index db60b229d..000000000 --- a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/TestPluginFactories.java +++ /dev/null @@ -1,63 +0,0 @@ -/*- - * ============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.ApexParameterHandler; -import org.onap.policy.apex.service.parameters.ApexParameters; -import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters; -import org.onap.policy.common.parameters.ParameterException; - -/** - * Test Plugin Factories. - * @author Liam Fallon (liam.fallon@ericsson.com) - * @author John Keeney (john.keeney@ericsson.com) - */ -public class TestPluginFactories { - - @Test - public void testEventConsumerFactory() throws ApexEventException, ParameterException { - 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 ce : parameters.getEventInputParameters().entrySet()) { - final ApexEventConsumer consumer = new EventConsumerFactory().createConsumer( - parameters.getEngineServiceParameters().getName() + "_consumer_" + ce.getKey(), ce.getValue()); - assertNotNull(consumer); - } - - for (final Entry 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/event/testpojos/DummyPojo.java b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/testpojos/DummyPojo.java new file mode 100644 index 000000000..f87c72521 --- /dev/null +++ b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/testpojos/DummyPojo.java @@ -0,0 +1,69 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 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.testpojos; + +/** + * A test Pojo for pojo decoding and encoding in Apex. + */ +public class DummyPojo { + private int anInt; + private Integer anInteger; + private String someString; + + private DummySubPojo testSubPojo; + + /** + * Gets the an int. + * + * @return the an int + */ + public int getAnInt() { + return anInt; + } + + /** + * Gets the an integer. + * + * @return the an integer + */ + public Integer getAnInteger() { + return anInteger; + } + + /** + * Gets the a string. + * + * @return the a string + */ + public String getSomeString() { + return someString; + } + + /** + * Gets the test sub pojo. + * + * @return the test sub pojo + */ + public DummySubPojo getTestSubPojo() { + return testSubPojo; + } + +} diff --git a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/testpojos/DummyPojoList.java b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/testpojos/DummyPojoList.java new file mode 100644 index 000000000..13c0e9652 --- /dev/null +++ b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/testpojos/DummyPojoList.java @@ -0,0 +1,39 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 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.testpojos; + +import java.util.List; + +/** + * A test list of POJO for decoding and encoding. + */ +public class DummyPojoList { + private List testPojoList; + + /** + * Gets the test pojo list. + * + * @return the test pojo list + */ + public List getTestPojoList() { + return testPojoList; + } +} diff --git a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/testpojos/DummySubPojo.java b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/testpojos/DummySubPojo.java new file mode 100644 index 000000000..121cc81a8 --- /dev/null +++ b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/testpojos/DummySubPojo.java @@ -0,0 +1,69 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 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.testpojos; + +/** + * A test Pojo for pojo decoding and encoding in Apex. + */ +public class DummySubPojo { + private int anInt; + private Integer anInteger; + private String someString; + + private DummySubSubPojo testSubSubPojo; + + /** + * Gets the an int. + * + * @return the an int + */ + public int getAnInt() { + return anInt; + } + + /** + * Gets the an integer. + * + * @return the an integer + */ + public Integer getAnInteger() { + return anInteger; + } + + /** + * Gets the a string. + * + * @return the a string + */ + public String getSomeString() { + return someString; + } + + /** + * Gets the test sub sub pojo. + * + * @return the test sub sub pojo + */ + public DummySubSubPojo getTestSubSubPojo() { + return testSubSubPojo; + } + +} diff --git a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/testpojos/DummySubSubPojo.java b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/testpojos/DummySubSubPojo.java new file mode 100644 index 000000000..d2e55f462 --- /dev/null +++ b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/testpojos/DummySubSubPojo.java @@ -0,0 +1,57 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 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.testpojos; + +/** + * A test Pojo for pojo decoding and encoding in Apex. + */ +public class DummySubSubPojo { + private int anInt; + private Integer anInteger; + private String someString; + + /** + * Gets the an int. + * + * @return the an int + */ + public int getAnInt() { + return anInt; + } + + /** + * Gets the an integer. + * + * @return the an integer + */ + public Integer getAnInteger() { + return anInteger; + } + + /** + * Gets the a string. + * + * @return the a string + */ + public String getSomeString() { + return someString; + } +} diff --git a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/testpojos/TestPojo.java b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/testpojos/TestPojo.java deleted file mode 100644 index 009b2a756..000000000 --- a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/testpojos/TestPojo.java +++ /dev/null @@ -1,69 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 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.testpojos; - -/** - * A test Pojo for pojo decoding and encoding in Apex. - */ -public class TestPojo { - private int anInt; - private Integer anInteger; - private String someString; - - private TestSubPojo testSubPojo; - - /** - * Gets the an int. - * - * @return the an int - */ - public int getAnInt() { - return anInt; - } - - /** - * Gets the an integer. - * - * @return the an integer - */ - public Integer getAnInteger() { - return anInteger; - } - - /** - * Gets the a string. - * - * @return the a string - */ - public String getSomeString() { - return someString; - } - - /** - * Gets the test sub pojo. - * - * @return the test sub pojo - */ - public TestSubPojo getTestSubPojo() { - return testSubPojo; - } - -} diff --git a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/testpojos/TestPojoList.java b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/testpojos/TestPojoList.java deleted file mode 100644 index 0b39a30dc..000000000 --- a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/testpojos/TestPojoList.java +++ /dev/null @@ -1,39 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 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.testpojos; - -import java.util.List; - -/** - * A test list of POJO for decoding and encoding. - */ -public class TestPojoList { - private List testPojoList; - - /** - * Gets the test pojo list. - * - * @return the test pojo list - */ - public List getTestPojoList() { - return testPojoList; - } -} diff --git a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/testpojos/TestSubPojo.java b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/testpojos/TestSubPojo.java deleted file mode 100644 index f5ea80efc..000000000 --- a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/testpojos/TestSubPojo.java +++ /dev/null @@ -1,69 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 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.testpojos; - -/** - * A test Pojo for pojo decoding and encoding in Apex. - */ -public class TestSubPojo { - private int anInt; - private Integer anInteger; - private String someString; - - private TestSubSubPojo testSubSubPojo; - - /** - * Gets the an int. - * - * @return the an int - */ - public int getAnInt() { - return anInt; - } - - /** - * Gets the an integer. - * - * @return the an integer - */ - public Integer getAnInteger() { - return anInteger; - } - - /** - * Gets the a string. - * - * @return the a string - */ - public String getSomeString() { - return someString; - } - - /** - * Gets the test sub sub pojo. - * - * @return the test sub sub pojo - */ - public TestSubSubPojo getTestSubSubPojo() { - return testSubSubPojo; - } - -} diff --git a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/testpojos/TestSubSubPojo.java b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/testpojos/TestSubSubPojo.java deleted file mode 100644 index 8e027f5d6..000000000 --- a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/testpojos/TestSubSubPojo.java +++ /dev/null @@ -1,57 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 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.testpojos; - -/** - * A test Pojo for pojo decoding and encoding in Apex. - */ -public class TestSubSubPojo { - private int anInt; - private Integer anInteger; - private String someString; - - /** - * Gets the an int. - * - * @return the an int - */ - public int getAnInt() { - return anInt; - } - - /** - * Gets the an integer. - * - * @return the an integer - */ - public Integer getAnInteger() { - return anInteger; - } - - /** - * Gets the a string. - * - * @return the a string - */ - public String getSomeString() { - return someString; - } -} diff --git a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/main/ApexCommandLineArgumentsTest.java b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/main/ApexCommandLineArgumentsTest.java new file mode 100644 index 000000000..40375b1f6 --- /dev/null +++ b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/main/ApexCommandLineArgumentsTest.java @@ -0,0 +1,262 @@ +/*- + * ============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.After; +import org.junit.Test; +import org.onap.policy.apex.model.basicmodel.concepts.ApexException; + +/** + * Test Apex Command Line Arguments. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public class ApexCommandLineArgumentsTest { + @After + public void clearRelativeFileRoot() { + System.clearProperty("APEX_RELATIVE_FILE_ROOT"); + } + + @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"); + } + } + + @Test + public void testCommandLineRelativeRootParameters() { + final ApexCommandLineArguments apexArguments = new ApexCommandLineArguments(); + + final String[] args00 = + { "-c", "src/test/resources/parameters/goodParams.json", "-rfr", "zooby" }; + try { + apexArguments.parse(args00); + apexArguments.validate(); + fail("Test should throw an exception here"); + } catch (final ApexException e) { + assertTrue(e.getMessage().contains("zooby\" does not exist or is not a directory")); + } + + final String[] args01 = + { "-rfr" }; + 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: rfr", e.getMessage()); + } + + final String[] args02 = + { "-c", "src/test/resources/parameters/goodParams.json", "-rfr", "pom.xml" }; + try { + apexArguments.parse(args02); + apexArguments.validate(); + fail("Test should throw an exception here"); + } catch (final ApexException e) { + assertTrue(e.getMessage().contains("pom.xml\" does not exist or is not a directory")); + } + + final String[] args03 = + { "-c", "src/test/resources/parameters/goodParams.json", "-rfr", "target" }; + try { + apexArguments.parse(args03); + apexArguments.validate(); + } catch (final ApexException e) { + fail("Test should not throw an exception here"); + } + + final String[] args04 = + { "-c", "src/test/resources/parameters/goodParamsRelative.json", "-rfr", "src/test/resources" }; + try { + apexArguments.parse(args04); + apexArguments.validate(); + } catch (final ApexException e) { + fail("Test should not throw an exception here"); + } + } +} 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 deleted file mode 100644 index c26e4fcb7..000000000 --- a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/main/TestApexCommandLineArguments.java +++ /dev/null @@ -1,262 +0,0 @@ -/*- - * ============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.After; -import org.junit.Test; -import org.onap.policy.apex.model.basicmodel.concepts.ApexException; - -/** - * Test Apex Command Line Arguments. - * - * @author Liam Fallon (liam.fallon@ericsson.com) - */ -public class TestApexCommandLineArguments { - @After - public void clearRelativeFileRoot() { - System.clearProperty("APEX_RELATIVE_FILE_ROOT"); - } - - @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"); - } - } - - @Test - public void testCommandLineRelativeRootParameters() { - final ApexCommandLineArguments apexArguments = new ApexCommandLineArguments(); - - final String[] args00 = - { "-c", "src/test/resources/parameters/goodParams.json", "-rfr", "zooby" }; - try { - apexArguments.parse(args00); - apexArguments.validate(); - fail("Test should throw an exception here"); - } catch (final ApexException e) { - assertTrue(e.getMessage().contains("zooby\" does not exist or is not a directory")); - } - - final String[] args01 = - { "-rfr" }; - 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: rfr", e.getMessage()); - } - - final String[] args02 = - { "-c", "src/test/resources/parameters/goodParams.json", "-rfr", "pom.xml" }; - try { - apexArguments.parse(args02); - apexArguments.validate(); - fail("Test should throw an exception here"); - } catch (final ApexException e) { - assertTrue(e.getMessage().contains("pom.xml\" does not exist or is not a directory")); - } - - final String[] args03 = - { "-c", "src/test/resources/parameters/goodParams.json", "-rfr", "target" }; - try { - apexArguments.parse(args03); - apexArguments.validate(); - } catch (final ApexException e) { - fail("Test should not throw an exception here"); - } - - final String[] args04 = - { "-c", "src/test/resources/parameters/goodParamsRelative.json", "-rfr", "src/test/resources" }; - try { - apexArguments.parse(args04); - apexArguments.validate(); - } catch (final ApexException e) { - fail("Test should not throw an exception here"); - } - } -} diff --git a/services/services-engine/src/test/resources/policymodels/PojoEventModel.apex b/services/services-engine/src/test/resources/policymodels/PojoEventModel.apex index b7153a317..b62875f5c 100644 --- a/services/services-engine/src/test/resources/policymodels/PojoEventModel.apex +++ b/services/services-engine/src/test/resources/policymodels/PojoEventModel.apex @@ -1,7 +1,7 @@ model create name=PojoEventModel version=0.0.1 -schema create name=TestPojoType version=0.0.1 flavour=Java schema=org.onap.policy.apex.service.engine.event.testpojos.TestPojo -schema create name=TestPojoListType version=0.0.1 flavour=Java schema=org.onap.policy.apex.service.engine.event.testpojos.TestPojoList +schema create name=TestPojoType version=0.0.1 flavour=Java schema=org.onap.policy.apex.service.engine.event.testpojos.DummyPojo +schema create name=TestPojoListType version=0.0.1 flavour=Java schema=org.onap.policy.apex.service.engine.event.testpojos.DummyPojoList event create name=PojoEvent version=0.0.1 nameSpace=org.onap.policy.apex.service.engine.event.testpojos source=Outside target=Apex diff --git a/services/services-engine/src/test/resources/policymodels/PojoEventModel.json b/services/services-engine/src/test/resources/policymodels/PojoEventModel.json index 3bd04a375..59ff76ebb 100644 --- a/services/services-engine/src/test/resources/policymodels/PojoEventModel.json +++ b/services/services-engine/src/test/resources/policymodels/PojoEventModel.json @@ -343,7 +343,7 @@ "version" : "0.0.1" }, "schemaFlavour" : "Java", - "schemaDefinition" : "org.onap.policy.apex.service.engine.event.testpojos.TestPojoList" + "schemaDefinition" : "org.onap.policy.apex.service.engine.event.testpojos.DummyPojoList" } }, { "key" : { @@ -356,7 +356,7 @@ "version" : "0.0.1" }, "schemaFlavour" : "Java", - "schemaDefinition" : "org.onap.policy.apex.service.engine.event.testpojos.TestPojo" + "schemaDefinition" : "org.onap.policy.apex.service.engine.event.testpojos.DummyPojo" } } ] } -- cgit 1.2.3-korg