f1fb70b0a7999c2f7aebe2b31d0d11f401858e13
[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 WebClient webClient = mock(WebClient.class);
59     private static RequestBodyUriSpec requestBodyUriSpec;
60     private static ResponseSpec responseSpec;
61
62
63     @BeforeAll
64     public static void setUp() {
65         when(dmaapPublisherConfigurationMock.dmaapHostName()).thenReturn("54.45.33.2");
66         when(dmaapPublisherConfigurationMock.dmaapProtocol()).thenReturn("https");
67         when(dmaapPublisherConfigurationMock.dmaapPortNumber()).thenReturn(1234);
68         when(dmaapPublisherConfigurationMock.dmaapUserName()).thenReturn("PRH");
69         when(dmaapPublisherConfigurationMock.dmaapUserPassword()).thenReturn("PRH");
70         when(dmaapPublisherConfigurationMock.dmaapContentType()).thenReturn("application/json");
71         when(dmaapPublisherConfigurationMock.dmaapTopicName()).thenReturn("pnfReady");
72
73         dMaaPProducerReactiveHttpClient = new DMaaPProducerReactiveHttpClient(dmaapPublisherConfigurationMock);
74
75         webClient = spy(WebClient.builder()
76             .defaultHeader(HttpHeaders.CONTENT_TYPE, dmaapPublisherConfigurationMock.dmaapContentType())
77             .filter(basicAuthentication(dmaapPublisherConfigurationMock.dmaapUserName(),
78                 dmaapPublisherConfigurationMock.dmaapUserPassword()))
79             .build());
80         requestBodyUriSpec = mock(RequestBodyUriSpec.class);
81         responseSpec = mock(ResponseSpec.class);
82     }
83
84     @Test
85     public void getHttpResponse_Success() {
86         //given
87         Mono<Integer> expectedResult = Mono.just(RESPONSE_SUCCESS);
88
89         //when
90         mockWebClientDependantObject();
91         doReturn(expectedResult).when(responseSpec).bodyToMono(String.class);
92         dMaaPProducerReactiveHttpClient.createDMaaPWebClient(webClient);
93         Mono<String> response = dMaaPProducerReactiveHttpClient.getDMaaPProducerResponse(Mono.just(consumerDmaapModel));
94
95         //then
96         Assertions.assertEquals(response.block(), expectedResult.block());
97     }
98
99     @Test
100     public void getHttpResponse_HttpResponse4xxClientError() {
101         //when
102         mockWebClientDependantObject();
103
104         doAnswer(invocationOnMock -> Mono.error(new Exception("400")))
105             .when(responseSpec).onStatus(HttpStatus::is4xxClientError, e -> Mono.error(new Exception("400")));
106         dMaaPProducerReactiveHttpClient.createDMaaPWebClient(webClient);
107
108         //then
109         StepVerifier.create(dMaaPProducerReactiveHttpClient.getDMaaPProducerResponse(Mono.just(consumerDmaapModel)))
110             .expectSubscription()
111             .expectError(Exception.class);
112
113     }
114
115     @Test
116     public void getHttpResponse_HttpResponse5xxClientError() {
117
118         //when
119         mockWebClientDependantObject();
120         doAnswer(invocationOnMock -> Mono.error(new Exception("500")))
121             .when(responseSpec).onStatus(HttpStatus::is4xxClientError, e -> Mono.error(new Exception("500")));
122         dMaaPProducerReactiveHttpClient.createDMaaPWebClient(webClient);
123
124         //then
125         StepVerifier.create(dMaaPProducerReactiveHttpClient.getDMaaPProducerResponse(Mono.just(consumerDmaapModel)))
126             .expectSubscription()
127             .expectError(Exception.class);
128     }
129
130     @Test
131     public void getHttpResponse_whenURISyntaxExceptionHasBeenThrown() throws URISyntaxException {
132         //given
133         dMaaPProducerReactiveHttpClient = spy(dMaaPProducerReactiveHttpClient);
134         //when
135         when(webClient.post()).thenReturn(requestBodyUriSpec);
136         dMaaPProducerReactiveHttpClient.createDMaaPWebClient(webClient);
137         when(dMaaPProducerReactiveHttpClient.getUri()).thenThrow(URISyntaxException.class);
138
139         //then
140         StepVerifier.create(dMaaPProducerReactiveHttpClient.getDMaaPProducerResponse(any())).expectSubscription()
141             .expectError(Exception.class);
142     }
143
144     private void mockWebClientDependantObject() {
145         RequestHeadersSpec requestHeadersSpec = mock(RequestHeadersSpec.class);
146         when(webClient.post()).thenReturn(requestBodyUriSpec);
147         when(requestBodyUriSpec.uri((URI) any())).thenReturn(requestBodyUriSpec);
148         when(requestBodyUriSpec.body(any())).thenReturn(requestHeadersSpec);
149         doReturn(responseSpec).when(requestHeadersSpec).retrieve();
150         doReturn(responseSpec).when(responseSpec).onStatus(any(), any());
151     }
152 }