1f97001eec262a0a2c07b976532f0da007215fb4
[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.model.ImmutableMessageRouterSubscribeRequest;
39 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.model.MessageRouterSubscribeRequest;
40 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.model.MessageRouterSubscribeResponse;
41 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.model.config.MessageRouterSubscriberConfig;
42 import reactor.core.publisher.Mono;
43
44 /**
45  * @author <a href="mailto:piotr.jaszczyk@nokia.com">Piotr Jaszczyk</a>
46  * @since May 2019
47  */
48 class MessageRouterSubscriberImplTest {
49
50     private final RxHttpClient httpClient = mock(RxHttpClient.class);
51     private final MessageRouterSubscriberConfig clientConfig = MessageRouterSubscriberConfig.createDefault();
52     private final MessageRouterSubscriber
53             cut = new MessageRouterSubscriberImpl(httpClient, clientConfig.gsonInstance());
54
55     private final ArgumentCaptor<HttpRequest> httpRequestArgumentCaptor = ArgumentCaptor.forClass(HttpRequest.class);
56     private final MessageRouterSource sourceDefinition = ImmutableMessageRouterSource.builder()
57             .name("sample topic")
58             .topicUrl("https://dmaap-mr/TOPIC")
59             .build();
60     private final MessageRouterSubscribeRequest mrRequest = ImmutableMessageRouterSubscribeRequest.builder()
61             .consumerGroup("SAMPLE-GROUP")
62             .sourceDefinition(sourceDefinition)
63             .build();
64     private final HttpResponse httpResponse = ImmutableHttpResponse.builder()
65             .statusCode(200)
66             .statusReason("OK")
67             .url(sourceDefinition.topicUrl())
68             .rawBody("[]".getBytes())
69             .build();
70     private final HttpResponse httpResponseWithWrongStatusCode = ImmutableHttpResponse.builder()
71             .statusCode(301)
72             .statusReason("Something braked")
73             .url(sourceDefinition.topicUrl())
74             .rawBody("[]".getBytes())
75             .build();
76     private final HttpResponse httpResponseWithIncorrectJson = ImmutableHttpResponse.builder()
77             .statusCode(200)
78             .statusReason("OK")
79             .url(sourceDefinition.topicUrl())
80             .rawBody("{}".getBytes())
81             .build();
82
83     @Test
84     void getWithProperRequest_shouldReturnCorrectResponse() {
85         // given
86         given(httpClient.call(any(HttpRequest.class))).willReturn(Mono.just(httpResponse));
87
88         // when
89         final Mono<MessageRouterSubscribeResponse> responses = cut
90                 .get(mrRequest);
91         final MessageRouterSubscribeResponse response = responses.block();
92
93         // then
94         assertThat(response.successful()).isTrue();
95         assertThat(response.failReason()).isNull();
96         assertThat(response.hasElements()).isFalse();
97
98
99         verify(httpClient).call(httpRequestArgumentCaptor.capture());
100         final HttpRequest httpRequest = httpRequestArgumentCaptor.getValue();
101         assertThat(httpRequest.method()).isEqualTo(HttpMethod.GET);
102         assertThat(httpRequest.url()).isEqualTo(String.format("%s/%s/%s", sourceDefinition.topicUrl(),
103                 mrRequest.consumerGroup(), mrRequest.consumerId()));
104         assertThat(httpRequest.body()).isNull();
105     }
106
107     @Test
108     void getWithProperRequestButNotSuccessfulHttpRequest_shouldReturnMonoWithFailReason() {
109         // given
110         given(httpClient.call(any(HttpRequest.class))).willReturn(Mono.just(httpResponseWithWrongStatusCode));
111
112         // when
113         final Mono<MessageRouterSubscribeResponse> responses = cut
114                 .get(mrRequest);
115         final MessageRouterSubscribeResponse response = responses.block();
116
117         // then
118         assertThat(response.failed()).isTrue();
119         assertThat(response.failReason()).
120                 isEqualTo(String.format("%d %s%n%s", httpResponseWithWrongStatusCode.statusCode(),
121                         httpResponseWithWrongStatusCode.statusReason(),
122                         httpResponseWithWrongStatusCode.bodyAsString()));
123     }
124
125     @Test
126     void getWithImproperRawBody_shouldThrowNPE() {
127         // given
128         given(httpClient.call(any(HttpRequest.class))).willReturn(Mono.just(httpResponseWithIncorrectJson));
129
130         // when
131         // then
132         assertThatExceptionOfType(JsonSyntaxException.class).isThrownBy(() -> cut.get(mrRequest).block());
133     }
134
135     @Test
136     void getWithProperRequest_shouldReturnTimeoutError() {
137
138         // given
139         given(httpClient.call(any(HttpRequest.class))).willReturn(Mono.error(ReadTimeoutException.INSTANCE));
140
141         // when
142         final Mono<MessageRouterSubscribeResponse> responses = cut
143                 .get(mrRequest);
144         final MessageRouterSubscribeResponse response = responses.block();
145
146         // then
147         assertThat(response.failed()).isTrue();
148         assertThat(response.failReason()).contains("408 Request Timeout");
149         assertThat(response.hasElements()).isFalse();
150
151
152         verify(httpClient).call(httpRequestArgumentCaptor.capture());
153         final HttpRequest httpRequest = httpRequestArgumentCaptor.getValue();
154         assertThat(httpRequest.method()).isEqualTo(HttpMethod.GET);
155         assertThat(httpRequest.url()).isEqualTo(String.format("%s/%s/%s", sourceDefinition.topicUrl(),
156                 mrRequest.consumerGroup(), mrRequest.consumerId()));
157         assertThat(httpRequest.body()).isNull();
158     }
159 }