Skip to content

Commit 32091c4

Browse files
authored
Merge pull request #250 from bugsnag/PLAT-15339/notifier-spec
Notifier spec refactoring
2 parents 0d46121 + ad30876 commit 32091c4

60 files changed

Lines changed: 655 additions & 696 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

bugsnag-spring/src/main/java/com/bugsnag/BugsnagSpringConfiguration.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.bugsnag;
22

3-
import com.bugsnag.callbacks.Callback;
3+
import com.bugsnag.callbacks.OnErrorCallback;
44

55
import org.springframework.beans.factory.InitializingBean;
66
import org.springframework.beans.factory.annotation.Autowired;
@@ -25,15 +25,15 @@ public class BugsnagSpringConfiguration implements InitializingBean {
2525
* Add a callback to add the version of Spring used by the application
2626
*/
2727
@Bean
28-
Callback springVersionErrorCallback() {
29-
Callback callback = new Callback() {
28+
OnErrorCallback springVersionErrorCallback() {
29+
OnErrorCallback callback = new OnErrorCallback() {
3030
@Override
31-
public boolean onError(Report report) {
31+
public boolean onError(BugsnagEvent report) {
3232
addSpringRuntimeVersion(report.getDevice());
3333
return true;
3434
}
3535
};
36-
bugsnag.addCallback(callback);
36+
bugsnag.addOnError(callback);
3737
return callback;
3838
}
3939

bugsnag-spring/src/main/java/com/bugsnag/ExceptionClassCallback.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.bugsnag;
22

33
import com.bugsnag.HandledState.SeverityReasonType;
4-
import com.bugsnag.callbacks.Callback;
4+
import com.bugsnag.callbacks.OnErrorCallback;
55

66
import org.springframework.beans.ConversionNotSupportedException;
77
import org.springframework.beans.TypeMismatchException;
@@ -35,7 +35,7 @@
3535
* Exceptions that automatically resolve to 500 (internal server error) responses
3636
* map to severity to ERROR.
3737
*/
38-
class ExceptionClassCallback implements Callback {
38+
class ExceptionClassCallback implements OnErrorCallback {
3939

4040
private static final Map<Class<? extends java.lang.Exception>, Severity> EXCEPTION_TO_SEVERITY;
4141

@@ -111,9 +111,9 @@ class ExceptionClassCallback implements Callback {
111111
}
112112

113113
@Override
114-
public boolean onError(Report report) {
114+
public boolean onError(BugsnagEvent event) {
115115

116-
HandledState handledState = report.getHandledState();
116+
HandledState handledState = event.getHandledState();
117117

118118
// A manually-set severity takes precedence
119119
SeverityReasonType severityReasonType = handledState.calculateSeverityReasonType();
@@ -122,12 +122,12 @@ public boolean onError(Report report) {
122122
return true; // do not change delivery decision
123123
}
124124

125-
Class exceptionClass = report.getException().getClass();
125+
Class exceptionClass = event.getException().getClass();
126126

127127
if (EXCEPTION_TO_SEVERITY.containsKey(exceptionClass)) {
128128
Severity severity = EXCEPTION_TO_SEVERITY.get(exceptionClass);
129-
report.setSeverity(severity);
130-
report.setHandledState(HandledState.newInstance(
129+
event.setSeverity(severity);
130+
event.setHandledState(HandledState.newInstance(
131131
SeverityReasonType.REASON_EXCEPTION_CLASS,
132132
Collections.singletonMap("exceptionClass", exceptionClass.getSimpleName()),
133133
severity,

bugsnag-spring/src/main/java/com/bugsnag/JakartaMvcConfiguration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,6 @@ BugsnagJakartaMvcExceptionHandler bugsnagHandlerExceptionResolver() {
3030
*/
3131
@Override
3232
public void afterPropertiesSet() {
33-
bugsnag.addCallback(new ExceptionClassCallback());
33+
bugsnag.addOnError(new ExceptionClassCallback());
3434
}
3535
}

bugsnag-spring/src/main/java/com/bugsnag/SpringBootConfiguration.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.bugsnag;
22

3-
import com.bugsnag.callbacks.Callback;
3+
import com.bugsnag.callbacks.OnErrorCallback;
44

55
import org.springframework.beans.factory.annotation.Autowired;
66
import org.springframework.boot.SpringBootVersion;
@@ -16,15 +16,15 @@ public class SpringBootConfiguration {
1616
* Add a callback to add the version of Spring Boot used by the application.
1717
*/
1818
@Bean
19-
Callback springBootVersionErrorCallback() {
20-
Callback callback = new Callback() {
19+
OnErrorCallback springBootVersionErrorCallback() {
20+
OnErrorCallback callback = new OnErrorCallback() {
2121
@Override
22-
public boolean onError(Report report) {
22+
public boolean onError(BugsnagEvent report) {
2323
addSpringRuntimeVersion(report.getDevice());
2424
return true;
2525
}
2626
};
27-
bugsnag.addCallback(callback);
27+
bugsnag.addOnError(callback);
2828
return callback;
2929
}
3030

bugsnag-spring/src/test/java/com/bugsnag/NotifierTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
public class NotifierTest {
1313

14-
private Report report;
14+
private BugsnagEvent event;
1515
private Configuration config;
1616
private SessionPayload sessionPayload;
1717

@@ -23,13 +23,13 @@ public class NotifierTest {
2323
@Before
2424
public void setUp() throws Throwable {
2525
config = new Configuration("api-key");
26-
report = new Report(config, new RuntimeException());
26+
event = new BugsnagEvent(config, new RuntimeException());
2727
sessionPayload = new SessionPayload(Collections.<SessionCount>emptyList(), config);
2828
}
2929

3030
@Test
3131
public void testNotificationSerialisation() throws Throwable {
32-
JsonNode payload = BugsnagTestUtils.mapReportToJson(config, this.report);
32+
JsonNode payload = BugsnagTestUtils.mapReportToJson(config, this.event);
3333
JsonNode notifier = payload.get("notifier");
3434

3535
assertEquals("Bugsnag Spring", notifier.get("name").asText());

bugsnag-spring/src/test/java/com/bugsnag/SpringAsyncTest.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -50,41 +50,41 @@ public void setUp() {
5050
public void bugsnagNotifyWhenAsyncVoidReturnTypeException() {
5151
asyncService.throwExceptionVoid();
5252

53-
Report report = verifyAndGetReport(delivery);
53+
BugsnagEvent event = verifyAndGetReport(delivery);
5454

5555
// Assert that the exception was detected correctly
56-
assertEquals("Async void test", report.getExceptionMessage());
57-
assertEquals("java.lang.RuntimeException", report.getExceptionName());
56+
assertEquals("Async void test", event.getExceptionMessage());
57+
assertEquals("java.lang.RuntimeException", event.getExceptionName());
5858

5959
// Assert that the severity, severity reason and unhandled values are correct
60-
assertEquals(Severity.ERROR.getValue(), report.getSeverity());
60+
assertEquals(Severity.ERROR.getValue(), event.getSeverity());
6161
assertEquals(
6262
SeverityReasonType.REASON_UNHANDLED_EXCEPTION_MIDDLEWARE.toString(),
63-
report.getSeverityReason().getType());
63+
event.getSeverityReason().getType());
6464
assertThat(
65-
report.getSeverityReason().getAttributes(),
65+
event.getSeverityReason().getAttributes(),
6666
is(Collections.singletonMap("framework", "Spring")));
67-
assertTrue(report.getUnhandled());
67+
assertTrue(event.getUnhandled());
6868
}
6969

7070
@Test
7171
public void bugsnagNotifyWhenAsyncFutureReturnTypeException() {
7272
asyncService.throwExceptionFuture();
7373

74-
Report report = verifyAndGetReport(delivery);
74+
BugsnagEvent event = verifyAndGetReport(delivery);
7575

7676
// Assert that the exception was detected correctly
77-
assertEquals("Async future test", report.getExceptionMessage());
78-
assertEquals("java.lang.RuntimeException", report.getExceptionName());
77+
assertEquals("Async future test", event.getExceptionMessage());
78+
assertEquals("java.lang.RuntimeException", event.getExceptionName());
7979

8080
// Assert that the severity, severity reason and unhandled values are correct
81-
assertEquals(Severity.ERROR.getValue(), report.getSeverity());
81+
assertEquals(Severity.ERROR.getValue(), event.getSeverity());
8282
assertEquals(
8383
SeverityReasonType.REASON_UNHANDLED_EXCEPTION_MIDDLEWARE.toString(),
84-
report.getSeverityReason().getType());
84+
event.getSeverityReason().getType());
8585
assertThat(
86-
report.getSeverityReason().getAttributes(),
86+
event.getSeverityReason().getAttributes(),
8787
is(Collections.singletonMap("framework", "Spring")));
88-
assertTrue(report.getUnhandled());
88+
assertTrue(event.getUnhandled());
8989
}
9090
}

bugsnag-spring/src/test/java/com/bugsnag/SpringMvcTest.java

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
import com.bugsnag.HandledState.SeverityReasonType;
1616

17-
import com.bugsnag.callbacks.Callback;
17+
import com.bugsnag.callbacks.OnErrorCallback;
1818
import com.bugsnag.delivery.Delivery;
1919

2020
import com.bugsnag.serialization.Serializer;
@@ -83,21 +83,21 @@ public void setUp() {
8383
public void bugsnagNotifyWhenUncaughtControllerException() {
8484
callRuntimeExceptionEndpoint();
8585

86-
Report report = verifyAndGetReport(delivery);
86+
BugsnagEvent event = verifyAndGetReport(delivery);
8787

8888
// Assert that the exception was detected correctly
89-
assertEquals("Test", report.getExceptionMessage());
90-
assertEquals("java.lang.RuntimeException", report.getExceptionName());
89+
assertEquals("Test", event.getExceptionMessage());
90+
assertEquals("java.lang.RuntimeException", event.getExceptionName());
9191

9292
// Assert that the severity, severity reason and unhandled values are correct
93-
assertEquals(Severity.ERROR.getValue(), report.getSeverity());
93+
assertEquals(Severity.ERROR.getValue(), event.getSeverity());
9494
assertEquals(
9595
SeverityReasonType.REASON_UNHANDLED_EXCEPTION_MIDDLEWARE.toString(),
96-
report.getSeverityReason().getType());
96+
event.getSeverityReason().getType());
9797
assertThat(
98-
report.getSeverityReason().getAttributes(),
98+
event.getSeverityReason().getAttributes(),
9999
is(Collections.singletonMap("framework", "Spring")));
100-
assertTrue(report.getUnhandled());
100+
assertTrue(event.getUnhandled());
101101
}
102102

103103
@Test
@@ -129,14 +129,14 @@ public void noBugsnagSessionStartedWhenAutoCaptureSessionsFalse() {
129129
public void requestMetadataSetCorrectly() {
130130
callRuntimeExceptionEndpoint();
131131

132-
Report report = verifyAndGetReport(delivery);
132+
BugsnagEvent event = verifyAndGetReport(delivery);
133133

134134
// Check that the context is set to the HTTP method and URI of the endpoint
135-
assertEquals("GET /throw-runtime-exception", report.getContext());
135+
assertEquals("GET /throw-runtime-exception", event.getContext());
136136

137137
// Check that the request metadata is set as expected
138138
@SuppressWarnings(value = "unchecked") Map<String, Object> requestMetadata =
139-
(Map<String, Object>) report.getMetadata().get("request");
139+
(Map<String, Object>) event.getMetadata().get("request");
140140
assertEquals("http://localhost:" + randomServerPort + "/throw-runtime-exception",
141141
requestMetadata.get("url"));
142142
assertEquals("GET", requestMetadata.get("method"));
@@ -161,10 +161,10 @@ public void requestMetadataSetCorrectly() {
161161
public void springVersionSetCorrectly() {
162162
callRuntimeExceptionEndpoint();
163163

164-
Report report = verifyAndGetReport(delivery);
164+
BugsnagEvent event = verifyAndGetReport(delivery);
165165

166166
// Check that the Spring version is set as expected
167-
Map<String, Object> deviceMetadata = report.getDevice();
167+
Map<String, Object> deviceMetadata = event.getDevice();
168168
Map<String, Object> runtimeVersions =
169169
(Map<String, Object>) deviceMetadata.get("runtimeVersions");
170170
assertEquals(SpringVersion.getVersion(), runtimeVersions.get("springFramework"));
@@ -175,67 +175,67 @@ public void springVersionSetCorrectly() {
175175
public void unhandledTypeMismatchExceptionSeverityInfo() {
176176
callUnhandledTypeMismatchExceptionEndpoint();
177177

178-
Report report = verifyAndGetReport(delivery);
178+
BugsnagEvent event = verifyAndGetReport(delivery);
179179

180-
assertTrue(report.getUnhandled());
181-
assertEquals("info", report.getSeverity());
182-
assertEquals("exceptionClass", report.getSeverityReason().getType());
183-
assertThat(report.getSeverityReason().getAttributes(),
180+
assertTrue(event.getUnhandled());
181+
assertEquals("info", event.getSeverity());
182+
assertEquals("exceptionClass", event.getSeverityReason().getType());
183+
assertThat(event.getSeverityReason().getAttributes(),
184184
is(Collections.singletonMap("exceptionClass", "TypeMismatchException")));
185185
}
186186

187187
@Test
188188
public void unhandledTypeMismatchExceptionCallbackSeverity()
189189
throws IllegalAccessException, NoSuchFieldException {
190-
Report report;
191-
Callback callback = new Callback() {
190+
BugsnagEvent event;
191+
OnErrorCallback callback = new OnErrorCallback() {
192192
@Override
193-
public boolean onError(Report report) {
193+
public boolean onError(BugsnagEvent report) {
194194
report.setSeverity(Severity.WARNING);
195195
return true;
196196
}
197197
};
198198

199199
try {
200-
bugsnag.addCallback(callback);
200+
bugsnag.addOnError(callback);
201201

202202
callUnhandledTypeMismatchExceptionEndpoint();
203203

204-
report = verifyAndGetReport(delivery);
204+
event = verifyAndGetReport(delivery);
205205
} finally {
206206
// Remove the callback via reflection so that subsequent tests do not use it
207207
Field callbacksField = Configuration.class.getDeclaredField("callbacks");
208-
@SuppressWarnings(value = "unchecked") Collection<Callback> callbacks =
209-
(Collection<Callback>) callbacksField.get(bugsnag.getConfig());
208+
@SuppressWarnings(value = "unchecked") Collection<OnErrorCallback> callbacks =
209+
(Collection<OnErrorCallback>) callbacksField.get(bugsnag.getConfig());
210210
callbacks.remove(callback);
211211
}
212212

213-
assertTrue(report.getUnhandled());
214-
assertEquals("warning", report.getSeverity());
215-
assertEquals("userCallbackSetSeverity", report.getSeverityReason().getType());
213+
assertTrue(event.getUnhandled());
214+
assertEquals("warning", event.getSeverity());
215+
assertEquals("userCallbackSetSeverity", event.getSeverityReason().getType());
216216
}
217217

218218
@Test
219219
public void handledTypeMismatchExceptionUserSeverity() {
220220
callHandledTypeMismatchExceptionUserSeverityEndpoint();
221221

222-
Report report = verifyAndGetReport(delivery);
222+
BugsnagEvent event = verifyAndGetReport(delivery);
223223

224-
assertFalse(report.getUnhandled());
225-
assertEquals("warning", report.getSeverity());
226-
assertEquals("userSpecifiedSeverity", report.getSeverityReason().getType());
227-
assertThat(report.getSeverityReason().getAttributes(), is(Collections.EMPTY_MAP));
224+
assertFalse(event.getUnhandled());
225+
assertEquals("warning", event.getSeverity());
226+
assertEquals("userSpecifiedSeverity", event.getSeverityReason().getType());
227+
assertThat(event.getSeverityReason().getAttributes(), is(Collections.EMPTY_MAP));
228228
}
229229

230230
@Test
231231
public void handledTypeMismatchExceptionCallbackSeverity() {
232232
callHandledTypeMismatchExceptionCallbackSeverityEndpoint();
233233

234-
Report report = verifyAndGetReport(delivery);
234+
BugsnagEvent event = verifyAndGetReport(delivery);
235235

236-
assertFalse(report.getUnhandled());
237-
assertEquals("warning", report.getSeverity());
238-
assertEquals("userCallbackSetSeverity", report.getSeverityReason().getType());
236+
assertFalse(event.getUnhandled());
237+
assertEquals("warning", event.getSeverity());
238+
assertEquals("userCallbackSetSeverity", event.getSeverityReason().getType());
239239
}
240240

241241
private void callUnhandledTypeMismatchExceptionEndpoint() {

bugsnag-spring/src/test/java/com/bugsnag/SpringScheduledTaskTest.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,21 +70,21 @@ public void run() {
7070
// Run the task now and wait for it to finish
7171
scheduler.submit(exampleRunnable).get();
7272

73-
Report report = verifyAndGetReport(delivery);
73+
BugsnagEvent event = verifyAndGetReport(delivery);
7474

7575
// Assert that the exception was detected correctly
76-
assertEquals("Scheduled test", report.getExceptionMessage());
77-
assertEquals("java.lang.RuntimeException", report.getExceptionName());
76+
assertEquals("Scheduled test", event.getExceptionMessage());
77+
assertEquals("java.lang.RuntimeException", event.getExceptionName());
7878

7979
// Assert that the severity, severity reason and unhandled values are correct
80-
assertEquals(Severity.ERROR.getValue(), report.getSeverity());
80+
assertEquals(Severity.ERROR.getValue(), event.getSeverity());
8181
assertEquals(
8282
SeverityReasonType.REASON_UNHANDLED_EXCEPTION_MIDDLEWARE.toString(),
83-
report.getSeverityReason().getType());
83+
event.getSeverityReason().getType());
8484
assertThat(
85-
report.getSeverityReason().getAttributes(),
85+
event.getSeverityReason().getAttributes(),
8686
is(Collections.singletonMap("framework", "Spring")));
87-
assertTrue(report.getUnhandled());
87+
assertTrue(event.getUnhandled());
8888

8989
// Assert that the exception is passed to an existing exception handler
9090
ArgumentCaptor<RuntimeException> exceptionCaptor =

bugsnag-spring/src/test/java/com/bugsnag/TestUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class TestUtils {
1616
/**
1717
* Verify that a report was received, then capture and return that report
1818
*/
19-
static Report verifyAndGetReport(Delivery delivery) {
19+
static BugsnagEvent verifyAndGetReport(Delivery delivery) {
2020
ArgumentCaptor<Notification> notificationCaptor =
2121
ArgumentCaptor.forClass(Notification.class);
2222
verify(delivery, timeout(100).times(1)).deliver(

0 commit comments

Comments
 (0)