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.verify;
30 import com.google.gson.JsonSyntaxException;
31 import io.netty.handler.timeout.ReadTimeoutException;
32 import org.junit.jupiter.api.Test;
33 import org.mockito.ArgumentCaptor;
34 import org.onap.dcaegen2.services.sdk.model.streams.dmaap.ImmutableMessageRouterSource;
35 import org.onap.dcaegen2.services.sdk.model.streams.dmaap.MessageRouterSource;
36 import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.*;
37 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.api.MessageRouterSubscriber;
38 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.error.ClientErrorReasonPresenter;
39 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.model.ImmutableMessageRouterSubscribeRequest;
40 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.model.MessageRouterSubscribeRequest;
41 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.model.MessageRouterSubscribeResponse;
42 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.model.config.MessageRouterSubscriberConfig;
43 import reactor.core.publisher.Mono;
46 * @author <a href="mailto:piotr.jaszczyk@nokia.com">Piotr Jaszczyk</a>
49 class MessageRouterSubscriberImplTest {
51 private static final String ERROR_MESSAGE = "Something went wrong";
52 private final RxHttpClient httpClient = mock(RxHttpClient.class);
53 private final ClientErrorReasonPresenter clientErrorReasonPresenter = mock(ClientErrorReasonPresenter.class);
54 private final MessageRouterSubscriberConfig clientConfig = MessageRouterSubscriberConfig.createDefault();
55 private final MessageRouterSubscriber
56 cut = new MessageRouterSubscriberImpl(httpClient, clientConfig.gsonInstance(),clientErrorReasonPresenter);
58 private final ArgumentCaptor<HttpRequest> httpRequestArgumentCaptor = ArgumentCaptor.forClass(HttpRequest.class);
59 private final MessageRouterSource sourceDefinition = ImmutableMessageRouterSource.builder()
61 .topicUrl("https://dmaap-mr/TOPIC")
63 private final MessageRouterSubscribeRequest mrRequest = ImmutableMessageRouterSubscribeRequest.builder()
64 .consumerGroup("SAMPLE-GROUP")
65 .sourceDefinition(sourceDefinition)
67 private final HttpResponse httpResponse = ImmutableHttpResponse.builder()
70 .url(sourceDefinition.topicUrl())
71 .rawBody("[]".getBytes())
73 private final HttpResponse httpResponseWithWrongStatusCode = ImmutableHttpResponse.builder()
75 .statusReason("Something braked")
76 .url(sourceDefinition.topicUrl())
77 .rawBody("[]".getBytes())
79 private final HttpResponse httpResponseWithIncorrectJson = ImmutableHttpResponse.builder()
82 .url(sourceDefinition.topicUrl())
83 .rawBody("{}".getBytes())
87 void getWithProperRequest_shouldReturnCorrectResponse() {
89 given(httpClient.call(any(HttpRequest.class))).willReturn(Mono.just(httpResponse));
92 final Mono<MessageRouterSubscribeResponse> responses = cut
94 final MessageRouterSubscribeResponse response = responses.block();
97 assertThat(response.successful()).isTrue();
98 assertThat(response.failReason()).isNull();
99 assertThat(response.hasElements()).isFalse();
102 verify(httpClient).call(httpRequestArgumentCaptor.capture());
103 final HttpRequest httpRequest = httpRequestArgumentCaptor.getValue();
104 assertThat(httpRequest.method()).isEqualTo(HttpMethod.GET);
105 assertThat(httpRequest.url()).isEqualTo(String.format("%s/%s/%s", sourceDefinition.topicUrl(),
106 mrRequest.consumerGroup(), mrRequest.consumerId()));
107 assertThat(httpRequest.body()).isNull();
111 void getWithProperRequestButNotSuccessfulHttpRequest_shouldReturnMonoWithFailReason() {
113 given(httpClient.call(any(HttpRequest.class))).willReturn(Mono.just(httpResponseWithWrongStatusCode));
116 final Mono<MessageRouterSubscribeResponse> responses = cut
118 final MessageRouterSubscribeResponse response = responses.block();
121 assertThat(response.failed()).isTrue();
122 assertThat(response.failReason()).
123 isEqualTo(String.format("%d %s%n%s", httpResponseWithWrongStatusCode.statusCode(),
124 httpResponseWithWrongStatusCode.statusReason(),
125 httpResponseWithWrongStatusCode.bodyAsString()));
129 void getWithImproperRawBody_shouldThrowNPE() {
131 given(httpClient.call(any(HttpRequest.class))).willReturn(Mono.just(httpResponseWithIncorrectJson));
135 assertThatExceptionOfType(JsonSyntaxException.class).isThrownBy(() -> cut.get(mrRequest).block());
139 void getWithProperRequest_shouldReturnTimeoutError() {
142 given(clientErrorReasonPresenter.present(any()))
143 .willReturn(ERROR_MESSAGE);
144 given(httpClient.call(any(HttpRequest.class)))
145 .willReturn(Mono.error(ReadTimeoutException.INSTANCE));
148 final Mono<MessageRouterSubscribeResponse> responses = cut
150 final MessageRouterSubscribeResponse response = responses.block();
153 assertThat(response.failed()).isTrue();
154 assertThat(response.failReason()).isEqualTo(ERROR_MESSAGE);
155 assertThat(response.hasElements()).isFalse();
158 verify(httpClient).call(httpRequestArgumentCaptor.capture());
159 final HttpRequest httpRequest = httpRequestArgumentCaptor.getValue();
160 assertThat(httpRequest.method()).isEqualTo(HttpMethod.GET);
161 assertThat(httpRequest.url()).isEqualTo(String.format("%s/%s/%s", sourceDefinition.topicUrl(),
162 mrRequest.consumerGroup(), mrRequest.consumerId()));
163 assertThat(httpRequest.body()).isNull();