Skip to content

Commit d428e7e

Browse files
authored
Improve smoke tests around exceptions (#3899)
1 parent 06935bd commit d428e7e

File tree

4 files changed

+82
-0
lines changed

4 files changed

+82
-0
lines changed

smoke-tests/apps/SpringBoot/src/smokeTest/java/com/microsoft/applicationinsights/smoketest/SpringBootControllerSpansEnabledTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@
1010
import com.microsoft.applicationinsights.smoketest.schemav2.Data;
1111
import com.microsoft.applicationinsights.smoketest.schemav2.Envelope;
1212
import com.microsoft.applicationinsights.smoketest.schemav2.EventData;
13+
import com.microsoft.applicationinsights.smoketest.schemav2.ExceptionData;
14+
import com.microsoft.applicationinsights.smoketest.schemav2.ExceptionDetails;
1315
import com.microsoft.applicationinsights.smoketest.schemav2.RemoteDependencyData;
1416
import com.microsoft.applicationinsights.smoketest.schemav2.RequestData;
17+
import com.microsoft.applicationinsights.smoketest.schemav2.SeverityLevel;
1518
import java.util.List;
1619
import org.junit.jupiter.api.Test;
1720
import org.junit.jupiter.api.extension.RegisterExtension;
@@ -71,6 +74,10 @@ void testResultCodeWhenRestControllerThrows() throws Exception {
7174
RequestData rd = testing.getTelemetryDataForType(0, "RequestData");
7275
RemoteDependencyData rdd1 =
7376
(RemoteDependencyData) ((Data<?>) rddEnvelope1.getData()).getBaseData();
77+
ExceptionData ed = (ExceptionData) ((Data<?>) edEnvelope1.getData()).getBaseData();
78+
79+
List<ExceptionDetails> details = ed.getExceptions();
80+
ExceptionDetails ex = details.get(0);
7481

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

95+
assertThat(ex.getTypeName()).isEqualTo("javax.servlet.ServletException");
96+
assertThat(ex.getMessage()).isEqualTo("This is an exception");
97+
assertThat(ed.getSeverityLevel()).isEqualTo(SeverityLevel.ERROR);
98+
assertThat(ed.getProperties())
99+
.containsKey("Logger Message"); // specific message varies by app server
100+
assertThat(ed.getProperties()).containsEntry("SourceType", "Logger");
101+
assertThat(ed.getProperties())
102+
.containsKey("LoggerName"); // specific logger varies by app server
103+
assertThat(ed.getProperties()).containsKey("ThreadName");
104+
assertThat(ed.getProperties()).hasSize(4);
105+
88106
SmokeTestExtension.assertParentChild(
89107
rd, rdEnvelope, edEnvelope1, "GET /SpringBoot/throwsException");
90108
SmokeTestExtension.assertParentChild(

smoke-tests/apps/SpringBoot/src/smokeTest/java/com/microsoft/applicationinsights/smoketest/SpringBootTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@
1717
import com.microsoft.applicationinsights.smoketest.schemav2.Data;
1818
import com.microsoft.applicationinsights.smoketest.schemav2.Envelope;
1919
import com.microsoft.applicationinsights.smoketest.schemav2.EventData;
20+
import com.microsoft.applicationinsights.smoketest.schemav2.ExceptionData;
21+
import com.microsoft.applicationinsights.smoketest.schemav2.ExceptionDetails;
2022
import com.microsoft.applicationinsights.smoketest.schemav2.RemoteDependencyData;
2123
import com.microsoft.applicationinsights.smoketest.schemav2.RequestData;
24+
import com.microsoft.applicationinsights.smoketest.schemav2.SeverityLevel;
2225
import java.util.List;
2326
import org.junit.jupiter.api.Test;
2427
import org.junit.jupiter.api.extension.RegisterExtension;
@@ -63,17 +66,34 @@ void testResultCodeWhenRestControllerThrows() throws Exception {
6366

6467
Envelope edEnvelope1 = exceptions.get(0);
6568

69+
// assert on edEnvelope1
70+
6671
assertThat(rdEnvelope.getSampleRate()).isNull();
6772
assertThat(edEnvelope1.getSampleRate()).isNull();
6873

6974
RequestData rd = testing.getTelemetryDataForType(0, "RequestData");
75+
ExceptionData ed = (ExceptionData) ((Data<?>) edEnvelope1.getData()).getBaseData();
76+
77+
List<ExceptionDetails> details = ed.getExceptions();
78+
ExceptionDetails ex = details.get(0);
7079

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

86+
assertThat(ex.getTypeName()).isEqualTo("javax.servlet.ServletException");
87+
assertThat(ex.getMessage()).isEqualTo("This is an exception");
88+
assertThat(ed.getSeverityLevel()).isEqualTo(SeverityLevel.ERROR);
89+
assertThat(ed.getProperties())
90+
.containsKey("Logger Message"); // specific message varies by app server
91+
assertThat(ed.getProperties()).containsEntry("SourceType", "Logger");
92+
assertThat(ed.getProperties())
93+
.containsKey("LoggerName"); // specific logger varies by app server
94+
assertThat(ed.getProperties()).containsKey("ThreadName");
95+
assertThat(ed.getProperties()).hasSize(4);
96+
7797
SmokeTestExtension.assertParentChild(
7898
rd, rdEnvelope, edEnvelope1, "GET /SpringBoot/throwsException");
7999
}

smoke-tests/apps/SpringScheduling/src/main/java/com/microsoft/applicationinsights/smoketestapp/SpringSchedulingApp.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,9 @@ protected SpringApplicationBuilder configure(SpringApplicationBuilder applicatio
2222
public void fixedRateScheduler() {
2323
System.out.println("Hello world.");
2424
}
25+
26+
@Scheduled(fixedRate = 100)
27+
public void exceptional() {
28+
throw new RuntimeException("exceptional");
29+
}
2530
}

smoke-tests/apps/SpringScheduling/src/smokeTest/java/com/microsoft/applicationinsights/smoketest/SpringSchedulingTest.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@
1515

1616
import com.microsoft.applicationinsights.smoketest.schemav2.Data;
1717
import com.microsoft.applicationinsights.smoketest.schemav2.Envelope;
18+
import com.microsoft.applicationinsights.smoketest.schemav2.ExceptionData;
19+
import com.microsoft.applicationinsights.smoketest.schemav2.ExceptionDetails;
1820
import com.microsoft.applicationinsights.smoketest.schemav2.RequestData;
21+
import com.microsoft.applicationinsights.smoketest.schemav2.SeverityLevel;
22+
import java.util.List;
1923
import java.util.concurrent.TimeUnit;
2024
import java.util.function.Predicate;
2125
import org.junit.jupiter.api.Test;
@@ -68,6 +72,41 @@ public boolean test(Envelope input) {
6872
2,
6973
10,
7074
TimeUnit.SECONDS);
75+
76+
List<Envelope> exceptionEnvelopes =
77+
testing.mockedIngestion.getItemsEnvelopeDataType("ExceptionData");
78+
79+
assertThat(exceptionEnvelopes)
80+
.anySatisfy(
81+
envelope -> {
82+
ExceptionData ed = (ExceptionData) ((Data<?>) envelope.getData()).getBaseData();
83+
List<ExceptionDetails> details = ed.getExceptions();
84+
ExceptionDetails ex = details.get(0);
85+
assertThat(ex.getTypeName()).isEqualTo("java.lang.RuntimeException");
86+
assertThat(ex.getMessage()).isEqualTo("exceptional");
87+
assertThat(ed.getSeverityLevel()).isEqualTo(SeverityLevel.ERROR);
88+
assertThat(ed.getProperties())
89+
.containsEntry("Logger Message", "Unexpected error occurred in scheduled task");
90+
assertThat(ed.getProperties()).containsEntry("SourceType", "Logger");
91+
assertThat(ed.getProperties())
92+
.containsEntry(
93+
"LoggerName",
94+
"org.springframework.scheduling.support.TaskUtils$LoggingErrorHandler");
95+
assertThat(ed.getProperties()).containsKey("ThreadName");
96+
assertThat(ed.getProperties()).hasSize(4);
97+
});
98+
99+
assertThat(exceptionEnvelopes)
100+
.anySatisfy(
101+
envelope -> {
102+
ExceptionData ed = (ExceptionData) ((Data<?>) envelope.getData()).getBaseData();
103+
List<ExceptionDetails> details = ed.getExceptions();
104+
ExceptionDetails ex = details.get(0);
105+
assertThat(ex.getTypeName()).isEqualTo("java.lang.RuntimeException");
106+
assertThat(ex.getMessage()).isEqualTo("exceptional");
107+
assertThat(ed.getSeverityLevel()).isNull();
108+
assertThat(ed.getProperties()).isEmpty();
109+
});
71110
}
72111

73112
@Environment(TOMCAT_8_JAVA_8)

0 commit comments

Comments
 (0)