diff --git a/sdk/core/azure-core/src/test/java/com/azure/core/util/FluxUtilTest.java b/sdk/core/azure-core/src/test/java/com/azure/core/util/FluxUtilTest.java index 7ed9764670a5..f354d1c5721d 100644 --- a/sdk/core/azure-core/src/test/java/com/azure/core/util/FluxUtilTest.java +++ b/sdk/core/azure-core/src/test/java/com/azure/core/util/FluxUtilTest.java @@ -3,6 +3,15 @@ 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; @@ -10,8 +19,10 @@ 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 { @@ -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 response = new SimpleResponse(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 mockReturnType() { + return Flux.just(ByteBuffer.wrap(new byte[0])); + } + private Mono getSingle() { return FluxUtil.withContext(this::serviceCallSingle); } diff --git a/sdk/core/azure-core/src/test/java/com/azure/core/util/logging/ClientLoggerTests.java b/sdk/core/azure-core/src/test/java/com/azure/core/util/logging/ClientLoggerTests.java index 48454669bbae..be652413517d 100644 --- a/sdk/core/azure-core/src/test/java/com/azure/core/util/logging/ClientLoggerTests.java +++ b/sdk/core/azure-core/src/test/java/com/azure/core/util/logging/ClientLoggerTests.java @@ -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