499b2608ee554dd22be5671b52be1a5f454f76b9
[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
29 import java.net.URI;
30 import java.net.URISyntaxException;
31
32 import org.junit.jupiter.api.Test;
33 import org.junit.jupiter.api.extension.ExtendWith;
34 import org.mockito.Mock;
35 import org.mockito.junit.jupiter.MockitoExtension;
36 import org.onap.dcaegen2.collectors.datafile.utils.LoggingUtils;
37 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.config.DmaapConsumerConfiguration;
38 import org.springframework.http.HttpHeaders;
39 import org.springframework.http.HttpMethod;
40 import org.springframework.http.HttpStatus;
41 import org.springframework.web.reactive.function.client.ClientRequest;
42 import org.springframework.web.reactive.function.client.ClientResponse;
43 import org.springframework.web.reactive.function.client.WebClient;
44 import reactor.core.publisher.Mono;
45
46 @ExtendWith(MockitoExtension.class)
47 class DmaapWebClientTest {
48
49     @Mock
50     private DmaapConsumerConfiguration dmaapConsumerConfigurationMock;
51
52     @Mock
53     private ClientResponse clientResponseMock;
54
55     @Mock
56     private ClientRequest clientRequesteMock;
57
58     @Test
59     void buildsDMaaPReactiveWebClientProperly() {
60         when(dmaapConsumerConfigurationMock.dmaapContentType()).thenReturn("*/*");
61         WebClient dmaapWebClientUndetTest = new DmaapWebClient() //
62             .fromConfiguration(dmaapConsumerConfigurationMock) //
63             .build();
64
65         verify(dmaapConsumerConfigurationMock, times(1)).dmaapContentType();
66         assertNotNull(dmaapWebClientUndetTest);
67     }
68
69     @Test
70     public void logResponseSuccess() {
71         DmaapWebClient dmaapWebClientUndetTest = new DmaapWebClient();
72
73         when(clientResponseMock.statusCode()).thenReturn(HttpStatus.OK);
74
75         ListAppender<ILoggingEvent> logAppender = LoggingUtils.getLogListAppender(DmaapWebClient.class, true);
76         Mono<ClientResponse> logResponse = dmaapWebClientUndetTest.logResponse(clientResponseMock);
77
78         assertEquals(clientResponseMock, logResponse.block());
79
80         assertEquals(logAppender.list.get(0).getLevel(), Level.TRACE);
81         assertEquals(logAppender.list.get(0).getFormattedMessage(), "Response Status 200 OK");
82
83         logAppender.stop();
84     }
85
86     @Test
87     public void logRequestSuccess() throws URISyntaxException {
88         when(clientRequesteMock.url()).thenReturn(new URI("http://test"));
89         when(clientRequesteMock.method()).thenReturn(HttpMethod.GET);
90         HttpHeaders httpHeaders = new HttpHeaders();
91         httpHeaders.add("header", "value");
92         when(clientRequesteMock.headers()).thenReturn(httpHeaders);
93
94         DmaapWebClient dmaapWebClientUndetTest = new DmaapWebClient();
95
96         ListAppender<ILoggingEvent> logAppender = LoggingUtils.getLogListAppender(DmaapWebClient.class, true);
97         Mono<ClientRequest> logRequest = dmaapWebClientUndetTest.logRequest(clientRequesteMock);
98
99         assertEquals(clientRequesteMock, logRequest.block());
100
101         assertEquals(logAppender.list.get(0).getLevel(), Level.TRACE);
102         assertEquals("Request: GET http://test", logAppender.list.get(0).getFormattedMessage());
103         assertEquals(logAppender.list.get(1).getLevel(), Level.TRACE);
104         assertEquals("HTTP request headers: [header:\"value\"]", logAppender.list.get(1).getFormattedMessage());
105
106         logAppender.stop();
107     }
108 }