diff options
Diffstat (limited to 'services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/impl/filecarrierplugin/producer/ApexFileEventProducerTest.java')
-rw-r--r-- | services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/impl/filecarrierplugin/producer/ApexFileEventProducerTest.java | 160 |
1 files changed, 160 insertions, 0 deletions
diff --git a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/impl/filecarrierplugin/producer/ApexFileEventProducerTest.java b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/impl/filecarrierplugin/producer/ApexFileEventProducerTest.java new file mode 100644 index 000000000..1ac1cdd7c --- /dev/null +++ b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/impl/filecarrierplugin/producer/ApexFileEventProducerTest.java @@ -0,0 +1,160 @@ +/* + * ============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.producer; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatCode; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.util.Random; +import org.apache.commons.lang3.RandomStringUtils; +import org.junit.After; +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.ApexEventRuntimeException; +import org.onap.policy.apex.service.engine.event.impl.filecarrierplugin.FileCarrierTechnologyParameters; +import org.onap.policy.apex.service.engine.parameters.dummyclasses.SuperDooperCarrierTechnologyParameters; +import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters; + +public class ApexFileEventProducerTest { + private final PrintStream out = System.out; + private final Random random = new Random(); + private ApexFileEventProducer eventProducer; + private EventHandlerParameters parameters; + private FileCarrierTechnologyParameters technologyParameters; + + /** + * Prepare for testing. + */ + @Before + public void setUp() { + eventProducer = new ApexFileEventProducer(); + parameters = new EventHandlerParameters(); + technologyParameters = new FileCarrierTechnologyParameters(); + } + + @After + public void tearDown() { + System.setOut(out); + } + + @Test + public void initNullParams() { + final String name = RandomStringUtils.randomAlphabetic(5); + assertThatThrownBy(() -> eventProducer.init(name, null)) + .isInstanceOf(ApexEventException.class); + } + + @Test + public void initParamsInvalidCarrier() { + final String name = RandomStringUtils.randomAlphabetic(5); + parameters.setCarrierTechnologyParameters(new SuperDooperCarrierTechnologyParameters()); + assertThatThrownBy(() -> eventProducer.init(name, parameters)) + .isInstanceOf(ApexEventException.class); + } + + @Test + public void initParamsWithStandardError() { + final String name = RandomStringUtils.randomAlphabetic(5); + technologyParameters.setStandardError(true); + + parameters.setCarrierTechnologyParameters(technologyParameters); + + assertThatCode(() -> eventProducer.init(name, parameters)) + .doesNotThrowAnyException(); + } + + @Test + public void initParamsWithStandardIo() { + final String name = RandomStringUtils.randomAlphabetic(5); + technologyParameters.setStandardIo(true); + + parameters.setCarrierTechnologyParameters(technologyParameters); + assertThatCode(() -> eventProducer.init(name, parameters)) + .doesNotThrowAnyException(); + } + + @Test + public void initParamsWithFileIsDirectory() { + final String name = RandomStringUtils.randomAlphabetic(5); + final String fileName = System.getProperty("user.dir"); + + technologyParameters.setFileName(fileName); + + parameters.setCarrierTechnologyParameters(technologyParameters); + assertThatThrownBy(() -> eventProducer.init(name, parameters)) + .isInstanceOf(ApexEventException.class); + } + + @Test + public void initParamsWithDelay() { + final String name = RandomStringUtils.randomAlphabetic(5); + technologyParameters.setStandardIo(true); + parameters.setCarrierTechnologyParameters(technologyParameters); + + assertThatCode(() -> eventProducer.init(name, parameters)) + .doesNotThrowAnyException(); + } + + @Test + public void sendEventWrongEvent() throws ApexEventException { + final long executionId = random.nextLong(); + final String eventName = RandomStringUtils.randomAlphanumeric(4); + final String producerName = RandomStringUtils.randomAlphanumeric(5); + final Object event = new Object(); + + technologyParameters.setStandardIo(true); + final EventHandlerParameters parameters = new EventHandlerParameters(); + parameters.setCarrierTechnologyParameters(technologyParameters); + + ApexFileEventProducer apexFileEventProducer = new ApexFileEventProducer(); + apexFileEventProducer.init(producerName, parameters); + assertThatThrownBy(() -> apexFileEventProducer.sendEvent(executionId, null, eventName, event)) + .isInstanceOf(ApexEventRuntimeException.class); + } + + @Test + public void sendEvent() throws ApexEventException { + // Prepare output stream to read data + final ByteArrayOutputStream outContent = new ByteArrayOutputStream(); + System.setOut(new PrintStream(outContent)); + + // Prepare producer + final String producerName = RandomStringUtils.randomAlphanumeric(5); + technologyParameters.setStandardIo(true); + final EventHandlerParameters parameters = new EventHandlerParameters(); + parameters.setCarrierTechnologyParameters(technologyParameters); + eventProducer.init(producerName, parameters); + + // Prepare for sending + final long executionId = random.nextLong(); + final String eventName = RandomStringUtils.randomAlphanumeric(4); + final String event = RandomStringUtils.randomAlphabetic(6); + eventProducer.sendEvent(executionId, null, eventName, event); + + // Check results + assertThat(outContent.toString()).contains(event); + } + +}
\ No newline at end of file |