diff options
22 files changed, 1096 insertions, 48 deletions
diff --git a/packages/apex-pdp-package-full/src/main/package/scripts/apexApps.sh b/packages/apex-pdp-package-full/src/main/package/scripts/apexApps.sh index ad7cc7cb4..444f65491 100755 --- a/packages/apex-pdp-package-full/src/main/package/scripts/apexApps.sh +++ b/packages/apex-pdp-package-full/src/main/package/scripts/apexApps.sh @@ -1,10 +1,10 @@ -#!/usr/bin/env ash +#!/usr/bin/env sh #------------------------------------------------------------------------------- # ============LICENSE_START======================================================= # Copyright (C) 2016-2018 Ericsson. All rights reserved. # Modifications Copyright (C) 2019-2020 Nordix Foundation. -# Modifications Copyright (C) 2020 AT&T Intellectual Property. +# Modifications Copyright (C) 2020-2021 AT&T Intellectual Property. # ================================================================================ # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -23,16 +23,12 @@ #------------------------------------------------------------------------------- ## ## Script to run APEX Applications, call with '-h' for help -## - requires BASH with associative arrays, bash of at least version 4 -## - for BASH examples with arrays see for instance: http://www.artificialworlds.net/blog/2012/10/17/bash-associative-array-examples/ ## - adding a new app means to add a command to APEX_APP_MAP and a description to APEX_APP_DESCR_MAP using same/unique key ## ## @package org.onap.policy.apex ## @author Sven van der Meer <sven.van.der.meer@ericsson.com> ## @version v2.0.0 ## -## convert to ash shell script 12/1/2020 -## ##set -x ## ## DO NOT CHANGE CODE BELOW, unless you know what you are doing @@ -71,13 +67,13 @@ _jmxconfig="-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=9 ## Maven/APEX version _version=$(cat $APEX_HOME/etc/app-version.txt) -## system to get CygWin paths -## NOTE: CygWin can not be tested with ash, due to lack of env setup +## system to get CygWin paths +## NOTE: CygWin can not be tested with sh, due to lack of env setup system=$(uname -s | cut -c1-6) cpsep=":" if [ "$system" = "CYGWIN" ] ; then APEX_HOME=`cygpath -m ${APEX_HOME}` - cpsep=";" + cpsep=";" fi ## CP for CP apps CLASSPATH="$APEX_HOME/etc${cpsep}$APEX_HOME/etc/hazelcast${cpsep}$APEX_HOME/etc/infinispan${cpsep}$APEX_HOME/lib/*" diff --git a/packages/apex-pdp-package-full/src/main/package/scripts/apexAsh.sh b/packages/apex-pdp-package-full/src/main/package/scripts/apexAsh.sh index ef14d903c..60608f922 100755 --- a/packages/apex-pdp-package-full/src/main/package/scripts/apexAsh.sh +++ b/packages/apex-pdp-package-full/src/main/package/scripts/apexAsh.sh @@ -1,27 +1,27 @@ -#!/usr/bin/env ash +#!/usr/bin/env sh #------------------------------------------------------------------------------- # ============LICENSE_START======================================================= # Copyright (C) 2016-2018 Ericsson. All rights reserved. -# Modifications Copyright (C) 2020 AT&T Intellectual Property. +# Modifications Copyright (C) 2020-2021 AT&T Intellectual Property. # ================================================================================ # 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========================================================= #------------------------------------------------------------------------------- -# Run from the Apex home directory +# Run from the Apex home directory if [ ! -d /home/apexuser ]; then echo Apex user home directory "/home/apexuser" not found exit diff --git a/packages/apex-pdp-package-full/src/main/package/scripts/apexCLIEditor.sh b/packages/apex-pdp-package-full/src/main/package/scripts/apexCLIEditor.sh index fb520e7fc..08cd118f0 100755 --- a/packages/apex-pdp-package-full/src/main/package/scripts/apexCLIEditor.sh +++ b/packages/apex-pdp-package-full/src/main/package/scripts/apexCLIEditor.sh @@ -1,22 +1,22 @@ -#!/usr/bin/env ash +#!/usr/bin/env sh #------------------------------------------------------------------------------- # ============LICENSE_START======================================================= # Copyright (C) 2016-2018 Ericsson. All rights reserved. -# Modifications Copyright (C) 2020 AT&T Intellectual Property. +# Modifications Copyright (C) 2020-2021 AT&T Intellectual Property. # ================================================================================ # 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========================================================= #------------------------------------------------------------------------------- diff --git a/packages/apex-pdp-package-full/src/main/package/scripts/apexCLIToscaEditor.sh b/packages/apex-pdp-package-full/src/main/package/scripts/apexCLIToscaEditor.sh index cdf4eb3a7..fb4018655 100644 --- a/packages/apex-pdp-package-full/src/main/package/scripts/apexCLIToscaEditor.sh +++ b/packages/apex-pdp-package-full/src/main/package/scripts/apexCLIToscaEditor.sh @@ -1,9 +1,9 @@ -#!/usr/bin/env ash +#!/usr/bin/env sh #------------------------------------------------------------------------------- # ============LICENSE_START======================================================= # Copyright (C) 2019 Nordix Foundation. -# Modifications Copyright (C) 2020 AT&T Intellectual Property. +# Modifications Copyright (C) 2020-2021 AT&T Intellectual Property. # ================================================================================ # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. diff --git a/packages/apex-pdp-package-full/src/main/package/scripts/apexEngine.sh b/packages/apex-pdp-package-full/src/main/package/scripts/apexEngine.sh index ca68a9871..44f4ca070 100755 --- a/packages/apex-pdp-package-full/src/main/package/scripts/apexEngine.sh +++ b/packages/apex-pdp-package-full/src/main/package/scripts/apexEngine.sh @@ -1,22 +1,22 @@ -#!/usr/bin/env ash +#!/usr/bin/env sh #------------------------------------------------------------------------------- # ============LICENSE_START======================================================= # Copyright (C) 2016-2018 Ericsson. All rights reserved. -# Modifications Copyright (C) 2020 AT&T Intellectual Property. +# Modifications Copyright (C) 2020-2021 AT&T Intellectual Property. # ================================================================================ # 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========================================================= #------------------------------------------------------------------------------- diff --git a/packages/apex-pdp-package-full/src/main/package/scripts/apexOnapPf.sh b/packages/apex-pdp-package-full/src/main/package/scripts/apexOnapPf.sh index 2c95cd331..ff2bcccdf 100644 --- a/packages/apex-pdp-package-full/src/main/package/scripts/apexOnapPf.sh +++ b/packages/apex-pdp-package-full/src/main/package/scripts/apexOnapPf.sh @@ -1,8 +1,8 @@ -#!/usr/bin/env ash +#!/usr/bin/env sh # # ============LICENSE_START======================================================= # Copyright (C) 2019-2020 Nordix Foundation. -# Modifications Copyright (C) 2020 AT&T Intellectual Property +# Modifications Copyright (C) 2020-2021 AT&T Intellectual Property # ================================================================================ # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. diff --git a/packages/apex-pdp-package-full/src/main/package/scripts/runBenchmark.sh b/packages/apex-pdp-package-full/src/main/package/scripts/runBenchmark.sh index d9ccaf81b..9985d7ac4 100644 --- a/packages/apex-pdp-package-full/src/main/package/scripts/runBenchmark.sh +++ b/packages/apex-pdp-package-full/src/main/package/scripts/runBenchmark.sh @@ -1,22 +1,22 @@ -#!/usr/bin/env ash +#!/usr/bin/env sh #------------------------------------------------------------------------------- # ============LICENSE_START======================================================= # Copyright (C) 2018 Ericsson. All rights reserved. -# Modifications Copyright (C) 2020 AT&T Intellectual Property. +# Modifications Copyright (C) 2020-2021 AT&T Intellectual Property. # ================================================================================ # 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========================================================= #------------------------------------------------------------------------------- diff --git a/packages/apex-pdp-package-full/src/main/package/scripts/runOneBenchmark.sh b/packages/apex-pdp-package-full/src/main/package/scripts/runOneBenchmark.sh index 1afb5347b..f38975970 100755 --- a/packages/apex-pdp-package-full/src/main/package/scripts/runOneBenchmark.sh +++ b/packages/apex-pdp-package-full/src/main/package/scripts/runOneBenchmark.sh @@ -1,22 +1,22 @@ -#!/usr/bin/env ash +#!/usr/bin/env sh #------------------------------------------------------------------------------- # ============LICENSE_START======================================================= # Copyright (C) 2018 Ericsson. All rights reserved. -# Modifications Copyright (C) 2020 AT&T Intellectual Property. +# Modifications Copyright (C) 2020-2021 AT&T Intellectual Property. # ================================================================================ # 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========================================================= #------------------------------------------------------------------------------- @@ -43,24 +43,24 @@ if [ "$1" != "Javascript" ] && [ "$1" != "Jython" ] && [ "$1" != "JRuby" ] && [ echo "executor-type must be a member of the set [Javascript|Jython|JRuby|Mvel|Java]" exit 1 fi - + if [ "$2" != "01" ] && [ "$2" != "02" ] && [ "$2" != "04" ] && [ "$2" != "08" ] && [ "$2" != "16" ] && [ "$2" != "32" ] && [ "$2" != "64" ]; then echo "thread-count must be a member of the set [01|02|04|08|16|32|64]" exit 1 fi - + # Remove the old benchmark test result file if it exists rm -fr examples/benchmark/Bm$1$2.json # Start the event generator -/bin/ash bin/apexApps.sh event-gen -c examples/benchmark/EventGeneratorConfig.json -o examples/benchmark/Bm$1$2.json > examples/benchmark/Bm$1$2_gen.log 2>&1 & +/bin/sh bin/apexApps.sh event-gen -c examples/benchmark/EventGeneratorConfig.json -o examples/benchmark/Bm$1$2.json > examples/benchmark/Bm$1$2_gen.log 2>&1 & # Start Apex sleep 2 -/bin/ash bin/apexApps.sh engine -c examples/benchmark/$1$2.json > examples/benchmark/Bm$1$2_apex.log 2>&1 & +/bin/sh bin/apexApps.sh engine -c examples/benchmark/$1$2.json > examples/benchmark/Bm$1$2_apex.log 2>&1 & apex_pid=`ps -A -o pid,cmd | grep ApexMain | grep -v grep | head -n 1 | awk '{print $1}'` -echo "running benchmark test for executor $1 with $2 threads" +echo "running benchmark test for executor $1 with $2 threads" # Loop until result file exists while [ ! -f examples/benchmark/Bm$1$2.json ] diff --git a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/PeeredReferenceTest.java b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/PeeredReferenceTest.java new file mode 100644 index 000000000..cadb5c8a1 --- /dev/null +++ b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/PeeredReferenceTest.java @@ -0,0 +1,57 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Nordix Foundation + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.apex.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.filecarrierplugin.consumer.ApexFileEventConsumer; +import org.onap.policy.apex.service.engine.event.impl.filecarrierplugin.producer.ApexFileEventProducer; +import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerPeeredMode; + +public class PeeredReferenceTest { + + @Test + public void getPeeredConsumer() { + final ApexFileEventConsumer eventConsumer = new ApexFileEventConsumer(); + final ApexFileEventProducer eventProducer = new ApexFileEventProducer(); + final EventHandlerPeeredMode peeredMode = EventHandlerPeeredMode.REQUESTOR; + final PeeredReference peeredReference = + new PeeredReference(peeredMode, eventConsumer, eventProducer); + + final ApexEventConsumer actual = peeredReference.getPeeredConsumer(); + assertNotNull(actual); + assertEquals(peeredReference, actual.getPeeredReference(peeredMode)); + } + + @Test + public void getPeeredProducer() { + final ApexEventConsumer eventConsumer = new ApexFileEventConsumer(); + final ApexEventProducer eventProducer = new ApexFileEventProducer(); + final EventHandlerPeeredMode peeredMode = EventHandlerPeeredMode.SYNCHRONOUS; + final PeeredReference reference = + new PeeredReference(peeredMode, eventConsumer, eventProducer); + + final ApexEventProducer actual = reference.getPeeredProducer(); + assertNotNull(actual); + assertEquals(reference, actual.getPeeredReference(peeredMode)); + } +}
\ No newline at end of file diff --git a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/impl/EventConsumerFactoryTest.java b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/impl/EventConsumerFactoryTest.java new file mode 100644 index 000000000..976376e5a --- /dev/null +++ b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/impl/EventConsumerFactoryTest.java @@ -0,0 +1,83 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Nordix Foundation + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.apex.service.engine.event.impl; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.Assert.assertNotNull; + +import org.apache.commons.lang3.RandomStringUtils; +import org.junit.Before; +import org.junit.Test; +import org.onap.policy.apex.service.engine.event.ApexEventConsumer; +import org.onap.policy.apex.service.engine.event.ApexEventException; +import org.onap.policy.apex.service.engine.parameters.dummyclasses.SuperDooperCarrierTechnologyParameters; +import org.onap.policy.apex.service.parameters.carriertechnology.RestPluginCarrierTechnologyParameters; +import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters; + +public class EventConsumerFactoryTest { + private EventConsumerFactory factory; + + @Before + public void setUp() throws Exception { + factory = new EventConsumerFactory(); + } + + @Test + public void createConsumerNoTechnologyParameter() { + final String name = RandomStringUtils.randomAlphabetic(6); + final EventHandlerParameters parameters = new EventHandlerParameters(); + + assertThatThrownBy(() -> factory.createConsumer(name, parameters)) + .isInstanceOf(NullPointerException.class); + } + + @Test + public void createConsumerNoConsumerPlugin() { + final String name = RandomStringUtils.randomAlphabetic(6); + final EventHandlerParameters parameters = new EventHandlerParameters(); + parameters.setCarrierTechnologyParameters(new RestPluginCarrierTechnologyParameters()); + + assertThatThrownBy(() -> factory.createConsumer(name, parameters)) + .isInstanceOf(ApexEventException.class); + } + + @Test + public void createConsumerWrongPluginClassName() { + final String name = RandomStringUtils.randomAlphabetic(6); + final EventHandlerParameters parameters = new EventHandlerParameters(); + final RestPluginCarrierTechnologyParameters technologyParameters = + new RestPluginCarrierTechnologyParameters(); + technologyParameters.setEventConsumerPluginClass("java.lang.Object"); + parameters.setCarrierTechnologyParameters(technologyParameters); + + assertThatThrownBy(() -> factory.createConsumer(name, parameters)) + .isInstanceOf(ApexEventException.class); + } + + @Test + public void createConsumer() throws ApexEventException { + final String name = RandomStringUtils.randomAlphabetic(6); + final EventHandlerParameters parameters = new EventHandlerParameters(); + parameters.setCarrierTechnologyParameters(new SuperDooperCarrierTechnologyParameters()); + + final ApexEventConsumer actual = factory.createConsumer(name, parameters); + assertNotNull(actual); + } +}
\ No newline at end of file diff --git a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/impl/EventProducerFactoryTest.java b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/impl/EventProducerFactoryTest.java new file mode 100644 index 000000000..7e3cb5478 --- /dev/null +++ b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/impl/EventProducerFactoryTest.java @@ -0,0 +1,83 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Nordix Foundation + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.apex.service.engine.event.impl; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.Assert.assertNotNull; + +import org.apache.commons.lang3.RandomStringUtils; +import org.junit.Before; +import org.junit.Test; +import org.onap.policy.apex.service.engine.event.ApexEventException; +import org.onap.policy.apex.service.engine.event.ApexEventProducer; +import org.onap.policy.apex.service.engine.parameters.dummyclasses.SuperDooperCarrierTechnologyParameters; +import org.onap.policy.apex.service.parameters.carriertechnology.RestPluginCarrierTechnologyParameters; +import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters; + +public class EventProducerFactoryTest { + private EventProducerFactory factory; + + @Before + public void setUp() throws Exception { + factory = new EventProducerFactory(); + } + + @Test + public void createConsumerNoTechnologyParameter() { + final EventHandlerParameters parameters = new EventHandlerParameters(); + final String name = RandomStringUtils.randomAlphabetic(4); + + assertThatThrownBy(() -> factory.createProducer(name, parameters)) + .isInstanceOf(NullPointerException.class); + } + + @Test + public void createConsumerNoConsumerPlugin() { + final EventHandlerParameters parameters = new EventHandlerParameters(); + final String name = RandomStringUtils.randomAlphabetic(4); + parameters.setCarrierTechnologyParameters(new RestPluginCarrierTechnologyParameters()); + + assertThatThrownBy(() -> factory.createProducer(name, parameters)) + .isInstanceOf(ApexEventException.class); + } + + @Test + public void createConsumerWrongProducerPluginName() { + final EventHandlerParameters parameters = new EventHandlerParameters(); + final RestPluginCarrierTechnologyParameters technologyParameters = + new RestPluginCarrierTechnologyParameters(); + final String name = RandomStringUtils.randomAlphabetic(4); + technologyParameters.setEventProducerPluginClass("java.lang.Object"); + parameters.setCarrierTechnologyParameters(technologyParameters); + + assertThatThrownBy(() -> factory.createProducer(name, parameters)) + .isInstanceOf(ApexEventException.class); + } + + @Test + public void createConsumer() throws ApexEventException { + final EventHandlerParameters parameters = new EventHandlerParameters(); + parameters.setCarrierTechnologyParameters(new SuperDooperCarrierTechnologyParameters()); + final String name = RandomStringUtils.randomAlphabetic(4); + + final ApexEventProducer actual = factory.createProducer(name, parameters); + assertNotNull(actual); + } +}
\ No newline at end of file diff --git a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/impl/EventProtocolFactoryTest.java b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/impl/EventProtocolFactoryTest.java new file mode 100644 index 000000000..8ed3a9fec --- /dev/null +++ b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/impl/EventProtocolFactoryTest.java @@ -0,0 +1,70 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Nordix Foundation + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.apex.service.engine.event.impl; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.Assert.assertNotNull; + +import org.apache.commons.lang3.RandomStringUtils; +import org.junit.Before; +import org.junit.Test; +import org.onap.policy.apex.service.engine.event.ApexEventProtocolConverter; +import org.onap.policy.apex.service.engine.event.ApexEventRuntimeException; +import org.onap.policy.apex.service.engine.event.impl.apexprotocolplugin.ApexEventProtocolParameters; +import org.onap.policy.apex.service.engine.event.impl.jsonprotocolplugin.JsonEventProtocolParameters; +import org.onap.policy.apex.service.parameters.eventprotocol.EventProtocolParameters; + +public class EventProtocolFactoryTest { + private EventProtocolFactory factory; + + @Before + public void setUp() throws Exception { + factory = new EventProtocolFactory(); + } + + @Test + public void createConsumerNoConsumerPlugin() { + final EventProtocolParameters parameters = new ApexEventProtocolParameters(); + parameters.setEventProtocolPluginClass(""); + final String name = RandomStringUtils.randomAlphabetic(9); + + assertThatThrownBy(() -> factory.createConverter(name, parameters)) + .isInstanceOf(ApexEventRuntimeException.class); + } + + @Test + public void createConsumer2() { + final EventProtocolParameters parameters = new ApexEventProtocolParameters(); + final String name = RandomStringUtils.randomAlphabetic(9); + parameters.setEventProtocolPluginClass("java.lang.Object"); + + assertThatThrownBy(() -> factory.createConverter(name, parameters)) + .isInstanceOf(ApexEventRuntimeException.class); + } + + @Test + public void createConsumer() { + final EventProtocolParameters parameters = new JsonEventProtocolParameters(); + final String name = RandomStringUtils.randomAlphabetic(9); + + final ApexEventProtocolConverter actual = factory.createConverter(name, parameters); + assertNotNull(actual); + } +}
\ No newline at end of file diff --git a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/impl/apexprotocolplugin/Apex2ApexEventConverterTest.java b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/impl/apexprotocolplugin/Apex2ApexEventConverterTest.java new file mode 100644 index 000000000..04e87ee42 --- /dev/null +++ b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/impl/apexprotocolplugin/Apex2ApexEventConverterTest.java @@ -0,0 +1,180 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Nordix Foundation + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.apex.service.engine.event.impl.apexprotocolplugin; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatNoException; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertSame; + +import java.util.List; +import org.apache.commons.lang3.RandomStringUtils; +import org.junit.Before; +import org.junit.Test; +import org.onap.policy.apex.service.engine.event.ApexEvent; +import org.onap.policy.apex.service.engine.event.ApexEventException; +import org.onap.policy.apex.service.engine.event.ApexEventList; +import org.onap.policy.apex.service.engine.event.ApexEventRuntimeException; + +public class Apex2ApexEventConverterTest { + private Apex2ApexEventConverter converter; + + @Before + public void setUp() throws Exception { + converter = new Apex2ApexEventConverter(); + } + + @Test + public void initWithNull() { + assertThatThrownBy(() -> converter.init(null)) + .isInstanceOf(ApexEventRuntimeException.class); + } + + @Test + public void init() { + assertThatNoException() + .isThrownBy(() -> converter.init(new ApexEventProtocolParameters())); + } + + @Test + public void toApexEventWithNull() { + final String eventName = RandomStringUtils.randomAlphanumeric(5); + assertThatThrownBy(() -> converter.toApexEvent(eventName, null)) + .isInstanceOf(ApexEventException.class); + } + + @Test + public void toApexEventWithNonApexEvent() { + final String eventName = RandomStringUtils.randomAlphanumeric(5); + assertThatThrownBy(() -> converter.toApexEvent(eventName, new Object())) + .isInstanceOf(ApexEventException.class); + } + + @Test + public void toApexEmptyEvent() throws ApexEventException { + final String eventName = RandomStringUtils.randomAlphanumeric(4); + final String name = RandomStringUtils.randomAlphanumeric(5); + final String version = RandomStringUtils.randomAlphanumeric(6); + final String nameSpace = "a" + RandomStringUtils.randomAlphanumeric(7); + final String source = RandomStringUtils.randomAlphanumeric(8); + final String target = RandomStringUtils.randomAlphanumeric(9); + + final ApexEvent event = new ApexEvent(name, version, nameSpace, source, target); + final List<ApexEvent> result = converter.toApexEvent(eventName, event); + assertThat(result).isEmpty(); + } + + @Test + public void toApexEventWithApexAndOtherFields() throws ApexEventException { + final String eventName = RandomStringUtils.randomAlphanumeric(4); + final String name1 = RandomStringUtils.randomAlphanumeric(5); + final String version1 = RandomStringUtils.randomAlphanumeric(6); + final String nameSpace1 = "a" + RandomStringUtils.randomAlphanumeric(7); + final String source1 = RandomStringUtils.randomAlphanumeric(8); + final String target1 = RandomStringUtils.randomAlphanumeric(9); + + final ApexEvent event = new ApexEvent(name1, version1, nameSpace1, source1, target1); + + final String key = RandomStringUtils.randomAlphabetic(3); + event.put(key, new Object()); + final List<ApexEvent> result = converter.toApexEvent(eventName, event); + Object[] expected = {event}; + assertArrayEquals(expected, result.toArray()); + } + + @Test + public void toApexEventWithApexAndList() throws ApexEventException { + final String eventName = RandomStringUtils.randomAlphanumeric(4); + final String name1 = RandomStringUtils.randomAlphanumeric(5); + final String version1 = RandomStringUtils.randomAlphanumeric(6); + final String nameSpace1 = "a" + RandomStringUtils.randomAlphanumeric(7); + final String source1 = RandomStringUtils.randomAlphanumeric(8); + final String target1 = RandomStringUtils.randomAlphanumeric(9); + + final ApexEvent event = new ApexEvent(name1, version1, nameSpace1, source1, target1); + + final ApexEventList eventList = new ApexEventList(); + eventList.add(event); + + final String name2 = RandomStringUtils.randomAlphanumeric(15); + final String version2 = RandomStringUtils.randomAlphanumeric(16); + final String nameSpace2 = "b" + RandomStringUtils.randomAlphanumeric(17); + final String source2 = RandomStringUtils.randomAlphanumeric(18); + final String target2 = RandomStringUtils.randomAlphanumeric(19); + + final ApexEvent parentEvent = new ApexEvent(name2, version2, nameSpace2, source2, target2); + final String key = RandomStringUtils.randomAlphabetic(3); + parentEvent.put(key, eventList); + final List<ApexEvent> result = converter.toApexEvent(eventName, parentEvent); + Object[] expected = {event}; + assertArrayEquals(expected, result.toArray()); + } + + @Test + public void toApexEventWithApexAndListAndOtherFields() throws ApexEventException { + final String eventName = RandomStringUtils.randomAlphanumeric(4); + final String name1 = RandomStringUtils.randomAlphanumeric(5); + final String version1 = RandomStringUtils.randomAlphanumeric(6); + final String nameSpace1 = "a" + RandomStringUtils.randomAlphanumeric(7); + final String source1 = RandomStringUtils.randomAlphanumeric(8); + final String target1 = RandomStringUtils.randomAlphanumeric(9); + + final ApexEvent event = new ApexEvent(name1, version1, nameSpace1, source1, target1); + + final ApexEventList eventList = new ApexEventList(); + eventList.add(event); + + final String name2 = RandomStringUtils.randomAlphanumeric(15); + final String version2 = RandomStringUtils.randomAlphanumeric(16); + final String nameSpace2 = "b" + RandomStringUtils.randomAlphanumeric(17); + final String source2 = RandomStringUtils.randomAlphanumeric(18); + final String target2 = RandomStringUtils.randomAlphanumeric(19); + + final ApexEvent parentEvent = new ApexEvent(name2, version2, nameSpace2, source2, target2); + final String key1 = RandomStringUtils.randomAlphabetic(3); + final String key2 = RandomStringUtils.randomAlphabetic(2); + parentEvent.put(key1, eventList); + parentEvent.put(key2, new Object()); + assertThatThrownBy(() -> converter.toApexEvent(eventName, parentEvent)) + .isInstanceOf(ApexEventException.class); + } + + @Test + public void fromApexEventNull() { + assertThatThrownBy(() -> converter.fromApexEvent(null)) + .isInstanceOf(ApexEventException.class); + } + + @Test + public void fromApexEvent() throws ApexEventException { + final String name1 = RandomStringUtils.randomAlphanumeric(5); + final String version1 = RandomStringUtils.randomAlphanumeric(6); + final String nameSpace1 = "a" + RandomStringUtils.randomAlphanumeric(7); + final String source1 = RandomStringUtils.randomAlphanumeric(8); + final String target1 = RandomStringUtils.randomAlphanumeric(9); + + final ApexEvent event = new ApexEvent(name1, version1, nameSpace1, source1, target1); + + final Object actual = converter.fromApexEvent(event); + assertSame(event, actual); + } + +} diff --git a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/impl/apexprotocolplugin/ApexEventProtocolParametersTest.java b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/impl/apexprotocolplugin/ApexEventProtocolParametersTest.java new file mode 100644 index 000000000..8b39b0a74 --- /dev/null +++ b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/impl/apexprotocolplugin/ApexEventProtocolParametersTest.java @@ -0,0 +1,50 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Nordix Foundation + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.apex.service.engine.event.impl.apexprotocolplugin; + +import static org.junit.Assert.assertEquals; + +import org.apache.commons.lang3.RandomStringUtils; +import org.junit.Test; + +public class ApexEventProtocolParametersTest { + + @Test + public void testNoArgConstructor() { + final ApexEventProtocolParameters apexEventProtocolParameters = new ApexEventProtocolParameters(); + final String actual = apexEventProtocolParameters.getLabel(); + final String pluginClass = apexEventProtocolParameters.getEventProtocolPluginClass(); + + assertEquals(ApexEventProtocolParameters.APEX_EVENT_PROTOCOL_LABEL, actual); + assertEquals(Apex2ApexEventConverter.class.getName(), pluginClass); + } + + @Test + public void testConstructor() { + final String expected = RandomStringUtils.randomAlphabetic(6); + final ApexEventProtocolParameters apexEventProtocolParameters = new ApexEventProtocolParameters(expected); + final String actual = apexEventProtocolParameters.getLabel(); + final String pluginClass = apexEventProtocolParameters.getEventProtocolPluginClass(); + + assertEquals(expected, actual); + assertEquals(Apex2ApexEventConverter.class.getName(), pluginClass); + } + +}
\ No newline at end of file diff --git a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/impl/enevent/ApexEvent2EnEventConverterTest.java b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/impl/enevent/ApexEvent2EnEventConverterTest.java new file mode 100644 index 000000000..562760f69 --- /dev/null +++ b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/impl/enevent/ApexEvent2EnEventConverterTest.java @@ -0,0 +1,100 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Nordix Foundation + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.apex.service.engine.event.impl.enevent; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.Assert.assertArrayEquals; + +import java.util.List; +import java.util.Random; +import org.apache.commons.lang3.RandomStringUtils; +import org.junit.Before; +import org.junit.Test; +import org.onap.policy.apex.core.engine.event.EnEvent; +import org.onap.policy.apex.model.basicmodel.concepts.ApexException; +import org.onap.policy.apex.model.eventmodel.concepts.AxEvent; +import org.onap.policy.apex.service.engine.event.ApexEvent; +import org.onap.policy.apex.service.engine.event.ApexEventException; +import org.onap.policy.apex.service.engine.event.ApexEventRuntimeException; + +public class ApexEvent2EnEventConverterTest { + private ApexEvent2EnEventConverter converter; + private final Random random = new Random(); + + @Before + public void setUp() throws Exception { + converter = new ApexEvent2EnEventConverter(null); + } + + @Test + public void toApexEventNull() { + final String eventName = RandomStringUtils.randomAlphabetic(3); + assertThatThrownBy(() -> converter.toApexEvent(eventName, null)) + .isInstanceOf(ApexEventException.class); + } + + @Test + public void toApexEventWrongClass() throws ApexException { + final String eventName = RandomStringUtils.randomAlphabetic(3); + final String name = RandomStringUtils.randomAlphanumeric(5); + final String version = RandomStringUtils.randomAlphanumeric(6); + final String nameSpace = "a" + RandomStringUtils.randomAlphanumeric(7); + final String source = RandomStringUtils.randomAlphanumeric(8); + final String target = RandomStringUtils.randomAlphanumeric(9); + + final ApexEvent event = new ApexEvent(name, version, nameSpace, source, target); + + assertThatThrownBy(() -> converter.toApexEvent(eventName, event)) + .isInstanceOf(ApexEventRuntimeException.class); + } + + @Test + public void toApex() throws ApexException { + // prepare String values for events + final String name = RandomStringUtils.randomAlphabetic(5); + final String version = RandomStringUtils.randomAlphabetic(6); + final String nameSpace = "b" + RandomStringUtils.randomAlphabetic(7); + final String source = RandomStringUtils.randomAlphabetic(8); + final String target = RandomStringUtils.randomAlphabetic(9); + final int executionId = random.nextInt(1000); + final String exceptionMessage = RandomStringUtils.randomAlphabetic(11); + + // prepare events + final AxEvent axEvent = new AxEvent(); + axEvent.getKey().setName(name); + axEvent.getKey().setVersion(version); + axEvent.setNameSpace(nameSpace); + axEvent.setSource(source); + axEvent.setTarget(target); + final EnEvent enEvent = new EnEvent(axEvent); + enEvent.setExecutionId(executionId); + enEvent.setExceptionMessage(exceptionMessage); + + // prepare expected event + final ApexEvent apexEvent = new ApexEvent(name, version, nameSpace, source, target); + apexEvent.setExecutionId(executionId); + apexEvent.setExceptionMessage(exceptionMessage); + final Object[] expected = {apexEvent}; + + // Test + final List<ApexEvent> actual = converter.toApexEvent(RandomStringUtils.randomAlphabetic(3), enEvent); + assertArrayEquals(expected, actual.toArray()); + } +}
\ No newline at end of file diff --git a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/impl/eventrequestor/EventRequestorCarrierTechnologyParametersTest.java b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/impl/eventrequestor/EventRequestorCarrierTechnologyParametersTest.java new file mode 100644 index 000000000..c0da81625 --- /dev/null +++ b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/impl/eventrequestor/EventRequestorCarrierTechnologyParametersTest.java @@ -0,0 +1,43 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Nordix Foundation + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.apex.service.engine.event.impl.eventrequestor; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import org.junit.Test; +import org.onap.policy.common.parameters.GroupValidationResult; + +public class EventRequestorCarrierTechnologyParametersTest { + + @Test + public void getName() { + final EventRequestorCarrierTechnologyParameters parameters = new EventRequestorCarrierTechnologyParameters(); + final String actual = parameters.getName(); + assertEquals(EventRequestorCarrierTechnologyParameters.EVENT_REQUESTOR_CARRIER_TECHNOLOGY_LABEL, actual); + } + + @Test + public void validate() { + final EventRequestorCarrierTechnologyParameters parameters = new EventRequestorCarrierTechnologyParameters(); + final GroupValidationResult actual = parameters.validate(); + assertNotNull(actual); + } +}
\ No newline at end of file diff --git a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/impl/eventrequestor/EventRequestorConsumerTest.java b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/impl/eventrequestor/EventRequestorConsumerTest.java new file mode 100644 index 000000000..dd44da7dd --- /dev/null +++ b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/impl/eventrequestor/EventRequestorConsumerTest.java @@ -0,0 +1,86 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Nordix Foundation + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.apex.service.engine.event.impl.eventrequestor; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.Assert.assertEquals; + +import org.apache.commons.lang3.RandomStringUtils; +import org.junit.Before; +import org.junit.Test; +import org.onap.policy.apex.service.engine.event.ApexEventException; +import org.onap.policy.apex.service.engine.event.PeeredReference; +import org.onap.policy.apex.service.engine.event.impl.filecarrierplugin.consumer.ApexFileEventConsumer; +import org.onap.policy.apex.service.engine.event.impl.filecarrierplugin.producer.ApexFileEventProducer; +import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters; +import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerPeeredMode; + +public class EventRequestorConsumerTest { + private EventRequestorConsumer consumer; + + @Before + public void setUp() throws Exception { + consumer = new EventRequestorConsumer(); + } + + @Test + public void initNoCarrierTechnologyParameters() { + final String consumerName = RandomStringUtils.randomAlphabetic(6); + final EventHandlerParameters eventHandlerParameters = new EventHandlerParameters(); + + assertThatThrownBy(() -> consumer.init(consumerName, eventHandlerParameters, null)) + .isInstanceOf(ApexEventException.class); + } + + @Test + public void initNoPeered() { + final String consumerName = RandomStringUtils.randomAlphabetic(6); + final EventHandlerParameters eventHandlerParameters = new EventHandlerParameters(); + eventHandlerParameters.setCarrierTechnologyParameters(new EventRequestorCarrierTechnologyParameters()); + + assertThatThrownBy(() -> consumer.init(consumerName, eventHandlerParameters, null)) + .isInstanceOf(ApexEventException.class); + } + + @Test + public void getName() throws ApexEventException { + final String consumerName = RandomStringUtils.randomAlphabetic(6); + final EventHandlerParameters eventHandlerParameters = new EventHandlerParameters(); + eventHandlerParameters.setCarrierTechnologyParameters(new EventRequestorCarrierTechnologyParameters()); + eventHandlerParameters.setPeeredMode(EventHandlerPeeredMode.REQUESTOR, true); + + consumer.init(consumerName, eventHandlerParameters, null); + final String actual = consumer.getName(); + + assertEquals(consumerName, actual); + } + + @Test + public void getSetPeeeredReference() { + final PeeredReference peeredReference = + new PeeredReference(EventHandlerPeeredMode.REQUESTOR, new ApexFileEventConsumer(), + new ApexFileEventProducer()); + consumer.setPeeredReference(EventHandlerPeeredMode.REQUESTOR, peeredReference); + + final PeeredReference actual = consumer.getPeeredReference(EventHandlerPeeredMode.REQUESTOR); + + assertEquals(peeredReference, actual); + } +}
\ No newline at end of file diff --git a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/impl/eventrequestor/EventRequestorProducerTest.java b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/impl/eventrequestor/EventRequestorProducerTest.java new file mode 100644 index 000000000..2f4764fb5 --- /dev/null +++ b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/impl/eventrequestor/EventRequestorProducerTest.java @@ -0,0 +1,161 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Nordix Foundation + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.apex.service.engine.event.impl.eventrequestor; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.Assert.assertEquals; + +import java.util.Random; +import org.apache.commons.lang3.RandomStringUtils; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Matchers; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.onap.policy.apex.service.engine.event.ApexEventException; +import org.onap.policy.apex.service.engine.event.ApexEventProducer; +import org.onap.policy.apex.service.engine.event.ApexEventRuntimeException; +import org.onap.policy.apex.service.engine.event.PeeredReference; +import org.onap.policy.apex.service.engine.event.SynchronousEventCache; +import org.onap.policy.apex.service.engine.event.impl.filecarrierplugin.consumer.ApexFileEventConsumer; +import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters; +import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerPeeredMode; + +public class EventRequestorProducerTest { + private final Random random = new Random(); + private EventRequestorProducer producer; + + @Mock + private ApexEventProducer apexProducer; + @Mock + private EventRequestorConsumer apexConsumer; + + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + producer = new EventRequestorProducer(); + } + + @Test + public void initWithEmptyParams() { + final String producerName = RandomStringUtils.random(4); + final EventHandlerParameters eventHandlerParameters = new EventHandlerParameters(); + + assertThatThrownBy(() -> producer.init(producerName, eventHandlerParameters)) + .isInstanceOf(ApexEventException.class); + } + + @Test + public void initNotPeered() { + final String producerName = RandomStringUtils.random(4); + final EventHandlerParameters eventHandlerParameters = new EventHandlerParameters(); + eventHandlerParameters.setCarrierTechnologyParameters(new EventRequestorCarrierTechnologyParameters()); + + assertThatThrownBy(() -> producer.init(producerName, eventHandlerParameters)) + .isInstanceOf(ApexEventException.class); + } + + @Test + public void getName() throws ApexEventException { + final String expected = RandomStringUtils.random(4); + final EventHandlerParameters eventHandlerParameters = new EventHandlerParameters(); + eventHandlerParameters.setCarrierTechnologyParameters(new EventRequestorCarrierTechnologyParameters()); + eventHandlerParameters.setPeeredMode(EventHandlerPeeredMode.REQUESTOR, true); + + producer.init(expected, eventHandlerParameters); + final String actual = producer.getName(); + + assertEquals(expected, actual); + } + + @Test + public void getSetPeeredReference() { + final PeeredReference peeredReference = new PeeredReference(EventHandlerPeeredMode.SYNCHRONOUS, apexConsumer, + apexProducer); + producer.setPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS, peeredReference); + + final PeeredReference actual = this.producer.getPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS); + assertEquals(peeredReference, actual); + } + + @Test + public void sendEventNoRequestor() { + final int id = random.nextInt(1000); + + assertThatThrownBy(() -> producer.sendEvent(id, null, null, null)) + .isInstanceOf(ApexEventRuntimeException.class); + } + + @Test + public void sendEventNoEventRequestorConsumer() { + final int id = random.nextInt(1000); + + final ApexFileEventConsumer fileEventConsumer = Mockito.mock(ApexFileEventConsumer.class); + + final PeeredReference reference = + new PeeredReference(EventHandlerPeeredMode.REQUESTOR, fileEventConsumer, apexProducer); + + producer.setPeeredReference(EventHandlerPeeredMode.REQUESTOR, reference); + + assertThatThrownBy(() -> producer.sendEvent(id, null, null, null)) + .isInstanceOf(ApexEventRuntimeException.class); + } + + @Test + public void sendEvent() { + final int id = random.nextInt(1000); + // Prepare mocks + final PeeredReference peeredReference = Mockito.mock(PeeredReference.class); + + Mockito.when(apexConsumer.getPeeredReference(Matchers.any())).thenReturn(peeredReference); + Mockito.when(peeredReference.getPeeredConsumer()).thenReturn(apexConsumer); + + final PeeredReference reference = + new PeeredReference(EventHandlerPeeredMode.REQUESTOR, apexConsumer, apexProducer); + producer.setPeeredReference(EventHandlerPeeredMode.REQUESTOR, reference); + + producer.sendEvent(id, null, null, null); + Mockito.verify(apexConsumer, Mockito.times(1)).processEvent(Matchers.any()); + } + + @Test + public void sendEventCached() { + final int id = random.nextInt(1000); + + // Set event cache + final SynchronousEventCache eventCache = Mockito.mock(SynchronousEventCache.class); + producer.setPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS, eventCache); + + // Prepare other mocks + final PeeredReference peeredReference = Mockito.mock(PeeredReference.class); + + Mockito.when(peeredReference.getPeeredConsumer()).thenReturn(apexConsumer); + Mockito.when(apexConsumer.getPeeredReference(Matchers.any())).thenReturn(peeredReference); + + final PeeredReference reference = + new PeeredReference(EventHandlerPeeredMode.REQUESTOR, apexConsumer, apexProducer); + producer.setPeeredReference(EventHandlerPeeredMode.REQUESTOR, reference); + + producer.sendEvent(id, null, null, null); + Mockito.verify(apexConsumer, Mockito.times(1)).processEvent(Matchers.any()); + Mockito.verify(eventCache, Mockito.times(1)).removeCachedEventToApexIfExists(id); + } +}
\ No newline at end of file diff --git a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/impl/filecarrierplugin/consumer/TextBlockReaderFactoryTest.java b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/impl/filecarrierplugin/consumer/TextBlockReaderFactoryTest.java new file mode 100644 index 000000000..4fa42a727 --- /dev/null +++ b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/impl/filecarrierplugin/consumer/TextBlockReaderFactoryTest.java @@ -0,0 +1,79 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Nordix Foundation + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.apex.service.engine.event.impl.filecarrierplugin.consumer; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import java.io.ByteArrayInputStream; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; +import org.apache.commons.lang3.RandomStringUtils; +import org.junit.Before; +import org.junit.Test; +import org.onap.policy.apex.service.engine.event.ApexEventException; +import org.onap.policy.apex.service.engine.event.impl.apexprotocolplugin.ApexEventProtocolParameters; +import org.onap.policy.apex.service.engine.event.impl.jsonprotocolplugin.JsonEventProtocolParameters; +import org.onap.policy.apex.service.engine.parameters.dummyclasses.SuperTokenDelimitedEventProtocolParameters; +import org.onap.policy.apex.service.parameters.eventprotocol.EventProtocolParameters; + +public class TextBlockReaderFactoryTest { + private TextBlockReaderFactory factory; + + @Before + public void setUp() throws Exception { + factory = new TextBlockReaderFactory(); + } + + @Test + public void getTaggedReaderTextCharDelimitedParametersParams() throws ApexEventException { + final String text = RandomStringUtils.randomAlphanumeric(22); + final InputStream inputStream = prepareInputStream(text); + final EventProtocolParameters parameters = new JsonEventProtocolParameters(); + + final TextBlockReader actual = factory.getTaggedReader(inputStream, parameters); + assertThat(actual).isNotInstanceOf(HeaderDelimitedTextBlockReader.class); + } + + @Test + public void getTaggedReaderTextTokenDelimitedParams() throws ApexEventException { + final String text = RandomStringUtils.randomAlphanumeric(22); + final InputStream inputStream = prepareInputStream(text); + new ApexEventProtocolParameters(); + final EventProtocolParameters parameters = new SuperTokenDelimitedEventProtocolParameters(); + + final TextBlockReader actual = factory.getTaggedReader(inputStream, parameters); + assertThat(actual).isInstanceOf(HeaderDelimitedTextBlockReader.class); + } + + @Test + public void getTaggedReaderNotSupportedParams() { + final String text = RandomStringUtils.randomAlphanumeric(22); + final InputStream inputStream = prepareInputStream(text); + final EventProtocolParameters parameters = new ApexEventProtocolParameters(); + assertThatThrownBy(() -> factory.getTaggedReader(inputStream, parameters)) + .isInstanceOf(ApexEventException.class); + } + + + private InputStream prepareInputStream(String text) { + return new ByteArrayInputStream(text.getBytes(StandardCharsets.UTF_8)); + } +}
\ No newline at end of file diff --git a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/impl/filecarrierplugin/consumer/TextBlockTest.java b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/impl/filecarrierplugin/consumer/TextBlockTest.java new file mode 100644 index 000000000..4390c3f3a --- /dev/null +++ b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/impl/filecarrierplugin/consumer/TextBlockTest.java @@ -0,0 +1,57 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Nordix Foundation + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.apex.service.engine.event.impl.filecarrierplugin.consumer; + +import static org.junit.Assert.assertEquals; + +import java.util.Random; +import org.apache.commons.lang3.RandomStringUtils; +import org.junit.Test; + +public class TextBlockTest { + private final Random random = new Random(); + + @Test + public void isEndOfText() { + final boolean endOfText = random.nextBoolean(); + final TextBlock textBlock = new TextBlock(endOfText, null); + + assertEquals(endOfText, textBlock.isEndOfText()); + } + + @Test + public void getText() { + final boolean endOfText = random.nextBoolean(); + final String text = RandomStringUtils.randomAlphanumeric(8); + final TextBlock textBlock = new TextBlock(endOfText, text); + + assertEquals(text, textBlock.getText()); + } + + @Test + public void setText() { + final boolean endOfText = random.nextBoolean(); + final String text = RandomStringUtils.randomAlphanumeric(8); + final TextBlock textBlock = new TextBlock(endOfText, null); + + textBlock.setText(text); + assertEquals(text, textBlock.getText()); + } +}
\ No newline at end of file diff --git a/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/handler/ApexEngineHandler.java b/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/handler/ApexEngineHandler.java index 9fe3c94ce..65c4b0373 100644 --- a/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/handler/ApexEngineHandler.java +++ b/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/handler/ApexEngineHandler.java @@ -160,9 +160,10 @@ public class ApexEngineHandler { policyMapToRetain.putAll(policyModel.getPolicies().getPolicyMap()); }); for (ApexMain main : undeployedPoliciesMainMap.values()) { - handleParametersRemoval(inputParamKeysToRetain, outputParamKeysToRetain, taskParametersToRetain, - executorParamKeysToRetain, schemaParamKeysToRetain, main); - + if (null != main.getApexParameters()) { + handleParametersRemoval(inputParamKeysToRetain, outputParamKeysToRetain, taskParametersToRetain, + executorParamKeysToRetain, schemaParamKeysToRetain, main); + } if (null != main.getActivator() && null != main.getActivator().getPolicyModel()) { handleAxConceptsRemoval(keyInfoMapToRetain, schemaMapToRetain, eventMapToRetain, albumMapToRetain, taskMapToRetain, policyMapToRetain, main); diff --git a/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/handler/PdpUpdateMessageHandler.java b/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/handler/PdpUpdateMessageHandler.java index bf72b0999..8dc49eefb 100644 --- a/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/handler/PdpUpdateMessageHandler.java +++ b/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/handler/PdpUpdateMessageHandler.java @@ -1,6 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2019-2021 Nordix Foundation. + * Modifications Copyright (C) 2021 Bell Canada. 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. @@ -97,7 +98,8 @@ public class PdpUpdateMessageHandler { if (pdpStatusContext.getState().equals(PdpState.ACTIVE)) { pdpResponseDetails = startOrStopApexEngineBasedOnPolicies(pdpUpdateMsg, pdpMessageHandler); - ApexEngineHandler apexEngineHandler = Registry.get(ApexStarterConstants.REG_APEX_ENGINE_HANDLER); + ApexEngineHandler apexEngineHandler = + Registry.getOrDefault(ApexStarterConstants.REG_APEX_ENGINE_HANDLER, ApexEngineHandler.class, null); // in hearbeat while in active state, only the policies which are running should be there. // if some policy fails, that shouldn't go in the heartbeat. // If no policies are running, then the policy list in the heartbeat can be empty |