Commit 3fb619c5 authored by Hema Vishnu Pola [Microsoft]'s avatar Hema Vishnu Pola [Microsoft]
Browse files

Merge branch 'users/amaverma/addCreateItemMethod' into 'master'

adding CreateItem method to the comosStore class

See merge request !16
parents d5161b67 5871c627
Pipeline #5018 passed with stages
in 24 minutes and 42 seconds
......@@ -21,7 +21,7 @@
<groupId>org.opengroup.osdu</groupId>
<artifactId>core-lib-azure</artifactId>
<packaging>jar</packaging>
<version>0.0.15</version>
<version>0.0.16</version>
<name>core-lib-azure</name>
<properties>
......
......@@ -14,6 +14,7 @@
package org.opengroup.osdu.azure;
import com.azure.cosmos.ConflictException;
import com.azure.cosmos.CosmosClientException;
import com.azure.cosmos.CosmosContainer;
import com.azure.cosmos.CosmosItem;
......@@ -69,6 +70,10 @@ import java.util.logging.Logger;
*
* List<MyObject> objects = cosmosStore.queryItems("dataPartitionId", "cosmosDb", "collection", query, options, MyObject.class);
* }
*
* void createItemExample() {
* cosmosStore.createItem("dataPartitionId", "cosmosDb", "collection", "some-data");
* }
* }
* </pre>
*/
......@@ -316,6 +321,32 @@ public class CosmosStore {
}
}
/**
* @param dataPartitionId Data partition id to fetch appropriate cosmos client for each partition
* @param cosmosDBName Database to be used
* @param collection Collection to be used
* @param item Data object to store
* @param <T> Type of response
*/
public <T> void createItem(
final String dataPartitionId,
final String cosmosDBName,
final String collection,
final T item) {
try {
CosmosContainer cosmosContainer = getCosmosContainer(dataPartitionId, cosmosDBName, collection);
cosmosContainer.createItem(item);
} catch (ConflictException e) {
String errorMessage = "Resource with specified id or name already exists.";
LOGGER.log(Level.WARNING, errorMessage, e);
throw new AppException(409, errorMessage, e.getMessage(), e);
} catch (CosmosClientException e) {
String errorMessage = "Unexpectedly failed to insert item into CosmosDB";
LOGGER.log(Level.WARNING, errorMessage, e);
throw new AppException(500, errorMessage, e.getMessage(), e);
}
}
/**
* @param cosmos Container to query
* @param id ID of item
......
......@@ -14,6 +14,7 @@
package org.opengroup.osdu.azure;
import com.azure.cosmos.ConflictException;
import com.azure.cosmos.CosmosClient;
import com.azure.cosmos.CosmosClientException;
import com.azure.cosmos.CosmosContainer;
......@@ -47,6 +48,7 @@ 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.Mockito.doReturn;
......@@ -158,6 +160,33 @@ class CosmosStoreTest {
assertEquals(500, exception.getError().getCode());
}
@Test
void createItem_throws409_ifDuplicateDocument() throws CosmosClientException {
doThrow(ConflictException.class).when(container).createItem(any());
AppException exception = assertThrows(AppException.class, () -> {
cosmosStore.createItem(DATA_PARTITION_ID, COSMOS_DB, COLLECTION, "some-data");
});
assertEquals(409, exception.getError().getCode());
}
@Test
void createItem_throws500_ifUnknownError() throws CosmosClientException {
doThrow(CosmosClientException.class).when(container).createItem(any());
AppException exception = assertThrows(AppException.class, () -> {
cosmosStore.createItem(DATA_PARTITION_ID, COSMOS_DB, COLLECTION, "some-data");
});
assertEquals(500, exception.getError().getCode());
}
@Test
void createItem_Success() throws CosmosClientException {
try {
cosmosStore.createItem(DATA_PARTITION_ID, COSMOS_DB, COLLECTION, "some-data");
} catch (Exception ex) {
fail("Should not fail.");
}
}
@Test
void findAllItems_executesCorrectQuery() throws IOException {
mockQueryResponse("s1");
......
Supports Markdown
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