Skip to content

Commit 75d9db9

Browse files
Copilottrask
andcommitted
Add MessageAssert and ExceptionAssert classes and extend TraceAssert
Co-authored-by: trask <[email protected]>
1 parent aaacaa5 commit 75d9db9

File tree

3 files changed

+255
-0
lines changed

3 files changed

+255
-0
lines changed
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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 java.util.HashMap;
14+
import java.util.Map;
15+
import org.assertj.core.api.AbstractAssert;
16+
17+
public class ExceptionAssert extends AbstractAssert<ExceptionAssert, Envelope> {
18+
19+
public ExceptionAssert(Envelope envelope) {
20+
super(envelope, ExceptionAssert.class);
21+
}
22+
23+
@CanIgnoreReturnValue
24+
public ExceptionAssert hasExceptionType(String exceptionType) {
25+
isNotNull();
26+
assertThat(getExceptionData().getExceptions()).isNotEmpty();
27+
assertThat(getExceptionData().getExceptions().get(0).getTypeName()).isEqualTo(exceptionType);
28+
return this;
29+
}
30+
31+
@CanIgnoreReturnValue
32+
public ExceptionAssert hasExceptionMessage(String message) {
33+
isNotNull();
34+
assertThat(getExceptionData().getExceptions()).isNotEmpty();
35+
assertThat(getExceptionData().getExceptions().get(0).getMessage()).isEqualTo(message);
36+
return this;
37+
}
38+
39+
@CanIgnoreReturnValue
40+
public ExceptionAssert hasSeverityLevel(SeverityLevel severityLevel) {
41+
isNotNull();
42+
assertThat(getExceptionData().getSeverityLevel()).isEqualTo(severityLevel);
43+
return this;
44+
}
45+
46+
@CanIgnoreReturnValue
47+
public ExceptionAssert hasProperty(String key, String value) {
48+
isNotNull();
49+
assertThat(getExceptionData().getProperties()).containsEntry(key, value);
50+
return this;
51+
}
52+
53+
@CanIgnoreReturnValue
54+
public ExceptionAssert hasPropertyKey(String key) {
55+
isNotNull();
56+
assertThat(getExceptionData().getProperties()).containsKey(key);
57+
return this;
58+
}
59+
60+
@SafeVarargs
61+
@CanIgnoreReturnValue
62+
public final ExceptionAssert hasPropertiesExactly(Map.Entry<String, String>... entries) {
63+
Map<String, String> map = new HashMap<>();
64+
for (Map.Entry<String, String> entry : entries) {
65+
map.put(entry.getKey(), entry.getValue());
66+
}
67+
68+
isNotNull();
69+
ExceptionData exceptionData = getExceptionData();
70+
assertThat(exceptionData.getProperties()).containsExactlyInAnyOrderEntriesOf(map);
71+
return this;
72+
}
73+
74+
@CanIgnoreReturnValue
75+
public ExceptionAssert hasPropertiesSize(int expectedSize) {
76+
isNotNull();
77+
assertThat(getExceptionData().getProperties()).hasSize(expectedSize);
78+
return this;
79+
}
80+
81+
@CanIgnoreReturnValue
82+
public ExceptionAssert hasEmptyProperties() {
83+
isNotNull();
84+
assertThat(getExceptionData().getProperties()).isEmpty();
85+
return this;
86+
}
87+
88+
@CanIgnoreReturnValue
89+
public ExceptionAssert hasTag(String key, String value) {
90+
isNotNull();
91+
assertThat(actual.getTags()).containsEntry(key, value);
92+
return this;
93+
}
94+
95+
@CanIgnoreReturnValue
96+
public ExceptionAssert hasNoParent() {
97+
isNotNull();
98+
assertThat(actual.getTags()).doesNotContainKey("ai.operation.parentId");
99+
return this;
100+
}
101+
102+
@CanIgnoreReturnValue
103+
public ExceptionAssert hasParent(String parentId) {
104+
isNotNull();
105+
assertThat(actual.getTags()).containsEntry("ai.operation.parentId", parentId);
106+
return this;
107+
}
108+
109+
@CanIgnoreReturnValue
110+
public ExceptionAssert hasNoSampleRate() {
111+
isNotNull();
112+
assertThat(actual.getSampleRate()).isNull();
113+
return this;
114+
}
115+
116+
private ExceptionData getExceptionData() {
117+
Data<?> data = (Data<?>) actual.getData();
118+
return (ExceptionData) data.getBaseData();
119+
}
120+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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.MessageData;
12+
import com.microsoft.applicationinsights.smoketest.schemav2.SeverityLevel;
13+
import java.util.HashMap;
14+
import java.util.Map;
15+
import org.assertj.core.api.AbstractAssert;
16+
17+
public class MessageAssert extends AbstractAssert<MessageAssert, Envelope> {
18+
19+
public MessageAssert(Envelope envelope) {
20+
super(envelope, MessageAssert.class);
21+
}
22+
23+
@CanIgnoreReturnValue
24+
public MessageAssert hasMessage(String message) {
25+
isNotNull();
26+
assertThat(getMessageData().getMessage()).isEqualTo(message);
27+
return this;
28+
}
29+
30+
@CanIgnoreReturnValue
31+
public MessageAssert hasSeverityLevel(SeverityLevel severityLevel) {
32+
isNotNull();
33+
assertThat(getMessageData().getSeverityLevel()).isEqualTo(severityLevel);
34+
return this;
35+
}
36+
37+
@CanIgnoreReturnValue
38+
public MessageAssert hasProperty(String key, String value) {
39+
isNotNull();
40+
assertThat(getMessageData().getProperties()).containsEntry(key, value);
41+
return this;
42+
}
43+
44+
@CanIgnoreReturnValue
45+
public MessageAssert hasPropertyKey(String key) {
46+
isNotNull();
47+
assertThat(getMessageData().getProperties()).containsKey(key);
48+
return this;
49+
}
50+
51+
@SafeVarargs
52+
@CanIgnoreReturnValue
53+
public final MessageAssert hasPropertiesExactly(Map.Entry<String, String>... entries) {
54+
Map<String, String> map = new HashMap<>();
55+
for (Map.Entry<String, String> entry : entries) {
56+
map.put(entry.getKey(), entry.getValue());
57+
}
58+
59+
isNotNull();
60+
MessageData messageData = getMessageData();
61+
assertThat(messageData.getProperties()).containsExactlyInAnyOrderEntriesOf(map);
62+
return this;
63+
}
64+
65+
@CanIgnoreReturnValue
66+
public MessageAssert hasPropertiesSize(int expectedSize) {
67+
isNotNull();
68+
assertThat(getMessageData().getProperties()).hasSize(expectedSize);
69+
return this;
70+
}
71+
72+
@CanIgnoreReturnValue
73+
public MessageAssert hasTag(String key, String value) {
74+
isNotNull();
75+
assertThat(actual.getTags()).containsEntry(key, value);
76+
return this;
77+
}
78+
79+
@CanIgnoreReturnValue
80+
public MessageAssert hasNoParent() {
81+
isNotNull();
82+
assertThat(actual.getTags()).doesNotContainKey("ai.operation.parentId");
83+
return this;
84+
}
85+
86+
@CanIgnoreReturnValue
87+
public MessageAssert hasParent(String parentId) {
88+
isNotNull();
89+
assertThat(actual.getTags()).containsEntry("ai.operation.parentId", parentId);
90+
return this;
91+
}
92+
93+
@CanIgnoreReturnValue
94+
public MessageAssert hasNoSampleRate() {
95+
isNotNull();
96+
assertThat(actual.getSampleRate()).isNull();
97+
return this;
98+
}
99+
100+
private MessageData getMessageData() {
101+
Data<?> data = (Data<?>) actual.getData();
102+
return (MessageData) data.getBaseData();
103+
}
104+
}

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

Lines changed: 31 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
@@ -63,6 +68,24 @@ public TraceAssert hasMessageCount(int count) {
6368
return this;
6469
}
6570

71+
@CanIgnoreReturnValue
72+
public TraceAssert hasMessageSatisfying(Consumer<MessageAssert> assertion) {
73+
assertThat(messages).anySatisfy(envelope -> assertion.accept(new MessageAssert(envelope)));
74+
return this;
75+
}
76+
77+
@CanIgnoreReturnValue
78+
public TraceAssert hasExceptionCount(int count) {
79+
assertThat(exceptions).hasSize(count);
80+
return this;
81+
}
82+
83+
@CanIgnoreReturnValue
84+
public TraceAssert hasExceptionSatisfying(Consumer<ExceptionAssert> assertion) {
85+
assertThat(exceptions).anySatisfy(envelope -> assertion.accept(new ExceptionAssert(envelope)));
86+
return this;
87+
}
88+
6689
public String getRequestId(int index) {
6790
Data<?> data = (Data<?>) requests.get(index).getData();
6891
return ((RequestData) data.getBaseData()).getId();
@@ -72,4 +95,12 @@ public String getDependencyId(int index) {
7295
Data<?> data = (Data<?>) dependencies.get(index).getData();
7396
return ((RemoteDependencyData) data.getBaseData()).getId();
7497
}
98+
99+
public List<Envelope> getMessages() {
100+
return messages;
101+
}
102+
103+
public List<Envelope> getExceptions() {
104+
return exceptions;
105+
}
75106
}

0 commit comments

Comments
 (0)