c0b0c40b14cc1da7db8c9cc0efbd8b076e490f95
[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.producer;
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.DmaapPublisherConfiguration;
36 import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModel;
37 import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModelForUnitTest;
38 import org.springframework.http.HttpHeaders;
39 import org.springframework.http.HttpStatus;
40 import org.springframework.web.reactive.function.client.WebClient;
41 import org.springframework.web.reactive.function.client.WebClient.RequestBodyUriSpec;
42 import org.springframework.web.reactive.function.client.WebClient.RequestHeadersSpec;
43 import org.springframework.web.reactive.function.client.WebClient.ResponseSpec;
44 import reactor.core.publisher.Mono;
45 import reactor.test.StepVerifier;
46
47 /**
48  * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 7/4/18
49  */
50 public class DMaaPProducerReactiveHttpClientTest {
51
52     private static DMaaPProducerReactiveHttpClient dMaaPProducerReactiveHttpClient;
53
54     private static DmaapPublisherConfiguration dmaapPublisherConfigurationMock = mock(
55         DmaapPublisherConfiguration.class);
56     private static final Integer RESPONSE_SUCCESS = 200;
57     private static ConsumerDmaapModel consumerDmaapModel = new ConsumerDmaapModelForUnitTest();
58     private static Mono<Integer> expectedResult = Mono.empty();
59     private static WebClient webClient = mock(WebClient.class);
60     private static RequestBodyUriSpec requestBodyUriSpec;
61     private static ResponseSpec responseSpec;
62
63
64     @BeforeAll
65     public static void setUp() {
66         when(dmaapPublisherConfigurationMock.dmaapHostName()).thenReturn("54.45.33.2");
67         when(dmaapPublisherConfigurationMock.dmaapProtocol()).thenReturn("https");
68         when(dmaapPublisherConfigurationMock.dmaapPortNumber()).thenReturn(1234);
69         when(dmaapPublisherConfigurationMock.dmaapUserName()).thenReturn("PRH");
70         when(dmaapPublisherConfigurationMock.dmaapUserPassword()).thenReturn("PRH");
71         when(dmaapPublisherConfigurationMock.dmaapContentType()).thenReturn("application/json");
72         when(dmaapPublisherConfigurationMock.dmaapTopicName()).thenReturn("pnfReady");
73
74         dMaaPProducerReactiveHttpClient = new DMaaPProducerReactiveHttpClient(dmaapPublisherConfigurationMock);
75
76         webClient = spy(WebClient.builder()
77             .defaultHeader(HttpHeaders.CONTENT_TYPE, dmaapPublisherConfigurationMock.dmaapContentType())
78             .filter(basicAuthentication(dmaapPublisherConfigurationMock.dmaapUserName(),
79                 dmaapPublisherConfigurationMock.dmaapUserPassword()))
80             .build());
81         requestBodyUriSpec = mock(RequestBodyUriSpec.class);
82         responseSpec = mock(ResponseSpec.class);
83     }
84
85     @Test
86     public void getHttpResponse_Success() {
87         //given
88         expectedResult = Mono.just(RESPONSE_SUCCESS);
89
90         //when
91         mockWebClientDependantObject();
92         doReturn(expectedResult).when(responseSpec).bodyToMono(String.class);
93         dMaaPProducerReactiveHttpClient.createDMaaPWebClient(webClient);
94         Mono<String> response = dMaaPProducerReactiveHttpClient.getDMaaPProducerResponse(Mono.just(consumerDmaapModel));
95
96         //then
97         Assertions.assertEquals(response.block(), expectedResult.block());
98     }
99
100     @Test
101     public void getHttpResponse_HttpResponse4xxClientError() {
102         //when
103         mockWebClientDependantObject();
104
105         doAnswer(invocationOnMock -> Mono.error(new Exception("400")))
106             .when(responseSpec).onStatus(HttpStatus::is4xxClientError, e -> Mono.error(new Exception("400")));
107         dMaaPProducerReactiveHttpClient.createDMaaPWebClient(webClient);
108
109         //then
110         StepVerifier.create(dMaaPProducerReactiveHttpClient.getDMaaPProducerResponse(Mono.just(consumerDmaapModel)))
111             .expectSubscription()
112             .expectError(Exception.class);
113
114     }
115
116     @Test
117     public void getHttpResponse_HttpResponse5xxClientError() {
118
119         //when
120         mockWebClientDependantObject();
121         doAnswer(invocationOnMock -> Mono.error(new Exception("500")))
122             .when(responseSpec).onStatus(HttpStatus::is4xxClientError, e -> Mono.error(new Exception("500")));
123         dMaaPProducerReactiveHttpClient.createDMaaPWebClient(webClient);
124
125         //then
126         StepVerifier.create(dMaaPProducerReactiveHttpClient.getDMaaPProducerResponse(Mono.just(consumerDmaapModel)))
127             .expectSubscription()
128             .expectError(Exception.class);
129     }
130
131     @Test
132     public void getHttpResponse_whenURISyntaxExceptionHasBeenThrown() throws URISyntaxException {
133         //given
134         dMaaPProducerReactiveHttpClient = spy(dMaaPProducerReactiveHttpClient);
135         //when
136         when(webClient.post()).thenReturn(requestBodyUriSpec);
137         dMaaPProducerReactiveHttpClient.createDMaaPWebClient(webClient);
138         when(dMaaPProducerReactiveHttpClient.getUri()).thenThrow(URISyntaxException.class);
139
140         //then
141         StepVerifier.create(dMaaPProducerReactiveHttpClient.getDMaaPProducerResponse(any())).expectSubscription()
142             .expectError(Exception.class);
143     }
144
145     private void mockWebClientDependantObject() {
146         RequestHeadersSpec requestHeadersSpec = mock(RequestHeadersSpec.class);
147         when(webClient.post()).thenReturn(requestBodyUriSpec);
148         when(requestBodyUriSpec.uri((URI) any())).thenReturn(requestBodyUriSpec);
149         when(requestBodyUriSpec.body(any())).thenReturn(requestHeadersSpec);
150         doReturn(responseSpec).when(requestHeadersSpec).retrieve();
151         doReturn(responseSpec).when(responseSpec).onStatus(any(), any());
152     }
153 }