diff --git a/pom.xml b/pom.xml
index aeecd6fd781119fb8346598fdf0d857e8aab2150..ed1aae4c7264546dfb58fdedaef60fb064ae90c2 100644
--- a/pom.xml
+++ b/pom.xml
@@ -21,7 +21,7 @@
org.opengroup.osdu
core-lib-azure
jar
- 0.0.15
+ 0.0.16
core-lib-azure
diff --git a/src/main/java/org/opengroup/osdu/azure/CosmosStore.java b/src/main/java/org/opengroup/osdu/azure/CosmosStore.java
index a7e166b0e580d49aa8468a4dbd5fe64af338809e..3a64c1695d900dd24b12713a8dade45e5297faa0 100644
--- a/src/main/java/org/opengroup/osdu/azure/CosmosStore.java
+++ b/src/main/java/org/opengroup/osdu/azure/CosmosStore.java
@@ -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 objects = cosmosStore.queryItems("dataPartitionId", "cosmosDb", "collection", query, options, MyObject.class);
* }
+ *
+ * void createItemExample() {
+ * cosmosStore.createItem("dataPartitionId", "cosmosDb", "collection", "some-data");
+ * }
* }
*
*/
@@ -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 Type of response
+ */
+ public 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
diff --git a/src/test/java/org/opengroup/osdu/azure/CosmosStoreTest.java b/src/test/java/org/opengroup/osdu/azure/CosmosStoreTest.java
index 6d78f55049ed5ccba1ece030e6df9fe0ff193c2d..badef31c1cc734da3e11d788b06644d031ab48db 100644
--- a/src/test/java/org/opengroup/osdu/azure/CosmosStoreTest.java
+++ b/src/test/java/org/opengroup/osdu/azure/CosmosStoreTest.java
@@ -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");