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