diff options
Diffstat (limited to 'services')
4 files changed, 596 insertions, 0 deletions
diff --git a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/impl/filecarrierplugin/FileCarrierTechnologyParametersTest.java b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/impl/filecarrierplugin/FileCarrierTechnologyParametersTest.java new file mode 100644 index 000000000..609dc31c9 --- /dev/null +++ b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/impl/filecarrierplugin/FileCarrierTechnologyParametersTest.java @@ -0,0 +1,226 @@ +/* + * ============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; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.Assume.assumeTrue; + +import java.io.File; +import java.io.IOException; +import java.net.URL; +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.common.parameters.ParameterRuntimeException; +import org.onap.policy.common.parameters.ValidationResult; + +public class FileCarrierTechnologyParametersTest { + private final Random random = new Random(); + private static final String APEX_RELATIVE_FILE_ROOT = "APEX_RELATIVE_FILE_ROOT"; + private final String defaultApesRelativeFileRoot = System.getProperty(APEX_RELATIVE_FILE_ROOT); + private FileCarrierTechnologyParameters parameters; + private File tempFile; + + @Before + public void setUp() throws Exception { + parameters = new FileCarrierTechnologyParameters(); + } + + /** + * Cleaning after testing. + */ + @After + public void tearDown() { + if (tempFile != null) { + tempFile.delete(); + } + if (defaultApesRelativeFileRoot != null) { + System.setProperty(APEX_RELATIVE_FILE_ROOT, defaultApesRelativeFileRoot); + } else { + System.clearProperty(APEX_RELATIVE_FILE_ROOT); + } + } + + @Test + public void getSetFileName() { + final String fileName = RandomStringUtils.random(10); + parameters.setFileName(fileName); + assertThat(parameters.getFileName()) + .isEqualTo(fileName); + } + + @Test + public void isStandardIo() { + assertThat(parameters.isStandardIo()).isFalse(); + } + + @Test + public void isStandardError() { + assertThat(parameters.isStandardError()).isFalse(); + } + + @Test + public void isStreamingMode() { + assertThat(parameters.isStreamingMode()).isFalse(); + } + + @Test + public void setStandardIo() { + final boolean standardIo = random.nextBoolean(); + parameters.setStandardIo(standardIo); + assertThat(parameters.isStandardIo()).isEqualTo(standardIo); + } + + @Test + public void setStandardError() { + final boolean standardError = random.nextBoolean(); + parameters.setStandardError(standardError); + assertThat(parameters.isStandardError()).isEqualTo(standardError); + } + + @Test + public void getStartDelay() { + assertThat(parameters.getStartDelay()).isEqualTo(0L); + } + + @Test + public void setStartDelay() { + final long delay = random.nextInt(); + parameters.setStartDelay(delay); + assertThat(parameters.getStartDelay()).isEqualTo(delay); + } + + @Test + public void getLabel() { + final String label = RandomStringUtils.random(10); + parameters.setLabel(label); + assertThat(parameters.getLabel()).isEqualTo(label); + } + + @Test + public void setName() { + final String name = RandomStringUtils.random(10); + assertThatThrownBy(() -> parameters.setName(name)).isInstanceOf(ParameterRuntimeException.class); + } + + @Test + public void getName() { + final String label = RandomStringUtils.random(10); + parameters.setLabel(label); + assertThat(parameters.getName()).isEqualTo(label); + } + + @Test + public void getStreamingMode() { + assertThat(parameters.isStreamingMode()).isFalse(); + } + + @Test + public void setStreamingMode() { + final boolean streamingMode = random.nextBoolean(); + parameters.setStreamingMode(streamingMode); + assertThat(parameters.isStreamingMode()).isEqualTo(streamingMode); + } + + @Test + public void validateFileNameNull() { + final ValidationResult result = parameters.validate(); + assertThat(result.isValid()).isFalse(); + } + + @Test + public void validateFileNameAbsolutePath() throws IOException { + tempFile = File.createTempFile("test_", ".tmp"); + parameters.setFileName(tempFile.getAbsolutePath()); + final ValidationResult result = parameters.validate(); + assertThat(result.isValid()).isTrue(); + } + + @Test + public void validateFileNameAbsolutePathNotExisting() { + parameters.setFileName(RandomStringUtils.randomAlphabetic(5) + ".tmp"); + System.setProperty(APEX_RELATIVE_FILE_ROOT, System.getProperty("user.home")); + final ValidationResult result = parameters.validate(); + assertThat(result.isValid()).isTrue(); + } + + @Test + public void validateDirectoryName() { + parameters.setFileName(System.getProperty("user.dir")); + final ValidationResult result = parameters.validate(); + assertThat(result.isValid()).isFalse(); + } + + @Test + public void validateParentNotDirectory() { + final URL resource = FileCarrierTechnologyParameters.class + .getResource("FileCarrierTechnologyParameters.class"); + assumeTrue(resource != null && "file".equalsIgnoreCase(resource.getProtocol())); + final String fileParentPath = resource.getPath(); + final String fileName = RandomStringUtils.randomAlphabetic(5); + final String absolutePath = new File(fileParentPath, fileName).getAbsolutePath(); + parameters.setFileName(absolutePath); + final ValidationResult result = parameters.validate(); + assertThat(result.isValid()).isFalse(); + } + + @Test + public void validateParentDoesNOtExists() { + final File fileParent = new File(System.getProperty("user.home"), RandomStringUtils.randomAlphabetic(6)); + final String fileName = RandomStringUtils.randomAlphabetic(5); + final String absolutePath = new File(fileParent, fileName).getAbsolutePath(); + parameters.setFileName(absolutePath); + final ValidationResult result = parameters.validate(); + assertThat(result.isValid()).isFalse(); + } + + @Test + public void validateDirectorNoSystemVariableSet() { + final ValidationResult result = parameters.validate(); + assertThat(result.isValid()).isFalse(); + } + + @Test + public void validateStandardIo() { + parameters.setStandardIo(true); + final ValidationResult result = parameters.validate(); + assertThat(result.isValid()).isTrue(); + } + + @Test + public void validateStandardError() { + parameters.setStandardError(true); + final ValidationResult result = parameters.validate(); + assertThat(result.isValid()).isTrue(); + } + + @Test + public void validateNegativeDelay() { + final long delay = random.nextInt() * -1; + parameters.setStartDelay(delay); + final ValidationResult result = parameters.validate(); + assertThat(result.isValid()).isFalse(); + } + +}
\ 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/ApexFileEventConsumerTest.java b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/impl/filecarrierplugin/consumer/ApexFileEventConsumerTest.java new file mode 100644 index 000000000..91e705624 --- /dev/null +++ b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/impl/filecarrierplugin/consumer/ApexFileEventConsumerTest.java @@ -0,0 +1,103 @@ +/* + * ============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.assertThatCode; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import java.io.File; +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.filecarrierplugin.FileCarrierTechnologyParameters; +import org.onap.policy.apex.service.engine.parameters.dummyclasses.SuperDooperCarrierTechnologyParameters; +import org.onap.policy.apex.service.engine.parameters.dummyclasses.SuperTokenDelimitedEventProtocolParameters; +import org.onap.policy.apex.service.parameters.carriertechnology.CarrierTechnologyParameters; +import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters; +import org.onap.policy.apex.service.parameters.eventprotocol.EventProtocolTextTokenDelimitedParameters; + +public class ApexFileEventConsumerTest { + private ApexFileEventConsumer consumer; + private EventHandlerParameters handlerParameters; + private File tempFile; + + /** + * Prepare tests. + * + * @throws Exception while file cannot be created + */ + @Before + public void setUp() throws Exception { + consumer = new ApexFileEventConsumer(); + handlerParameters = new EventHandlerParameters(); + tempFile = File.createTempFile("afec", ".tmp"); + tempFile.deleteOnExit(); + } + + @Test + public void initNoConsumerParameters() { + final String name = RandomStringUtils.randomAlphanumeric(4); + assertThatThrownBy(() -> consumer.init(name, null, null)) + .isInstanceOf(ApexEventException.class); + } + + @Test + public void initWrongCarrier() { + final String name = RandomStringUtils.randomAlphanumeric(4); + final CarrierTechnologyParameters technologyParameters = new SuperDooperCarrierTechnologyParameters(); + handlerParameters.setCarrierTechnologyParameters(technologyParameters); + + assertThatThrownBy(() -> consumer.init(name, handlerParameters, null)) + .isInstanceOf(ApexEventException.class); + } + + @Test + public void initWithoutReceiver() { + final String name = RandomStringUtils.randomAlphanumeric(4); + final EventHandlerParameters handlerParameters = new EventHandlerParameters(); + final FileCarrierTechnologyParameters technologyParameters = new FileCarrierTechnologyParameters(); + technologyParameters.setFileName(tempFile.getAbsolutePath()); + final EventProtocolTextTokenDelimitedParameters params = new SuperTokenDelimitedEventProtocolParameters(); + + handlerParameters.setCarrierTechnologyParameters(technologyParameters); + handlerParameters.setEventProtocolParameters(params); + + assertThatCode(() -> consumer.init(name, handlerParameters, null)) + .doesNotThrowAnyException(); + } + + @Test + public void getName() throws ApexEventException { + final String name = RandomStringUtils.randomAlphabetic(5); + final EventHandlerParameters handlerParameters = new EventHandlerParameters(); + final FileCarrierTechnologyParameters technologyParameters = new FileCarrierTechnologyParameters(); + technologyParameters.setFileName(tempFile.getAbsolutePath()); + final EventProtocolTextTokenDelimitedParameters params = new SuperTokenDelimitedEventProtocolParameters(); + + handlerParameters.setCarrierTechnologyParameters(technologyParameters); + handlerParameters.setEventProtocolParameters(params); + + consumer.init(name, handlerParameters, null); + assertThat(consumer.getName()).isEqualTo(name); + } +} diff --git a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/impl/filecarrierplugin/consumer/HeaderDelimitedTextBlockReaderTest.java b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/impl/filecarrierplugin/consumer/HeaderDelimitedTextBlockReaderTest.java new file mode 100644 index 000000000..6c595072f --- /dev/null +++ b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/impl/filecarrierplugin/consumer/HeaderDelimitedTextBlockReaderTest.java @@ -0,0 +1,107 @@ +/* + * ============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 java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; +import org.apache.commons.lang3.RandomStringUtils; +import org.junit.Test; + + +public class HeaderDelimitedTextBlockReaderTest { + + @Test + public void readTextBlockWithDelimeter() throws IOException { + final String startToken = RandomStringUtils.randomAlphabetic(5); + final String endToken = RandomStringUtils.randomAlphabetic(6); + final boolean delimeter = true; + final String text = RandomStringUtils.randomAlphanumeric(20); + final String expected = startToken + text; + // Prepare the stream + final InputStream stream = new ByteArrayInputStream(expected.getBytes(StandardCharsets.UTF_8)); + + final HeaderDelimitedTextBlockReader reader = + new HeaderDelimitedTextBlockReader(startToken, endToken, delimeter); + reader.init(stream); + + final TextBlock textBlock = reader.readTextBlock(); + assertThat(textBlock.getText()).isEqualTo(expected); + } + + @Test + public void readTextBlockWithEndTokenDelimeter() throws IOException { + final String startToken = RandomStringUtils.randomAlphabetic(5); + final String endToken = RandomStringUtils.randomAlphabetic(6); + final boolean delimeter = true; + final String text = RandomStringUtils.randomAlphanumeric(20); + final String input = startToken + "\n" + text + "\n" + endToken + "\n" + text; + final String expected = startToken + "\n" + text + "\n" + endToken; + // Prepare the stream + final InputStream stream = new ByteArrayInputStream(input.getBytes(StandardCharsets.UTF_8)); + + final HeaderDelimitedTextBlockReader reader = + new HeaderDelimitedTextBlockReader(startToken, endToken, delimeter); + reader.init(stream); + + final TextBlock textBlock = reader.readTextBlock(); + assertThat(textBlock.getText()).isEqualTo(expected); + } + + @Test + public void readTextBlockWithoutDelimeter() throws IOException { + final String startToken = RandomStringUtils.randomAlphabetic(5); + final String endToken = RandomStringUtils.randomAlphabetic(6); + final boolean delimeter = false; + final String text = RandomStringUtils.randomAlphanumeric(20); + // Prepare the stream + final InputStream stream = new ByteArrayInputStream(text.getBytes(StandardCharsets.UTF_8)); + + final HeaderDelimitedTextBlockReader reader = + new HeaderDelimitedTextBlockReader(startToken, endToken, delimeter); + reader.init(stream); + + final TextBlock textBlock = reader.readTextBlock(); + assertThat(textBlock.getText()).isEqualTo(text); + } + + @Test + public void readTextBlockWithEndTokenWithoutDelimeter() throws IOException { + final String startToken = RandomStringUtils.randomAlphabetic(5); + final String endToken = RandomStringUtils.randomAlphabetic(6); + final boolean delimeter = false; + final String text = RandomStringUtils.randomAlphanumeric(20); + final String input = startToken + "\n" + text + "\n" + endToken + "\n" + text; + final String expected = startToken + "\n" + text + "\n" + endToken; + // Prepare the stream + final InputStream stream = new ByteArrayInputStream(input.getBytes(StandardCharsets.UTF_8)); + + final HeaderDelimitedTextBlockReader reader = + new HeaderDelimitedTextBlockReader(startToken, endToken, delimeter); + reader.init(stream); + + final TextBlock textBlock = reader.readTextBlock(); + assertThat(textBlock.getText()).isEqualTo(expected); + } +} 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 |