696140552f792ddfba3343061e244542e6073bd8
[dcaegen2/analytics/tca.git] / dcae-analytics-cdap-tca / src / test / java / org / openecomp / dcae / apod / analytics / cdap / tca / flowlet / TCAVESMessageRouterFlowletTest.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.cdap.tca.flowlet;
22
23 import co.cask.cdap.api.flow.flowlet.OutputEmitter;
24 import co.cask.cdap.api.flow.flowlet.StreamEvent;
25 import com.google.common.base.Charsets;
26 import org.junit.Test;
27 import org.mockito.Mockito;
28 import org.openecomp.dcae.apod.analytics.cdap.common.CDAPComponentsConstants;
29 import org.openecomp.dcae.apod.analytics.cdap.tca.BaseAnalyticsCDAPTCAUnitTest;
30 import org.openecomp.dcae.apod.analytics.common.AnalyticsConstants;
31
32 import java.nio.ByteBuffer;
33
34 import static org.mockito.ArgumentMatchers.eq;
35 import static org.mockito.Mockito.doNothing;
36 import static org.mockito.Mockito.times;
37 import static org.mockito.Mockito.verify;
38 import static org.mockito.Mockito.when;
39
40 /**
41  * @author Rajiv Singla . Creation Date: 12/19/2016.
42  */
43 public class TCAVESMessageRouterFlowletTest extends BaseAnalyticsCDAPTCAUnitTest {
44
45     private static final String TEST_MESSAGE = "test message";
46     private final OutputEmitter mockOutputEmitter = Mockito.mock(OutputEmitter.class);
47
48     private class TCATestVESMessageRouterFlowlet extends TCAVESMessageRouterFlowlet {
49
50         @SuppressWarnings("unchecked")
51         public TCATestVESMessageRouterFlowlet() {
52             this.vesMessageEmitter = mockOutputEmitter;
53             doNothing().when(mockOutputEmitter).emit(eq(TEST_MESSAGE),
54                     eq(AnalyticsConstants.TCA_VES_MESSAGE_ROUTER_PARTITION_KEY),
55                     eq(TEST_MESSAGE.hashCode()));
56         }
57     }
58
59     @Test
60     public void testConfigure() throws Exception {
61         final TCAVESMessageRouterFlowlet tcavesMessageRouterFlowlet = new TCAVESMessageRouterFlowlet();
62         assertFlowletNameAndDescription(CDAPComponentsConstants.TCA_FIXED_VES_MESSAGE_ROUTER_NAME_FLOWLET,
63                 CDAPComponentsConstants.TCA_FIXED_VES_MESSAGE_ROUTER_DESCRIPTION_FLOWLET, tcavesMessageRouterFlowlet);
64     }
65
66     @SuppressWarnings("unchecked")
67     @Test
68     public void routeVESMessage() throws Exception {
69         final TCATestVESMessageRouterFlowlet tcavesMessageRouterFlowlet = new TCATestVESMessageRouterFlowlet();
70         final StreamEvent mockStreamEvent = Mockito.mock(StreamEvent.class);
71         final ByteBuffer testMessage = Charsets.UTF_8.encode(TEST_MESSAGE);
72         when(mockStreamEvent.getBody()).thenReturn(testMessage);
73         tcavesMessageRouterFlowlet.routeVESMessage(mockStreamEvent);
74         verify(mockOutputEmitter,
75                 times(1)).emit(eq(TEST_MESSAGE),
76                 eq(AnalyticsConstants.TCA_VES_MESSAGE_ROUTER_PARTITION_KEY),
77                 eq(TEST_MESSAGE.hashCode()));
78     }
79
80 }