-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add MessageProducerClientBuilder for creating configurable message pr…
…oducer clients
- Loading branch information
1 parent
6b86dd2
commit 38732d0
Showing
4 changed files
with
177 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
.../java/uk/gov/justice/services/test/utils/core/messaging/MessageProducerClientBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package uk.gov.justice.services.test.utils.core.messaging; | ||
|
||
import static java.util.Optional.empty; | ||
import static java.util.Optional.of; | ||
|
||
import java.util.Optional; | ||
|
||
import com.google.common.annotations.VisibleForTesting; | ||
import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory; | ||
|
||
public class MessageProducerClientBuilder { | ||
|
||
private Optional<Integer> retryInterval = empty(); | ||
private Optional<Integer> maxRetryInterval = empty(); | ||
private Optional<Double> retryIntervalMultiplier = empty(); | ||
private Optional<Integer> reconnectAttempts = empty(); | ||
|
||
private final ActiveMQConnectionFactory activeMQConnectionFactory; | ||
|
||
public static MessageProducerClientBuilder aMessageProducerClient() { | ||
return new MessageProducerClientBuilder(new ActiveMQConnectionFactory()); | ||
} | ||
|
||
@VisibleForTesting | ||
private MessageProducerClientBuilder(final ActiveMQConnectionFactory activeMQConnectionFactory) { | ||
this.activeMQConnectionFactory = activeMQConnectionFactory; | ||
} | ||
|
||
public MessageProducerClientBuilder withRetryInterval(final int retryInterval) { | ||
this.retryInterval = of(retryInterval); | ||
return this; | ||
} | ||
|
||
public MessageProducerClientBuilder withMaxRetryInterval(final int maxRetryInterval) { | ||
this.maxRetryInterval = of(maxRetryInterval); | ||
return this; | ||
} | ||
|
||
public MessageProducerClientBuilder withRetryIntervalMultiplier(final double retryIntervalMultiplier) { | ||
this.retryIntervalMultiplier = of(retryIntervalMultiplier); | ||
return this; | ||
} | ||
|
||
public MessageProducerClientBuilder withReconnectAttempts(int reconnectAttempts) { | ||
this.reconnectAttempts = of(reconnectAttempts); | ||
return this; | ||
} | ||
|
||
public MessageProducerClient build() { | ||
|
||
retryInterval.ifPresent(activeMQConnectionFactory::setRetryInterval); | ||
maxRetryInterval.ifPresent(activeMQConnectionFactory::setMaxRetryInterval); | ||
retryIntervalMultiplier.ifPresent(activeMQConnectionFactory::setRetryIntervalMultiplier); | ||
reconnectAttempts.ifPresent(activeMQConnectionFactory::setReconnectAttempts); | ||
|
||
return new MessageProducerClient(activeMQConnectionFactory); | ||
} | ||
|
||
} |
103 changes: 103 additions & 0 deletions
103
...a/uk/gov/justice/services/test/utils/core/messaging/MessageProducerClientBuilderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package uk.gov.justice.services.test.utils.core.messaging; | ||
|
||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.hamcrest.CoreMatchers.notNullValue; | ||
import static org.hamcrest.CoreMatchers.sameInstance; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.verifyNoMoreInteractions; | ||
import static org.mockito.Mockito.verifyZeroInteractions; | ||
import static uk.gov.justice.services.test.utils.core.messaging.MessageProducerClientBuilder.aMessageProducerClient; | ||
import static uk.gov.justice.services.test.utils.core.reflection.ReflectionUtil.getValueOfField; | ||
|
||
import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.runners.MockitoJUnitRunner; | ||
|
||
@RunWith(MockitoJUnitRunner.class) | ||
public class MessageProducerClientBuilderTest { | ||
|
||
@Mock | ||
private ActiveMQConnectionFactory activeMQConnectionFactory; | ||
|
||
@InjectMocks | ||
private MessageProducerClientBuilder messageProducerClientBuilder; | ||
|
||
@Test | ||
public void shouldHaveStaticFactoryMethod() throws Exception { | ||
assertThat(aMessageProducerClient(), is(notNullValue())); | ||
} | ||
|
||
@Test | ||
public void shouldCreateMessageConsumerClientWithNoExtraActiveMQConnectionFactoryProperties() throws Exception { | ||
|
||
final MessageProducerClient messageProducerClient = messageProducerClientBuilder.build(); | ||
assertThat(messageProducerClient, is(notNullValue())); | ||
|
||
verifyZeroInteractions(activeMQConnectionFactory); | ||
|
||
final ActiveMQConnectionFactory theActiveMQConnectionFactory = getValueOfField(messageProducerClient, "activeMQConnectionFactory", ActiveMQConnectionFactory.class); | ||
|
||
|
||
assertThat(theActiveMQConnectionFactory, is(sameInstance(activeMQConnectionFactory))); | ||
} | ||
|
||
@Test | ||
public void shouldSetRetryIntervalOnActiveMQConnectionFactory() throws Exception { | ||
|
||
final int retryInterval = 23; | ||
|
||
final MessageProducerClient messageProducerClient = messageProducerClientBuilder | ||
.withRetryInterval(retryInterval) | ||
.build(); | ||
assertThat(messageProducerClient, is(notNullValue())); | ||
|
||
verify(activeMQConnectionFactory).setRetryInterval(retryInterval); | ||
verifyNoMoreInteractions(activeMQConnectionFactory); | ||
} | ||
|
||
@Test | ||
public void shouldSetMaxRetryIntervalOnActiveMQConnectionFactory() throws Exception { | ||
|
||
final int maxRetryInterval = 23; | ||
|
||
final MessageProducerClient messageProducerClient = messageProducerClientBuilder | ||
.withMaxRetryInterval(maxRetryInterval) | ||
.build(); | ||
assertThat(messageProducerClient, is(notNullValue())); | ||
|
||
verify(activeMQConnectionFactory).setMaxRetryInterval(maxRetryInterval); | ||
verifyNoMoreInteractions(activeMQConnectionFactory); | ||
} | ||
|
||
@Test | ||
public void shouldSetMaxRetryIntervalMultiplierOnActiveMQConnectionFactory() throws Exception { | ||
|
||
final double retryIntervalMultiplier = 23.1; | ||
|
||
final MessageProducerClient messageProducerClient = messageProducerClientBuilder | ||
.withRetryIntervalMultiplier(retryIntervalMultiplier) | ||
.build(); | ||
assertThat(messageProducerClient, is(notNullValue())); | ||
|
||
verify(activeMQConnectionFactory).setRetryIntervalMultiplier(retryIntervalMultiplier); | ||
verifyNoMoreInteractions(activeMQConnectionFactory); | ||
} | ||
|
||
@Test | ||
public void shouldSetReconnectAttemptsOnActiveMQConnectionFactory() throws Exception { | ||
|
||
final int reconnectAttempts = 23; | ||
|
||
final MessageProducerClient messageProducerClient = messageProducerClientBuilder | ||
.withReconnectAttempts(reconnectAttempts) | ||
.build(); | ||
assertThat(messageProducerClient, is(notNullValue())); | ||
|
||
verify(activeMQConnectionFactory).setReconnectAttempts(reconnectAttempts); | ||
verifyNoMoreInteractions(activeMQConnectionFactory); | ||
} | ||
} |