Skip to content

Commit de4303f

Browse files
dbuosDaniel Bustamante Ospina
authored andcommitted
Create CustomErrorReporter to enable custom error reporting (Work in progress)
1 parent deca841 commit de4303f

File tree

2 files changed

+120
-0
lines changed

2 files changed

+120
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package org.reactivecommons.async.impl.ext;
2+
3+
import org.reactivecommons.api.domain.Command;
4+
import org.reactivecommons.api.domain.DomainEvent;
5+
import org.reactivecommons.async.api.AsyncQuery;
6+
import org.reactivecommons.async.impl.communications.Message;
7+
import reactor.core.publisher.Mono;
8+
9+
public interface CustomErrorReporter {
10+
11+
String COMMAND_CLASS = "org.reactivecommons.api.domain.Command";
12+
String EVENT_CLASS = "org.reactivecommons.api.domain.DomainEvent";
13+
String QUERY_CLASS = "org.reactivecommons.async.api.AsyncQuery";
14+
15+
default Mono<Void> reportError(Throwable ex, Message rawMessage, Object message) {
16+
switch (message.getClass().getName()){
17+
case COMMAND_CLASS:
18+
return reportError(ex, rawMessage, (Command<?>) message);
19+
case EVENT_CLASS:
20+
return reportError(ex, rawMessage, (DomainEvent<?>) message);
21+
case QUERY_CLASS:
22+
return reportError(ex, rawMessage, (AsyncQuery<?>) message);
23+
default:
24+
return Mono.empty();
25+
}
26+
}
27+
28+
Mono<Void> reportError(Throwable ex, Message rawMessage, Command<?> message);
29+
Mono<Void> reportError(Throwable ex, Message rawMessage, DomainEvent<?> message);
30+
Mono<Void> reportError(Throwable ex, Message rawMessage, AsyncQuery<?> message);
31+
32+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package org.reactivecommons.async.impl.ext;
2+
3+
import org.junit.Test;
4+
import org.reactivecommons.api.domain.Command;
5+
import org.reactivecommons.api.domain.DomainEvent;
6+
import org.reactivecommons.async.api.AsyncQuery;
7+
import org.reactivecommons.async.impl.communications.Message;
8+
import reactor.core.publisher.Mono;
9+
import reactor.test.StepVerifier;
10+
import reactor.test.publisher.PublisherProbe;
11+
12+
import java.util.HashMap;
13+
import java.util.Map;
14+
15+
import static org.mockito.Mockito.mock;
16+
17+
public class CustomErrorReporterTest {
18+
19+
private final PublisherProbe<Void> commandProbe = PublisherProbe.empty();
20+
private final PublisherProbe<Void> eventProbe = PublisherProbe.empty();
21+
private final PublisherProbe<Void> queryProbe = PublisherProbe.empty();
22+
23+
private final TestCustomReporter customReporter = new TestCustomReporter();
24+
private final Message rawMessage = mock(Message.class);
25+
26+
@Test
27+
public void reportErrorCommand() {
28+
final Command<Object> message = new Command<>("", "", null);
29+
30+
assertReportError(message);
31+
32+
commandProbe.assertWasSubscribed();
33+
}
34+
35+
@Test
36+
public void testReportErrorEvent() {
37+
final DomainEvent<Object> message = new DomainEvent<>("", "", null);
38+
39+
assertReportError(message);
40+
41+
eventProbe.assertWasSubscribed();
42+
}
43+
44+
@Test
45+
public void testReportErrorQuery() {
46+
final AsyncQuery<Object> message = new AsyncQuery<>("", null);
47+
48+
assertReportError(message);
49+
50+
queryProbe.assertWasSubscribed();
51+
}
52+
53+
@Test
54+
public void shouldIgnoreUnknownMessageType() {
55+
final Map<?,?> message = new HashMap<>();
56+
57+
assertReportError(message);
58+
59+
queryProbe.assertWasNotSubscribed();
60+
commandProbe.assertWasNotSubscribed();
61+
eventProbe.assertWasNotSubscribed();
62+
}
63+
64+
private void assertReportError(Object message){
65+
customReporter.reportError(new RuntimeException(), rawMessage, message)
66+
.as(StepVerifier::create)
67+
.verifyComplete();
68+
}
69+
70+
class TestCustomReporter implements CustomErrorReporter{
71+
@Override
72+
public Mono<Void> reportError(Throwable ex, Message rawMessage, Command<?> message) {
73+
return commandProbe.mono();
74+
}
75+
76+
@Override
77+
public Mono<Void> reportError(Throwable ex, Message rawMessage, DomainEvent<?> message) {
78+
return eventProbe.mono();
79+
}
80+
81+
@Override
82+
public Mono<Void> reportError(Throwable ex, Message rawMessage, AsyncQuery<?> message) {
83+
return queryProbe.mono();
84+
}
85+
}
86+
87+
}
88+

0 commit comments

Comments
 (0)