Support retry in DCAE-SDK DMaaP-Client
[dcaegen2/services/sdk.git] / rest-services / dmaap-client / src / test / java / org / onap / dcaegen2 / services / sdk / rest / services / dmaap / client / impl / MessageRouterSubscriberImplTest.java
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.verify;
29
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;
44
45 /**
46  * @author <a href="mailto:piotr.jaszczyk@nokia.com">Piotr Jaszczyk</a>
47  * @since May 2019
48  */
49 class MessageRouterSubscriberImplTest {
50
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);
57
58     private final ArgumentCaptor<HttpRequest> httpRequestArgumentCaptor = ArgumentCaptor.forClass(HttpRequest.class);
59     private final MessageRouterSource sourceDefinition = ImmutableMessageRouterSource.builder()
60             .name("sample topic")
61             .topicUrl("https://dmaap-mr/TOPIC")
62             .build();
63     private final MessageRouterSubscribeRequest mrRequest = ImmutableMessageRouterSubscribeRequest.builder()
64             .consumerGroup("SAMPLE-GROUP")
65             .sourceDefinition(sourceDefinition)
66             .build();
67     private final HttpResponse httpResponse = ImmutableHttpResponse.builder()
68             .statusCode(200)
69             .statusReason("OK")
70             .url(sourceDefinition.topicUrl())
71             .rawBody("[]".getBytes())
72             .build();
73     private final HttpResponse httpResponseWithWrongStatusCode = ImmutableHttpResponse.builder()
74             .statusCode(301)
75             .statusReason("Something braked")
76             .url(sourceDefinition.topicUrl())
77             .rawBody("[]".getBytes())
78             .build();
79     private final HttpResponse httpResponseWithIncorrectJson = ImmutableHttpResponse.builder()
80             .statusCode(200)
81             .statusReason("OK")
82             .url(sourceDefinition.topicUrl())
83             .rawBody("{}".getBytes())
84             .build();
85
86     @Test
87     void getWithProperRequest_shouldReturnCorrectResponse() {
88         // given
89         given(httpClient.call(any(HttpRequest.class))).willReturn(Mono.just(httpResponse));
90
91         // when
92         final Mono<MessageRouterSubscribeResponse> responses = cut
93                 .get(mrRequest);
94         final MessageRouterSubscribeResponse response = responses.block();
95
96         // then
97         assertThat(response.successful()).isTrue();
98         assertThat(response.failReason()).isNull();
99         assertThat(response.hasElements()).isFalse();
100
101
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();
108     }
109
110     @Test
111     void getWithProperRequestButNotSuccessfulHttpRequest_shouldReturnMonoWithFailReason() {
112         // given
113         given(httpClient.call(any(HttpRequest.class))).willReturn(Mono.just(httpResponseWithWrongStatusCode));
114
115         // when
116         final Mono<MessageRouterSubscribeResponse> responses = cut
117                 .get(mrRequest);
118         final MessageRouterSubscribeResponse response = responses.block();
119
120         // then
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()));
126     }
127
128     @Test
129     void getWithImproperRawBody_shouldThrowNPE() {
130         // given
131         given(httpClient.call(any(HttpRequest.class))).willReturn(Mono.just(httpResponseWithIncorrectJson));
132
133         // when
134         // then
135         assertThatExceptionOfType(JsonSyntaxException.class).isThrownBy(() -> cut.get(mrRequest).block());
136     }
137
138     @Test
139     void getWithProperRequest_shouldReturnTimeoutError() {
140
141         // given
142         given(clientErrorReasonPresenter.present(any()))
143                 .willReturn(ERROR_MESSAGE);
144         given(httpClient.call(any(HttpRequest.class)))
145                 .willReturn(Mono.error(ReadTimeoutException.INSTANCE));
146
147         // when
148         final Mono<MessageRouterSubscribeResponse> responses = cut
149                 .get(mrRequest);
150         final MessageRouterSubscribeResponse response = responses.block();
151
152         // then
153         assertThat(response.failed()).isTrue();
154         assertThat(response.failReason()).isEqualTo(ERROR_MESSAGE);
155         assertThat(response.hasElements()).isFalse();
156
157
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();
164     }
165 }