Skip to content

Commit ce68488

Browse files
Copilottrask
andcommitted
Add ExceptionAssert class and extend TraceAssert to handle exception data
Co-authored-by: trask <[email protected]>
1 parent ffa8a8d commit ce68488

File tree

5 files changed

+157
-98
lines changed

5 files changed

+157
-98
lines changed

smoke-tests/apps/Logback/src/smokeTest/java/com/microsoft/applicationinsights/smoketest/LogbackDisabledTest.java

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,7 @@
44
package com.microsoft.applicationinsights.smoketest;
55

66
import static com.microsoft.applicationinsights.smoketest.EnvironmentValue.TOMCAT_8_JAVA_8;
7-
import static org.assertj.core.api.Assertions.assertThat;
87

9-
import com.microsoft.applicationinsights.smoketest.schemav2.Data;
10-
import com.microsoft.applicationinsights.smoketest.schemav2.Envelope;
11-
import com.microsoft.applicationinsights.smoketest.schemav2.ExceptionData;
12-
import com.microsoft.applicationinsights.smoketest.schemav2.RequestData;
13-
import java.util.List;
148
import org.junit.jupiter.api.Test;
159
import org.junit.jupiter.api.extension.RegisterExtension;
1610

@@ -33,31 +27,20 @@ void testDisabled() {
3327

3428
@Test
3529
@TargetUri("/testWithSpanException")
36-
void testWithSpanException() throws Exception {
30+
void testWithSpanException() {
3731
testing.waitAndAssertTrace(
3832
trace ->
3933
trace
4034
.hasRequestSatisying(
4135
request ->
4236
request.hasName("GET /Logback/testWithSpanException").hasSuccess(true))
43-
.hasMessageCount(0));
44-
45-
// Check that span exception is still captured
46-
List<Envelope> rdList = testing.mockedIngestion.waitForItems("RequestData", 1);
47-
Envelope rdEnvelope = rdList.get(0);
48-
String operationId = rdEnvelope.getTags().get("ai.operation.id");
49-
List<Envelope> edList =
50-
testing.mockedIngestion.waitForItemsInOperation("ExceptionData", 1, operationId);
51-
52-
Envelope edEnvelope = edList.get(0);
53-
ExceptionData ed = (ExceptionData) ((Data<?>) edEnvelope.getData()).getBaseData();
54-
55-
assertThat(ed.getExceptions().get(0).getTypeName()).isEqualTo("java.lang.RuntimeException");
56-
assertThat(ed.getExceptions().get(0).getMessage()).isEqualTo("Test Exception");
57-
assertThat(ed.getProperties()).isEmpty(); // this is not a logger-based exception
58-
59-
RequestData rd = (RequestData) ((Data<?>) rdEnvelope.getData()).getBaseData();
60-
SmokeTestExtension.assertParentChild(
61-
rd, rdEnvelope, edEnvelope, "GET /Logback/testWithSpanException");
37+
.hasMessageCount(0)
38+
.hasExceptionCount(1)
39+
.hasExceptionSatisying(
40+
exception ->
41+
exception
42+
.hasExceptionType("java.lang.RuntimeException")
43+
.hasExceptionMessage("Test Exception")
44+
.hasEmptyProperties())); // this is not a logger-based exception
6245
}
6346
}

smoke-tests/apps/Logback/src/smokeTest/java/com/microsoft/applicationinsights/smoketest/LogbackLevelOffTest.java

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,7 @@
44
package com.microsoft.applicationinsights.smoketest;
55

66
import static com.microsoft.applicationinsights.smoketest.EnvironmentValue.TOMCAT_8_JAVA_8;
7-
import static org.assertj.core.api.Assertions.assertThat;
87

9-
import com.microsoft.applicationinsights.smoketest.schemav2.Data;
10-
import com.microsoft.applicationinsights.smoketest.schemav2.Envelope;
11-
import com.microsoft.applicationinsights.smoketest.schemav2.ExceptionData;
12-
import com.microsoft.applicationinsights.smoketest.schemav2.RequestData;
13-
import java.util.List;
148
import org.junit.jupiter.api.Test;
159
import org.junit.jupiter.api.extension.RegisterExtension;
1610

@@ -33,31 +27,20 @@ void testDisabled() {
3327

3428
@Test
3529
@TargetUri("/testWithSpanException")
36-
void testWithSpanException() throws Exception {
30+
void testWithSpanException() {
3731
testing.waitAndAssertTrace(
3832
trace ->
3933
trace
4034
.hasRequestSatisying(
4135
request ->
4236
request.hasName("GET /Logback/testWithSpanException").hasSuccess(true))
43-
.hasMessageCount(0));
44-
45-
// Check that span exception is still captured
46-
List<Envelope> rdList = testing.mockedIngestion.waitForItems("RequestData", 1);
47-
Envelope rdEnvelope = rdList.get(0);
48-
String operationId = rdEnvelope.getTags().get("ai.operation.id");
49-
List<Envelope> edList =
50-
testing.mockedIngestion.waitForItemsInOperation("ExceptionData", 1, operationId);
51-
52-
Envelope edEnvelope = edList.get(0);
53-
ExceptionData ed = (ExceptionData) ((Data<?>) edEnvelope.getData()).getBaseData();
54-
55-
assertThat(ed.getExceptions().get(0).getTypeName()).isEqualTo("java.lang.RuntimeException");
56-
assertThat(ed.getExceptions().get(0).getMessage()).isEqualTo("Test Exception");
57-
assertThat(ed.getProperties()).isEmpty(); // this is not a logger-based exception
58-
59-
RequestData rd = (RequestData) ((Data<?>) rdEnvelope.getData()).getBaseData();
60-
SmokeTestExtension.assertParentChild(
61-
rd, rdEnvelope, edEnvelope, "GET /Logback/testWithSpanException");
37+
.hasMessageCount(0)
38+
.hasExceptionCount(1)
39+
.hasExceptionSatisying(
40+
exception ->
41+
exception
42+
.hasExceptionType("java.lang.RuntimeException")
43+
.hasExceptionMessage("Test Exception")
44+
.hasEmptyProperties())); // this is not a logger-based exception
6245
}
6346
}

smoke-tests/apps/Logback/src/smokeTest/java/com/microsoft/applicationinsights/smoketest/LogbackTest.java

Lines changed: 27 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,8 @@
1515
import static com.microsoft.applicationinsights.smoketest.EnvironmentValue.TOMCAT_8_JAVA_8_OPENJ9;
1616
import static com.microsoft.applicationinsights.smoketest.EnvironmentValue.WILDFLY_13_JAVA_8;
1717
import static com.microsoft.applicationinsights.smoketest.EnvironmentValue.WILDFLY_13_JAVA_8_OPENJ9;
18-
import static org.assertj.core.api.Assertions.assertThat;
1918

20-
import com.microsoft.applicationinsights.smoketest.schemav2.Data;
21-
import com.microsoft.applicationinsights.smoketest.schemav2.Envelope;
22-
import com.microsoft.applicationinsights.smoketest.schemav2.ExceptionData;
23-
import com.microsoft.applicationinsights.smoketest.schemav2.RequestData;
2419
import com.microsoft.applicationinsights.smoketest.schemav2.SeverityLevel;
25-
import java.util.List;
2620
import org.junit.jupiter.api.Test;
2721
import org.junit.jupiter.api.extension.RegisterExtension;
2822

@@ -103,7 +97,7 @@ void test() {
10397

10498
@Test
10599
@TargetUri("/testWithException")
106-
void testWithException() throws Exception {
100+
void testWithException() {
107101
testing.waitAndAssertTrace(
108102
trace ->
109103
trace
@@ -114,48 +108,35 @@ void testWithException() throws Exception {
114108
.hasSuccess(true)
115109
.hasProperty("_MS.ProcessedByMetricExtractors", "True")
116110
.hasNoParent())
117-
.hasMessageCount(0)); // No MessageData, only ExceptionData
118-
119-
// Check for exception data separately as it's not part of the trace assertion framework yet
120-
List<Envelope> rdList = testing.mockedIngestion.waitForItems("RequestData", 1);
121-
Envelope rdEnvelope = rdList.get(0);
122-
String operationId = rdEnvelope.getTags().get("ai.operation.id");
123-
List<Envelope> edList =
124-
testing.mockedIngestion.waitForItemsInOperation("ExceptionData", 1, operationId);
125-
assertThat(testing.mockedIngestion.getCountForType("EventData")).isZero();
126-
127-
Envelope edEnvelope = edList.get(0);
128-
assertThat(rdEnvelope.getSampleRate()).isNull();
129-
assertThat(edEnvelope.getSampleRate()).isNull();
130-
131-
RequestData rd = (RequestData) ((Data<?>) rdEnvelope.getData()).getBaseData();
132-
ExceptionData ed = (ExceptionData) ((Data<?>) edEnvelope.getData()).getBaseData();
133-
134-
assertThat(ed.getExceptions().get(0).getTypeName()).isEqualTo("java.lang.Exception");
135-
assertThat(ed.getExceptions().get(0).getMessage()).isEqualTo("Fake Exception");
136-
assertThat(ed.getSeverityLevel()).isEqualTo(SeverityLevel.ERROR);
137-
assertThat(ed.getProperties())
138-
.containsEntry("Logger Message", "This is an exception!")
139-
.containsEntry("SourceType", "Logger")
140-
.containsEntry("LoggerName", "smoketestapp")
141-
.containsKey("ThreadName")
142-
.containsEntry("MDC key", "MDC value");
111+
.hasMessageCount(0) // No MessageData, only ExceptionData
112+
.hasExceptionCount(1)
113+
.hasExceptionSatisying(
114+
exception ->
115+
exception
116+
.hasExceptionType("java.lang.Exception")
117+
.hasExceptionMessage("Fake Exception")
118+
.hasSeverityLevel(SeverityLevel.ERROR)
119+
.hasProperty("Logger Message", "This is an exception!")
120+
.hasProperty("SourceType", "Logger")
121+
.hasProperty("LoggerName", "smoketestapp")
122+
.hasPropertyKey("ThreadName")
123+
.hasProperty("MDC key", "MDC value")
124+
.hasPropertiesSize(isWildflyServer() ? 5 : 9)));
143125

126+
// Additional assertions for non-Wildfly servers
144127
if (!isWildflyServer()) {
145-
assertThat(ed.getProperties())
146-
.containsEntry("FileName", "LogbackWithExceptionServlet.java")
147-
.containsEntry(
148-
"ClassName",
149-
"com.microsoft.applicationinsights.smoketestapp.LogbackWithExceptionServlet")
150-
.containsEntry("MethodName", "doGet")
151-
.containsEntry("LineNumber", "21")
152-
.hasSize(9);
153-
} else {
154-
assertThat(ed.getProperties()).hasSize(5);
128+
testing.waitAndAssertTrace(
129+
trace ->
130+
trace.hasExceptionSatisying(
131+
exception ->
132+
exception
133+
.hasProperty("FileName", "LogbackWithExceptionServlet.java")
134+
.hasProperty(
135+
"ClassName",
136+
"com.microsoft.applicationinsights.smoketestapp.LogbackWithExceptionServlet")
137+
.hasProperty("MethodName", "doGet")
138+
.hasProperty("LineNumber", "21")));
155139
}
156-
157-
SmokeTestExtension.assertParentChild(
158-
rd, rdEnvelope, edEnvelope, "GET /Logback/testWithException");
159140
}
160141

161142
@Environment(TOMCAT_8_JAVA_8)
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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 org.assertj.core.api.Assertions.assertThat;
7+
8+
import com.google.errorprone.annotations.CanIgnoreReturnValue;
9+
import com.microsoft.applicationinsights.smoketest.schemav2.Data;
10+
import com.microsoft.applicationinsights.smoketest.schemav2.Envelope;
11+
import com.microsoft.applicationinsights.smoketest.schemav2.ExceptionData;
12+
import com.microsoft.applicationinsights.smoketest.schemav2.SeverityLevel;
13+
import org.assertj.core.api.AbstractAssert;
14+
15+
public class ExceptionAssert extends AbstractAssert<ExceptionAssert, Envelope> {
16+
17+
public ExceptionAssert(Envelope envelope) {
18+
super(envelope, ExceptionAssert.class);
19+
}
20+
21+
@CanIgnoreReturnValue
22+
public ExceptionAssert hasExceptionType(String typeName) {
23+
isNotNull();
24+
assertThat(getExceptionData().getExceptions().get(0).getTypeName()).isEqualTo(typeName);
25+
return this;
26+
}
27+
28+
@CanIgnoreReturnValue
29+
public ExceptionAssert hasExceptionMessage(String message) {
30+
isNotNull();
31+
assertThat(getExceptionData().getExceptions().get(0).getMessage()).isEqualTo(message);
32+
return this;
33+
}
34+
35+
@CanIgnoreReturnValue
36+
public ExceptionAssert hasSeverityLevel(SeverityLevel severityLevel) {
37+
isNotNull();
38+
assertThat(getExceptionData().getSeverityLevel()).isEqualTo(severityLevel);
39+
return this;
40+
}
41+
42+
@CanIgnoreReturnValue
43+
public ExceptionAssert hasProperty(String key, String value) {
44+
isNotNull();
45+
assertThat(getExceptionData().getProperties()).containsEntry(key, value);
46+
return this;
47+
}
48+
49+
@CanIgnoreReturnValue
50+
public ExceptionAssert hasPropertyKey(String key) {
51+
isNotNull();
52+
assertThat(getExceptionData().getProperties()).containsKey(key);
53+
return this;
54+
}
55+
56+
@CanIgnoreReturnValue
57+
public ExceptionAssert hasPropertiesSize(int size) {
58+
isNotNull();
59+
assertThat(getExceptionData().getProperties()).hasSize(size);
60+
return this;
61+
}
62+
63+
@CanIgnoreReturnValue
64+
public ExceptionAssert hasEmptyProperties() {
65+
isNotNull();
66+
assertThat(getExceptionData().getProperties()).isEmpty();
67+
return this;
68+
}
69+
70+
@CanIgnoreReturnValue
71+
public ExceptionAssert hasTag(String key, String value) {
72+
isNotNull();
73+
assertThat(actual.getTags()).containsEntry(key, value);
74+
return this;
75+
}
76+
77+
@CanIgnoreReturnValue
78+
public ExceptionAssert hasNoParent() {
79+
isNotNull();
80+
assertThat(getExceptionData().getProperties().get("ai.operation.parentId")).isNull();
81+
return this;
82+
}
83+
84+
@CanIgnoreReturnValue
85+
public ExceptionAssert hasParent(String parentId) {
86+
isNotNull();
87+
assertThat(getExceptionData().getProperties().get("ai.operation.parentId")).isEqualTo(parentId);
88+
return this;
89+
}
90+
91+
private ExceptionData getExceptionData() {
92+
Data<?> data = (Data<?>) actual.getData();
93+
return (ExceptionData) data.getBaseData();
94+
}
95+
}

smoke-tests/framework/src/main/java/com/microsoft/applicationinsights/smoketest/TraceAssert.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public class TraceAssert {
2121
private final List<Envelope> requests;
2222
private final List<Envelope> dependencies;
2323
private final List<Envelope> messages;
24+
private final List<Envelope> exceptions;
2425

2526
public TraceAssert(List<Envelope> trace) {
2627

@@ -42,6 +43,10 @@ public TraceAssert(List<Envelope> trace) {
4243
sorted.stream()
4344
.filter(envelope -> envelope.getData().getBaseType().equals("MessageData"))
4445
.collect(toList());
46+
exceptions =
47+
sorted.stream()
48+
.filter(envelope -> envelope.getData().getBaseType().equals("ExceptionData"))
49+
.collect(toList());
4550
}
4651

4752
@CanIgnoreReturnValue
@@ -69,6 +74,18 @@ public TraceAssert hasMessageSatisying(Consumer<MessageAssert> assertion) {
6974
return this;
7075
}
7176

77+
@CanIgnoreReturnValue
78+
public TraceAssert hasExceptionCount(int count) {
79+
assertThat(exceptions).hasSize(count);
80+
return this;
81+
}
82+
83+
@CanIgnoreReturnValue
84+
public TraceAssert hasExceptionSatisying(Consumer<ExceptionAssert> assertion) {
85+
assertThat(exceptions).anySatisfy(envelope -> assertion.accept(new ExceptionAssert(envelope)));
86+
return this;
87+
}
88+
7289
public String getRequestId(int index) {
7390
Data<?> data = (Data<?>) requests.get(index).getData();
7491
return ((RequestData) data.getBaseData()).getId();

0 commit comments

Comments
 (0)