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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
14 * ============LICENSE_END========================================================================
17 package org.onap.dcaegen2.collectors.datafile.service;
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;
25 import ch.qos.logback.classic.Level;
26 import ch.qos.logback.classic.spi.ILoggingEvent;
27 import ch.qos.logback.core.read.ListAppender;
30 import java.net.URISyntaxException;
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;
46 @ExtendWith(MockitoExtension.class)
47 class DmaapWebClientTest {
50 private DmaapConsumerConfiguration dmaapConsumerConfigurationMock;
53 private ClientResponse clientResponseMock;
56 private ClientRequest clientRequesteMock;
59 void buildsDMaaPReactiveWebClientProperly() {
60 when(dmaapConsumerConfigurationMock.dmaapContentType()).thenReturn("*/*");
61 WebClient dmaapWebClientUndetTest = new DmaapWebClient() //
62 .fromConfiguration(dmaapConsumerConfigurationMock) //
65 verify(dmaapConsumerConfigurationMock, times(1)).dmaapContentType();
66 assertNotNull(dmaapWebClientUndetTest);
70 public void logResponseSuccess() {
71 DmaapWebClient dmaapWebClientUndetTest = new DmaapWebClient();
73 when(clientResponseMock.statusCode()).thenReturn(HttpStatus.OK);
75 ListAppender<ILoggingEvent> logAppender = LoggingUtils.getLogListAppender(DmaapWebClient.class, true);
76 Mono<ClientResponse> logResponse = dmaapWebClientUndetTest.logResponse(clientResponseMock);
78 assertEquals(clientResponseMock, logResponse.block());
80 assertEquals(logAppender.list.get(0).getLevel(), Level.TRACE);
81 assertEquals(logAppender.list.get(0).getFormattedMessage(), "Response Status 200 OK");
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);
94 DmaapWebClient dmaapWebClientUndetTest = new DmaapWebClient();
96 ListAppender<ILoggingEvent> logAppender = LoggingUtils.getLogListAppender(DmaapWebClient.class, true);
97 Mono<ClientRequest> logRequest = dmaapWebClientUndetTest.logRequest(clientRequesteMock);
99 assertEquals(clientRequesteMock, logRequest.block());
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());