|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +package com.microsoft.applicationinsights.smoketest; |
| 5 | + |
| 6 | +import static com.microsoft.applicationinsights.smoketest.EnvironmentValue.TOMCAT_8_JAVA_11; |
| 7 | +import static com.microsoft.applicationinsights.smoketest.EnvironmentValue.TOMCAT_8_JAVA_17; |
| 8 | +import static com.microsoft.applicationinsights.smoketest.EnvironmentValue.TOMCAT_8_JAVA_21; |
| 9 | +import static com.microsoft.applicationinsights.smoketest.EnvironmentValue.TOMCAT_8_JAVA_8; |
| 10 | +import static org.assertj.core.api.Assertions.assertThat; |
| 11 | + |
| 12 | +import com.microsoft.applicationinsights.smoketest.schemav2.Data; |
| 13 | +import com.microsoft.applicationinsights.smoketest.schemav2.Envelope; |
| 14 | +import com.microsoft.applicationinsights.smoketest.schemav2.ExceptionData; |
| 15 | +import com.microsoft.applicationinsights.smoketest.schemav2.SeverityLevel; |
| 16 | +import java.util.List; |
| 17 | +import org.junit.jupiter.api.Test; |
| 18 | +import org.junit.jupiter.api.extension.RegisterExtension; |
| 19 | + |
| 20 | +@UseAgent |
| 21 | +abstract class ExceptionMessageHandlingTest { |
| 22 | + |
| 23 | + @RegisterExtension static final SmokeTestExtension testing = SmokeTestExtension.create(); |
| 24 | + |
| 25 | + @Test |
| 26 | + @TargetUri("/testExceptionWithoutMessage") |
| 27 | + void testExceptionWithoutMessage() throws Exception { |
| 28 | + List<Envelope> rdList = testing.mockedIngestion.waitForItems("RequestData", 1); |
| 29 | + |
| 30 | + Envelope rdEnvelope = rdList.get(0); |
| 31 | + String operationId = rdEnvelope.getTags().get("ai.operation.id"); |
| 32 | + List<Envelope> edList = |
| 33 | + testing.mockedIngestion.waitForItemsInOperation("ExceptionData", 1, operationId); |
| 34 | + |
| 35 | + Envelope edEnvelope = edList.get(0); |
| 36 | + ExceptionData ed = (ExceptionData) ((Data<?>) edEnvelope.getData()).getBaseData(); |
| 37 | + |
| 38 | + // Verify that exceptions without messages have their class name as the message |
| 39 | + // This prevents the 206 error: "Field 'message' on type 'ExceptionDetails' is required but |
| 40 | + // missing or empty" |
| 41 | + assertThat(ed.getExceptions().get(0).getTypeName()).isEqualTo("java.lang.NullPointerException"); |
| 42 | + assertThat(ed.getExceptions().get(0).getMessage()).isEqualTo("java.lang.NullPointerException"); |
| 43 | + assertThat(ed.getExceptions().get(0).getMessage()).isNotEmpty(); |
| 44 | + assertThat(ed.getSeverityLevel()).isEqualTo(SeverityLevel.ERROR); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + @TargetUri("/testExceptionWithEmptyMessage") |
| 49 | + void testExceptionWithEmptyMessage() throws Exception { |
| 50 | + List<Envelope> rdList = testing.mockedIngestion.waitForItems("RequestData", 1); |
| 51 | + |
| 52 | + Envelope rdEnvelope = rdList.get(0); |
| 53 | + String operationId = rdEnvelope.getTags().get("ai.operation.id"); |
| 54 | + List<Envelope> edList = |
| 55 | + testing.mockedIngestion.waitForItemsInOperation("ExceptionData", 1, operationId); |
| 56 | + |
| 57 | + Envelope edEnvelope = edList.get(0); |
| 58 | + ExceptionData ed = (ExceptionData) ((Data<?>) edEnvelope.getData()).getBaseData(); |
| 59 | + |
| 60 | + // Verify that exceptions with empty messages have their class name as the message |
| 61 | + assertThat(ed.getExceptions().get(0).getTypeName()).isEqualTo("java.lang.RuntimeException"); |
| 62 | + assertThat(ed.getExceptions().get(0).getMessage()).isEqualTo("java.lang.RuntimeException"); |
| 63 | + assertThat(ed.getExceptions().get(0).getMessage()).isNotEmpty(); |
| 64 | + assertThat(ed.getSeverityLevel()).isEqualTo(SeverityLevel.ERROR); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + @TargetUri("/testExceptionWithWhitespaceMessage") |
| 69 | + void testExceptionWithWhitespaceMessage() throws Exception { |
| 70 | + List<Envelope> rdList = testing.mockedIngestion.waitForItems("RequestData", 1); |
| 71 | + |
| 72 | + Envelope rdEnvelope = rdList.get(0); |
| 73 | + String operationId = rdEnvelope.getTags().get("ai.operation.id"); |
| 74 | + List<Envelope> edList = |
| 75 | + testing.mockedIngestion.waitForItemsInOperation("ExceptionData", 1, operationId); |
| 76 | + |
| 77 | + Envelope edEnvelope = edList.get(0); |
| 78 | + ExceptionData ed = (ExceptionData) ((Data<?>) edEnvelope.getData()).getBaseData(); |
| 79 | + |
| 80 | + // Verify that exceptions with whitespace-only messages have their class name as the message |
| 81 | + assertThat(ed.getExceptions().get(0).getTypeName()) |
| 82 | + .isEqualTo("java.lang.IllegalArgumentException"); |
| 83 | + assertThat(ed.getExceptions().get(0).getMessage()) |
| 84 | + .isEqualTo("java.lang.IllegalArgumentException"); |
| 85 | + assertThat(ed.getExceptions().get(0).getMessage()).isNotEmpty(); |
| 86 | + assertThat(ed.getSeverityLevel()).isEqualTo(SeverityLevel.ERROR); |
| 87 | + } |
| 88 | + |
| 89 | + @Environment(TOMCAT_8_JAVA_8) |
| 90 | + static class Tomcat8Java8Test extends ExceptionMessageHandlingTest {} |
| 91 | + |
| 92 | + @Environment(TOMCAT_8_JAVA_11) |
| 93 | + static class Tomcat8Java11Test extends ExceptionMessageHandlingTest {} |
| 94 | + |
| 95 | + @Environment(TOMCAT_8_JAVA_17) |
| 96 | + static class Tomcat8Java17Test extends ExceptionMessageHandlingTest {} |
| 97 | + |
| 98 | + @Environment(TOMCAT_8_JAVA_21) |
| 99 | + static class Tomcat8Java21Test extends ExceptionMessageHandlingTest {} |
| 100 | +} |
0 commit comments