25784060424140f079c352aecd121e55d1e15497
[dcaegen2/analytics/tca.git] / dcae-analytics-common / src / test / java / org / openecomp / dcae / apod / analytics / common / service / processor / GenericMessageChainProcessorTest.java
1 /*
2  * ===============================LICENSE_START======================================
3  *  dcae-analytics
4  * ================================================================================
5  *    Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  *  Licensed under the Apache License, Version 2.0 (the "License");
8  *  you may not use this file except in compliance with the License.
9  *   You may obtain a copy of the License at
10  *
11  *          http://www.apache.org/licenses/LICENSE-2.0
12  *
13  *  Unless required by applicable law or agreed to in writing, software
14  *  distributed under the License is distributed on an "AS IS" BASIS,
15  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  *  See the License for the specific language governing permissions and
17  *  limitations under the License.
18  *  ============================LICENSE_END===========================================
19  */
20
21 package org.openecomp.dcae.apod.analytics.common.service.processor;
22
23 import com.google.common.collect.ImmutableList;
24 import org.junit.Test;
25 import org.openecomp.dcae.apod.analytics.common.BaseAnalyticsCommonUnitTest;
26 import org.openecomp.dcae.apod.analytics.common.exception.MessageProcessingException;
27
28 import static org.hamcrest.CoreMatchers.is;
29 import static org.junit.Assert.assertNull;
30 import static org.junit.Assert.assertThat;
31
32
33 /**
34  *
35  * @author Rajiv Singla . Creation Date: 11/8/2016.
36  */
37 public class GenericMessageChainProcessorTest extends BaseAnalyticsCommonUnitTest {
38
39
40     @Test
41     public void testProcessChainWhenProcessChainHasNoEarlyTermination() throws Exception {
42
43         final TestMessageProcessor1 testMessageProcessor1 = getTestMessageProcessor1();
44         final TestMessageProcessor2 testMessageProcessor2 = getTestMessageProcessor2();
45         final ImmutableList<? extends MessageProcessor<TestProcessorContext>> testMessageChain =
46                 ImmutableList.of(testMessageProcessor1, testMessageProcessor2);
47
48         final TestProcessorContext testProcessorContext = new TestProcessorContext("Hello", true);
49
50         final GenericMessageChainProcessor<TestProcessorContext> genericMessageChainProcessor =
51                 new GenericMessageChainProcessor<>(testMessageChain, testProcessorContext);
52
53         final TestProcessorContext finalProcessorContext = genericMessageChainProcessor.processChain();
54
55         final String result = finalProcessorContext.getResult();
56         assertThat("Final Result must be Hello World! Again", result, is("Hello World! Again"));
57         assertThat("TestProcessor1 state is correct", testMessageProcessor1.getProcessingState(),
58                 is(ProcessingState.PROCESSING_FINISHED_SUCCESSFULLY));
59         assertThat("TestProcessor2 state is correct", testMessageProcessor2.getProcessingState(),
60                 is(ProcessingState.PROCESSING_FINISHED_SUCCESSFULLY));
61     }
62
63
64     @Test
65     public void testProcessChainWhenProcessChainEarlyTermination() throws Exception {
66
67         final TestEarlyTerminatingProcessor testEarlyTerminatingProcessor = getTestEarlyTerminationProcessor();
68         final ImmutableList<? extends MessageProcessor<TestProcessorContext>> testMessageChain =
69                 ImmutableList.of(testEarlyTerminatingProcessor, getTestMessageProcessor2());
70         final TestProcessorContext testProcessorContext = new TestProcessorContext("Hello", true);
71
72         final GenericMessageChainProcessor<TestProcessorContext> genericMessageChainProcessor =
73                 new GenericMessageChainProcessor<>(testMessageChain, testProcessorContext);
74
75         final TestProcessorContext finalProcessorContext = genericMessageChainProcessor.processChain();
76         final String result = finalProcessorContext.getResult();
77         assertNull("Final Result must be null", result);
78         assertThat("TestEarlyTerminatingProcessor state is correct",
79                 testEarlyTerminatingProcessor.getProcessingState(), is(ProcessingState.PROCESSING_TERMINATED_EARLY));
80     }
81
82     @Test(expected = MessageProcessingException.class)
83     public void testProcessChainWhenIncomingMessageContextIsNull() throws Exception {
84
85         final TestEarlyTerminatingProcessor testEarlyTerminatingProcessor = getTestEarlyTerminationProcessor();
86         final ImmutableList<? extends MessageProcessor<TestProcessorContext>> testMessageChain =
87                 ImmutableList.of(testEarlyTerminatingProcessor, getTestMessageProcessor2());
88         final TestProcessorContext testProcessorContext = null;
89
90         final GenericMessageChainProcessor<TestProcessorContext> genericMessageChainProcessor =
91                 new GenericMessageChainProcessor<>(testMessageChain, testProcessorContext);
92
93        genericMessageChainProcessor.processChain();
94     }
95
96 }