Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,26 @@

package com.azure.core.util;

import com.azure.core.http.HttpHeaders;
import com.azure.core.http.HttpMethod;
import com.azure.core.http.HttpRequest;
import com.azure.core.http.rest.Response;
import com.azure.core.http.rest.SimpleResponse;
import com.azure.core.util.logging.ClientLogger;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.nio.ByteBuffer;
import org.junit.jupiter.api.Test;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import reactor.test.StepVerifier;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class FluxUtilTest {
Expand Down Expand Up @@ -60,6 +71,59 @@ public void toReactorContext() {
assertEquals("value2", reactorContext.get("key2"));
}

@Test
public void testIsFluxByteBufferInvalidType() {
assertFalse(FluxUtil.isFluxByteBuffer(Mono.class));
}

@Test
public void testIsFluxByteBufferValidType() throws Exception {
Method method = FluxUtilTest.class.getMethod("mockReturnType");
Type returnType = method.getGenericReturnType();
assertTrue(FluxUtil.isFluxByteBuffer(returnType));
}

@Test
public void testToMono() {
String testValue = "some value";
Response<String> response = new SimpleResponse<String>(new HttpRequest(HttpMethod.GET, "http://www.test.com"),
202, new HttpHeaders(), testValue);
StepVerifier.create(FluxUtil.toMono(response))
.assertNext(val -> assertEquals(val, testValue))
.verifyComplete();
}

@Test
public void testMonoError() {
String errMsg = "It is an error message";
RuntimeException ex = new RuntimeException(errMsg);
ClientLogger logger = new ClientLogger(FluxUtilTest.class);
StepVerifier.create(FluxUtil.monoError(logger, ex))
.verifyErrorMessage(errMsg);
}

@Test
public void testFluxError() {
String errMsg = "It is an error message";
RuntimeException ex = new RuntimeException(errMsg);
ClientLogger logger = new ClientLogger(FluxUtilTest.class);
StepVerifier.create(FluxUtil.fluxError(logger, ex))
.verifyErrorMessage(errMsg);
}

@Test
public void testPageFluxError() {
String errMsg = "It is an error message";
RuntimeException ex = new RuntimeException(errMsg);
ClientLogger logger = new ClientLogger(FluxUtilTest.class);
StepVerifier.create(FluxUtil.pagedFluxError(logger, ex))
.verifyErrorMessage(errMsg);
}

public Flux<ByteBuffer> mockReturnType() {
return Flux.just(ByteBuffer.wrap(new byte[0]));
}

private Mono<String> getSingle() {
return FluxUtil.withContext(this::serviceCallSingle);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,58 @@ public void logExceptionStackTrace() {
assertTrue(logValues.contains(runtimeException.getStackTrace()[0].toString()));
}

/**
* Tests that logging an exception when the log level is ERROR the stack trace is logged.
*/
@Test
public void logExceptionStackTraceWithErrorLevel() {
String logMessage = "This is an exception";
String exceptionMessage = "An exception message";
RuntimeException runtimeException = createRuntimeException(exceptionMessage);

String originalLogLevel = setupLogLevel(1);
logMessage(new ClientLogger(ClientLoggerTests.class), 4, logMessage, runtimeException);
setPropertyToOriginalOrClear(Configuration.PROPERTY_AZURE_LOG_LEVEL, originalLogLevel);

String logValues = new String(logCaptureStream.toByteArray(), StandardCharsets.UTF_8);
assertTrue(logValues.contains(logMessage + System.lineSeparator() + runtimeException.getMessage()));
assertTrue(logValues.contains(runtimeException.getStackTrace()[0].toString()));
}


/**
* Tests that logging an exception when the log level is ERROR the stack trace is logged.
*/
@Test
public void logExceptionStackTraceWithNoLogLevel() {
String logMessage = "This is an exception";
String exceptionMessage = "An exception message";
RuntimeException runtimeException = createRuntimeException(exceptionMessage);

String originalLogLevel = setupLogLevel(1);
logMessage(new ClientLogger(ClientLoggerTests.class), 5, logMessage, runtimeException);
setPropertyToOriginalOrClear(Configuration.PROPERTY_AZURE_LOG_LEVEL, originalLogLevel);

String logValues = new String(logCaptureStream.toByteArray(), StandardCharsets.UTF_8);
assertTrue(logValues.isEmpty());
}

/**
* Tests that logging an exception when the log level is ERROR the stack trace is logged.
*/
@Test
public void logExceptionWithInvalidLogLevel() {
String logMessage = "This is an exception";
Object runtimeException = new Object();

String originalLogLevel = setupLogLevel(1);
logMessage(new ClientLogger(ClientLoggerTests.class), 3, logMessage, runtimeException);
setPropertyToOriginalOrClear(Configuration.PROPERTY_AZURE_LOG_LEVEL, originalLogLevel);

String logValues = new String(logCaptureStream.toByteArray(), StandardCharsets.UTF_8);
assertTrue(logValues.contains(logMessage));
}

/**
* Tests that logging an exception as warning won't include the stack trace when the environment log level isn't
* VERBOSE. Additionally, this tests that the exception message isn't logged twice as logging an exception uses
Expand Down