Skip to content

Commit

Permalink
Improve smoke tests around exceptions (#3899)
Browse files Browse the repository at this point in the history
  • Loading branch information
trask authored Oct 10, 2024
1 parent 06935bd commit d428e7e
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@
import com.microsoft.applicationinsights.smoketest.schemav2.Data;
import com.microsoft.applicationinsights.smoketest.schemav2.Envelope;
import com.microsoft.applicationinsights.smoketest.schemav2.EventData;
import com.microsoft.applicationinsights.smoketest.schemav2.ExceptionData;
import com.microsoft.applicationinsights.smoketest.schemav2.ExceptionDetails;
import com.microsoft.applicationinsights.smoketest.schemav2.RemoteDependencyData;
import com.microsoft.applicationinsights.smoketest.schemav2.RequestData;
import com.microsoft.applicationinsights.smoketest.schemav2.SeverityLevel;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
Expand Down Expand Up @@ -71,6 +74,10 @@ void testResultCodeWhenRestControllerThrows() throws Exception {
RequestData rd = testing.getTelemetryDataForType(0, "RequestData");
RemoteDependencyData rdd1 =
(RemoteDependencyData) ((Data<?>) rddEnvelope1.getData()).getBaseData();
ExceptionData ed = (ExceptionData) ((Data<?>) edEnvelope1.getData()).getBaseData();

List<ExceptionDetails> details = ed.getExceptions();
ExceptionDetails ex = details.get(0);

assertThat(rd.getName()).isEqualTo("GET /SpringBoot/throwsException");
assertThat(rd.getResponseCode()).isEqualTo("500");
Expand All @@ -85,6 +92,17 @@ void testResultCodeWhenRestControllerThrows() throws Exception {
assertThat(rdd1.getProperties()).isEmpty();
assertThat(rdd1.getSuccess()).isFalse();

assertThat(ex.getTypeName()).isEqualTo("javax.servlet.ServletException");
assertThat(ex.getMessage()).isEqualTo("This is an exception");
assertThat(ed.getSeverityLevel()).isEqualTo(SeverityLevel.ERROR);
assertThat(ed.getProperties())
.containsKey("Logger Message"); // specific message varies by app server
assertThat(ed.getProperties()).containsEntry("SourceType", "Logger");
assertThat(ed.getProperties())
.containsKey("LoggerName"); // specific logger varies by app server
assertThat(ed.getProperties()).containsKey("ThreadName");
assertThat(ed.getProperties()).hasSize(4);

SmokeTestExtension.assertParentChild(
rd, rdEnvelope, edEnvelope1, "GET /SpringBoot/throwsException");
SmokeTestExtension.assertParentChild(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@
import com.microsoft.applicationinsights.smoketest.schemav2.Data;
import com.microsoft.applicationinsights.smoketest.schemav2.Envelope;
import com.microsoft.applicationinsights.smoketest.schemav2.EventData;
import com.microsoft.applicationinsights.smoketest.schemav2.ExceptionData;
import com.microsoft.applicationinsights.smoketest.schemav2.ExceptionDetails;
import com.microsoft.applicationinsights.smoketest.schemav2.RemoteDependencyData;
import com.microsoft.applicationinsights.smoketest.schemav2.RequestData;
import com.microsoft.applicationinsights.smoketest.schemav2.SeverityLevel;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
Expand Down Expand Up @@ -63,17 +66,34 @@ void testResultCodeWhenRestControllerThrows() throws Exception {

Envelope edEnvelope1 = exceptions.get(0);

// assert on edEnvelope1

assertThat(rdEnvelope.getSampleRate()).isNull();
assertThat(edEnvelope1.getSampleRate()).isNull();

RequestData rd = testing.getTelemetryDataForType(0, "RequestData");
ExceptionData ed = (ExceptionData) ((Data<?>) edEnvelope1.getData()).getBaseData();

List<ExceptionDetails> details = ed.getExceptions();
ExceptionDetails ex = details.get(0);

assertThat(rd.getName()).isEqualTo("GET /SpringBoot/throwsException");
assertThat(rd.getResponseCode()).isEqualTo("500");
assertThat(rd.getProperties())
.containsExactly(entry("_MS.ProcessedByMetricExtractors", "True"));
assertThat(rd.getSuccess()).isFalse();

assertThat(ex.getTypeName()).isEqualTo("javax.servlet.ServletException");
assertThat(ex.getMessage()).isEqualTo("This is an exception");
assertThat(ed.getSeverityLevel()).isEqualTo(SeverityLevel.ERROR);
assertThat(ed.getProperties())
.containsKey("Logger Message"); // specific message varies by app server
assertThat(ed.getProperties()).containsEntry("SourceType", "Logger");
assertThat(ed.getProperties())
.containsKey("LoggerName"); // specific logger varies by app server
assertThat(ed.getProperties()).containsKey("ThreadName");
assertThat(ed.getProperties()).hasSize(4);

SmokeTestExtension.assertParentChild(
rd, rdEnvelope, edEnvelope1, "GET /SpringBoot/throwsException");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,9 @@ protected SpringApplicationBuilder configure(SpringApplicationBuilder applicatio
public void fixedRateScheduler() {
System.out.println("Hello world.");
}

@Scheduled(fixedRate = 100)
public void exceptional() {
throw new RuntimeException("exceptional");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@

import com.microsoft.applicationinsights.smoketest.schemav2.Data;
import com.microsoft.applicationinsights.smoketest.schemav2.Envelope;
import com.microsoft.applicationinsights.smoketest.schemav2.ExceptionData;
import com.microsoft.applicationinsights.smoketest.schemav2.ExceptionDetails;
import com.microsoft.applicationinsights.smoketest.schemav2.RequestData;
import com.microsoft.applicationinsights.smoketest.schemav2.SeverityLevel;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.function.Predicate;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -68,6 +72,41 @@ public boolean test(Envelope input) {
2,
10,
TimeUnit.SECONDS);

List<Envelope> exceptionEnvelopes =
testing.mockedIngestion.getItemsEnvelopeDataType("ExceptionData");

assertThat(exceptionEnvelopes)
.anySatisfy(
envelope -> {
ExceptionData ed = (ExceptionData) ((Data<?>) envelope.getData()).getBaseData();
List<ExceptionDetails> details = ed.getExceptions();
ExceptionDetails ex = details.get(0);
assertThat(ex.getTypeName()).isEqualTo("java.lang.RuntimeException");
assertThat(ex.getMessage()).isEqualTo("exceptional");
assertThat(ed.getSeverityLevel()).isEqualTo(SeverityLevel.ERROR);
assertThat(ed.getProperties())
.containsEntry("Logger Message", "Unexpected error occurred in scheduled task");
assertThat(ed.getProperties()).containsEntry("SourceType", "Logger");
assertThat(ed.getProperties())
.containsEntry(
"LoggerName",
"org.springframework.scheduling.support.TaskUtils$LoggingErrorHandler");
assertThat(ed.getProperties()).containsKey("ThreadName");
assertThat(ed.getProperties()).hasSize(4);
});

assertThat(exceptionEnvelopes)
.anySatisfy(
envelope -> {
ExceptionData ed = (ExceptionData) ((Data<?>) envelope.getData()).getBaseData();
List<ExceptionDetails> details = ed.getExceptions();
ExceptionDetails ex = details.get(0);
assertThat(ex.getTypeName()).isEqualTo("java.lang.RuntimeException");
assertThat(ex.getMessage()).isEqualTo("exceptional");
assertThat(ed.getSeverityLevel()).isNull();
assertThat(ed.getProperties()).isEmpty();
});
}

@Environment(TOMCAT_8_JAVA_8)
Expand Down

0 comments on commit d428e7e

Please sign in to comment.