256d3c210ed9b60d131454b670cdb4fe48f0edc5
[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
21 package org.onap.dcaegen2.services.prh.service.consumer;
22
23 import static org.mockito.ArgumentMatchers.any;
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.BeforeEach;
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.web.reactive.function.client.WebClient;
38 import org.springframework.web.reactive.function.client.WebClient.RequestHeadersUriSpec;
39 import org.springframework.web.reactive.function.client.WebClient.ResponseSpec;
40 import reactor.core.publisher.Mono;
41 import reactor.test.StepVerifier;
42
43 /**
44  * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 6/27/18
45  */
46 class DMaaPConsumerReactiveHttpClientTest {
47
48     private static final String JSON_MESSAGE = "{ \"responseFromDmaap\": \"Success\"}";
49     private DMaaPConsumerReactiveHttpClient dmaapConsumerReactiveHttpClient;
50     private DmaapConsumerConfiguration consumerConfigurationMock = mock(DmaapConsumerConfiguration.class);
51     private Mono<String> expectedResult = Mono.empty();
52     private WebClient webClient;
53     private RequestHeadersUriSpec requestHeadersSpec;
54     private ResponseSpec responseSpec;
55
56
57     @BeforeEach
58     void setUp() {
59         when(consumerConfigurationMock.dmaapHostName()).thenReturn("54.45.33.2");
60         when(consumerConfigurationMock.dmaapProtocol()).thenReturn("https");
61         when(consumerConfigurationMock.dmaapPortNumber()).thenReturn(1234);
62         when(consumerConfigurationMock.dmaapUserName()).thenReturn("PRH");
63         when(consumerConfigurationMock.dmaapUserPassword()).thenReturn("PRH");
64         when(consumerConfigurationMock.dmaapContentType()).thenReturn("application/json");
65         when(consumerConfigurationMock.dmaapTopicName()).thenReturn("unauthenticated.SEC_OTHER_OUTPUT");
66         when(consumerConfigurationMock.consumerGroup()).thenReturn("OpenDCAE-c12");
67         when(consumerConfigurationMock.consumerId()).thenReturn("c12");
68
69         webClient = spy(WebClient.builder()
70             .defaultHeader(HttpHeaders.CONTENT_TYPE, consumerConfigurationMock.dmaapContentType())
71             .filter(basicAuthentication(consumerConfigurationMock.dmaapUserName(),
72                 consumerConfigurationMock.dmaapUserPassword()))
73             .build());
74         dmaapConsumerReactiveHttpClient = new DMaaPConsumerReactiveHttpClient(consumerConfigurationMock, webClient);
75         requestHeadersSpec = mock(RequestHeadersUriSpec.class);
76         responseSpec = mock(ResponseSpec.class);
77     }
78
79
80     @Test
81     void getHttpResponse_Success() {
82         //given
83         expectedResult = Mono.just(JSON_MESSAGE);
84
85         //when
86         mockDependantObjects();
87         doReturn(expectedResult).when(responseSpec).bodyToMono(String.class);
88         Mono<String> response = dmaapConsumerReactiveHttpClient.getDMaaPConsumerResponse();
89
90         //then
91         StepVerifier.create(response).expectSubscription()
92             .expectNextMatches(results -> {
93                 Assertions.assertEquals(results, expectedResult.block());
94                 return true;
95             }).verifyComplete();
96     }
97
98     @Test
99     void getAppropriateUri_whenPassingCorrectedPathForPnf() {
100         Assertions.assertEquals(dmaapConsumerReactiveHttpClient.getUri(),
101             URI.create("https://54.45.33.2:1234/unauthenticated.SEC_OTHER_OUTPUT/OpenDCAE-c12/c12"));
102     }
103
104     private void mockDependantObjects() {
105         when(webClient.get()).thenReturn(requestHeadersSpec);
106         when(requestHeadersSpec.uri((URI) any())).thenReturn(requestHeadersSpec);
107         when(requestHeadersSpec.headers(any())).thenReturn(requestHeadersSpec);
108         when(requestHeadersSpec.retrieve()).thenReturn(responseSpec);
109         doReturn(responseSpec).when(responseSpec).onStatus(any(), any());
110     }
111
112 }