|
| 1 | +/* |
| 2 | + * Copyright Debezium Authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache Software License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 |
| 5 | + */ |
| 6 | +package io.debezium.server.rabbitmq; |
| 7 | + |
| 8 | +import static org.mockito.ArgumentMatchers.any; |
| 9 | +import static org.mockito.ArgumentMatchers.anyBoolean; |
| 10 | +import static org.mockito.Mockito.mock; |
| 11 | +import static org.mockito.Mockito.never; |
| 12 | +import static org.mockito.Mockito.verify; |
| 13 | +import static org.mockito.Mockito.when; |
| 14 | + |
| 15 | +import java.io.IOException; |
| 16 | +import java.util.List; |
| 17 | +import java.util.Map; |
| 18 | +import java.util.Optional; |
| 19 | +import java.util.concurrent.TimeoutException; |
| 20 | +import java.util.stream.Stream; |
| 21 | + |
| 22 | +import org.junit.jupiter.api.BeforeEach; |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | +import org.junit.jupiter.params.ParameterizedTest; |
| 25 | +import org.junit.jupiter.params.provider.Arguments; |
| 26 | +import org.junit.jupiter.params.provider.MethodSource; |
| 27 | + |
| 28 | +import com.rabbitmq.client.AMQP; |
| 29 | +import com.rabbitmq.client.Channel; |
| 30 | + |
| 31 | +import io.debezium.engine.ChangeEvent; |
| 32 | +import io.debezium.engine.DebeziumEngine.RecordCommitter; |
| 33 | +import io.debezium.server.StreamNameMapper; |
| 34 | + |
| 35 | +class RabbitMqStreamChangeConsumerTest { |
| 36 | + |
| 37 | + private static final int DELIVERY_MODE = 2; |
| 38 | + private static final int ACK_TIMEOUT = 1000; |
| 39 | + |
| 40 | + private Channel channelMock; |
| 41 | + private StreamNameMapper streamNameMapperMock; |
| 42 | + private ChangeEvent<Object, Object> eventMock; |
| 43 | + private RecordCommitter<ChangeEvent<Object, Object>> committerMock; |
| 44 | + |
| 45 | + private RabbitMqStreamChangeConsumer rabbitMqStreamChangeConsumer; |
| 46 | + |
| 47 | + public static Stream<Arguments> testHandleBatch_StaticRoutingKeySourceParameters() { |
| 48 | + return Stream.of( |
| 49 | + Arguments.of("static-routing-key", "static-routing-key"), |
| 50 | + Arguments.of(null, "")); |
| 51 | + } |
| 52 | + |
| 53 | + public static Stream<Arguments> testHandleBatch_KeyRoutingKeySourceParameters() { |
| 54 | + return Stream.of( |
| 55 | + Arguments.of("test-routing-key", "test-routing-key"), |
| 56 | + Arguments.of(null, "")); |
| 57 | + } |
| 58 | + |
| 59 | + @BeforeEach |
| 60 | + @SuppressWarnings("unchecked") |
| 61 | + void setUp() { |
| 62 | + eventMock = mock(ChangeEvent.class); |
| 63 | + channelMock = mock(Channel.class); |
| 64 | + committerMock = mock(RecordCommitter.class); |
| 65 | + streamNameMapperMock = mock(StreamNameMapper.class); |
| 66 | + |
| 67 | + rabbitMqStreamChangeConsumer = new RabbitMqStreamChangeConsumer(); |
| 68 | + rabbitMqStreamChangeConsumer.channel = channelMock; |
| 69 | + rabbitMqStreamChangeConsumer.setStreamNameMapper(streamNameMapperMock); |
| 70 | + rabbitMqStreamChangeConsumer.deliveryMode = DELIVERY_MODE; |
| 71 | + rabbitMqStreamChangeConsumer.ackTimeout = ACK_TIMEOUT; |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + void testHandleBatch_TopicRoutingKeySource() throws InterruptedException, IOException, TimeoutException { |
| 76 | + // given |
| 77 | + String topicName = "test-topic"; |
| 78 | + String payload = "test content"; |
| 79 | + List<ChangeEvent<Object, Object>> records = List.of(eventMock); |
| 80 | + |
| 81 | + when(eventMock.destination()).thenReturn(topicName); |
| 82 | + when(eventMock.value()).thenReturn(payload); |
| 83 | + when(eventMock.headers()).thenReturn(List.of()); |
| 84 | + when(streamNameMapperMock.map(topicName)).thenReturn(topicName); |
| 85 | + |
| 86 | + rabbitMqStreamChangeConsumer.exchange = Optional.of(topicName); |
| 87 | + rabbitMqStreamChangeConsumer.routingKeySource = "topic"; |
| 88 | + rabbitMqStreamChangeConsumer.autoCreateRoutingKey = true; |
| 89 | + rabbitMqStreamChangeConsumer.routingKeyDurable = true; |
| 90 | + rabbitMqStreamChangeConsumer.routingKey = Optional.of("ignored"); |
| 91 | + |
| 92 | + // when |
| 93 | + rabbitMqStreamChangeConsumer.handleBatch(records, committerMock); |
| 94 | + |
| 95 | + // then |
| 96 | + verify(channelMock).queueDeclare(topicName, true, false, false, null); |
| 97 | + |
| 98 | + final AMQP.BasicProperties expectedProperties = new AMQP.BasicProperties.Builder() |
| 99 | + .deliveryMode(DELIVERY_MODE) |
| 100 | + .headers(Map.of()) |
| 101 | + .build(); |
| 102 | + |
| 103 | + verify(channelMock).basicPublish(topicName, topicName, expectedProperties, payload.getBytes()); |
| 104 | + verify(channelMock).waitForConfirmsOrDie(ACK_TIMEOUT); |
| 105 | + } |
| 106 | + |
| 107 | + @ParameterizedTest |
| 108 | + @MethodSource("testHandleBatch_StaticRoutingKeySourceParameters") |
| 109 | + void testHandleBatch_StaticRoutingKeySource(String staticRoutingKey, String expectedRoutingKey) throws InterruptedException, IOException, TimeoutException { |
| 110 | + // given |
| 111 | + String topicName = "test-topic"; |
| 112 | + String payload = "test content"; |
| 113 | + List<ChangeEvent<Object, Object>> records = List.of(eventMock); |
| 114 | + |
| 115 | + when(eventMock.destination()).thenReturn(topicName); |
| 116 | + when(eventMock.value()).thenReturn(payload); |
| 117 | + when(eventMock.headers()).thenReturn(List.of()); |
| 118 | + when(streamNameMapperMock.map(topicName)).thenReturn(topicName); |
| 119 | + |
| 120 | + rabbitMqStreamChangeConsumer.exchange = Optional.of(topicName); |
| 121 | + rabbitMqStreamChangeConsumer.routingKeySource = "static"; |
| 122 | + rabbitMqStreamChangeConsumer.routingKey = Optional.ofNullable(staticRoutingKey); |
| 123 | + |
| 124 | + // when |
| 125 | + rabbitMqStreamChangeConsumer.handleBatch(records, committerMock); |
| 126 | + |
| 127 | + // then |
| 128 | + verify(channelMock, never()).queueDeclare(any(), anyBoolean(), anyBoolean(), anyBoolean(), any()); |
| 129 | + |
| 130 | + final AMQP.BasicProperties expectedProperties = new AMQP.BasicProperties.Builder() |
| 131 | + .deliveryMode(DELIVERY_MODE) |
| 132 | + .headers(Map.of()) |
| 133 | + .build(); |
| 134 | + |
| 135 | + verify(channelMock).basicPublish(topicName, expectedRoutingKey, expectedProperties, payload.getBytes()); |
| 136 | + verify(channelMock).waitForConfirmsOrDie(ACK_TIMEOUT); |
| 137 | + } |
| 138 | + |
| 139 | + @ParameterizedTest |
| 140 | + @MethodSource("testHandleBatch_KeyRoutingKeySourceParameters") |
| 141 | + void testHandleBatch_KeyRoutingKeySource(String routingKey, String expectedRoutingKey) throws InterruptedException, IOException, TimeoutException { |
| 142 | + // given |
| 143 | + String topicName = "test-topic"; |
| 144 | + String payload = "test content"; |
| 145 | + List<ChangeEvent<Object, Object>> records = List.of(eventMock); |
| 146 | + |
| 147 | + when(eventMock.destination()).thenReturn(topicName); |
| 148 | + when(eventMock.value()).thenReturn(payload); |
| 149 | + when(eventMock.key()).thenReturn(routingKey); |
| 150 | + when(eventMock.headers()).thenReturn(List.of()); |
| 151 | + when(streamNameMapperMock.map(topicName)).thenReturn(topicName); |
| 152 | + when(streamNameMapperMock.map(routingKey)).thenReturn(routingKey); |
| 153 | + |
| 154 | + rabbitMqStreamChangeConsumer.exchange = Optional.of(topicName); |
| 155 | + rabbitMqStreamChangeConsumer.routingKeySource = "key"; |
| 156 | + rabbitMqStreamChangeConsumer.routingKey = Optional.of("ignored"); |
| 157 | + |
| 158 | + // when |
| 159 | + rabbitMqStreamChangeConsumer.handleBatch(records, committerMock); |
| 160 | + |
| 161 | + // then |
| 162 | + verify(channelMock, never()).queueDeclare(any(), anyBoolean(), anyBoolean(), anyBoolean(), any()); |
| 163 | + |
| 164 | + final AMQP.BasicProperties expectedProperties = new AMQP.BasicProperties.Builder() |
| 165 | + .deliveryMode(DELIVERY_MODE) |
| 166 | + .headers(Map.of()) |
| 167 | + .build(); |
| 168 | + |
| 169 | + verify(channelMock).basicPublish(topicName, expectedRoutingKey, expectedProperties, payload.getBytes()); |
| 170 | + verify(channelMock).waitForConfirmsOrDie(ACK_TIMEOUT); |
| 171 | + } |
| 172 | +} |
0 commit comments