Commit 82dbeccc authored by Krishna Nikhil Vedurumudi's avatar Krishna Nikhil Vedurumudi
Browse files

Fixed tests and removed unrequired cache classes

parent 787984a9
......@@ -20,7 +20,7 @@
<groupId>org.opengroup.osdu</groupId>
<artifactId>core-lib-azure</artifactId>
<packaging>jar</packaging>
<version>0.9.0-COSMOS-SNAPSHOT</version>
<version>0.9.0-SNAPSHOT</version>
<name>core-lib-azure</name>
<properties>
......
package org.opengroup.osdu.azure.cache;
import com.microsoft.azure.documentdb.bulkexecutor.DocumentBulkExecutor;
import org.opengroup.osdu.core.common.cache.VmCache;
import org.opengroup.osdu.core.common.cache.enums.CachingStrategy;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
/**
* Implementation of ICache for DocumentBulkExecutor.
*/
@Component
@Lazy
public class CosmosBulkExecutorCache extends VmCache<String, DocumentBulkExecutor> {
/**
* Default cache constructor.
*/
public CosmosBulkExecutorCache() {
super(60 * 60, 1000, CachingStrategy.EXPIRE_AFTER_WRITE);
}
/**
* @param key cache key
* @return true if found in cache
*/
public boolean containsKey(final String key) {
return this.get(key) != null;
}
}
\ No newline at end of file
package org.opengroup.osdu.azure.cache;
import com.azure.cosmos.CosmosClient;
import org.opengroup.osdu.core.common.cache.VmCache;
import org.opengroup.osdu.core.common.cache.enums.CachingStrategy;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
/**
* Implementation of ICache for CosmosClient.
*/
@Component
@Lazy
public class CosmosClientCache extends VmCache<String, CosmosClient> {
/**
* Default cache constructor.
*/
public CosmosClientCache() {
super(60 * 60, 1000, CachingStrategy.EXPIRE_AFTER_WRITE);
}
/**
* @param key cache key
* @return true if found in cache
*/
public boolean containsKey(final String key) {
return this.get(key) != null;
}
}
\ No newline at end of file
......@@ -6,7 +6,6 @@ import com.microsoft.azure.documentdb.DocumentClient;
import com.microsoft.azure.documentdb.DocumentClientException;
import com.microsoft.azure.documentdb.DocumentCollection;
import com.microsoft.azure.documentdb.bulkexecutor.DocumentBulkExecutor;
import org.opengroup.osdu.azure.cache.CosmosBulkExecutorCache;
import org.opengroup.osdu.azure.partition.PartitionInfoAzure;
import org.opengroup.osdu.azure.partition.PartitionServiceClient;
import org.opengroup.osdu.common.Validators;
......@@ -17,6 +16,10 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;
/**
* A factory class to generate DocumentBulkExecutor objects to perform bulk operations.
*/
......@@ -30,9 +33,7 @@ public class CosmosBulkExecutorFactoryImpl implements ICosmosBulkExecutorFactory
@Autowired
private PartitionServiceClient partitionService;
@Lazy
@Autowired
private CosmosBulkExecutorCache cosmosBulkExecutorCache;
private Map<String, DocumentBulkExecutor> cosmosClientMap;
@Autowired
private int documentClientMaxPoolSize;
......@@ -44,6 +45,14 @@ public class CosmosBulkExecutorFactoryImpl implements ICosmosBulkExecutorFactory
private final String unformattedCosmosBulkExecutorCacheKey = "%s-%s-%s-cosmosBulkExecutor";
private final String unformattedDocumentClientCacheKey = "%s-documentClient";
/**
* Initializes the private variables as required.
*/
@PostConstruct
public void initialize() {
cosmosClientMap = new HashMap<>();
}
/**
*
* @param dataPartitionId name of data partition.
......@@ -60,8 +69,8 @@ public class CosmosBulkExecutorFactoryImpl implements ICosmosBulkExecutorFactory
Validators.checkNotNullAndNotEmpty(collectionName, "collectionName");
String cacheKey = String.format(unformattedCosmosBulkExecutorCacheKey, dataPartitionId, cosmosDBName, collectionName);
if (this.cosmosBulkExecutorCache.containsKey(cacheKey)) {
return this.cosmosBulkExecutorCache.get(cacheKey);
if (this.cosmosClientMap.containsKey(cacheKey)) {
return this.cosmosClientMap.get(cacheKey);
}
PartitionInfoAzure pi = this.partitionService.getPartition(dataPartitionId);
......@@ -80,7 +89,8 @@ public class CosmosBulkExecutorFactoryImpl implements ICosmosBulkExecutorFactory
collection.getPartitionKey(),
bulkExecutorMaxRUs
).build();
cosmosBulkExecutorCache.put(String.format(unformattedCosmosBulkExecutorCacheKey, dataPartitionId, cosmosDBName, collectionName), executor);
cosmosClientMap.put(String.format(unformattedCosmosBulkExecutorCacheKey, dataPartitionId, cosmosDBName, collectionName), executor);
// Set client retry options to 0 because retries are handled by DocumentBulkExecutor class.
client.getConnectionPolicy().getRetryOptions().setMaxRetryAttemptsOnThrottledRequests(0);
......
......@@ -7,9 +7,10 @@ import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.opengroup.osdu.azure.cache.CosmosBulkExecutorCache;
import org.opengroup.osdu.azure.partition.PartitionServiceClient;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;
import static org.mockito.Mockito.*;
......@@ -20,7 +21,7 @@ import static org.mockito.MockitoAnnotations.initMocks;
public class ComosBulkExecutorImplTest {
@Mock
private CosmosBulkExecutorCache clientCache;
private Map<String, DocumentBulkExecutor> cosmosClientMap;
@Mock
private PartitionServiceClient partitionService;
@InjectMocks
......@@ -61,8 +62,8 @@ public class ComosBulkExecutorImplTest {
public void should_return_cachedClient_when_cachedEarlier() {
DocumentBulkExecutor cosmosClient = mock(DocumentBulkExecutor.class);
final String cacheKey = String.format("%s-%s-%s-cosmosBulkExecutor", PARTITION_ID, COSMOS_DB_NAME, COSMOS_COLLECTION_NAME);
when(this.clientCache.containsKey(cacheKey)).thenReturn(true);
when(this.clientCache.get(cacheKey)).thenReturn(cosmosClient);
when(this.cosmosClientMap.containsKey(cacheKey)).thenReturn(true);
when(this.cosmosClientMap.get(cacheKey)).thenReturn(cosmosClient);
this.sut.getClient(PARTITION_ID, COSMOS_DB_NAME, COSMOS_COLLECTION_NAME);
verify(this.partitionService, never()).getPartition(PARTITION_ID);
......
......@@ -7,9 +7,10 @@ import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.opengroup.osdu.azure.cache.CosmosClientCache;
import org.opengroup.osdu.azure.partition.PartitionServiceClient;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;
import static org.mockito.Mockito.*;
......@@ -19,7 +20,7 @@ import static org.mockito.MockitoAnnotations.initMocks;
public class CosmosClientFactoryImplTest {
@Mock
private CosmosClientCache clientCache;
private Map<String, CosmosClient> cosmosClientMap;
@Mock
private PartitionServiceClient partitionService;
@InjectMocks
......@@ -54,11 +55,12 @@ public class CosmosClientFactoryImplTest {
}
}
@Test
public void should_return_cachedClient_when_cachedEarlier() {
CosmosClient cosmosClient = mock(CosmosClient.class);
final String cacheKey = String.format("%s-cosmosClient", PARTITION_ID);
when(this.clientCache.containsKey(cacheKey)).thenReturn(true);
when(this.clientCache.get(cacheKey)).thenReturn(cosmosClient);
when(this.cosmosClientMap.containsKey(cacheKey)).thenReturn(true);
when(this.cosmosClientMap.get(cacheKey)).thenReturn(cosmosClient);
this.sut.getClient(PARTITION_ID);
verify(this.partitionService, never()).getPartition(PARTITION_ID);
......
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