This repository has been archived by the owner on Jul 2, 2020. It is now read-only.
/
AirbrakeNotifierTest.java
182 lines (143 loc) · 5.67 KB
/
AirbrakeNotifierTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// Modified or written by Luca Marrocco for inclusion with airbrake.
// Copyright (c) 2009 Luca Marrocco.
// Licensed under the Apache License, Version 2.0 (the "License")
package airbrake;
import static airbrake.Exceptions.*;
import static airbrake.Slurp.*;
import static java.util.Arrays.*;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import java.util.*;
import org.apache.commons.logging.*;
import org.hamcrest.*;
import org.junit.*;
import static airbrake.ApiKeys.*;
public class AirbrakeNotifierTest {
protected static final Backtrace BACKTRACE = new Backtrace(asList("backtrace is empty"));;
protected static final Map<String, Object> REQUEST = new HashMap<String, Object>();
protected static final Map<String, Object> SESSION = new HashMap<String, Object>();
protected static final Map<String, Object> ENVIRONMENT = new HashMap<String, Object>();
private final Log logger = LogFactory.getLog(getClass());
private final Map<String, Object> EC2 = new HashMap<String, Object>();
private AirbrakeNotifier notifier;
private <T> Matcher<T> internalServerError() {
return new BaseMatcher<T>() {
public void describeTo(final Description description) {
description.appendText("internal server error");
}
public boolean matches(final Object item) {
return item.equals(500);
}
};
}
private int notifing(final String string) {
return new AirbrakeNotifier().notify(new AirbrakeNoticeBuilder(API_KEY, ERROR_MESSAGE) {
{
backtrace(new Backtrace(asList(string)));
}
}.newNotice());
}
@Before
public void setUp() {
ENVIRONMENT.put("A_KEY", "test");
EC2.put("AWS_SECRET", "AWS_SECRET");
EC2.put("EC2_PRIVATE_KEY", "EC2_PRIVATE_KEY");
EC2.put("AWS_ACCESS", "AWS_ACCESS");
EC2.put("EC2_CERT", "EC2_CERT");
notifier = new AirbrakeNotifier();
}
@Test
public void testHowBacktraceairbrakeNotInternalServerError() {
assertThat(notifing(ERROR_MESSAGE), not(internalServerError()));
assertThat(notifing("java.lang.RuntimeException: an expression is not valid"), not(internalServerError()));
assertThat(notifing("Caused by: java.lang.NullPointerException"), not(internalServerError()));
assertThat(notifing("at code.lucamarrocco.notifier.Exceptions.newException(Exceptions.java:11)"), not(internalServerError()));
assertThat(notifing("... 23 more"), not(internalServerError()));
}
@Test
public void testLogErrorWithException() {
logger.error("error", newException(ERROR_MESSAGE));
}
@Test
public void testLogErrorWithoutException() {
logger.error("error");
}
@Test
public void testLogThresholdLesserThatErrorWithExceptionDoNotNotifyToairbrake() {
logger.info("info", newException(ERROR_MESSAGE));
logger.warn("warn", newException(ERROR_MESSAGE));
}
@Test
public void testLogThresholdLesserThatErrorWithoutExceptionDoNotNotifyToairbrake() {
logger.info("info");
logger.warn("warn");
}
@Test
public void testNotifyToairbrakeUsingBuilderNoticeFromExceptionInEnv() {
final Exception EXCEPTION = newException(ERROR_MESSAGE);
final AirbrakeNotice notice = new AirbrakeNoticeBuilder(API_KEY, EXCEPTION, "test").newNotice();
assertThat(notifier.notify(notice), is(200));
}
@Test
public void testNotifyToairbrakeUsingBuilderNoticeFromExceptionInEnvAndSystemProperties() {
final Exception EXCEPTION = newException(ERROR_MESSAGE);
final AirbrakeNotice notice = new AirbrakeNoticeBuilder(API_KEY, EXCEPTION, "test") {
{
filteredSystemProperties();
}
}.newNotice();
assertThat(notifier.notify(notice), is(200));
}
@Test
public void testNotifyToairbrakeUsingBuilderNoticeInEnv() {
final AirbrakeNotice notice = new AirbrakeNoticeBuilder(API_KEY, ERROR_MESSAGE, "test").newNotice();
assertThat(notifier.notify(notice), is(200));
}
@Test
public void testSendExceptionNoticeWithFilteredBacktrace() {
final Exception EXCEPTION = newException(ERROR_MESSAGE);
final AirbrakeNotice notice = new AirbrakeNoticeBuilder(API_KEY, new QuietRubyBacktrace(), EXCEPTION, "test").newNotice();
assertThat(notifier.notify(notice), is(200));
}
@Test
public void testSendExceptionToairbrake() {
final Exception EXCEPTION = newException(ERROR_MESSAGE);
final AirbrakeNotice notice = new AirbrakeNoticeBuilder(API_KEY, EXCEPTION).newNotice();
assertThat(notifier.notify(notice), is(200));
}
@Test
public void testSendExceptionToairbrakeUsingRubyBacktrace() {
final Exception EXCEPTION = newException(ERROR_MESSAGE);
final AirbrakeNotice notice = new AirbrakeNoticeBuilder(API_KEY, new RubyBacktrace(), EXCEPTION, "test").newNotice();
assertThat(notifier.notify(notice), is(200));
}
@Test
public void testSendExceptionToairbrakeUsingRubyBacktraceAndFilteredSystemProperties() {
final Exception EXCEPTION = newException(ERROR_MESSAGE);
final AirbrakeNotice notice = new AirbrakeNoticeBuilderUsingFilteredSystemProperties(API_KEY, new RubyBacktrace(), EXCEPTION, "test", null).newNotice();
assertThat(notifier.notify(notice), is(200));
}
@Test
public void testSendNoticeToairbrake() {
final AirbrakeNotice notice = new AirbrakeNoticeBuilder(API_KEY, ERROR_MESSAGE).newNotice();
assertThat(notifier.notify(notice), is(200));
}
@Test
public void testSendNoticeWithFilteredBacktrace() {
final AirbrakeNotice notice = new AirbrakeNoticeBuilder(API_KEY, ERROR_MESSAGE) {
{
backtrace(new QuietRubyBacktrace(strings(slurp(read("backtrace.txt")))));
}
}.newNotice();
assertThat(notifier.notify(notice), is(200));
}
@Test
public void testSendNoticeWithLargeBacktrace() {
final AirbrakeNotice notice = new AirbrakeNoticeBuilder(API_KEY, ERROR_MESSAGE) {
{
backtrace(new Backtrace(strings(slurp(read("backtrace.txt")))));
}
}.newNotice();
assertThat(notifier.notify(notice), is(200));
}
}