006965c20114ddbb76d27e2363b3c5f6d661edef
[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 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;
46
47 import java.net.ConnectException;
48
49 /**
50  * @author <a href="mailto:piotr.jaszczyk@nokia.com">Piotr Jaszczyk</a>
51  * @since May 2019
52  */
53 class MessageRouterSubscriberImplTest {
54
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);
61
62     private final ArgumentCaptor<HttpRequest> httpRequestArgumentCaptor = ArgumentCaptor.forClass(HttpRequest.class);
63     private final MessageRouterSource sourceDefinition = ImmutableMessageRouterSource.builder()
64             .name("sample topic")
65             .topicUrl("https://dmaap-mr/TOPIC")
66             .build();
67     private final MessageRouterSubscribeRequest mrRequest = ImmutableMessageRouterSubscribeRequest.builder()
68             .consumerGroup("SAMPLE-GROUP")
69             .sourceDefinition(sourceDefinition)
70             .build();
71     private final HttpResponse httpResponse = ImmutableHttpResponse.builder()
72             .statusCode(200)
73             .statusReason("OK")
74             .url(sourceDefinition.topicUrl())
75             .rawBody("[]".getBytes())
76             .headers(HashMultimap.withSeq().empty())
77             .build();
78     private final HttpResponse retryableHttpResponse = ImmutableHttpResponse.builder()
79             .statusCode(500)
80             .statusReason("Something braked")
81             .url(sourceDefinition.topicUrl())
82             .rawBody("[]".getBytes())
83             .headers(HashMultimap.withSeq().empty())
84             .build();
85     private final HttpResponse httpResponseWithWrongStatusCode = ImmutableHttpResponse.builder()
86             .statusCode(301)
87             .statusReason("Something braked")
88             .url(sourceDefinition.topicUrl())
89             .rawBody("[]".getBytes())
90             .headers(HashMultimap.withSeq().empty())
91             .build();
92     private final HttpResponse httpResponseWithIncorrectJson = ImmutableHttpResponse.builder()
93             .statusCode(200)
94             .statusReason("OK")
95             .url(sourceDefinition.topicUrl())
96             .rawBody("{}".getBytes())
97             .headers(HashMultimap.withSeq().empty())
98             .build();
99
100     @Test
101     void getWithProperRequest_shouldReturnCorrectResponse() {
102         // given
103         given(httpClient.call(any(HttpRequest.class))).willReturn(Mono.just(httpResponse));
104
105         // when
106         final Mono<MessageRouterSubscribeResponse> responses = cut
107                 .get(mrRequest);
108         final MessageRouterSubscribeResponse response = responses.block();
109
110         // then
111         assertThat(response.successful()).isTrue();
112         assertThat(response.failReason()).isNull();
113         assertThat(response.hasElements()).isFalse();
114
115
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();
122     }
123
124     @Test
125     void getWithProperRequestButNotSuccessfulHttpRequest_shouldReturnMonoWithFailReason() {
126         // given
127         given(httpClient.call(any(HttpRequest.class))).willReturn(Mono.just(httpResponseWithWrongStatusCode));
128
129         // when
130         final Mono<MessageRouterSubscribeResponse> responses = cut
131                 .get(mrRequest);
132         final MessageRouterSubscribeResponse response = responses.block();
133
134         // then
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()));
140     }
141
142     @Test
143     void getWithImproperRawBody_shouldThrowNPE() {
144         // given
145         given(httpClient.call(any(HttpRequest.class))).willReturn(Mono.just(httpResponseWithIncorrectJson));
146
147         // when
148         // then
149         assertThatExceptionOfType(JsonSyntaxException.class).isThrownBy(() -> cut.get(mrRequest).block());
150     }
151
152     @Test
153     void getWithProperRequest_shouldReturnTimeoutError() {
154
155         // given
156         given(clientErrorReasonPresenter.present(any()))
157                 .willReturn(ERROR_MESSAGE);
158         given(httpClient.call(any(HttpRequest.class)))
159                 .willReturn(Mono.error(ReadTimeoutException.INSTANCE));
160
161         // when
162         final Mono<MessageRouterSubscribeResponse> responses = cut
163                 .get(mrRequest);
164         final MessageRouterSubscribeResponse response = responses.block();
165
166         // then
167         assertThat(response.failed()).isTrue();
168         assertThat(response.failReason()).isEqualTo(ERROR_MESSAGE);
169         assertThat(response.hasElements()).isFalse();
170
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();
178     }
179
180     @Test
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()));
186
187         // when
188         final Mono<MessageRouterSubscribeResponse> responses = cut
189                 .get(mrRequest);
190         final MessageRouterSubscribeResponse response = responses.block();
191
192         assertThat(response.failed()).isTrue();
193         assertThat(response.failReason()).isEqualTo(ERROR_MESSAGE);
194         assertThat(response.hasElements()).isFalse();
195
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();
203     }
204
205     @Test
206     void getWithProperRequest_shouldReturnCertainFailedResponse() {
207         given(httpClient.call(any(HttpRequest.class)))
208                 .willReturn(Mono.just(retryableHttpResponse));
209
210         // when
211         final Mono<MessageRouterSubscribeResponse> responses = cut
212                 .get(mrRequest);
213         final MessageRouterSubscribeResponse response = responses.block();
214
215         assertThat(response.failed()).isTrue();
216         assertThat(response.failReason()).startsWith("500 Something braked");
217         assertThat(response.hasElements()).isFalse();
218
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();
225     }
226 }