74b21ad60422e2b16d25e4ad27f3da1e9f8405ea
[dcaegen2/services/sdk.git] /
1 /*
2  * ============LICENSE_START====================================
3  * DCAEGEN2-SERVICES-SDK
4  * =========================================================
5  * Copyright (C) 2019-2021 Nokia. 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.onap.dcaegen2.services.sdk.rest.services.dmaap.client.impl;
22
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
25 import static org.mockito.ArgumentMatchers.any;
26 import static org.mockito.BDDMockito.given;
27 import static org.mockito.Mockito.mock;
28 import static org.mockito.Mockito.times;
29 import static org.mockito.Mockito.verify;
30
31 import com.google.gson.JsonSyntaxException;
32 import io.netty.handler.timeout.ReadTimeoutException;
33 import org.junit.jupiter.api.Test;
34 import org.mockito.ArgumentCaptor;
35 import org.onap.dcaegen2.services.sdk.model.streams.dmaap.ImmutableMessageRouterSource;
36 import org.onap.dcaegen2.services.sdk.model.streams.dmaap.MessageRouterSource;
37 import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.*;
38 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.api.MessageRouterSubscriber;
39 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.error.ClientErrorReasonPresenter;
40 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.model.ImmutableMessageRouterSubscribeRequest;
41 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.model.MessageRouterSubscribeRequest;
42 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.model.MessageRouterSubscribeResponse;
43 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.model.config.MessageRouterSubscriberConfig;
44 import reactor.core.publisher.Mono;
45
46 import java.net.ConnectException;
47
48 /**
49  * @author <a href="mailto:piotr.jaszczyk@nokia.com">Piotr Jaszczyk</a>
50  * @since May 2019
51  */
52 class MessageRouterSubscriberImplTest {
53
54     private static final String ERROR_MESSAGE = "Something went wrong";
55     private final RxHttpClient httpClient = mock(RxHttpClient.class);
56     private final ClientErrorReasonPresenter clientErrorReasonPresenter = mock(ClientErrorReasonPresenter.class);
57     private final MessageRouterSubscriberConfig clientConfig = MessageRouterSubscriberConfig.createDefault();
58     private final MessageRouterSubscriber
59             cut = new MessageRouterSubscriberImpl(httpClient, clientConfig.gsonInstance(),clientErrorReasonPresenter);
60
61     private final ArgumentCaptor<HttpRequest> httpRequestArgumentCaptor = ArgumentCaptor.forClass(HttpRequest.class);
62     private final MessageRouterSource sourceDefinition = ImmutableMessageRouterSource.builder()
63             .name("sample topic")
64             .topicUrl("https://dmaap-mr/TOPIC")
65             .build();
66     private final MessageRouterSubscribeRequest mrRequest = ImmutableMessageRouterSubscribeRequest.builder()
67             .consumerGroup("SAMPLE-GROUP")
68             .sourceDefinition(sourceDefinition)
69             .build();
70     private final HttpResponse httpResponse = ImmutableHttpResponse.builder()
71             .statusCode(200)
72             .statusReason("OK")
73             .url(sourceDefinition.topicUrl())
74             .rawBody("[]".getBytes())
75             .build();
76     private final HttpResponse retryableHttpResponse = ImmutableHttpResponse.builder()
77             .statusCode(500)
78             .statusReason("Something braked")
79             .url(sourceDefinition.topicUrl())
80             .rawBody("[]".getBytes())
81             .build();
82     private final HttpResponse httpResponseWithWrongStatusCode = ImmutableHttpResponse.builder()
83             .statusCode(301)
84             .statusReason("Something braked")
85             .url(sourceDefinition.topicUrl())
86             .rawBody("[]".getBytes())
87             .build();
88     private final HttpResponse httpResponseWithIncorrectJson = ImmutableHttpResponse.builder()
89             .statusCode(200)
90             .statusReason("OK")
91             .url(sourceDefinition.topicUrl())
92             .rawBody("{}".getBytes())
93             .build();
94
95     @Test
96     void getWithProperRequest_shouldReturnCorrectResponse() {
97         // given
98         given(httpClient.call(any(HttpRequest.class))).willReturn(Mono.just(httpResponse));
99
100         // when
101         final Mono<MessageRouterSubscribeResponse> responses = cut
102                 .get(mrRequest);
103         final MessageRouterSubscribeResponse response = responses.block();
104
105         // then
106         assertThat(response.successful()).isTrue();
107         assertThat(response.failReason()).isNull();
108         assertThat(response.hasElements()).isFalse();
109
110
111         verify(httpClient).call(httpRequestArgumentCaptor.capture());
112         final HttpRequest httpRequest = httpRequestArgumentCaptor.getValue();
113         assertThat(httpRequest.method()).isEqualTo(HttpMethod.GET);
114         assertThat(httpRequest.url()).isEqualTo(String.format("%s/%s/%s", sourceDefinition.topicUrl(),
115                 mrRequest.consumerGroup(), mrRequest.consumerId()));
116         assertThat(httpRequest.body()).isNull();
117     }
118
119     @Test
120     void getWithProperRequestButNotSuccessfulHttpRequest_shouldReturnMonoWithFailReason() {
121         // given
122         given(httpClient.call(any(HttpRequest.class))).willReturn(Mono.just(httpResponseWithWrongStatusCode));
123
124         // when
125         final Mono<MessageRouterSubscribeResponse> responses = cut
126                 .get(mrRequest);
127         final MessageRouterSubscribeResponse response = responses.block();
128
129         // then
130         assertThat(response.failed()).isTrue();
131         assertThat(response.failReason()).
132                 isEqualTo(String.format("%d %s%n%s", httpResponseWithWrongStatusCode.statusCode(),
133                         httpResponseWithWrongStatusCode.statusReason(),
134                         httpResponseWithWrongStatusCode.bodyAsString()));
135     }
136
137     @Test
138     void getWithImproperRawBody_shouldThrowNPE() {
139         // given
140         given(httpClient.call(any(HttpRequest.class))).willReturn(Mono.just(httpResponseWithIncorrectJson));
141
142         // when
143         // then
144         assertThatExceptionOfType(JsonSyntaxException.class).isThrownBy(() -> cut.get(mrRequest).block());
145     }
146
147     @Test
148     void getWithProperRequest_shouldReturnTimeoutError() {
149
150         // given
151         given(clientErrorReasonPresenter.present(any()))
152                 .willReturn(ERROR_MESSAGE);
153         given(httpClient.call(any(HttpRequest.class)))
154                 .willReturn(Mono.error(ReadTimeoutException.INSTANCE));
155
156         // when
157         final Mono<MessageRouterSubscribeResponse> responses = cut
158                 .get(mrRequest);
159         final MessageRouterSubscribeResponse response = responses.block();
160
161         // then
162         assertThat(response.failed()).isTrue();
163         assertThat(response.failReason()).isEqualTo(ERROR_MESSAGE);
164         assertThat(response.hasElements()).isFalse();
165
166         verify(clientErrorReasonPresenter, times(1)).present(any());
167         verify(httpClient).call(httpRequestArgumentCaptor.capture());
168         final HttpRequest httpRequest = httpRequestArgumentCaptor.getValue();
169         assertThat(httpRequest.method()).isEqualTo(HttpMethod.GET);
170         assertThat(httpRequest.url()).isEqualTo(String.format("%s/%s/%s", sourceDefinition.topicUrl(),
171                 mrRequest.consumerGroup(), mrRequest.consumerId()));
172         assertThat(httpRequest.body()).isNull();
173     }
174
175     @Test
176     void getWithProperRequest_shouldReturnConnectionException() {
177         given(clientErrorReasonPresenter.present(any()))
178                 .willReturn(ERROR_MESSAGE);
179         given(httpClient.call(any(HttpRequest.class)))
180                 .willReturn(Mono.error(new ConnectException()));
181
182         // when
183         final Mono<MessageRouterSubscribeResponse> responses = cut
184                 .get(mrRequest);
185         final MessageRouterSubscribeResponse response = responses.block();
186
187         assertThat(response.failed()).isTrue();
188         assertThat(response.failReason()).isEqualTo(ERROR_MESSAGE);
189         assertThat(response.hasElements()).isFalse();
190
191         verify(clientErrorReasonPresenter, times(1)).present(any());
192         verify(httpClient).call(httpRequestArgumentCaptor.capture());
193         final HttpRequest httpRequest = httpRequestArgumentCaptor.getValue();
194         assertThat(httpRequest.method()).isEqualTo(HttpMethod.GET);
195         assertThat(httpRequest.url()).isEqualTo(String.format("%s/%s/%s", sourceDefinition.topicUrl(),
196                 mrRequest.consumerGroup(), mrRequest.consumerId()));
197         assertThat(httpRequest.body()).isNull();
198     }
199
200     @Test
201     void getWithProperRequest_shouldReturnCertainFailedResponse() {
202         given(httpClient.call(any(HttpRequest.class)))
203                 .willReturn(Mono.just(retryableHttpResponse));
204
205         // when
206         final Mono<MessageRouterSubscribeResponse> responses = cut
207                 .get(mrRequest);
208         final MessageRouterSubscribeResponse response = responses.block();
209
210         assertThat(response.failed()).isTrue();
211         assertThat(response.failReason()).startsWith("500 Something braked");
212         assertThat(response.hasElements()).isFalse();
213
214         verify(httpClient).call(httpRequestArgumentCaptor.capture());
215         final HttpRequest httpRequest = httpRequestArgumentCaptor.getValue();
216         assertThat(httpRequest.method()).isEqualTo(HttpMethod.GET);
217         assertThat(httpRequest.url()).isEqualTo(String.format("%s/%s/%s", sourceDefinition.topicUrl(),
218                 mrRequest.consumerGroup(), mrRequest.consumerId()));
219         assertThat(httpRequest.body()).isNull();
220     }
221 }