|
18 | 18 | package com.rabbitmq.client.amqp.impl; |
19 | 19 |
|
20 | 20 | import static com.rabbitmq.client.amqp.impl.Assertions.assertThat; |
| 21 | +import static com.rabbitmq.client.amqp.impl.Cli.*; |
| 22 | +import static com.rabbitmq.client.amqp.impl.TestConditions.BrokerVersion.RABBITMQ_4_1_0; |
| 23 | +import static com.rabbitmq.client.amqp.impl.TestUtils.sync; |
21 | 24 | import static org.assertj.core.api.Assertions.assertThat; |
22 | 25 | import static org.assertj.core.api.Assertions.assertThatThrownBy; |
23 | 26 |
|
24 | | -import com.rabbitmq.client.amqp.AmqpException; |
25 | | -import com.rabbitmq.client.amqp.Management; |
| 27 | +import com.rabbitmq.client.amqp.*; |
| 28 | +import com.rabbitmq.client.amqp.impl.TestConditions.BrokerVersionAtLeast; |
| 29 | +import com.rabbitmq.client.amqp.impl.TestUtils.Sync; |
26 | 30 | import java.time.Duration; |
27 | 31 | import java.util.concurrent.atomic.AtomicInteger; |
28 | 32 | import java.util.function.Supplier; |
29 | 33 | import org.junit.jupiter.api.*; |
| 34 | +import org.junit.jupiter.params.ParameterizedTest; |
| 35 | +import org.junit.jupiter.params.provider.ValueSource; |
30 | 36 |
|
31 | 37 | @AmqpTestInfrastructure |
32 | 38 | public class ManagementTest { |
33 | 39 |
|
| 40 | + Environment environment; |
34 | 41 | AmqpConnection connection; |
35 | 42 | AmqpManagement management; |
36 | 43 |
|
@@ -88,4 +95,66 @@ void receiveLoopShouldStopAfterBeingIdle() { |
88 | 95 | assertThat(management.queueInfo(info1.name())).hasName(info1.name()); |
89 | 96 | assertThat(management.queueInfo(info2.name())).hasName(info2.name()); |
90 | 97 | } |
| 98 | + |
| 99 | + @ParameterizedTest |
| 100 | + @ValueSource(booleans = {true, false}) |
| 101 | + @BrokerVersionAtLeast(RABBITMQ_4_1_0) |
| 102 | + void setToken(boolean isolateResources, TestInfo info) { |
| 103 | + String username = "foo"; |
| 104 | + String password = "bar"; |
| 105 | + String vh = "/"; |
| 106 | + String q = TestUtils.name(info); |
| 107 | + |
| 108 | + Connection c = null; |
| 109 | + try { |
| 110 | + addUser(username, password); |
| 111 | + setPermissions(username, vh, ".*"); |
| 112 | + this.connection.management().queue(q).declare(); |
| 113 | + |
| 114 | + c = |
| 115 | + ((AmqpConnectionBuilder) environment.connectionBuilder()) |
| 116 | + .isolateResources(isolateResources) |
| 117 | + .username(username) |
| 118 | + .password(password) |
| 119 | + .build(); |
| 120 | + Sync consumeSync = sync(); |
| 121 | + Sync publisherClosedSync = sync(); |
| 122 | + Sync consumerClosedSync = sync(); |
| 123 | + Publisher p = |
| 124 | + c.publisherBuilder().queue(q).listeners(closedListener(publisherClosedSync)).build(); |
| 125 | + c.consumerBuilder() |
| 126 | + .queue(q) |
| 127 | + .messageHandler( |
| 128 | + (ctx, msg) -> { |
| 129 | + ctx.accept(); |
| 130 | + consumeSync.down(); |
| 131 | + }) |
| 132 | + .listeners(closedListener(consumerClosedSync)) |
| 133 | + .build(); |
| 134 | + |
| 135 | + p.publish(p.message(), ctx -> {}); |
| 136 | + assertThat(consumeSync).completes(); |
| 137 | + |
| 138 | + setPermissions(username, vh, "foobar"); |
| 139 | + AmqpManagement m = (AmqpManagement) c.management(); |
| 140 | + m.setToken(password); |
| 141 | + assertThat(publisherClosedSync).completes(); |
| 142 | + assertThat(consumerClosedSync).completes(); |
| 143 | + } finally { |
| 144 | + if (c != null) { |
| 145 | + c.close(); |
| 146 | + } |
| 147 | + this.connection.management().queueDeletion().delete(q); |
| 148 | + deleteUser(username); |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + private static Resource.StateListener closedListener(Sync sync) { |
| 153 | + return context -> { |
| 154 | + if (context.currentState() == Resource.State.CLOSED |
| 155 | + && context.failureCause() instanceof AmqpException.AmqpSecurityException) { |
| 156 | + sync.down(); |
| 157 | + } |
| 158 | + }; |
| 159 | + } |
91 | 160 | } |
0 commit comments