Commit 6621c838 authored by Komal Makkar's avatar Komal Makkar
Browse files

The subscription Client creation is extracted to a new method so that UTs can...

The subscription Client creation is extracted to a new method so that UTs can have the subscription client creation mocked.
parent a215bea3
......@@ -4,6 +4,7 @@ import com.microsoft.azure.servicebus.ReceiveMode;
import com.microsoft.azure.servicebus.SubscriptionClient;
import com.microsoft.azure.servicebus.primitives.ConnectionStringBuilder;
import com.microsoft.azure.servicebus.primitives.ServiceBusException;
import org.jetbrains.annotations.NotNull;
import org.opengroup.osdu.azure.cache.SubscriptionClientCache;
import org.opengroup.osdu.azure.partition.PartitionInfoAzure;
import org.opengroup.osdu.azure.partition.PartitionServiceClient;
......@@ -26,8 +27,8 @@ public class SubscriptionClientFactoryImpl implements ISubscriptionClientFactory
private SubscriptionClientCache clientCache;
/**
* @param dataPartitionId Data Partition Id
* @param topicName Service Bus Topic Name
* @param dataPartitionId Data Partition Id
* @param topicName Service Bus Topic Name
* @param subscriptionName Service Bus Subscription Name
* @return A client configured to communicate with a Service Bus Subscription
* @throws ServiceBusException Exception thrown by {@link SubscriptionClient}
......@@ -47,16 +48,27 @@ public class SubscriptionClientFactoryImpl implements ISubscriptionClientFactory
PartitionInfoAzure pi = this.partitionService.getPartition(dataPartitionId);
String serviceBusConnectionString = pi.getSbConnection();
ConnectionStringBuilder connectionStringBuilder = new ConnectionStringBuilder(
serviceBusConnectionString,
entityPath
);
SubscriptionClient subscriptionClient = new SubscriptionClient(connectionStringBuilder, ReceiveMode.PEEKLOCK);
SubscriptionClient subscriptionClient = getSubscriptionClient(entityPath, serviceBusConnectionString);
this.clientCache.put(cacheKey, subscriptionClient);
return subscriptionClient;
}
/***
* @param entityPath Service Bus entity path
* @param serviceBusConnectionString Service Bus Connection String
* @return SubscriptionClient object
* @throws InterruptedException Exception thrown by {@link SubscriptionClient}
* @throws ServiceBusException Exception thrown by {@link SubscriptionClient}
*/
@NotNull SubscriptionClient getSubscriptionClient(final String entityPath, final String serviceBusConnectionString) throws InterruptedException, ServiceBusException {
ConnectionStringBuilder connectionStringBuilder = new ConnectionStringBuilder(
serviceBusConnectionString,
entityPath
);
return new SubscriptionClient(connectionStringBuilder, ReceiveMode.PEEKLOCK);
}
}
\ No newline at end of file
package org.opengroup.osdu.azure.servicebus;
import com.microsoft.azure.servicebus.SubscriptionClient;
import com.microsoft.azure.servicebus.primitives.ConnectionStringBuilder;
import com.microsoft.azure.servicebus.primitives.ServiceBusException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
import org.opengroup.osdu.azure.cache.SubscriptionClientCache;
import org.opengroup.osdu.azure.partition.PartitionInfoAzure;
......@@ -33,6 +35,7 @@ public class SubscriptionClientFactoryImplTest {
private static final String SUBSCRIPTION_NAME = "testSubscription";
private static final String SB_ENTITY_PATH = "testTopic/subscriptions/testSubscription";
private static final String SB_CONNECTION_STRING = "Endpoint=sb://test-bus.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=testKey";
@BeforeEach
void init() {
initMocks(this);
......@@ -96,10 +99,14 @@ public class SubscriptionClientFactoryImplTest {
@Test
public void should_return_client_when_partition_valid() throws ServiceBusException, InterruptedException {
SubscriptionClient subscriptionClient = mock(SubscriptionClient.class);
SubscriptionClientFactoryImpl subscriptionClientFactorySpy = Mockito.spy(subscriptionClientFactory);
doReturn(subscriptionClient).when(subscriptionClientFactorySpy).getSubscriptionClient(anyString(), anyString());
when(this.partitionServiceClient.getPartition(PARTITION_ID)).thenReturn(
PartitionInfoAzure.builder().sbConnectionConfig(Property.builder().value(SB_CONNECTION_STRING).build())
.build());
assertNotNull(this.subscriptionClientFactory.getClient(PARTITION_ID, TOPIC_NAME, SUBSCRIPTION_NAME));
assertNotNull(subscriptionClientFactorySpy.getClient(PARTITION_ID, TOPIC_NAME, SUBSCRIPTION_NAME));
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment