-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
RabbitMQProducerAutoDeclareIT.java
122 lines (103 loc) · 5.59 KB
/
RabbitMQProducerAutoDeclareIT.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.camel.component.springrabbit.integration;
import java.nio.charset.Charset;
import java.util.Map;
import org.apache.camel.RoutesBuilder;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.springrabbit.SpringRabbitMQConstants;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.core.MessageDeliveryMode;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.core.MessagePropertiesBuilder;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
public class RabbitMQProducerAutoDeclareIT extends RabbitMQITSupport {
@Test
public void testProducer() throws Exception {
ConnectionFactory cf = context.getRegistry().lookupByNameAndType("myCF", ConnectionFactory.class);
template.sendBody("direct:start", "Hello World");
AmqpTemplate template = new RabbitTemplate(cf);
String out = (String) template.receiveAndConvert("myqueue");
Assertions.assertEquals("Hello World", out);
}
@Test
public void testProducerWithHeader() throws Exception {
ConnectionFactory cf = context.getRegistry().lookupByNameAndType("myCF", ConnectionFactory.class);
template.sendBodyAndHeader("direct:start", "Hello World", "cheese", "gouda");
AmqpTemplate template = new RabbitTemplate(cf);
Message out = template.receive("myqueue");
byte[] body = out.getBody();
Assertions.assertNotNull(body, "The body should not be null");
Assertions.assertEquals("Hello World", new String(body));
Assertions.assertEquals("gouda", out.getMessageProperties().getHeader("cheese"));
}
@Test
public void testProducerWithMessage() throws Exception {
ConnectionFactory cf = context.getRegistry().lookupByNameAndType("myCF", ConnectionFactory.class);
MessageProperties props = MessagePropertiesBuilder.newInstance()
.setContentType(MessageProperties.CONTENT_TYPE_TEXT_PLAIN)
.setMessageId("123")
.setHeader("bar", "baz")
.build();
Message body = MessageBuilder.withBody("foo".getBytes())
.andProperties(props)
.build();
template.sendBody("direct:start", body);
AmqpTemplate template = new RabbitTemplate(cf);
Message out = template.receive("myqueue");
Assertions.assertEquals("foo", new String(out.getBody()));
Assertions.assertEquals("baz", out.getMessageProperties().getHeader("bar"));
}
@Test
public void testProducerWithMessageProperties() throws Exception {
ConnectionFactory cf = context.getRegistry().lookupByNameAndType("myCF", ConnectionFactory.class);
template.sendBodyAndHeaders("direct:start", "<price>123</price>",
Map.of(SpringRabbitMQConstants.DELIVERY_MODE, MessageDeliveryMode.PERSISTENT,
SpringRabbitMQConstants.TYPE, "price",
SpringRabbitMQConstants.CONTENT_TYPE, "application/xml",
SpringRabbitMQConstants.MESSAGE_ID, "0fe9c142-f9c1-426f-9237-f5a4c988a8ae",
SpringRabbitMQConstants.PRIORITY, 1));
AmqpTemplate template = new RabbitTemplate(cf);
Message out = template.receive("myqueue");
final MessageProperties messageProperties = out.getMessageProperties();
Assertions.assertNotNull(messageProperties, "The message properties should not be null");
String encoding = messageProperties.getContentEncoding();
Assertions.assertEquals(Charset.defaultCharset().name(), encoding);
Assertions.assertEquals("<price>123</price>", new String(out.getBody(), encoding));
Assertions.assertEquals(MessageDeliveryMode.PERSISTENT, messageProperties.getReceivedDeliveryMode());
Assertions.assertEquals("price", messageProperties.getType());
Assertions.assertEquals("application/xml", messageProperties.getContentType());
Assertions.assertEquals("0fe9c142-f9c1-426f-9237-f5a4c988a8ae", messageProperties.getMessageId());
Assertions.assertEquals(1, messageProperties.getPriority());
Assertions.assertEquals(0, messageProperties.getHeaders().size());
}
@Override
protected RoutesBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {
@Override
public void configure() throws Exception {
from("direct:start")
.to("spring-rabbitmq:foo?autoDeclareProducer=true&routingKey=foo.bar.#&queues=myqueue&exchangeType=topic");
}
};
}
}