ef2cb5ef00af601139f6f265540ba75d4e2cf2e5
[dcaegen2/services/sdk.git] /
1 /*
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
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 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;
42
43 /**
44  * @author <a href="mailto:piotr.jaszczyk@nokia.com">Piotr Jaszczyk</a>
45  * @since May 2019
46  */
47 class MessageRouterSubscriberImplTest {
48
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());
52
53     private final ArgumentCaptor<HttpRequest> httpRequestArgumentCaptor = ArgumentCaptor.forClass(HttpRequest.class);
54     private final MessageRouterSource sourceDefinition = ImmutableMessageRouterSource.builder()
55             .name("sample topic")
56             .topicUrl("https://dmaap-mr/TOPIC")
57             .build();
58     private final MessageRouterSubscribeRequest mrRequest = ImmutableMessageRouterSubscribeRequest.builder()
59             .consumerGroup("SAMPLE-GROUP")
60             .sourceDefinition(sourceDefinition)
61             .build();
62     private final HttpResponse httpResponse = ImmutableHttpResponse.builder()
63             .statusCode(200)
64             .statusReason("OK")
65             .url(sourceDefinition.topicUrl())
66             .rawBody("[]".getBytes())
67             .build();
68     private final HttpResponse httpResponseWithWrongStatusCode = ImmutableHttpResponse.builder()
69             .statusCode(301)
70             .statusReason("Something braked")
71             .url(sourceDefinition.topicUrl())
72             .rawBody("[]".getBytes())
73             .build();
74     private final HttpResponse httpResponseWithIncorrectJson = ImmutableHttpResponse.builder()
75             .statusCode(200)
76             .statusReason("OK")
77             .url(sourceDefinition.topicUrl())
78             .rawBody("{}".getBytes())
79             .build();
80
81     @Test
82     void getWithProperRequest_shouldReturnCorrectResponse() {
83         // given
84         given(httpClient.call(any(HttpRequest.class))).willReturn(Mono.just(httpResponse));
85
86         // when
87         final Mono<MessageRouterSubscribeResponse> responses = cut
88                 .get(mrRequest);
89         final MessageRouterSubscribeResponse response = responses.block();
90
91         // then
92         assertThat(response.successful()).isTrue();
93         assertThat(response.failReason()).isNull();
94         assertThat(response.hasElements()).isFalse();
95
96
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();
103     }
104
105     @Test
106     void getWithProperRequestButNotSuccessfulHttpRequest_shouldReturnMonoWithFailReason() {
107         // given
108         given(httpClient.call(any(HttpRequest.class))).willReturn(Mono.just(httpResponseWithWrongStatusCode));
109
110         // when
111         final Mono<MessageRouterSubscribeResponse> responses = cut
112                 .get(mrRequest);
113         final MessageRouterSubscribeResponse response = responses.block();
114
115         // then
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()));
121     }
122
123     @Test
124     void getWithImproperRawBody_shouldThrowNPE() {
125         // given
126         given(httpClient.call(any(HttpRequest.class))).willReturn(Mono.just(httpResponseWithIncorrectJson));
127
128         // when
129         // then
130         assertThatExceptionOfType(JsonSyntaxException.class).isThrownBy(() -> cut.get(mrRequest).block());
131     }
132 }