|
| 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