Commit 44825624 authored by Mayank Saggar [Microsoft]'s avatar Mayank Saggar [Microsoft] Committed by Kishore Battula
Browse files

Revert "Replace Item Now returns a Optional container instance of the updated Item"

This reverts commit 854d570b.
parent a215bea3
......@@ -20,7 +20,7 @@
<groupId>org.opengroup.osdu</groupId>
<artifactId>core-lib-azure</artifactId>
<packaging>jar</packaging>
<version>0.0.52</version>
<version>0.0.53</version>
<name>core-lib-azure</name>
<properties>
......
......@@ -178,6 +178,38 @@ public class CosmosStore {
}
}
/**
* @param dataPartitionId Data partition id
* @param cosmosDBName Database name
* @param collection Collection name
* @param id ID of item
* @param partitionKey Partition key of item
* @param item Data object to store
* @param <T> Type of item
*/
public <T> void replaceItem(
final String dataPartitionId,
final String cosmosDBName,
final String collection,
final String id,
final String partitionKey,
final T item) {
try {
CosmosContainer cosmosContainer = getCosmosContainer(dataPartitionId, cosmosDBName, collection);
PartitionKey key = new PartitionKey(partitionKey);
CosmosItemRequestOptions options = new CosmosItemRequestOptions();
cosmosContainer.replaceItem(item, id, key, options);
} catch (NotFoundException e) {
String errorMessage = "Item was unexpectedly not found";
LOGGER.warn(errorMessage, e);
throw new AppException(404, errorMessage, e.getMessage(), e);
} catch (CosmosException e) {
String errorMessage = "Unexpectedly failed to replace item into CosmosDB";
LOGGER.warn(errorMessage, e);
throw new AppException(500, errorMessage, e.getMessage(), e);
}
}
/**
*
* @param dataPartitionId Data partition id
......
......@@ -26,41 +26,29 @@ import com.azure.cosmos.implementation.NotFoundException;
import com.azure.cosmos.models.CosmosItemRequestOptions;
import com.azure.cosmos.models.CosmosItemResponse;
import com.azure.cosmos.models.PartitionKey;
import com.azure.cosmos.models.SqlQuerySpec;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
import org.opengroup.osdu.azure.cosmosdb.CosmosStore;
import org.opengroup.osdu.azure.cosmosdb.ICosmosClientFactory;
import org.opengroup.osdu.azure.multitenancy.TenantInfoDoc;
import org.opengroup.osdu.core.common.model.http.AppException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.eq;
import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
class CosmosStoreTest {
......@@ -71,6 +59,7 @@ class CosmosStoreTest {
private static final String COLLECTION = "collection";
private static final String COLLECTION_LINK = "/dbs/cosmosdb/colls/collection";
private static final String DATA_PARTITION_ID = "data-partition-id";
private static final String ITEM = "ITEM";
@Mock
......@@ -157,6 +146,40 @@ class CosmosStoreTest {
assertEquals(500, exception.getError().getCode());
}
@Test
void replaceItem_throws500_ifUnknownError() throws CosmosException {
ArgumentCaptor<PartitionKey> partitionKeyArgumentCaptor = ArgumentCaptor.forClass(PartitionKey.class);
doThrow(CosmosException.class).when(container).replaceItem(eq(ITEM), eq(ID), partitionKeyArgumentCaptor.capture(), any(CosmosItemRequestOptions.class));
AppException exception = assertThrows(AppException.class, () -> {
cosmosStore.replaceItem(DATA_PARTITION_ID, COSMOS_DB, COLLECTION, ID, PARTITION_KEY, ITEM);
});
assertEquals(500, exception.getError().getCode());
verify(container).replaceItem(eq(ITEM), eq(ID), any(PartitionKey.class), any(CosmosItemRequestOptions.class));
Assertions.assertTrue(partitionKeyArgumentCaptor.getValue().toString().contains(PARTITION_KEY));
}
@Test
void replaceItem_throws404_ifNotFound() throws CosmosException {
ArgumentCaptor<PartitionKey> partitionKeyArgumentCaptor = ArgumentCaptor.forClass(PartitionKey.class);
doThrow(NotFoundException.class).when(container).replaceItem(eq(ITEM), eq(ID), partitionKeyArgumentCaptor.capture(), any(CosmosItemRequestOptions.class));
AppException exception = assertThrows(AppException.class, () -> {
cosmosStore.replaceItem(DATA_PARTITION_ID, COSMOS_DB, COLLECTION, ID, PARTITION_KEY, ITEM);
});
assertEquals(404, exception.getError().getCode());
verify(container).replaceItem(eq(ITEM), eq(ID), any(PartitionKey.class), any(CosmosItemRequestOptions.class));
Assertions.assertTrue(partitionKeyArgumentCaptor.getValue().toString().contains(PARTITION_KEY));
}
@Test
void replaceItem_Success() {
CosmosItemResponse<String> cosmosItemResponse = mock(CosmosItemResponse.class);
ArgumentCaptor<PartitionKey> partitionKeyArgumentCaptor = ArgumentCaptor.forClass(PartitionKey.class);
doReturn(cosmosItemResponse).when(container).replaceItem(eq(ITEM), eq(ID), partitionKeyArgumentCaptor.capture(), any(CosmosItemRequestOptions.class));
cosmosStore.replaceItem(DATA_PARTITION_ID, COSMOS_DB, COLLECTION, ID, PARTITION_KEY, ITEM);
verify(container).replaceItem(eq(ITEM), eq(ID), any(PartitionKey.class), any(CosmosItemRequestOptions.class));
Assertions.assertTrue(partitionKeyArgumentCaptor.getValue().toString().contains(PARTITION_KEY));
}
@Test
void createItem_throws409_ifDuplicateDocument() throws CosmosException {
doThrow(ConflictException.class).when(container).createItem(any(), any(), any());
......
......@@ -3,6 +3,7 @@ package org.opengroup.osdu.azure.servicebus;
import com.microsoft.azure.servicebus.SubscriptionClient;
import com.microsoft.azure.servicebus.primitives.ServiceBusException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
......@@ -95,6 +96,7 @@ public class SubscriptionClientFactoryImplTest {
}
@Test
@Disabled
public void should_return_client_when_partition_valid() throws ServiceBusException, InterruptedException {
when(this.partitionServiceClient.getPartition(PARTITION_ID)).thenReturn(
PartitionInfoAzure.builder().sbConnectionConfig(Property.builder().value(SB_CONNECTION_STRING).build())
......
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