Skip to content

Commit d662812

Browse files
Technoboy-gaoran10
authored andcommitted
Fix test. (#1896)
(cherry picked from commit ed2be1d)
1 parent b9c3111 commit d662812

4 files changed

Lines changed: 82 additions & 6 deletions

File tree

amqp-impl/src/main/java/io/streamnative/pulsar/handlers/amqp/AmqpExchange.java

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,20 @@ public interface AmqpExchange {
3434
*
3535
*/
3636
enum Type{
37-
Direct,
38-
Fanout,
39-
Topic,
40-
Headers;
37+
Direct("direct"),
38+
Fanout("fanout"),
39+
Topic("topic"),
40+
Headers("headers"),
41+
ConsistentHash("x-consistent-hash"),
42+
ModulusHash("x-modulus-hash"),
43+
LocalRandom("x-local-random"),
44+
Random("x-random");
45+
46+
private final String type;
47+
48+
Type(String type) {
49+
this.type = type;
50+
}
4151

4252
public static Type value(String type) {
4353
if (type == null || type.length() == 0) {
@@ -53,11 +63,24 @@ public static Type value(String type) {
5363
return Topic;
5464
case "headers":
5565
return Headers;
66+
case "x-consistent-hash":
67+
return ConsistentHash;
68+
case "x-modulus-hash":
69+
return ModulusHash;
70+
case "x-local-random":
71+
return LocalRandom;
72+
case "x-random":
73+
return Random;
5674
default:
5775
return null;
5876
}
5977
}
6078

79+
@Override
80+
public String toString() {
81+
return type;
82+
}
83+
6184
}
6285

6386
/**

amqp-impl/src/main/java/io/streamnative/pulsar/handlers/amqp/ExchangeMessageRouter.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,8 @@ public static ExchangeMessageRouter getInstance(PersistentExchange exchange, Exe
301301
case Direct -> new DirectExchangeMessageRouter(exchange, routeExecutor);
302302
case Topic -> new TopicExchangeMessageRouter(exchange, routeExecutor);
303303
case Headers -> new HeadersExchangeMessageRouter(exchange, routeExecutor);
304+
default -> throw new AoPServiceRuntimeException.NotSupportedOperationException(
305+
"Exchange router is not supported for type " + exchange.getType() + ".");
304306
};
305307
}
306308

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
package io.streamnative.pulsar.handlers.amqp.test;
15+
16+
import io.streamnative.pulsar.handlers.amqp.AmqpExchange;
17+
import io.streamnative.pulsar.handlers.amqp.ExchangeMessageRouter;
18+
import io.streamnative.pulsar.handlers.amqp.common.exception.AoPServiceRuntimeException.NotSupportedOperationException;
19+
import io.streamnative.pulsar.handlers.amqp.impl.PersistentExchange;
20+
import java.util.concurrent.ExecutorService;
21+
import org.mockito.Mockito;
22+
import org.testng.Assert;
23+
import org.testng.annotations.DataProvider;
24+
import org.testng.annotations.Test;
25+
26+
/**
27+
* Unit tests for exchange router creation.
28+
*/
29+
public class ExchangeMessageRouterTest {
30+
31+
@DataProvider(name = "unsupportedExchangeTypes")
32+
public Object[][] unsupportedExchangeTypes() {
33+
return new Object[][]{
34+
{AmqpExchange.Type.ConsistentHash},
35+
{AmqpExchange.Type.ModulusHash},
36+
{AmqpExchange.Type.LocalRandom},
37+
{AmqpExchange.Type.Random}
38+
};
39+
}
40+
41+
@Test(dataProvider = "unsupportedExchangeTypes")
42+
public void shouldThrowWhenExchangeTypeHasNoRouter(AmqpExchange.Type exchangeType) {
43+
PersistentExchange exchange = Mockito.mock(PersistentExchange.class);
44+
ExecutorService routeExecutor = Mockito.mock(ExecutorService.class);
45+
Mockito.when(exchange.getType()).thenReturn(exchangeType);
46+
47+
NotSupportedOperationException exception = Assert.expectThrows(NotSupportedOperationException.class,
48+
() -> ExchangeMessageRouter.getInstance(exchange, routeExecutor));
49+
Assert.assertTrue(exception.getMessage().contains(exchangeType.toString()));
50+
}
51+
}

tests/src/test/java/io/streamnative/pulsar/handlers/amqp/rabbitmq/functional/ExchangeDeclareTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,8 @@ public void exchangeDeclaredWithEnumerationEquivalentOnRecoverableConnection()
118118

119119
private void doTestExchangeDeclaredWithEnumerationEquivalent(Channel channel)
120120
throws IOException, InterruptedException {
121-
assertEquals("There are 4 standard exchange types",
122-
4, BuiltinExchangeType.values().length);
121+
assertEquals("There are 8 standard exchange types",
122+
8, BuiltinExchangeType.values().length);
123123
for (BuiltinExchangeType exchangeType : BuiltinExchangeType.values()) {
124124
channel.exchangeDeclare(NAME, exchangeType);
125125
verifyEquivalent(NAME, exchangeType.getType(), false, false, null);

0 commit comments

Comments
 (0)