|
| 1 | +/* |
| 2 | + * Copyright 2021 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.kafka.listener; |
| 18 | + |
| 19 | +import static org.mockito.ArgumentMatchers.any; |
| 20 | +import static org.mockito.Mockito.mock; |
| 21 | +import static org.mockito.Mockito.verify; |
| 22 | + |
| 23 | +import java.io.IOException; |
| 24 | +import java.util.Collections; |
| 25 | +import java.util.Map; |
| 26 | + |
| 27 | +import org.apache.kafka.clients.consumer.Consumer; |
| 28 | +import org.apache.kafka.clients.consumer.ConsumerRecords; |
| 29 | +import org.junit.jupiter.api.Test; |
| 30 | + |
| 31 | +/** |
| 32 | + * @author Gary Russell |
| 33 | + * @since 2.7.4 |
| 34 | + * |
| 35 | + */ |
| 36 | +public class ConditionalDelegatingErrorHandlerTests { |
| 37 | + |
| 38 | + @Test |
| 39 | + void testRecordDelegates() { |
| 40 | + var def = mock(ContainerAwareErrorHandler.class); |
| 41 | + var one = mock(ContainerAwareErrorHandler.class); |
| 42 | + var two = mock(ContainerAwareErrorHandler.class); |
| 43 | + var three = mock(ContainerAwareErrorHandler.class); |
| 44 | + var eh = new ConditionalDelegatingErrorHandler(def); |
| 45 | + eh.setErrorHandlers(Map.of(IllegalStateException.class, one, IllegalArgumentException.class, two)); |
| 46 | + eh.addDelegate(RuntimeException.class, three); |
| 47 | + |
| 48 | + eh.handle(wrap(new IOException()), Collections.emptyList(), mock(Consumer.class), |
| 49 | + mock(MessageListenerContainer.class)); |
| 50 | + verify(def).handle(any(), any(), any(), any()); |
| 51 | + eh.handle(wrap(new RuntimeException()), Collections.emptyList(), mock(Consumer.class), |
| 52 | + mock(MessageListenerContainer.class)); |
| 53 | + verify(three).handle(any(), any(), any(), any()); |
| 54 | + eh.handle(wrap(new IllegalArgumentException()), Collections.emptyList(), mock(Consumer.class), |
| 55 | + mock(MessageListenerContainer.class)); |
| 56 | + verify(two).handle(any(), any(), any(), any()); |
| 57 | + eh.handle(wrap(new IllegalStateException()), Collections.emptyList(), mock(Consumer.class), |
| 58 | + mock(MessageListenerContainer.class)); |
| 59 | + verify(one).handle(any(), any(), any(), any()); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + void testBatchDelegates() { |
| 64 | + var def = mock(ContainerAwareBatchErrorHandler.class); |
| 65 | + var one = mock(ContainerAwareBatchErrorHandler.class); |
| 66 | + var two = mock(ContainerAwareBatchErrorHandler.class); |
| 67 | + var three = mock(ContainerAwareBatchErrorHandler.class); |
| 68 | + var eh = new ConditionalDelegatingBatchErrorHandler(def); |
| 69 | + eh.setErrorHandlers(Map.of(IllegalStateException.class, one, IllegalArgumentException.class, two)); |
| 70 | + eh.addDelegate(RuntimeException.class, three); |
| 71 | + |
| 72 | + eh.handle(wrap(new IOException()), mock(ConsumerRecords.class), mock(Consumer.class), |
| 73 | + mock(MessageListenerContainer.class), mock(Runnable.class)); |
| 74 | + verify(def).handle(any(), any(), any(), any(), any()); |
| 75 | + eh.handle(wrap(new RuntimeException()), mock(ConsumerRecords.class), mock(Consumer.class), |
| 76 | + mock(MessageListenerContainer.class), mock(Runnable.class)); |
| 77 | + verify(three).handle(any(), any(), any(), any(), any()); |
| 78 | + eh.handle(wrap(new IllegalArgumentException()), mock(ConsumerRecords.class), mock(Consumer.class), |
| 79 | + mock(MessageListenerContainer.class), mock(Runnable.class)); |
| 80 | + verify(two).handle(any(), any(), any(), any(), any()); |
| 81 | + eh.handle(wrap(new IllegalStateException()), mock(ConsumerRecords.class), mock(Consumer.class), |
| 82 | + mock(MessageListenerContainer.class), mock(Runnable.class)); |
| 83 | + verify(one).handle(any(), any(), any(), any(), any()); |
| 84 | + } |
| 85 | + |
| 86 | + private Exception wrap(Exception ex) { |
| 87 | + return new ListenerExecutionFailedException("test", ex); |
| 88 | + } |
| 89 | + |
| 90 | +} |
0 commit comments