|
19 | 19 | import java.util.List; |
20 | 20 | import java.util.Map; |
21 | 21 |
|
| 22 | +import static org.mockito.ArgumentMatchers.anyString; |
22 | 23 | import static org.mockito.ArgumentMatchers.eq; |
23 | 24 | import static org.mockito.ArgumentMatchers.same; |
24 | 25 | import static org.mockito.Mockito.mock; |
| 26 | +import static org.mockito.Mockito.never; |
25 | 27 | import static org.mockito.Mockito.verify; |
26 | 28 | import static org.mockito.Mockito.when; |
27 | 29 |
|
@@ -114,4 +116,84 @@ void send_exchangeAndRoutingKeyFromBindings() { |
114 | 116 |
|
115 | 117 | verify(rabbitTemplate).convertAndSend(eq("exchange-name"), eq("routing-key"), same(payload)); |
116 | 118 | } |
| 119 | + |
| 120 | + @Test |
| 121 | + void send_usesRabbitTemplateMatchingRoutingKey() { |
| 122 | + RabbitTemplate template1 = mock(RabbitTemplate.class); |
| 123 | + RabbitTemplate template2 = mock(RabbitTemplate.class); |
| 124 | + when(template1.getRoutingKey()).thenReturn("any-key"); |
| 125 | + when(template2.getRoutingKey()).thenReturn("routing-key"); |
| 126 | + |
| 127 | + springwolfAmqpProducer = new SpringwolfAmqpProducer(asyncApiService, List.of(template1, template2)); |
| 128 | + |
| 129 | + AMQPChannelExchangeProperties properties = new AMQPChannelExchangeProperties(); |
| 130 | + properties.setName("exchange-name"); |
| 131 | + ChannelObject channelItem = ChannelObject.builder() |
| 132 | + .bindings(Map.of( |
| 133 | + "amqp", |
| 134 | + AMQPChannelBinding.builder().exchange(properties).build())) |
| 135 | + .build(); |
| 136 | + Map<String, ChannelObject> channels = Map.of("channel-name", channelItem); |
| 137 | + Operation operation = Operation.builder() |
| 138 | + .bindings(Map.of( |
| 139 | + "amqp", |
| 140 | + AMQPOperationBinding.builder() |
| 141 | + .cc(List.of("routing-key")) |
| 142 | + .build())) |
| 143 | + .build(); |
| 144 | + Map<String, Operation> operations = Map.of("amqp", operation); |
| 145 | + |
| 146 | + AsyncAPI asyncAPI = AsyncAPI.builder() |
| 147 | + .info(new Info()) |
| 148 | + .channels(channels) |
| 149 | + .operations(operations) |
| 150 | + .build(); |
| 151 | + when(asyncApiService.getAsyncAPI()).thenReturn(asyncAPI); |
| 152 | + |
| 153 | + Map<String, Object> payload = new HashMap<>(); |
| 154 | + springwolfAmqpProducer.send("channel-name", payload); |
| 155 | + |
| 156 | + verify(template1, never()).convertAndSend(anyString(), anyString(), same(payload)); |
| 157 | + verify(template2).convertAndSend(eq("exchange-name"), eq("routing-key"), same(payload)); |
| 158 | + } |
| 159 | + |
| 160 | + @Test |
| 161 | + void send_fallsBackToFirstTemplateWhenNoMatch() { |
| 162 | + RabbitTemplate template1 = mock(RabbitTemplate.class); |
| 163 | + RabbitTemplate template2 = mock(RabbitTemplate.class); |
| 164 | + when(template1.getRoutingKey()).thenReturn("key-1"); |
| 165 | + when(template2.getRoutingKey()).thenReturn("key-2"); |
| 166 | + |
| 167 | + springwolfAmqpProducer = new SpringwolfAmqpProducer(asyncApiService, List.of(template1, template2)); |
| 168 | + |
| 169 | + AMQPChannelExchangeProperties properties = new AMQPChannelExchangeProperties(); |
| 170 | + properties.setName("exchange-name"); |
| 171 | + ChannelObject channelItem = ChannelObject.builder() |
| 172 | + .bindings(Map.of( |
| 173 | + "amqp", |
| 174 | + AMQPChannelBinding.builder().exchange(properties).build())) |
| 175 | + .build(); |
| 176 | + Map<String, ChannelObject> channels = Map.of("channel-name", channelItem); |
| 177 | + Operation operation = Operation.builder() |
| 178 | + .bindings(Map.of( |
| 179 | + "amqp", |
| 180 | + AMQPOperationBinding.builder() |
| 181 | + .cc(List.of("routing-key")) |
| 182 | + .build())) |
| 183 | + .build(); |
| 184 | + Map<String, Operation> operations = Map.of("amqp", operation); |
| 185 | + |
| 186 | + AsyncAPI asyncAPI = AsyncAPI.builder() |
| 187 | + .info(new Info()) |
| 188 | + .channels(channels) |
| 189 | + .operations(operations) |
| 190 | + .build(); |
| 191 | + when(asyncApiService.getAsyncAPI()).thenReturn(asyncAPI); |
| 192 | + |
| 193 | + Map<String, Object> payload = new HashMap<>(); |
| 194 | + springwolfAmqpProducer.send("channel-name", payload); |
| 195 | + |
| 196 | + verify(template1).convertAndSend(eq("exchange-name"), eq("routing-key"), same(payload)); |
| 197 | + verify(template2, never()).convertAndSend(anyString(), anyString(), same(payload)); |
| 198 | + } |
117 | 199 | } |
0 commit comments