63966602bb78a98baee6622ddfcd441ae352449a
[dcaegen2/services/prh.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * PNF-REGISTRATION-HANDLER
4  * ================================================================================
5  * Copyright (C) 2018 NOKIA Intellectual Property. 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 package org.onap.dcaegen2.services.prh.service.consumer;
21
22 import static org.mockito.ArgumentMatchers.any;
23 import static org.mockito.Mockito.doAnswer;
24 import static org.mockito.Mockito.doReturn;
25 import static org.mockito.Mockito.mock;
26 import static org.mockito.Mockito.spy;
27 import static org.mockito.Mockito.when;
28 import static org.springframework.web.reactive.function.client.ExchangeFilterFunctions.basicAuthentication;
29
30 import java.net.URI;
31 import java.net.URISyntaxException;
32 import org.junit.jupiter.api.Assertions;
33 import org.junit.jupiter.api.BeforeAll;
34 import org.junit.jupiter.api.Test;
35 import org.onap.dcaegen2.services.prh.config.DmaapConsumerConfiguration;
36 import org.springframework.http.HttpHeaders;
37 import org.springframework.http.HttpStatus;
38 import org.springframework.web.reactive.function.client.WebClient;
39 import org.springframework.web.reactive.function.client.WebClient.RequestHeadersUriSpec;
40 import org.springframework.web.reactive.function.client.WebClient.ResponseSpec;
41 import reactor.core.publisher.Mono;
42 import reactor.test.StepVerifier;
43
44 /**
45  * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 6/27/18
46  */
47 public class DmaapConsumerReactiveHttpClientTest {
48
49     private static DmaapConsumerReactiveHttpClient dmaapConsumerReactiveHttpClient;
50
51     private static DmaapConsumerConfiguration consumerConfigurationMock = mock(DmaapConsumerConfiguration.class);
52     private static final String JSON_MESSAGE = "{ \"responseFromDmaap\": \"Success\"}";
53     private static Mono<String> expectedResult = Mono.empty();
54     private static WebClient webClient = mock(WebClient.class);
55     private static RequestHeadersUriSpec requestHeadersSpec;
56     private static ResponseSpec responseSpec;
57
58
59     @BeforeAll
60     public static void setUp() {
61         when(consumerConfigurationMock.dmaapHostName()).thenReturn("54.45.33.2");
62         when(consumerConfigurationMock.dmaapProtocol()).thenReturn("https");
63         when(consumerConfigurationMock.dmaapPortNumber()).thenReturn(1234);
64         when(consumerConfigurationMock.dmaapUserName()).thenReturn("PRH");
65         when(consumerConfigurationMock.dmaapUserPassword()).thenReturn("PRH");
66         when(consumerConfigurationMock.dmaapContentType()).thenReturn("application/json");
67         when(consumerConfigurationMock.dmaapTopicName()).thenReturn("unauthenticated.SEC_OTHER_OUTPUT");
68         when(consumerConfigurationMock.consumerGroup()).thenReturn("OpenDCAE-c12");
69         when(consumerConfigurationMock.consumerId()).thenReturn("c12");
70
71         dmaapConsumerReactiveHttpClient = new DmaapConsumerReactiveHttpClient(consumerConfigurationMock);
72         webClient = spy(WebClient.builder()
73             .defaultHeader(HttpHeaders.CONTENT_TYPE, consumerConfigurationMock.dmaapContentType())
74             .filter(basicAuthentication(consumerConfigurationMock.dmaapUserName(),
75                 consumerConfigurationMock.dmaapUserPassword()))
76             .filter(dmaapConsumerReactiveHttpClient.logRequest())
77             .filter(dmaapConsumerReactiveHttpClient.logResponse())
78             .build());
79         requestHeadersSpec = mock(RequestHeadersUriSpec.class);
80         responseSpec = mock(ResponseSpec.class);
81     }
82
83
84     @Test
85     public void getHttpResponse_Success() {
86         //given
87         expectedResult = Mono.just(JSON_MESSAGE);
88
89         //when
90         mockDependantObjects();
91         doReturn(expectedResult).when(responseSpec).bodyToMono(String.class);
92         dmaapConsumerReactiveHttpClient.initWebClient(webClient);
93         Mono<String> response = dmaapConsumerReactiveHttpClient.getDmaaPConsumerResponse();
94
95         //then
96         StepVerifier.create(response).expectSubscription()
97             .expectNextMatches(results -> {
98                 Assertions.assertEquals(results, expectedResult.block());
99                 return true;
100             }).verifyComplete();
101     }
102
103
104     @Test
105     public void getHttpResponse_HttpResponse4xxClientError() {
106
107         //when
108         mockDependantObjects();
109         doAnswer(invocationOnMock -> Mono.error(new Exception("400")))
110             .when(responseSpec).onStatus(HttpStatus::is4xxClientError, e -> Mono.error(new Exception("400")));
111         dmaapConsumerReactiveHttpClient.initWebClient();
112         dmaapConsumerReactiveHttpClient.initWebClient(webClient);
113
114         //then
115         StepVerifier.create(dmaapConsumerReactiveHttpClient.getDmaaPConsumerResponse()).expectSubscription()
116             .expectError(Exception.class);
117
118     }
119
120     @Test
121     public void getHttpResponse_HttpResponse5xxClientError() {
122
123         //when
124         mockDependantObjects();
125         doAnswer(invocationOnMock -> Mono.error(new Exception("500")))
126             .when(responseSpec).onStatus(HttpStatus::is4xxClientError, e -> Mono.error(new Exception("500")));
127         dmaapConsumerReactiveHttpClient.initWebClient();
128         dmaapConsumerReactiveHttpClient.initWebClient(webClient);
129
130         //then
131         StepVerifier.create(dmaapConsumerReactiveHttpClient.getDmaaPConsumerResponse()).expectSubscription()
132             .expectError(Exception.class);
133     }
134
135     @Test
136     public void getHttpResponse_whenURISyntaxExceptionHasBeenThrown() throws URISyntaxException {
137         //given
138         dmaapConsumerReactiveHttpClient = spy(dmaapConsumerReactiveHttpClient);
139         //when
140         when(webClient.get()).thenReturn(requestHeadersSpec);
141         dmaapConsumerReactiveHttpClient.initWebClient(webClient);
142         when(dmaapConsumerReactiveHttpClient.getUri()).thenThrow(URISyntaxException.class);
143
144         //then
145         StepVerifier.create(dmaapConsumerReactiveHttpClient.getDmaaPConsumerResponse()).expectSubscription()
146             .expectError(Exception.class);
147     }
148
149     private void mockDependantObjects() {
150         when(webClient.get()).thenReturn(requestHeadersSpec);
151         when(requestHeadersSpec.uri((URI) any())).thenReturn(requestHeadersSpec);
152         when(requestHeadersSpec.retrieve()).thenReturn(responseSpec);
153         doReturn(responseSpec).when(responseSpec).onStatus(any(), any());
154     }
155
156 }