1
1
/*
2
- * Copyright 2015-2017 the original author or authors.
2
+ * Copyright 2015-2018 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
25
25
import static org .junit .Assert .assertThat ;
26
26
import static org .junit .Assert .assertTrue ;
27
27
28
+ import java .net .MalformedURLException ;
29
+ import java .net .URISyntaxException ;
28
30
import java .util .Collections ;
29
31
import java .util .List ;
30
32
import java .util .Map ;
45
47
46
48
import com .rabbitmq .client .Channel ;
47
49
import com .rabbitmq .client .DefaultConsumer ;
50
+ import com .rabbitmq .http .client .Client ;
51
+ import com .rabbitmq .http .client .domain .BindingInfo ;
52
+ import com .rabbitmq .http .client .domain .ExchangeInfo ;
48
53
import com .rabbitmq .http .client .domain .QueueInfo ;
49
54
50
55
/**
54
59
* @since 1.5
55
60
*
56
61
*/
57
- public class RabbitManagementTemplateTests {
62
+ public class RabbitRestApiTests {
58
63
59
64
private final CachingConnectionFactory connectionFactory = new CachingConnectionFactory ("localhost" );
60
65
61
- private final RabbitManagementTemplate template = new RabbitManagementTemplate ();
66
+ private final Client rabbitRestClient ;
67
+
62
68
63
69
@ ClassRule
64
70
public static BrokerRunning brokerAndManagementRunning = BrokerRunning .isBrokerAndManagementRunning ();
65
71
72
+ public RabbitRestApiTests () throws MalformedURLException , URISyntaxException {
73
+ this .rabbitRestClient = new Client ("http://localhost:15672/api/" , "guest" , "guest" );
74
+ }
75
+
66
76
@ After
67
77
public void tearDown () {
68
78
connectionFactory .destroy ();
69
79
}
70
80
71
81
@ Test
72
82
public void testExchanges () {
73
- List <Exchange > list = this .template .getExchanges ();
83
+ List <ExchangeInfo > list = this .rabbitRestClient .getExchanges ();
74
84
assertTrue (list .size () > 0 );
75
85
}
76
86
77
87
@ Test
78
88
public void testExchangesVhost () {
79
- List <Exchange > list = this .template .getExchanges ("/" );
89
+ List <ExchangeInfo > list = this .rabbitRestClient .getExchanges ("/" );
80
90
assertTrue (list .size () > 0 );
81
91
}
82
92
83
93
@ Test
84
94
public void testBindings () {
85
- List <Binding > list = this .template .getBindings ();
95
+ List <BindingInfo > list = this .rabbitRestClient .getBindings ();
86
96
assertTrue (list .size () > 0 );
87
97
}
88
98
89
99
@ Test
90
100
public void testBindingsVhost () {
91
- List <Binding > list = this .template .getBindings ("/" );
101
+ List <BindingInfo > list = this .rabbitRestClient .getBindings ("/" );
92
102
assertTrue (list .size () > 0 );
93
103
}
94
104
95
105
@ Test
96
106
public void testQueues () {
97
- List <Queue > list = this .template .getQueues ();
107
+ List <QueueInfo > list = this .rabbitRestClient .getQueues ();
98
108
assertTrue (list .size () > 0 );
99
109
}
100
110
101
111
@ Test
102
112
public void testQueuesVhost () {
103
- List <Queue > list = this .template .getQueues ("/" );
113
+ List <QueueInfo > list = this .rabbitRestClient .getQueues ("/" );
104
114
assertTrue (list .size () > 0 );
105
115
}
106
116
@@ -125,12 +135,12 @@ public void testBindingsDetail() {
125
135
.with ("bar" );
126
136
admin .declareBinding (binding2 );
127
137
128
- List <Binding > bindings = this .template . getBindingsForExchange ("/" , exchange1 .getName ());
138
+ List <BindingInfo > bindings = this .rabbitRestClient . getBindingsBySource ("/" , exchange1 .getName ());
129
139
assertEquals (2 , bindings .size ());
130
- assertEquals (exchange1 .getName (), bindings .get (0 ).getExchange ());
140
+ assertEquals (exchange1 .getName (), bindings .get (0 ).getSource ());
131
141
assertThat ("foo" , anyOf (equalTo (bindings .get (0 ).getRoutingKey ()), equalTo (bindings .get (1 ).getRoutingKey ())));
132
- Binding qout = null ;
133
- Binding eout = null ;
142
+ BindingInfo qout = null ;
143
+ BindingInfo eout = null ;
134
144
if (bindings .get (0 ).getRoutingKey ().equals ("foo" )) {
135
145
qout = bindings .get (0 );
136
146
eout = bindings .get (1 );
@@ -139,12 +149,12 @@ public void testBindingsDetail() {
139
149
eout = bindings .get (0 );
140
150
qout = bindings .get (1 );
141
151
}
142
- assertEquals (Binding . DestinationType . QUEUE , qout .getDestinationType ());
152
+ assertEquals ("queue" , qout .getDestinationType ());
143
153
assertEquals (queue .getName (), qout .getDestination ());
144
154
assertNotNull (qout .getArguments ());
145
155
assertEquals ("" , qout .getArguments ().get ("alternate-exchange" ));
146
156
147
- assertEquals (Binding . DestinationType . EXCHANGE , eout .getDestinationType ());
157
+ assertEquals ("exchange" , eout .getDestinationType ());
148
158
assertEquals (exchange2 .getName (), eout .getDestination ());
149
159
150
160
admin .deleteExchange (exchange1 .getName ());
@@ -157,7 +167,7 @@ public void testSpecificExchange() {
157
167
Map <String , Object > args = Collections .<String , Object >singletonMap ("alternate-exchange" , "" );
158
168
Exchange exchange = new DirectExchange (UUID .randomUUID ().toString (), true , true , args );
159
169
admin .declareExchange (exchange );
160
- Exchange exchangeOut = this .template .getExchange ("/" , exchange .getName ());
170
+ ExchangeInfo exchangeOut = this .rabbitRestClient .getExchange ("/" , exchange .getName ());
161
171
assertTrue (exchangeOut .isDurable ());
162
172
assertTrue (exchangeOut .isAutoDelete ());
163
173
assertEquals (exchange .getName (), exchangeOut .getName ());
@@ -180,13 +190,13 @@ public void testSpecificQueue() throws Exception {
180
190
admin .declareQueue (queue2 );
181
191
Channel channel = this .connectionFactory .createConnection ().createChannel (false );
182
192
String consumer = channel .basicConsume (queue1 .getName (), false , "" , false , true , null , new DefaultConsumer (channel ));
183
- QueueInfo qi = this .template . getClient () .getQueue ("/" , queue1 .getName ());
193
+ QueueInfo qi = this .rabbitRestClient .getQueue ("/" , queue1 .getName ());
184
194
int n = 0 ;
185
195
while (n ++ < 100 && (qi .getExclusiveConsumerTag () == null || qi .getExclusiveConsumerTag ().equals ("" ))) {
186
196
Thread .sleep (100 );
187
- qi = this .template . getClient () .getQueue ("/" , queue1 .getName ());
197
+ qi = this .rabbitRestClient .getQueue ("/" , queue1 .getName ());
188
198
}
189
- Queue queueOut = this .template .getQueue ("/" , queue1 .getName ());
199
+ QueueInfo queueOut = this .rabbitRestClient .getQueue ("/" , queue1 .getName ());
190
200
assertFalse (queueOut .isDurable ());
191
201
assertFalse (queueOut .isExclusive ());
192
202
assertTrue (queueOut .isAutoDelete ());
@@ -196,7 +206,7 @@ public void testSpecificQueue() throws Exception {
196
206
channel .basicCancel (consumer );
197
207
channel .close ();
198
208
199
- queueOut = this .template .getQueue ("/" , queue2 .getName ());
209
+ queueOut = this .rabbitRestClient .getQueue ("/" , queue2 .getName ());
200
210
assertTrue (queueOut .isDurable ());
201
211
assertFalse (queueOut .isExclusive ());
202
212
assertFalse (queueOut .isAutoDelete ());
@@ -211,12 +221,17 @@ public void testSpecificQueue() throws Exception {
211
221
public void testDeleteExchange () {
212
222
String exchangeName = "testExchange" ;
213
223
Exchange testExchange = new DirectExchange (exchangeName );
214
- this .template .addExchange (testExchange );
215
- Exchange exchangeToAssert = this .template .getExchange (exchangeName );
224
+ ExchangeInfo info = new ExchangeInfo ();
225
+ info .setArguments (testExchange .getArguments ());
226
+ info .setAutoDelete (testExchange .isAutoDelete ());
227
+ info .setDurable (testExchange .isDurable ());
228
+ info .setType (testExchange .getType ());
229
+ this .rabbitRestClient .declareExchange ("/" , testExchange .getName (), info );
230
+ ExchangeInfo exchangeToAssert = this .rabbitRestClient .getExchange ("/" , exchangeName );
216
231
assertEquals (testExchange .getName (), exchangeToAssert .getName ());
217
232
assertEquals (testExchange .getType (), exchangeToAssert .getType ());
218
- this .template .deleteExchange (testExchange );
219
- assertNull (this .template .getExchange (exchangeName ));
233
+ this .rabbitRestClient .deleteExchange ("/" , testExchange . getName () );
234
+ assertNull (this .rabbitRestClient .getExchange ("/" , exchangeName ));
220
235
}
221
236
222
237
}
0 commit comments