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 io.vavr.collection.HashMultimap;
34 import org.junit.jupiter.api.Test;
35 import org.mockito.ArgumentCaptor;
36 import org.onap.dcaegen2.services.sdk.model.streams.dmaap.ImmutableMessageRouterSource;
37 import org.onap.dcaegen2.services.sdk.model.streams.dmaap.MessageRouterSource;
38 import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.*;
39 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.api.MessageRouterSubscriber;
40 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.error.ClientErrorReasonPresenter;
41 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.model.ImmutableMessageRouterSubscribeRequest;
42 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.model.MessageRouterSubscribeRequest;
43 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.model.MessageRouterSubscribeResponse;
44 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.model.config.MessageRouterSubscriberConfig;
45 import reactor.core.publisher.Mono;
47 import java.net.ConnectException;
50 * @author <a href="mailto:piotr.jaszczyk@nokia.com">Piotr Jaszczyk</a>
53 class MessageRouterSubscriberImplTest {
55 private static final String ERROR_MESSAGE = "Something went wrong";
56 private final RxHttpClient httpClient = mock(RxHttpClient.class);
57 private final ClientErrorReasonPresenter clientErrorReasonPresenter = mock(ClientErrorReasonPresenter.class);
58 private final MessageRouterSubscriberConfig clientConfig = MessageRouterSubscriberConfig.createDefault();
59 private final MessageRouterSubscriber
60 cut = new MessageRouterSubscriberImpl(httpClient, clientConfig.gsonInstance(),clientErrorReasonPresenter);
62 private final ArgumentCaptor<HttpRequest> httpRequestArgumentCaptor = ArgumentCaptor.forClass(HttpRequest.class);
63 private final MessageRouterSource sourceDefinition = ImmutableMessageRouterSource.builder()
65 .topicUrl("https://dmaap-mr/TOPIC")
67 private final MessageRouterSubscribeRequest mrRequest = ImmutableMessageRouterSubscribeRequest.builder()
68 .consumerGroup("SAMPLE-GROUP")
69 .sourceDefinition(sourceDefinition)
71 private final HttpResponse httpResponse = ImmutableHttpResponse.builder()
74 .url(sourceDefinition.topicUrl())
75 .rawBody("[]".getBytes())
76 .headers(HashMultimap.withSeq().empty())
78 private final HttpResponse retryableHttpResponse = ImmutableHttpResponse.builder()
80 .statusReason("Something braked")
81 .url(sourceDefinition.topicUrl())
82 .rawBody("[]".getBytes())
83 .headers(HashMultimap.withSeq().empty())
85 private final HttpResponse httpResponseWithWrongStatusCode = ImmutableHttpResponse.builder()
87 .statusReason("Something braked")
88 .url(sourceDefinition.topicUrl())
89 .rawBody("[]".getBytes())
90 .headers(HashMultimap.withSeq().empty())
92 private final HttpResponse httpResponseWithIncorrectJson = ImmutableHttpResponse.builder()
95 .url(sourceDefinition.topicUrl())
96 .rawBody("{}".getBytes())
97 .headers(HashMultimap.withSeq().empty())
101 void getWithProperRequest_shouldReturnCorrectResponse() {
103 given(httpClient.call(any(HttpRequest.class))).willReturn(Mono.just(httpResponse));
106 final Mono<MessageRouterSubscribeResponse> responses = cut
108 final MessageRouterSubscribeResponse response = responses.block();
111 assertThat(response.successful()).isTrue();
112 assertThat(response.failReason()).isNull();
113 assertThat(response.hasElements()).isFalse();
116 verify(httpClient).call(httpRequestArgumentCaptor.capture());
117 final HttpRequest httpRequest = httpRequestArgumentCaptor.getValue();
118 assertThat(httpRequest.method()).isEqualTo(HttpMethod.GET);
119 assertThat(httpRequest.url()).isEqualTo(String.format("%s/%s/%s", sourceDefinition.topicUrl(),
120 mrRequest.consumerGroup(), mrRequest.consumerId()));
121 assertThat(httpRequest.body()).isNull();
125 void getWithProperRequestButNotSuccessfulHttpRequest_shouldReturnMonoWithFailReason() {
127 given(httpClient.call(any(HttpRequest.class))).willReturn(Mono.just(httpResponseWithWrongStatusCode));
130 final Mono<MessageRouterSubscribeResponse> responses = cut
132 final MessageRouterSubscribeResponse response = responses.block();
135 assertThat(response.failed()).isTrue();
136 assertThat(response.failReason()).
137 isEqualTo(String.format("%d %s%n%s", httpResponseWithWrongStatusCode.statusCode(),
138 httpResponseWithWrongStatusCode.statusReason(),
139 httpResponseWithWrongStatusCode.bodyAsString()));
143 void getWithImproperRawBody_shouldThrowNPE() {
145 given(httpClient.call(any(HttpRequest.class))).willReturn(Mono.just(httpResponseWithIncorrectJson));
149 assertThatExceptionOfType(JsonSyntaxException.class).isThrownBy(() -> cut.get(mrRequest).block());
153 void getWithProperRequest_shouldReturnTimeoutError() {
156 given(clientErrorReasonPresenter.present(any()))
157 .willReturn(ERROR_MESSAGE);
158 given(httpClient.call(any(HttpRequest.class)))
159 .willReturn(Mono.error(ReadTimeoutException.INSTANCE));
162 final Mono<MessageRouterSubscribeResponse> responses = cut
164 final MessageRouterSubscribeResponse response = responses.block();
167 assertThat(response.failed()).isTrue();
168 assertThat(response.failReason()).isEqualTo(ERROR_MESSAGE);
169 assertThat(response.hasElements()).isFalse();
171 verify(clientErrorReasonPresenter, times(1)).present(any());
172 verify(httpClient).call(httpRequestArgumentCaptor.capture());
173 final HttpRequest httpRequest = httpRequestArgumentCaptor.getValue();
174 assertThat(httpRequest.method()).isEqualTo(HttpMethod.GET);
175 assertThat(httpRequest.url()).isEqualTo(String.format("%s/%s/%s", sourceDefinition.topicUrl(),
176 mrRequest.consumerGroup(), mrRequest.consumerId()));
177 assertThat(httpRequest.body()).isNull();
181 void getWithProperRequest_shouldReturnConnectionException() {
182 given(clientErrorReasonPresenter.present(any()))
183 .willReturn(ERROR_MESSAGE);
184 given(httpClient.call(any(HttpRequest.class)))
185 .willReturn(Mono.error(new ConnectException()));
188 final Mono<MessageRouterSubscribeResponse> responses = cut
190 final MessageRouterSubscribeResponse response = responses.block();
192 assertThat(response.failed()).isTrue();
193 assertThat(response.failReason()).isEqualTo(ERROR_MESSAGE);
194 assertThat(response.hasElements()).isFalse();
196 verify(clientErrorReasonPresenter, times(1)).present(any());
197 verify(httpClient).call(httpRequestArgumentCaptor.capture());
198 final HttpRequest httpRequest = httpRequestArgumentCaptor.getValue();
199 assertThat(httpRequest.method()).isEqualTo(HttpMethod.GET);
200 assertThat(httpRequest.url()).isEqualTo(String.format("%s/%s/%s", sourceDefinition.topicUrl(),
201 mrRequest.consumerGroup(), mrRequest.consumerId()));
202 assertThat(httpRequest.body()).isNull();
206 void getWithProperRequest_shouldReturnCertainFailedResponse() {
207 given(httpClient.call(any(HttpRequest.class)))
208 .willReturn(Mono.just(retryableHttpResponse));
211 final Mono<MessageRouterSubscribeResponse> responses = cut
213 final MessageRouterSubscribeResponse response = responses.block();
215 assertThat(response.failed()).isTrue();
216 assertThat(response.failReason()).startsWith("500 Something braked");
217 assertThat(response.hasElements()).isFalse();
219 verify(httpClient).call(httpRequestArgumentCaptor.capture());
220 final HttpRequest httpRequest = httpRequestArgumentCaptor.getValue();
221 assertThat(httpRequest.method()).isEqualTo(HttpMethod.GET);
222 assertThat(httpRequest.url()).isEqualTo(String.format("%s/%s/%s", sourceDefinition.topicUrl(),
223 mrRequest.consumerGroup(), mrRequest.consumerId()));
224 assertThat(httpRequest.body()).isNull();