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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=====================================
21 package org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.impl;
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;
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;
46 import java.net.ConnectException;
49 * @author <a href="mailto:piotr.jaszczyk@nokia.com">Piotr Jaszczyk</a>
52 class MessageRouterSubscriberImplTest {
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);
61 private final ArgumentCaptor<HttpRequest> httpRequestArgumentCaptor = ArgumentCaptor.forClass(HttpRequest.class);
62 private final MessageRouterSource sourceDefinition = ImmutableMessageRouterSource.builder()
64 .topicUrl("https://dmaap-mr/TOPIC")
66 private final MessageRouterSubscribeRequest mrRequest = ImmutableMessageRouterSubscribeRequest.builder()
67 .consumerGroup("SAMPLE-GROUP")
68 .sourceDefinition(sourceDefinition)
70 private final HttpResponse httpResponse = ImmutableHttpResponse.builder()
73 .url(sourceDefinition.topicUrl())
74 .rawBody("[]".getBytes())
76 private final HttpResponse retryableHttpResponse = ImmutableHttpResponse.builder()
78 .statusReason("Something braked")
79 .url(sourceDefinition.topicUrl())
80 .rawBody("[]".getBytes())
82 private final HttpResponse httpResponseWithWrongStatusCode = ImmutableHttpResponse.builder()
84 .statusReason("Something braked")
85 .url(sourceDefinition.topicUrl())
86 .rawBody("[]".getBytes())
88 private final HttpResponse httpResponseWithIncorrectJson = ImmutableHttpResponse.builder()
91 .url(sourceDefinition.topicUrl())
92 .rawBody("{}".getBytes())
96 void getWithProperRequest_shouldReturnCorrectResponse() {
98 given(httpClient.call(any(HttpRequest.class))).willReturn(Mono.just(httpResponse));
101 final Mono<MessageRouterSubscribeResponse> responses = cut
103 final MessageRouterSubscribeResponse response = responses.block();
106 assertThat(response.successful()).isTrue();
107 assertThat(response.failReason()).isNull();
108 assertThat(response.hasElements()).isFalse();
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();
120 void getWithProperRequestButNotSuccessfulHttpRequest_shouldReturnMonoWithFailReason() {
122 given(httpClient.call(any(HttpRequest.class))).willReturn(Mono.just(httpResponseWithWrongStatusCode));
125 final Mono<MessageRouterSubscribeResponse> responses = cut
127 final MessageRouterSubscribeResponse response = responses.block();
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()));
138 void getWithImproperRawBody_shouldThrowNPE() {
140 given(httpClient.call(any(HttpRequest.class))).willReturn(Mono.just(httpResponseWithIncorrectJson));
144 assertThatExceptionOfType(JsonSyntaxException.class).isThrownBy(() -> cut.get(mrRequest).block());
148 void getWithProperRequest_shouldReturnTimeoutError() {
151 given(clientErrorReasonPresenter.present(any()))
152 .willReturn(ERROR_MESSAGE);
153 given(httpClient.call(any(HttpRequest.class)))
154 .willReturn(Mono.error(ReadTimeoutException.INSTANCE));
157 final Mono<MessageRouterSubscribeResponse> responses = cut
159 final MessageRouterSubscribeResponse response = responses.block();
162 assertThat(response.failed()).isTrue();
163 assertThat(response.failReason()).isEqualTo(ERROR_MESSAGE);
164 assertThat(response.hasElements()).isFalse();
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();
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()));
183 final Mono<MessageRouterSubscribeResponse> responses = cut
185 final MessageRouterSubscribeResponse response = responses.block();
187 assertThat(response.failed()).isTrue();
188 assertThat(response.failReason()).isEqualTo(ERROR_MESSAGE);
189 assertThat(response.hasElements()).isFalse();
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();
201 void getWithProperRequest_shouldReturnCertainFailedResponse() {
202 given(httpClient.call(any(HttpRequest.class)))
203 .willReturn(Mono.just(retryableHttpResponse));
206 final Mono<MessageRouterSubscribeResponse> responses = cut
208 final MessageRouterSubscribeResponse response = responses.block();
210 assertThat(response.failed()).isTrue();
211 assertThat(response.failReason()).startsWith("500 Something braked");
212 assertThat(response.hasElements()).isFalse();
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();