From db08f580e2aad6ae52925050857e328b44bf5b32 Mon Sep 17 00:00:00 2001 From: Sima Zhu Date: Tue, 10 Dec 2019 16:18:58 -0800 Subject: [PATCH 1/5] Added some tests for make up the logging coverage --- .../core/util/logging/ClientLoggerTests.java | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) 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..8c1f1c799967 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,74 @@ 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 logExceptionStackTraceWithoutException() { + 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 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 From 521fa5ec26c9953ad611bfde479cb356796bd7ff Mon Sep 17 00:00:00 2001 From: Sima Zhu Date: Tue, 10 Dec 2019 16:37:00 -0800 Subject: [PATCH 2/5] Removed the extra one --- .../core/util/logging/ClientLoggerTests.java | 16 ---------------- 1 file changed, 16 deletions(-) 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 8c1f1c799967..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 @@ -186,22 +186,6 @@ public void logExceptionStackTraceWithNoLogLevel() { assertTrue(logValues.isEmpty()); } - /** - * Tests that logging an exception when the log level is ERROR the stack trace is logged. - */ - @Test - public void logExceptionStackTraceWithoutException() { - 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 when the log level is ERROR the stack trace is logged. */ From 55c3a102a2bb27bb60800ebee539cda76262bc87 Mon Sep 17 00:00:00 2001 From: Sima Zhu Date: Tue, 10 Dec 2019 17:03:56 -0800 Subject: [PATCH 3/5] Added tests for fluxUtils --- .../java/com/azure/core/util/FluxUtilTest.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) 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..3295e855a2cd 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,8 @@ package com.azure.core.util; +import java.lang.reflect.ParameterizedType; +import java.nio.ByteBuffer; import org.junit.jupiter.api.Test; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; @@ -12,6 +14,7 @@ import java.util.List; 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 +63,17 @@ public void toReactorContext() { assertEquals("value2", reactorContext.get("key2")); } + @Test + public void testIsFluxByteBufferInvalidType() { + assertFalse(FluxUtil.isFluxByteBuffer(Mono.class)); + } + + @Test + public void testIsFluxByteBufferValidType() { + Flux; + assertTrue(FluxUtil.isFluxByteBuffer(body.getClass())); + } + private Mono getSingle() { return FluxUtil.withContext(this::serviceCallSingle); } From 98480be5b7635e9a1adf28b2884f4a019165e440 Mon Sep 17 00:00:00 2001 From: Sima Zhu Date: Tue, 10 Dec 2019 18:07:44 -0800 Subject: [PATCH 4/5] Added tests for FluxUtils --- .../com/azure/core/util/FluxUtilTest.java | 59 +++++++++++++++++-- 1 file changed, 55 insertions(+), 4 deletions(-) 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 3295e855a2cd..148b442605ec 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,7 +3,15 @@ package com.azure.core.util; -import java.lang.reflect.ParameterizedType; +import com.azure.core.http.HttpHeaders; +import com.azure.core.http.HttpMethod; +import com.azure.core.http.HttpRequest; +import com.azure.core.http.rest.PagedFlux; +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; @@ -12,6 +20,7 @@ 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; @@ -69,9 +78,51 @@ public void testIsFluxByteBufferInvalidType() { } @Test - public void testIsFluxByteBufferValidType() { - Flux; - assertTrue(FluxUtil.isFluxByteBuffer(body.getClass())); + 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() { From ab674fd1c57a1639514503b1cfcf603da31c2590 Mon Sep 17 00:00:00 2001 From: Sima Zhu Date: Wed, 11 Dec 2019 10:12:57 -0800 Subject: [PATCH 5/5] Fixed compilation and linting --- .../src/test/java/com/azure/core/util/FluxUtilTest.java | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) 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 148b442605ec..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 @@ -6,7 +6,6 @@ import com.azure.core.http.HttpHeaders; import com.azure.core.http.HttpMethod; import com.azure.core.http.HttpRequest; -import com.azure.core.http.rest.PagedFlux; import com.azure.core.http.rest.Response; import com.azure.core.http.rest.SimpleResponse; import com.azure.core.util.logging.ClientLogger; @@ -78,19 +77,19 @@ public void testIsFluxByteBufferInvalidType() { } @Test - public void testIsFluxByteBufferValidType() throws Exception{ + public void testIsFluxByteBufferValidType() throws Exception { Method method = FluxUtilTest.class.getMethod("mockReturnType"); Type returnType = method.getGenericReturnType(); assertTrue(FluxUtil.isFluxByteBuffer(returnType)); } @Test - public void testToMono(){ + public void testToMono() { String testValue = "some value"; - Response response = new SimpleResponse(new HttpRequest(HttpMethod.GET, "http://www.test.com"), + 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)) + .assertNext(val -> assertEquals(val, testValue)) .verifyComplete(); }