diff options
Diffstat (limited to 'dcae-analytics-common/src/test')
7 files changed, 403 insertions, 0 deletions
diff --git a/dcae-analytics-common/src/test/java/org/openecomp/dcae/analytics/common/BaseAnalyticsCommonUnitTest.java b/dcae-analytics-common/src/test/java/org/openecomp/dcae/analytics/common/BaseAnalyticsCommonUnitTest.java new file mode 100644 index 0000000..06b7e63 --- /dev/null +++ b/dcae-analytics-common/src/test/java/org/openecomp/dcae/analytics/common/BaseAnalyticsCommonUnitTest.java @@ -0,0 +1,48 @@ +/* + * ============LICENSE_START========================================================= + * dcae-analytics + * ================================================================================ + * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.dcae.analytics.common; + +import org.openecomp.dcae.analytics.common.service.processor.TestEarlyTerminatingProcessor; +import org.openecomp.dcae.analytics.common.service.processor.TestMessageProcessor1; +import org.openecomp.dcae.analytics.common.service.processor.TestMessageProcessor2; +import org.openecomp.dcae.analytics.common.service.processor.TestProcessorContext; +import org.openecomp.dcae.analytics.test.BaseDCAEAnalyticsUnitTest; + +/** + * Base class from all DCEA Analytics Common Module Unit Tests + * <p> + * @author Rajiv Singla. Creation Date: 10/6/2016. + */ +public abstract class BaseAnalyticsCommonUnitTest extends BaseDCAEAnalyticsUnitTest { + + + protected TestProcessorContext testProcessorContext = new TestProcessorContext("", true); + + protected TestMessageProcessor1 getTestMessageProcessor1() { + return new TestMessageProcessor1(); + } + protected TestMessageProcessor2 getTestMessageProcessor2() { + return new TestMessageProcessor2(); + } + protected TestEarlyTerminatingProcessor getTestEarlyTerminationProcessor() { + return new TestEarlyTerminatingProcessor(); + } +} diff --git a/dcae-analytics-common/src/test/java/org/openecomp/dcae/analytics/common/service/processor/GenericMessageChainProcessorTest.java b/dcae-analytics-common/src/test/java/org/openecomp/dcae/analytics/common/service/processor/GenericMessageChainProcessorTest.java new file mode 100644 index 0000000..2ab9d40 --- /dev/null +++ b/dcae-analytics-common/src/test/java/org/openecomp/dcae/analytics/common/service/processor/GenericMessageChainProcessorTest.java @@ -0,0 +1,96 @@ +/* + * ============LICENSE_START========================================================= + * dcae-analytics + * ================================================================================ + * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.dcae.analytics.common.service.processor; + +import com.google.common.collect.ImmutableList; +import org.junit.Test; +import org.openecomp.dcae.analytics.common.BaseAnalyticsCommonUnitTest; +import org.openecomp.dcae.analytics.common.exception.MessageProcessingException; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertThat; + + +/** + * + * @author Rajiv Singla. Creation Date: 11/8/2016. + */ +public class GenericMessageChainProcessorTest extends BaseAnalyticsCommonUnitTest { + + + @Test + public void testProcessChainWhenProcessChainHasNoEarlyTermination() throws Exception { + + final TestMessageProcessor1 testMessageProcessor1 = getTestMessageProcessor1(); + final TestMessageProcessor2 testMessageProcessor2 = getTestMessageProcessor2(); + final ImmutableList<? extends MessageProcessor<TestProcessorContext>> testMessageChain = + ImmutableList.of(testMessageProcessor1, testMessageProcessor2); + + final TestProcessorContext testProcessorContext = new TestProcessorContext("Hello", true); + + final GenericMessageChainProcessor<TestProcessorContext> genericMessageChainProcessor = + new GenericMessageChainProcessor<>(testMessageChain, testProcessorContext); + + final TestProcessorContext finalProcessorContext = genericMessageChainProcessor.processChain(); + + final String result = finalProcessorContext.getResult(); + assertThat("Final Result must be Hello World! Again", result, is("Hello World! Again")); + assertThat("TestProcessor1 state is correct", testMessageProcessor1.getProcessingState(), + is(ProcessingState.PROCESSING_FINISHED_SUCCESSFULLY)); + assertThat("TestProcessor2 state is correct", testMessageProcessor2.getProcessingState(), + is(ProcessingState.PROCESSING_FINISHED_SUCCESSFULLY)); + } + + + @Test + public void testProcessChainWhenProcessChainEarlyTermination() throws Exception { + + final TestEarlyTerminatingProcessor testEarlyTerminatingProcessor = getTestEarlyTerminationProcessor(); + final ImmutableList<? extends MessageProcessor<TestProcessorContext>> testMessageChain = + ImmutableList.of(testEarlyTerminatingProcessor, getTestMessageProcessor2()); + final TestProcessorContext testProcessorContext = new TestProcessorContext("Hello", true); + + final GenericMessageChainProcessor<TestProcessorContext> genericMessageChainProcessor = + new GenericMessageChainProcessor<>(testMessageChain, testProcessorContext); + + final TestProcessorContext finalProcessorContext = genericMessageChainProcessor.processChain(); + final String result = finalProcessorContext.getResult(); + assertNull("Final Result must be null", result); + assertThat("TestEarlyTerminatingProcessor state is correct", + testEarlyTerminatingProcessor.getProcessingState(), is(ProcessingState.PROCESSING_TERMINATED_EARLY)); + } + + @Test(expected = MessageProcessingException.class) + public void testProcessChainWhenIncomingMessageContextIsNull() throws Exception { + + final TestEarlyTerminatingProcessor testEarlyTerminatingProcessor = getTestEarlyTerminationProcessor(); + final ImmutableList<? extends MessageProcessor<TestProcessorContext>> testMessageChain = + ImmutableList.of(testEarlyTerminatingProcessor, getTestMessageProcessor2()); + final TestProcessorContext testProcessorContext = null; + + final GenericMessageChainProcessor<TestProcessorContext> genericMessageChainProcessor = + new GenericMessageChainProcessor<>(testMessageChain, testProcessorContext); + + genericMessageChainProcessor.processChain(); + } + +} diff --git a/dcae-analytics-common/src/test/java/org/openecomp/dcae/analytics/common/service/processor/TestEarlyTerminatingProcessor.java b/dcae-analytics-common/src/test/java/org/openecomp/dcae/analytics/common/service/processor/TestEarlyTerminatingProcessor.java new file mode 100644 index 0000000..c5852de --- /dev/null +++ b/dcae-analytics-common/src/test/java/org/openecomp/dcae/analytics/common/service/processor/TestEarlyTerminatingProcessor.java @@ -0,0 +1,39 @@ +/* + * ============LICENSE_START========================================================= + * dcae-analytics + * ================================================================================ + * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.dcae.analytics.common.service.processor; + +/** + * + * @author Rajiv Singla. Creation Date: 11/8/2016. + */ +public class TestEarlyTerminatingProcessor extends AbstractMessageProcessor<TestProcessorContext> { + + @Override + public String getProcessorDescription() { + return "Terminates the chain early"; + } + + @Override + public TestProcessorContext processMessage(TestProcessorContext processorContext) { + setTerminatingProcessingMessage("Terminating early", processorContext); + return processorContext; + } +} diff --git a/dcae-analytics-common/src/test/java/org/openecomp/dcae/analytics/common/service/processor/TestMessageProcessor1.java b/dcae-analytics-common/src/test/java/org/openecomp/dcae/analytics/common/service/processor/TestMessageProcessor1.java new file mode 100644 index 0000000..6c56cb8 --- /dev/null +++ b/dcae-analytics-common/src/test/java/org/openecomp/dcae/analytics/common/service/processor/TestMessageProcessor1.java @@ -0,0 +1,41 @@ +/* + * ============LICENSE_START========================================================= + * dcae-analytics + * ================================================================================ + * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.dcae.analytics.common.service.processor; + +/** + * + * @author Rajiv Singla. Creation Date: 11/8/2016. + */ +public class TestMessageProcessor1 extends AbstractMessageProcessor<TestProcessorContext> { + + @Override + public String getProcessorDescription() { + return "Appends \" World!\" to the message string and set it to result string"; + } + + @Override + public TestProcessorContext processMessage(TestProcessorContext processorContext) { + final String message = processorContext.getMessage(); + processorContext.setResult(message + " World!"); + setFinishedProcessingMessage("Finished Appending world", processorContext); + return processorContext; + } +} diff --git a/dcae-analytics-common/src/test/java/org/openecomp/dcae/analytics/common/service/processor/TestMessageProcessor2.java b/dcae-analytics-common/src/test/java/org/openecomp/dcae/analytics/common/service/processor/TestMessageProcessor2.java new file mode 100644 index 0000000..0ecbedf --- /dev/null +++ b/dcae-analytics-common/src/test/java/org/openecomp/dcae/analytics/common/service/processor/TestMessageProcessor2.java @@ -0,0 +1,41 @@ +/* + * ============LICENSE_START========================================================= + * dcae-analytics + * ================================================================================ + * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.dcae.analytics.common.service.processor; + +/** + * + * @author Rajiv Singla. Creation Date: 11/8/2016. + */ +public class TestMessageProcessor2 extends AbstractMessageProcessor<TestProcessorContext> { + + @Override + public String getProcessorDescription() { + return "Appends \" Again\" to the result string"; + } + + @Override + public TestProcessorContext processMessage(TestProcessorContext processorContext) { + final String result = processorContext.getResult(); + processorContext.setResult(result + " Again"); + setFinishedProcessingMessage("Finished Appending again to result", processorContext); + return processorContext; + } +} diff --git a/dcae-analytics-common/src/test/java/org/openecomp/dcae/analytics/common/service/processor/TestProcessorContext.java b/dcae-analytics-common/src/test/java/org/openecomp/dcae/analytics/common/service/processor/TestProcessorContext.java new file mode 100644 index 0000000..9d9342f --- /dev/null +++ b/dcae-analytics-common/src/test/java/org/openecomp/dcae/analytics/common/service/processor/TestProcessorContext.java @@ -0,0 +1,84 @@ +/* + * ============LICENSE_START========================================================= + * dcae-analytics + * ================================================================================ + * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.dcae.analytics.common.service.processor; + +import java.util.LinkedList; +import java.util.List; + +/** + * + * @author Rajiv Singla. Creation Date: 11/8/2016. + */ +public class TestProcessorContext implements ProcessorContext { + + private String message; + private boolean continueProcessingFlag; + private String result; + private List<? super MessageProcessor<? extends ProcessorContext>> messageProcessors; + + public TestProcessorContext(String message, boolean continueProcessingFlag) { + this.message = message; + this.continueProcessingFlag = continueProcessingFlag; + this.messageProcessors = new LinkedList<>(); + } + + @Override + public String getMessage() { + return message; + } + + @Override + public boolean canProcessingContinue() { + return continueProcessingFlag; + } + + @Override + public void setProcessingContinueFlag(boolean canProcessingContinue) { + this.continueProcessingFlag = canProcessingContinue; + } + + @Override + public List<? super MessageProcessor<? extends ProcessorContext>> getMessageProcessors() { + return messageProcessors; + } + + + public void setMessage(String message) { + this.message = message; + } + + public boolean isContinueProcessingFlag() { + return continueProcessingFlag; + } + + public void setContinueProcessingFlag(boolean continueProcessingFlag) { + this.continueProcessingFlag = continueProcessingFlag; + } + + public String getResult() { + return result; + } + + public void setResult(String result) { + this.result = result; + } + +} diff --git a/dcae-analytics-common/src/test/resources/logback-test.xml b/dcae-analytics-common/src/test/resources/logback-test.xml new file mode 100644 index 0000000..2a874be --- /dev/null +++ b/dcae-analytics-common/src/test/resources/logback-test.xml @@ -0,0 +1,54 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + ~ ============LICENSE_START========================================================= + ~ dcae-analytics + ~ ================================================================================ + ~ Copyright © 2017 AT&T Intellectual Property. All rights reserved. + ~ ================================================================================ + ~ Licensed under the Apache License, Version 2.0 (the "License"); + ~ you may not use this file except in compliance with the License. + ~ You may obtain a copy of the License at + ~ + ~ http://www.apache.org/licenses/LICENSE-2.0 + ~ + ~ Unless required by applicable law or agreed to in writing, software + ~ distributed under the License is distributed on an "AS IS" BASIS, + ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + ~ See the License for the specific language governing permissions and + ~ limitations under the License. + ~ ============LICENSE_END========================================================= + --> +<configuration> + + <!-- + Disabling some chatty loggers. + --> + <logger name="org.apache.commons.beanutils" level="ERROR"/> + <logger name="org.apache.zookeeper.server" level="ERROR"/> + <logger name="org.apache.zookeeper" level="ERROR"/> + <logger name="com.ning" level="WARN"/> + <logger name="org.apache.spark" level="WARN"/> + <logger name="org.spark-project" level="WARN"/> + <logger name="org.apache.hadoop" level="WARN"/> + <logger name="org.apache.hive" level="WARN"/> + <logger name="org.quartz.core" level="WARN"/> + <logger name="org.eclipse.jetty" level="WARN"/> + <logger name="io.netty.util.internal" level="WARN"/> + + <logger name="org.apache.twill" level="WARN"/> + <logger name="co.cask.cdap" level="INFO"/> + <logger name="com.openecomp" level="DEBUG"/> + + <appender name="Console" class="ch.qos.logback.core.ConsoleAppender"> + <encoder> + <pattern>%d{ISO8601} - %-5p [%t:%C{1}@%L] - %m%n</pattern> + </encoder> + </appender> + + <root level="ERROR"> + <appender-ref ref="Console"/> + </root> + + +</configuration> + |