Add support for ABATED alerts within CDAP TCA
[dcaegen2/analytics/tca.git] / dcae-analytics-dmaap / src / test / java / org / openecomp / dcae / apod / analytics / dmaap / service / subscriber / DMaaPMRSubscriberImplTest.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.dmaap.service.subscriber;
22
23 import com.jayway.jsonassert.impl.matcher.IsCollectionWithSize;
24 import org.apache.commons.lang3.tuple.ImmutablePair;
25 import org.apache.http.client.ResponseHandler;
26 import org.apache.http.client.methods.HttpUriRequest;
27 import org.apache.http.impl.client.CloseableHttpClient;
28 import org.junit.After;
29 import org.junit.Before;
30 import org.junit.Rule;
31 import org.junit.Test;
32 import org.junit.rules.ExpectedException;
33 import org.junit.runner.RunWith;
34 import org.mockito.Mock;
35 import org.mockito.Mockito;
36 import org.mockito.junit.MockitoJUnitRunner;
37 import org.openecomp.dcae.apod.analytics.common.exception.DCAEAnalyticsRuntimeException;
38 import org.openecomp.dcae.apod.analytics.dmaap.BaseAnalyticsDMaaPUnitTest;
39 import org.openecomp.dcae.apod.analytics.dmaap.domain.config.DMaaPMRSubscriberConfig;
40 import org.openecomp.dcae.apod.analytics.dmaap.domain.response.DMaaPMRSubscriberResponse;
41
42 import java.io.IOException;
43 import java.util.Random;
44 import java.util.UUID;
45
46 import static org.hamcrest.CoreMatchers.isA;
47 import static org.hamcrest.MatcherAssert.assertThat;
48 import static org.hamcrest.core.Is.is;
49 import static org.mockito.BDDMockito.given;
50
51 /**
52  * @author Rajiv Singla . Creation Date: 10/21/2016.
53  */
54 @RunWith(MockitoJUnitRunner.class)
55 public class DMaaPMRSubscriberImplTest extends BaseAnalyticsDMaaPUnitTest {
56
57     @Mock
58     private CloseableHttpClient closeableHttpClient;
59
60     private String consumerGroup, consumerId;
61
62     @Before
63     public void setUp() throws Exception {
64         Random random = new Random(10000L);
65         consumerGroup = "Test-Consumer-Group" + Long.toString(random.nextLong());
66         consumerId = UUID.randomUUID().toString();
67     }
68
69     @After
70     public void tearDown() throws Exception {
71
72     }
73
74     @Test
75     public void testSubscriberSuccessfullyReceiveDmaapMessage() throws Exception {
76
77         String testMessages = "[{\"message\":\"I'm Object 1 Message\"}," +
78                 "{\"message\":\"I'm Object 2 Message\"}]";
79         Mockito.when(
80                 closeableHttpClient.execute(Mockito.any(HttpUriRequest.class), Mockito.any(ResponseHandler.class)))
81                 .thenReturn(new ImmutablePair<>(200, testMessages));
82
83         DMaaPMRSubscriberImpl dmaapMRSubscriberImpl = new DMaaPMRSubscriberImpl(
84                 getSubscriberConfig(consumerId, consumerGroup), closeableHttpClient);
85         DMaaPMRSubscriberResponse dmaapMRSubscriberResponse = dmaapMRSubscriberImpl.fetchMessages();
86         assertThat(dmaapMRSubscriberResponse.getResponseCode(), is(200));
87         assertThat(dmaapMRSubscriberResponse.getFetchedMessages(), IsCollectionWithSize.hasSize(2));
88     }
89
90     @Test
91     public void testSubscriberSuccessfullyReceiveDmaapMessageWithNoUsername() throws Exception {
92
93         DMaaPMRSubscriberConfig dmaapMRSubscriberConfig = new DMaaPMRSubscriberConfig.Builder(HOST_NAME, TOPIC_NAME)
94                 .setPortNumber(PORT_NUMBER)
95                 .setProtocol(HTTP_PROTOCOL)
96                 .setContentType(CONTENT_TYPE)
97                 .setConsumerGroup(consumerGroup != null ? consumerGroup : SUBSCRIBER_CONSUMER_GROUP_NAME)
98                 .setConsumerId(consumerId != null ? consumerId : SUBSCRIBER_CONSUMER_ID)
99                 .setTimeoutMS(SUBSCRIBER_TIMEOUT_MS)
100                 .setMessageLimit(SUBSCRIBER_MESSAGE_LIMIT).build();
101
102         String testMessages = "[{\"message\":\"I'm Object 1 Message\"}," +
103                 "{\"message\":\"I'm Object 2 Message\"}]";
104         Mockito.when(
105                 closeableHttpClient.execute(Mockito.any(HttpUriRequest.class), Mockito.any(ResponseHandler.class)))
106                 .thenReturn(new ImmutablePair<>(200, testMessages));
107
108         DMaaPMRSubscriberImpl dmaapMRSubscriberImpl = new DMaaPMRSubscriberImpl(
109                 dmaapMRSubscriberConfig, closeableHttpClient);
110         DMaaPMRSubscriberResponse dmaapMRSubscriberResponse = dmaapMRSubscriberImpl.fetchMessages();
111         assertThat(dmaapMRSubscriberResponse.getResponseCode(), is(200));
112         assertThat(dmaapMRSubscriberResponse.getFetchedMessages(), IsCollectionWithSize.hasSize(2));
113     }
114
115     @Test
116     public void testSubscriberSuccessfullyReceiveNoDmaapMessage() throws Exception {
117         Mockito.when(
118                 closeableHttpClient.execute(Mockito.any(HttpUriRequest.class), Mockito.any(ResponseHandler.class)))
119                 .thenReturn(new ImmutablePair<>(200, null));
120
121         DMaaPMRSubscriberImpl dmaapMRSubscriberImpl = new DMaaPMRSubscriberImpl(
122                 getSubscriberConfig(consumerId, consumerGroup), closeableHttpClient);
123         DMaaPMRSubscriberResponse dmaapMRSubscriberResponse = dmaapMRSubscriberImpl.fetchMessages();
124         assertThat(dmaapMRSubscriberResponse.getResponseCode(), is(200));
125         assertThat(dmaapMRSubscriberResponse.getFetchedMessages(), IsCollectionWithSize.hasSize(0));
126     }
127
128     @Test
129     public void testSubscriberSuccessfullyReceiveErrorMessage() throws Exception {
130         Mockito.when(
131                 closeableHttpClient.execute(Mockito.any(HttpUriRequest.class), Mockito.any(ResponseHandler.class)))
132                 .thenReturn(new ImmutablePair<>(400, "Bad Request"));
133
134         DMaaPMRSubscriberImpl dmaapMRSubscriberImpl = new DMaaPMRSubscriberImpl(
135                 getSubscriberConfig(consumerId, consumerGroup), closeableHttpClient);
136         DMaaPMRSubscriberResponse dmaapMRSubscriberResponse = dmaapMRSubscriberImpl.fetchMessages();
137         assertThat(dmaapMRSubscriberResponse.getResponseCode(), is(400));
138         assertThat(dmaapMRSubscriberResponse.getFetchedMessages(), IsCollectionWithSize.hasSize(0));
139     }
140
141     @Rule
142     public ExpectedException httpIOException = ExpectedException.none();
143
144     @Test
145     public void testSubscriberSuccessfullyReceiveException() throws Exception {
146
147         httpIOException.expect(DCAEAnalyticsRuntimeException.class);
148         httpIOException.expectCause(isA(IOException.class));
149
150         given(closeableHttpClient.execute(
151                 Mockito.any(HttpUriRequest.class), Mockito.any(ResponseHandler.class))).willThrow(IOException.class);
152
153         DMaaPMRSubscriberImpl dmaapMRSubscriberImpl = new DMaaPMRSubscriberImpl(
154                 getSubscriberConfig(consumerId, consumerGroup), closeableHttpClient);
155         dmaapMRSubscriberImpl.fetchMessages();
156     }
157
158 }