1e54d29dffcb55f4087c56a6ca4de6023a101659
[dcaegen2/collectors/datafile.git] /
1 /*
2  * ============LICENSE_START======================================================================
3  * Copyright (C) 2018-2019 Nordix Foundation. All rights reserved.
4  * ===============================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
6  * in compliance with the License. You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software distributed under the License
11  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12  * or implied. See the License for the specific language governing permissions and limitations under
13  * the License.
14  * ============LICENSE_END========================================================================
15  */
16
17 package org.onap.dcaegen2.collectors.datafile.service;
18
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.jupiter.api.Assertions.assertNotNull;
21 import static org.mockito.Mockito.times;
22 import static org.mockito.Mockito.verify;
23 import static org.mockito.Mockito.when;
24
25 import ch.qos.logback.classic.Level;
26 import ch.qos.logback.classic.spi.ILoggingEvent;
27 import ch.qos.logback.core.read.ListAppender;
28 import java.net.URI;
29 import java.net.URISyntaxException;
30 import org.junit.jupiter.api.Test;
31 import org.junit.jupiter.api.extension.ExtendWith;
32 import org.mockito.Mock;
33 import org.mockito.junit.jupiter.MockitoExtension;
34 import org.onap.dcaegen2.collectors.datafile.utils.LoggingUtils;
35 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.config.DmaapConsumerConfiguration;
36 import org.springframework.http.HttpHeaders;
37 import org.springframework.http.HttpMethod;
38 import org.springframework.http.HttpStatus;
39 import org.springframework.web.reactive.function.client.ClientRequest;
40 import org.springframework.web.reactive.function.client.ClientResponse;
41 import org.springframework.web.reactive.function.client.WebClient;
42 import reactor.core.publisher.Mono;
43
44 @ExtendWith(MockitoExtension.class)
45 class DmaapWebClientTest {
46
47     @Mock
48     private DmaapConsumerConfiguration dmaapConsumerConfigurationMock;
49
50     @Mock
51     private ClientResponse clientResponseMock;
52
53     @Mock
54     private ClientRequest clientRequesteMock;
55
56     @Test
57     void buildsDMaaPReactiveWebClientProperly() {
58         when(dmaapConsumerConfigurationMock.dmaapContentType()).thenReturn("*/*");
59         WebClient dmaapWebClientUndetTest = new DmaapWebClient() //
60             .fromConfiguration(dmaapConsumerConfigurationMock) //
61             .build();
62
63         verify(dmaapConsumerConfigurationMock, times(1)).dmaapContentType();
64         assertNotNull(dmaapWebClientUndetTest);
65     }
66
67     @Test
68     public void logResponseSuccess() {
69         DmaapWebClient dmaapWebClientUndetTest = new DmaapWebClient();
70
71         when(clientResponseMock.statusCode()).thenReturn(HttpStatus.OK);
72
73         ListAppender<ILoggingEvent> logAppender = LoggingUtils.getLogListAppender(DmaapWebClient.class, true);
74         Mono<ClientResponse> logResponse = dmaapWebClientUndetTest.logResponse(clientResponseMock);
75
76         assertEquals(clientResponseMock, logResponse.block());
77
78         assertEquals(logAppender.list.get(0).getLevel(), Level.TRACE);
79         assertEquals(logAppender.list.get(0).getFormattedMessage(), "Response Status 200 OK");
80
81         logAppender.stop();
82     }
83
84     @Test
85     public void logRequestSuccess() throws URISyntaxException {
86         when(clientRequesteMock.url()).thenReturn(new URI("http://test"));
87         when(clientRequesteMock.method()).thenReturn(HttpMethod.GET);
88         HttpHeaders httpHeaders = new HttpHeaders();
89         httpHeaders.add("header", "value");
90         when(clientRequesteMock.headers()).thenReturn(httpHeaders);
91
92         DmaapWebClient dmaapWebClientUndetTest = new DmaapWebClient();
93
94         ListAppender<ILoggingEvent> logAppender = LoggingUtils.getLogListAppender(DmaapWebClient.class, true);
95         Mono<ClientRequest> logRequest = dmaapWebClientUndetTest.logRequest(clientRequesteMock);
96
97         assertEquals(clientRequesteMock, logRequest.block());
98
99         assertEquals(logAppender.list.get(0).getLevel(), Level.TRACE);
100         assertEquals("Request: GET http://test", logAppender.list.get(0).getFormattedMessage());
101         assertEquals(logAppender.list.get(1).getLevel(), Level.TRACE);
102         assertEquals("HTTP request headers: [header:\"value\"]", logAppender.list.get(1).getFormattedMessage());
103
104         logAppender.stop();
105     }
106 }