TCA: Support for VES/A&AI enrichment
[dcaegen2/analytics/tca.git] / dcae-analytics-dmaap / src / test / java / org / openecomp / dcae / apod / analytics / dmaap / service / subscriber / DMaaPMRSubscriberImplTest.java
1 /*\r
2  * ===============================LICENSE_START======================================\r
3  *  dcae-analytics\r
4  * ================================================================================\r
5  *    Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
6  * ================================================================================\r
7  *  Licensed under the Apache License, Version 2.0 (the "License");\r
8  *  you may not use this file except in compliance with the License.\r
9  *   You may obtain a copy of the License at\r
10  *\r
11  *          http://www.apache.org/licenses/LICENSE-2.0\r
12  *\r
13  *  Unless required by applicable law or agreed to in writing, software\r
14  *  distributed under the License is distributed on an "AS IS" BASIS,\r
15  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  *  See the License for the specific language governing permissions and\r
17  *  limitations under the License.\r
18  *  ============================LICENSE_END===========================================\r
19  */\r
20 \r
21 package org.openecomp.dcae.apod.analytics.dmaap.service.subscriber;\r
22 \r
23 import com.jayway.jsonassert.impl.matcher.IsCollectionWithSize;\r
24 import org.apache.commons.lang3.tuple.ImmutablePair;\r
25 import org.apache.http.client.ResponseHandler;\r
26 import org.apache.http.client.methods.HttpUriRequest;\r
27 import org.apache.http.impl.client.CloseableHttpClient;\r
28 import org.junit.After;\r
29 import org.junit.Before;\r
30 import org.junit.Rule;\r
31 import org.junit.Test;\r
32 import org.junit.rules.ExpectedException;\r
33 import org.junit.runner.RunWith;\r
34 import org.mockito.Mock;\r
35 import org.mockito.Mockito;\r
36 import org.mockito.junit.MockitoJUnitRunner;\r
37 import org.openecomp.dcae.apod.analytics.common.exception.DCAEAnalyticsRuntimeException;\r
38 import org.openecomp.dcae.apod.analytics.dmaap.BaseAnalyticsDMaaPUnitTest;\r
39 import org.openecomp.dcae.apod.analytics.dmaap.domain.config.DMaaPMRSubscriberConfig;\r
40 import org.openecomp.dcae.apod.analytics.dmaap.domain.response.DMaaPMRSubscriberResponse;\r
41 \r
42 import java.io.IOException;\r
43 import java.util.Random;\r
44 import java.util.UUID;\r
45 \r
46 import static org.hamcrest.CoreMatchers.isA;\r
47 import static org.hamcrest.MatcherAssert.assertThat;\r
48 import static org.hamcrest.core.Is.is;\r
49 import static org.mockito.BDDMockito.given;\r
50 \r
51 /**\r
52  * @author Rajiv Singla . Creation Date: 10/21/2016.\r
53  */\r
54 @RunWith(MockitoJUnitRunner.class)\r
55 public class DMaaPMRSubscriberImplTest extends BaseAnalyticsDMaaPUnitTest {\r
56 \r
57     @Mock\r
58     private CloseableHttpClient closeableHttpClient;\r
59 \r
60     private String consumerGroup, consumerId;\r
61 \r
62     @Before\r
63     public void setUp() throws Exception {\r
64         Random random = new Random(10000L);\r
65         consumerGroup = "Test-Consumer-Group" + Long.toString(random.nextLong());\r
66         consumerId = UUID.randomUUID().toString();\r
67     }\r
68 \r
69     @After\r
70     public void tearDown() throws Exception {\r
71 \r
72     }\r
73 \r
74     @Test\r
75     public void testSubscriberSuccessfullyReceiveDmaapMessage() throws Exception {\r
76 \r
77         String testMessages = "[{\"message\":\"I'm Object 1 Message\"}," +\r
78                 "{\"message\":\"I'm Object 2 Message\"}]";\r
79         Mockito.when(\r
80                 closeableHttpClient.execute(Mockito.any(HttpUriRequest.class), Mockito.any(ResponseHandler.class)))\r
81                 .thenReturn(new ImmutablePair<>(200, testMessages));\r
82 \r
83         DMaaPMRSubscriberImpl dmaapMRSubscriberImpl = new DMaaPMRSubscriberImpl(\r
84                 getSubscriberConfig(consumerId, consumerGroup), closeableHttpClient);\r
85         DMaaPMRSubscriberResponse dmaapMRSubscriberResponse = dmaapMRSubscriberImpl.fetchMessages();\r
86         assertThat(dmaapMRSubscriberResponse.getResponseCode(), is(200));\r
87         assertThat(dmaapMRSubscriberResponse.getFetchedMessages(), IsCollectionWithSize.hasSize(2));\r
88     }\r
89 \r
90     @Test\r
91     public void testSubscriberSuccessfullyReceiveDmaapMessageWithNoUsername() throws Exception {\r
92 \r
93         DMaaPMRSubscriberConfig dmaapMRSubscriberConfig = new DMaaPMRSubscriberConfig.Builder(HOST_NAME, TOPIC_NAME)\r
94                 .setPortNumber(PORT_NUMBER)\r
95                 .setProtocol(HTTP_PROTOCOL)\r
96                 .setContentType(CONTENT_TYPE)\r
97                 .setConsumerGroup(consumerGroup != null ? consumerGroup : SUBSCRIBER_CONSUMER_GROUP_NAME)\r
98                 .setConsumerId(consumerId != null ? consumerId : SUBSCRIBER_CONSUMER_ID)\r
99                 .setTimeoutMS(SUBSCRIBER_TIMEOUT_MS)\r
100                 .setMessageLimit(SUBSCRIBER_MESSAGE_LIMIT).build();\r
101 \r
102         String testMessages = "[{\"message\":\"I'm Object 1 Message\"}," +\r
103                 "{\"message\":\"I'm Object 2 Message\"}]";\r
104         Mockito.when(\r
105                 closeableHttpClient.execute(Mockito.any(HttpUriRequest.class), Mockito.any(ResponseHandler.class)))\r
106                 .thenReturn(new ImmutablePair<>(200, testMessages));\r
107 \r
108         DMaaPMRSubscriberImpl dmaapMRSubscriberImpl = new DMaaPMRSubscriberImpl(\r
109                 dmaapMRSubscriberConfig, closeableHttpClient);\r
110         DMaaPMRSubscriberResponse dmaapMRSubscriberResponse = dmaapMRSubscriberImpl.fetchMessages();\r
111         assertThat(dmaapMRSubscriberResponse.getResponseCode(), is(200));\r
112         assertThat(dmaapMRSubscriberResponse.getFetchedMessages(), IsCollectionWithSize.hasSize(2));\r
113     }\r
114 \r
115     @Test\r
116     public void testSubscriberSuccessfullyReceiveNoDmaapMessage() throws Exception {\r
117         Mockito.when(\r
118                 closeableHttpClient.execute(Mockito.any(HttpUriRequest.class), Mockito.any(ResponseHandler.class)))\r
119                 .thenReturn(new ImmutablePair<>(200, null));\r
120 \r
121         DMaaPMRSubscriberImpl dmaapMRSubscriberImpl = new DMaaPMRSubscriberImpl(\r
122                 getSubscriberConfig(consumerId, consumerGroup), closeableHttpClient);\r
123         DMaaPMRSubscriberResponse dmaapMRSubscriberResponse = dmaapMRSubscriberImpl.fetchMessages();\r
124         assertThat(dmaapMRSubscriberResponse.getResponseCode(), is(200));\r
125         assertThat(dmaapMRSubscriberResponse.getFetchedMessages(), IsCollectionWithSize.hasSize(0));\r
126     }\r
127 \r
128     @Test\r
129     public void testSubscriberSuccessfullyReceiveErrorMessage() throws Exception {\r
130         Mockito.when(\r
131                 closeableHttpClient.execute(Mockito.any(HttpUriRequest.class), Mockito.any(ResponseHandler.class)))\r
132                 .thenReturn(new ImmutablePair<>(400, "Bad Request"));\r
133 \r
134         DMaaPMRSubscriberImpl dmaapMRSubscriberImpl = new DMaaPMRSubscriberImpl(\r
135                 getSubscriberConfig(consumerId, consumerGroup), closeableHttpClient);\r
136         DMaaPMRSubscriberResponse dmaapMRSubscriberResponse = dmaapMRSubscriberImpl.fetchMessages();\r
137         assertThat(dmaapMRSubscriberResponse.getResponseCode(), is(400));\r
138         assertThat(dmaapMRSubscriberResponse.getFetchedMessages(), IsCollectionWithSize.hasSize(0));\r
139     }\r
140 \r
141     @Rule\r
142     public ExpectedException httpIOException = ExpectedException.none();\r
143 \r
144     @Test\r
145     public void testSubscriberSuccessfullyReceiveException() throws Exception {\r
146 \r
147         httpIOException.expect(DCAEAnalyticsRuntimeException.class);\r
148         httpIOException.expectCause(isA(IOException.class));\r
149 \r
150         given(closeableHttpClient.execute(\r
151                 Mockito.any(HttpUriRequest.class), Mockito.any(ResponseHandler.class))).willThrow(IOException.class);\r
152 \r
153         DMaaPMRSubscriberImpl dmaapMRSubscriberImpl = new DMaaPMRSubscriberImpl(\r
154                 getSubscriberConfig(consumerId, consumerGroup), closeableHttpClient);\r
155         dmaapMRSubscriberImpl.fetchMessages();\r
156     }\r
157 \r
158 }\r