Skip to content

Commit

Permalink
Sonar: rename non-static field
Browse files Browse the repository at this point in the history
* Rename the non-static CLIENT_EXTENSION field to clientExtension
  in DropwizardManagedClientBuilderTest.
* Add a comment explaining why the field cannot be static.
  • Loading branch information
sleberknight committed Jul 8, 2024
1 parent b4bc9af commit 3c5b05e
Showing 1 changed file with 23 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,21 @@ public Response verifyHeadersWereSent(@Context HttpHeaders httpHeaders) {

private String baseUri;

private final DropwizardClientExtension CLIENT_EXTENSION = new DropwizardClientExtension(new TestResource());
//
// This field cannot be static; making it static results in an java.lang.IllegalArgumentException in the tests
// which call buildManagedJerseyClient. It works the first time it is called, but all tests that call it
// afterwards fail with the message:
//
// "A metric named org.apache.hc.client5.http.io.HttpClientConnectionManager.jersey-client.available-connections already exists"
//
// This is because, if the field is static, the MetricRegistry has already been created. Therefore, we need
// a fresh instance for each test, even though this slows the test down.
//
private final DropwizardClientExtension clientExtension = new DropwizardClientExtension(new TestResource());

@BeforeEach
void setUp() {
baseUri = CLIENT_EXTENSION.baseUri().toString() + "/test";
baseUri = clientExtension.baseUri().toString() + "/test";
}

@Nested
Expand Down Expand Up @@ -114,7 +124,7 @@ void shouldUseGivenJerseyClientConfiguration() {

client = new DropwizardManagedClientBuilder()
.clientName(CLIENT_NAME)
.environment(CLIENT_EXTENSION.getEnvironment())
.environment(clientExtension.getEnvironment())
.jerseyClientConfiguration(config)
.buildManagedJerseyClient();

Expand All @@ -126,7 +136,7 @@ void shouldUseGivenJerseyClientConfiguration() {
void shouldSetupDefaultJerseyClientConfigurationIfNotGiven_IgnoringTLSIfOptedOut() {
client = new DropwizardManagedClientBuilder()
.clientName(CLIENT_NAME)
.environment(CLIENT_EXTENSION.getEnvironment())
.environment(clientExtension.getEnvironment())
.withoutTls()
.buildManagedJerseyClient();

Expand All @@ -137,7 +147,7 @@ void shouldSetupDefaultJerseyClientConfigurationIfNotGiven_IgnoringTLSIfOptedOut
void shouldSetupDefaultJerseyClientConfigurationIfNotGiven_IgnoringTLSIfConfigProviderNotGiven() {
client = new DropwizardManagedClientBuilder()
.clientName(CLIENT_NAME)
.environment(CLIENT_EXTENSION.getEnvironment())
.environment(clientExtension.getEnvironment())
.buildManagedJerseyClient();

assertThat(client).isInstanceOf(Client.class);
Expand All @@ -147,7 +157,7 @@ void shouldSetupDefaultJerseyClientConfigurationIfNotGiven_IgnoringTLSIfConfigPr
void shouldSetupDefaultJerseyClientConfigurationIfNotGiven_IgnoringTLSIfConfigProviderCannotProvide() {
client = new DropwizardManagedClientBuilder()
.clientName(CLIENT_NAME)
.environment(CLIENT_EXTENSION.getEnvironment())
.environment(clientExtension.getEnvironment())
.tlsConfigProvider(TlsConfigProvider.builder().build())
.buildManagedJerseyClient();

Expand All @@ -168,7 +178,7 @@ void shouldSetupDefaultJerseyClientConfigurationWithTls() {

client = new DropwizardManagedClientBuilder()
.clientName(CLIENT_NAME)
.environment(CLIENT_EXTENSION.getEnvironment())
.environment(clientExtension.getEnvironment())
.tlsConfigProvider(tlsConfigProvider)
.buildManagedJerseyClient();

Expand All @@ -181,7 +191,7 @@ void shouldNotAllowNullTlsContextConfiguration() {
.isThrownBy(() ->
new DropwizardManagedClientBuilder()
.clientName(CLIENT_NAME)
.environment(CLIENT_EXTENSION.getEnvironment())
.environment(clientExtension.getEnvironment())
.tlsContextConfiguration(null)
.buildManagedJerseyClient())
.withMessage("tlsConfig must not be null");
Expand All @@ -197,7 +207,7 @@ void shouldSetupDefaultJerseyClientConfigurationWithTlsContextConfiguration() {

client = new DropwizardManagedClientBuilder()
.clientName(CLIENT_NAME)
.environment(CLIENT_EXTENSION.getEnvironment())
.environment(clientExtension.getEnvironment())
.tlsContextConfiguration(tlsConfig)
.buildManagedJerseyClient();

Expand All @@ -208,7 +218,7 @@ void shouldSetupDefaultJerseyClientConfigurationWithTlsContextConfiguration() {
void shouldUseGivenHeadersSupplier() {
client = new DropwizardManagedClientBuilder()
.clientName(CLIENT_NAME)
.environment(CLIENT_EXTENSION.getEnvironment())
.environment(clientExtension.getEnvironment())
.headersSupplier(() ->
Map.of(
"Header-1", "Value-1",
Expand Down Expand Up @@ -246,7 +256,7 @@ void tearDown() {
void shouldThrowIllegalStateExceptionIfRegistryClientNotSet() {
var builder = new DropwizardManagedClientBuilder()
.clientName(CLIENT_NAME)
.environment(CLIENT_EXTENSION.getEnvironment());
.environment(clientExtension.getEnvironment());

assertThatThrownBy(builder::buildManagedRegistryAwareClient)
.isInstanceOf(IllegalStateException.class)
Expand All @@ -257,7 +267,7 @@ void shouldThrowIllegalStateExceptionIfRegistryClientNotSet() {
void shouldBuildRegistryAwareClient() {
client = new DropwizardManagedClientBuilder()
.clientName(CLIENT_NAME)
.environment(CLIENT_EXTENSION.getEnvironment())
.environment(clientExtension.getEnvironment())
.registryClient(mock(RegistryClient.class))
.buildManagedRegistryAwareClient();

Expand All @@ -269,7 +279,7 @@ void shouldBuildRegistryAwareClient() {
void shouldUseGivenHeadersSupplier() {
client = new DropwizardManagedClientBuilder()
.clientName(CLIENT_NAME)
.environment(CLIENT_EXTENSION.getEnvironment())
.environment(clientExtension.getEnvironment())
.registryClient(mock(RegistryClient.class))
.headersSupplier(() -> Map.of(
"Header-A", "Value-A",
Expand Down

0 comments on commit 3c5b05e

Please sign in to comment.