Skip to content

Commit 431ed8c

Browse files
committed
Refactor OAuth 2 tests to be standalone
1 parent 9f2312b commit 431ed8c

File tree

6 files changed

+274
-128
lines changed

6 files changed

+274
-128
lines changed

src/main/java/com/rabbitmq/client/amqp/oauth2/TokenCredentialsManager.java

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
1-
// Copyright (c) 2024 Broadcom. All Rights Reserved.
1+
// Copyright (c) 2024-2025 Broadcom. All Rights Reserved.
22
// The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.
33
//
4-
// Licensed under the Apache License, Version 2.0 (the "License");
5-
// you may not use this file except in compliance with the License.
6-
// You may obtain a copy of the License at
4+
// This software, the RabbitMQ Stream Java client library, is dual-licensed under the
5+
// Mozilla Public License 2.0 ("MPL"), and the Apache License version 2 ("ASL").
6+
// For the MPL, please see LICENSE-MPL-RabbitMQ. For the ASL,
7+
// please see LICENSE-APACHE2.
78
//
8-
// http://www.apache.org/licenses/LICENSE-2.0
9-
//
10-
// Unless required by applicable law or agreed to in writing, software
11-
// distributed under the License is distributed on an "AS IS" BASIS,
12-
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
// See the License for the specific language governing permissions and
14-
// limitations under the License.
9+
// This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND,
10+
// either express or implied. See the LICENSE file for specific language governing
11+
// rights and limitations of this software.
1512
//
1613
// If you have any questions regarding licensing, please contact us at
1714
@@ -80,16 +77,16 @@ private boolean expiresSoon(Token ignores) {
8077
}
8178

8279
private Token getToken() {
83-
if (LOGGER.isDebugEnabled()) {
80+
if (debug()) {
8481
LOGGER.debug(
8582
"Requesting new token ({})...", registrationSummary(this.registrations.values()));
8683
}
8784
long start = 0L;
88-
if (LOGGER.isDebugEnabled()) {
85+
if (debug()) {
8986
start = System.nanoTime();
9087
}
9188
Token token = requester.request();
92-
if (LOGGER.isDebugEnabled()) {
89+
if (debug()) {
9390
LOGGER.debug(
9491
"Got new token in {} ms, token expires on {} ({})",
9592
Duration.ofNanos(System.nanoTime() - start),
@@ -128,14 +125,14 @@ private void updateRegistrations(Token t) {
128125
registration.registrationToken = this.token;
129126
refreshedCount++;
130127
} else {
131-
if (LOGGER.isDebugEnabled()) {
128+
if (debug()) {
132129
LOGGER.debug(
133130
"Not updating registration {} (closed or already has the new token)",
134131
registration.name());
135132
}
136133
}
137134
} else {
138-
if (LOGGER.isDebugEnabled()) {
135+
if (debug()) {
139136
LOGGER.debug(
140137
"Not updating registration {} (the token has changed)", registration.name());
141138
}
@@ -160,33 +157,46 @@ private void token(Token t) {
160157
private void scheduleTokenRefresh(Token t) {
161158
if (this.schedulingRefresh.compareAndSet(false, true)) {
162159
if (this.refreshTask != null) {
160+
if (debug()) {
161+
LOGGER.debug("Cancelling refresh task (scheduling a new one)");
162+
}
163163
this.refreshTask.cancel(false);
164164
}
165165
Duration delay = this.refreshDelayStrategy.apply(t.expirationTime());
166166
if (!this.registrations.isEmpty()) {
167-
if (LOGGER.isDebugEnabled()) {
167+
if (debug()) {
168168
LOGGER.debug(
169-
"Scheduling token retrieval in {} ({})",
169+
"Scheduling token update in {} ({})",
170170
delay,
171171
registrationSummary(this.registrations.values()));
172172
}
173173
this.refreshTask =
174174
this.scheduledExecutorService.schedule(
175175
() -> {
176+
if (debug()) {
177+
LOGGER.debug("Starting token update task");
178+
}
176179
Token previousToken = this.token;
177180
this.lock();
178181
try {
179182
if (this.token.equals(previousToken)) {
180183
Token newToken = getToken();
181184
token(newToken);
182185
updateRegistrations(newToken);
186+
} else {
187+
if (debug()) {
188+
LOGGER.debug("Token has already been updated");
189+
}
183190
}
184191
} finally {
185192
unlock();
186193
}
187194
},
188195
delay.toMillis(),
189196
TimeUnit.MILLISECONDS);
197+
if (debug()) {
198+
LOGGER.debug("Task scheduled");
199+
}
190200
} else {
191201
this.refreshTask = null;
192202
}
@@ -214,6 +224,9 @@ private RegistrationImpl(Long id, String name, AuthenticationCallback updateCall
214224

215225
@Override
216226
public void connect(AuthenticationCallback callback) {
227+
if (debug()) {
228+
LOGGER.debug("Connecting registration {}", this.name);
229+
}
217230
boolean shouldRefresh = false;
218231
Token tokenToUse;
219232
lock();
@@ -235,6 +248,11 @@ public void connect(AuthenticationCallback callback) {
235248
} finally {
236249
unlock();
237250
}
251+
if (debug()) {
252+
if (debug()) {
253+
LOGGER.debug("Authenticating registration {}", this.name);
254+
}
255+
}
238256
callback.authenticate("", tokenToUse.value());
239257
if (shouldRefresh) {
240258
updateRegistrations(tokenToUse);
@@ -244,6 +262,7 @@ public void connect(AuthenticationCallback callback) {
244262
@Override
245263
public void close() {
246264
if (this.closed.compareAndSet(false, true)) {
265+
LOGGER.debug("Closing credentials registration {}", this.name);
247266
registrations.remove(this.id);
248267
ScheduledFuture<?> task = refreshTask;
249268
if (registrations.isEmpty() && task != null) {
@@ -325,4 +344,8 @@ public Duration apply(Instant expirationTime) {
325344
private static String registrationSummary(Collection<? extends Registration> registrations) {
326345
return registrations.stream().map(Registration::toString).collect(Collectors.joining(", "));
327346
}
347+
348+
private static boolean debug() {
349+
return LOGGER.isDebugEnabled();
350+
}
328351
}

src/test/java/com/rabbitmq/client/amqp/oauth2/GsonTokenParserTest.java

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,20 @@
1-
// Copyright (c) 2024 Broadcom. All Rights Reserved.
1+
// Copyright (c) 2024-2025 Broadcom. All Rights Reserved.
22
// The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.
33
//
4-
// Licensed under the Apache License, Version 2.0 (the "License");
5-
// you may not use this file except in compliance with the License.
6-
// You may obtain a copy of the License at
4+
// This software, the RabbitMQ Stream Java client library, is dual-licensed under the
5+
// Mozilla Public License 2.0 ("MPL"), and the Apache License version 2 ("ASL").
6+
// For the MPL, please see LICENSE-MPL-RabbitMQ. For the ASL,
7+
// please see LICENSE-APACHE2.
78
//
8-
// http://www.apache.org/licenses/LICENSE-2.0
9-
//
10-
// Unless required by applicable law or agreed to in writing, software
11-
// distributed under the License is distributed on an "AS IS" BASIS,
12-
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
// See the License for the specific language governing permissions and
14-
// limitations under the License.
9+
// This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND,
10+
// either express or implied. See the LICENSE file for specific language governing
11+
// rights and limitations of this software.
1512
//
1613
// If you have any questions regarding licensing, please contact us at
1714
1815
package com.rabbitmq.client.amqp.oauth2;
1916

20-
import static com.rabbitmq.client.amqp.oauth2.OAuth2TestUtils.sampleJsonToken;
17+
import static com.rabbitmq.stream.oauth2.OAuth2TestUtils.sampleJsonToken;
2118
import static java.time.Duration.ofSeconds;
2219
import static org.assertj.core.api.Assertions.assertThat;
2320
import static org.assertj.core.api.Assertions.within;

src/test/java/com/rabbitmq/client/amqp/oauth2/HttpTokenRequesterTest.java

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,24 @@
1-
// Copyright (c) 2024 Broadcom. All Rights Reserved.
1+
// Copyright (c) 2024-2025 Broadcom. All Rights Reserved.
22
// The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.
33
//
4-
// Licensed under the Apache License, Version 2.0 (the "License");
5-
// you may not use this file except in compliance with the License.
6-
// You may obtain a copy of the License at
4+
// This software, the RabbitMQ Stream Java client library, is dual-licensed under the
5+
// Mozilla Public License 2.0 ("MPL"), and the Apache License version 2 ("ASL").
6+
// For the MPL, please see LICENSE-MPL-RabbitMQ. For the ASL,
7+
// please see LICENSE-APACHE2.
78
//
8-
// http://www.apache.org/licenses/LICENSE-2.0
9-
//
10-
// Unless required by applicable law or agreed to in writing, software
11-
// distributed under the License is distributed on an "AS IS" BASIS,
12-
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
// See the License for the specific language governing permissions and
14-
// limitations under the License.
9+
// This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND,
10+
// either express or implied. See the LICENSE file for specific language governing
11+
// rights and limitations of this software.
1512
//
1613
// If you have any questions regarding licensing, please contact us at
1714
1815
package com.rabbitmq.client.amqp.oauth2;
1916

20-
import static com.rabbitmq.client.amqp.impl.TestUtils.randomNetworkPort;
2117
import static java.nio.charset.StandardCharsets.UTF_8;
2218
import static org.assertj.core.api.Assertions.assertThat;
2319

2420
import com.google.gson.Gson;
2521
import com.google.gson.reflect.TypeToken;
26-
import com.rabbitmq.client.amqp.impl.HttpTestUtils;
2722
import com.sun.net.httpserver.Headers;
2823
import com.sun.net.httpserver.HttpServer;
2924
import java.io.IOException;
@@ -53,7 +48,7 @@ public class HttpTokenRequesterTest {
5348

5449
@BeforeEach
5550
void init() throws IOException {
56-
this.port = randomNetworkPort();
51+
this.port = OAuth2TestUtils.randomNetworkPort();
5752
}
5853

5954
@ParameterizedTest
@@ -64,7 +59,7 @@ void requestToken(boolean tls) throws Exception {
6459
Consumer<HttpClient.Builder> clientBuilderConsumer;
6560
if (tls) {
6661
protocol = "https";
67-
keyStore = HttpTestUtils.generateKeyPair();
62+
keyStore = OAuth2TestUtils.generateKeyPair();
6863
SSLContext sslContext = SSLContext.getInstance("TLS");
6964
TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
7065
tmf.init(keyStore);
@@ -86,7 +81,7 @@ void requestToken(boolean tls) throws Exception {
8681

8782
Duration expiresIn = Duration.ofSeconds(60);
8883
server =
89-
HttpTestUtils.startServer(
84+
OAuth2TestUtils.startServer(
9085
port,
9186
contextPath,
9287
keyStore,

0 commit comments

Comments
 (0)