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;
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;
44 @ExtendWith(MockitoExtension.class)
45 class DmaapWebClientTest {
48 private DmaapConsumerConfiguration dmaapConsumerConfigurationMock;
51 private ClientResponse clientResponseMock;
54 private ClientRequest clientRequesteMock;
57 void buildsDMaaPReactiveWebClientProperly() {
58 when(dmaapConsumerConfigurationMock.dmaapContentType()).thenReturn("*/*");
59 WebClient dmaapWebClientUndetTest = new DmaapWebClient() //
60 .fromConfiguration(dmaapConsumerConfigurationMock) //
63 verify(dmaapConsumerConfigurationMock, times(1)).dmaapContentType();
64 assertNotNull(dmaapWebClientUndetTest);
68 public void logResponseSuccess() {
69 DmaapWebClient dmaapWebClientUndetTest = new DmaapWebClient();
71 when(clientResponseMock.statusCode()).thenReturn(HttpStatus.OK);
73 ListAppender<ILoggingEvent> logAppender = LoggingUtils.getLogListAppender(DmaapWebClient.class, true);
74 Mono<ClientResponse> logResponse = dmaapWebClientUndetTest.logResponse(clientResponseMock);
76 assertEquals(clientResponseMock, logResponse.block());
78 assertEquals(logAppender.list.get(0).getLevel(), Level.TRACE);
79 assertEquals(logAppender.list.get(0).getFormattedMessage(), "Response Status 200 OK");
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);
92 DmaapWebClient dmaapWebClientUndetTest = new DmaapWebClient();
94 ListAppender<ILoggingEvent> logAppender = LoggingUtils.getLogListAppender(DmaapWebClient.class, true);
95 Mono<ClientRequest> logRequest = dmaapWebClientUndetTest.logRequest(clientRequesteMock);
97 assertEquals(clientRequesteMock, logRequest.block());
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());