2 * ============LICENSE_START====================================
3 * DCAEGEN2-SERVICES-SDK
4 * =========================================================
5 * Copyright (C) 2019 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 org.junit.jupiter.api.Test;
32 import org.mockito.ArgumentCaptor;
33 import org.onap.dcaegen2.services.sdk.model.streams.dmaap.ImmutableMessageRouterSource;
34 import org.onap.dcaegen2.services.sdk.model.streams.dmaap.MessageRouterSource;
35 import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.*;
36 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.api.MessageRouterSubscriber;
37 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.model.ImmutableMessageRouterSubscribeRequest;
38 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.model.MessageRouterSubscribeRequest;
39 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.model.MessageRouterSubscribeResponse;
40 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.model.config.MessageRouterSubscriberConfig;
41 import reactor.core.publisher.Mono;
44 * @author <a href="mailto:piotr.jaszczyk@nokia.com">Piotr Jaszczyk</a>
47 class MessageRouterSubscriberImplTest {
49 private final RxHttpClient httpClient = mock(RxHttpClient.class);
50 private final MessageRouterSubscriberConfig clientConfig = MessageRouterSubscriberConfig.createDefault();
51 private final MessageRouterSubscriber cut = new MessageRouterSubscriberImpl(httpClient, clientConfig.gsonInstance());
53 private final ArgumentCaptor<HttpRequest> httpRequestArgumentCaptor = ArgumentCaptor.forClass(HttpRequest.class);
54 private final MessageRouterSource sourceDefinition = ImmutableMessageRouterSource.builder()
56 .topicUrl("https://dmaap-mr/TOPIC")
58 private final MessageRouterSubscribeRequest mrRequest = ImmutableMessageRouterSubscribeRequest.builder()
59 .consumerGroup("SAMPLE-GROUP")
60 .sourceDefinition(sourceDefinition)
62 private final HttpResponse httpResponse = ImmutableHttpResponse.builder()
65 .url(sourceDefinition.topicUrl())
66 .rawBody("[]".getBytes())
68 private final HttpResponse httpResponseWithWrongStatusCode = ImmutableHttpResponse.builder()
70 .statusReason("Something braked")
71 .url(sourceDefinition.topicUrl())
72 .rawBody("[]".getBytes())
74 private final HttpResponse httpResponseWithIncorrectJson = ImmutableHttpResponse.builder()
77 .url(sourceDefinition.topicUrl())
78 .rawBody("{}".getBytes())
82 void getWithProperRequest_shouldReturnCorrectResponse() {
84 given(httpClient.call(any(HttpRequest.class))).willReturn(Mono.just(httpResponse));
87 final Mono<MessageRouterSubscribeResponse> responses = cut
89 final MessageRouterSubscribeResponse response = responses.block();
92 assertThat(response.successful()).isTrue();
93 assertThat(response.failReason()).isNull();
94 assertThat(response.hasElements()).isFalse();
97 verify(httpClient).call(httpRequestArgumentCaptor.capture());
98 final HttpRequest httpRequest = httpRequestArgumentCaptor.getValue();
99 assertThat(httpRequest.method()).isEqualTo(HttpMethod.GET);
100 assertThat(httpRequest.url()).isEqualTo(String.format("%s/%s/%s", sourceDefinition.topicUrl(),
101 mrRequest.consumerGroup(), mrRequest.consumerId()));
102 assertThat(httpRequest.body()).isNull();
106 void getWithProperRequestButNotSuccessfulHttpRequest_shouldReturnMonoWithFailReason() {
108 given(httpClient.call(any(HttpRequest.class))).willReturn(Mono.just(httpResponseWithWrongStatusCode));
111 final Mono<MessageRouterSubscribeResponse> responses = cut
113 final MessageRouterSubscribeResponse response = responses.block();
116 assertThat(response.failed()).isTrue();
117 assertThat(response.failReason()).
118 isEqualTo(String.format("%d %s%n%s", httpResponseWithWrongStatusCode.statusCode(),
119 httpResponseWithWrongStatusCode.statusReason(),
120 httpResponseWithWrongStatusCode.bodyAsString()));
124 void getWithImproperRawBody_shouldThrowNPE() {
126 given(httpClient.call(any(HttpRequest.class))).willReturn(Mono.just(httpResponseWithIncorrectJson));
130 assertThatExceptionOfType(JsonSyntaxException.class).isThrownBy(() -> cut.get(mrRequest).block());