Skip to content

Commit

Permalink
allow null value for add, set, replace (Azure#27501)
Browse files Browse the repository at this point in the history
* allow null value for add, set, replace

Co-authored-by: annie-mac <annie-mac@CHIEQUAL006.redmond.corp.microsoft.com>
  • Loading branch information
xinlian12 and annie-mac authored Mar 8, 2022
1 parent b8c422a commit 9584e9c
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,6 @@ public static CosmosPatchOperations create() {
* @return same instance of {@link CosmosPatchOperations}
*/
public <T> CosmosPatchOperations add(String path, T value) {

checkNotNull(value, "expected non-null value");
checkArgument(StringUtils.isNotEmpty(path), "path empty %s", path);

this.patchOperations.add(
Expand Down Expand Up @@ -182,7 +180,6 @@ public <T> CosmosPatchOperations replace(String path, T value) {
*/
public <T> CosmosPatchOperations set(String path, T value) {

checkNotNull(value, "expected non-null value");
checkArgument(StringUtils.isNotEmpty(path), "path empty %s", path);

this.patchOperations.add(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

package com.azure.cosmos;

import com.azure.cosmos.implementation.Utils;
import com.azure.cosmos.models.CosmosBatch;
import com.azure.cosmos.models.CosmosBatchPatchItemRequestOptions;
import com.azure.cosmos.models.CosmosBatchResponse;
Expand All @@ -12,7 +13,9 @@
import com.azure.cosmos.models.CosmosBulkPatchItemRequestOptions;
import com.azure.cosmos.models.CosmosItemOperation;
import com.azure.cosmos.models.CosmosItemResponse;
import com.azure.cosmos.models.CosmosPatchOperations;
import com.azure.cosmos.models.PartitionKey;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.netty.handler.codec.http.HttpResponseStatus;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
Expand All @@ -22,6 +25,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import static org.assertj.core.api.Assertions.assertThat;

Expand Down Expand Up @@ -251,4 +255,35 @@ public void patchInBulk() {
testDocForPatch.setDescription("xx");
this.verifyByRead(container, testDocForPatch);
}

@Test(groups = { "emulator" }, timeOut = TIMEOUT)
public void itemPatchSuccessForNullValue() {
// Null value should be allowed for add, set, and replace

ObjectNode objectNode = Utils.getSimpleObjectMapper().createObjectNode();
String id = UUID.randomUUID().toString();
String pkValue = "mypk";

String stringColumnName = "stringColumn";
String stringColumnName2 = "stringColumn2";
String newStringColumnName = "newStringColumn";

objectNode.put("id", id);
objectNode.put("mypk", pkValue);
objectNode.put(stringColumnName, UUID.randomUUID().toString());
objectNode.put(stringColumnName2, UUID.randomUUID().toString());

this.container.createItem(objectNode).block();

CosmosPatchOperations patchOperations = CosmosPatchOperations.create();
patchOperations.add("/" + newStringColumnName, null);
patchOperations.replace("/" + stringColumnName, null);
patchOperations.set("/" + stringColumnName2, null);

ObjectNode patchedItem = this.container.patchItem(id, new PartitionKey(pkValue), patchOperations, ObjectNode.class).block().getItem();
assertThat(patchedItem).isNotNull();
assert(patchedItem.get(newStringColumnName)).isNull();
assert(patchedItem.get(stringColumnName)).isNull();
assert(patchedItem.get(stringColumnName2)).isNull();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@

package com.azure.cosmos;

import com.azure.cosmos.implementation.Utils;
import com.azure.cosmos.models.CosmosItemRequestOptions;
import com.azure.cosmos.models.CosmosItemResponse;
import com.azure.cosmos.models.CosmosPatchItemRequestOptions;
import com.azure.cosmos.models.CosmosPatchOperations;
import com.azure.cosmos.models.PartitionKey;
import com.azure.cosmos.rx.TestSuiteBase;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.netty.handler.codec.http.HttpResponseStatus;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
Expand Down Expand Up @@ -55,7 +58,7 @@ public void itemConditionalPatchSuccess() {

assertThat(testItem.children[1].status).isNull();

com.azure.cosmos.models.CosmosPatchOperations cosmosPatchOperations = com.azure.cosmos.models.CosmosPatchOperations.create();
CosmosPatchOperations cosmosPatchOperations = CosmosPatchOperations.create();
cosmosPatchOperations.add("/children/1/CamelCase", "patched");
cosmosPatchOperations.remove("/description");
cosmosPatchOperations.replace("/taskNum", newTaskNum);
Expand Down Expand Up @@ -166,7 +169,7 @@ public void itemPatchContentResponseOnWriteEnabled() {

assertThat(testItem.children[1].status).isNull();

com.azure.cosmos.models.CosmosPatchOperations cosmosPatchOperations = com.azure.cosmos.models.CosmosPatchOperations.create();
CosmosPatchOperations cosmosPatchOperations = CosmosPatchOperations.create();
cosmosPatchOperations.add("/children/1/CamelCase", "alpha");
cosmosPatchOperations.remove("/description");
cosmosPatchOperations.replace("/taskNum", newTaskNum);
Expand All @@ -186,7 +189,7 @@ public void itemPatchContentResponseOnWriteEnabled() {
assertThat(response.getItem()).isNull(); // skip content is true

// Right now
com.azure.cosmos.models.CosmosPatchOperations cosmosPatchOperations2 = com.azure.cosmos.models.CosmosPatchOperations.create();
CosmosPatchOperations cosmosPatchOperations2 = CosmosPatchOperations.create();
cosmosPatchOperations2.set("/valid", false);

CosmosItemResponse<ToDoActivity> response2 = this.container.patchItem(
Expand Down Expand Up @@ -220,7 +223,7 @@ public void itemPatchContentResponseOnWriteEnabled() {
public void itemPatchFailure() {
// Create an item
ToDoActivity testItem = ToDoActivity.createRandomItem(this.container);
com.azure.cosmos.models.CosmosPatchOperations cosmosPatchOperations = com.azure.cosmos.models.CosmosPatchOperations.create();
CosmosPatchOperations cosmosPatchOperations = CosmosPatchOperations.create();
cosmosPatchOperations.add("/nonExistentParent/child", "bar");
cosmosPatchOperations.remove("/cost");

Expand Down Expand Up @@ -271,6 +274,37 @@ public void itemPatchFailure() {
}
}

@Test(groups = { "emulator" }, timeOut = TIMEOUT * 100)
public void itemPatchSuccessForNullValue() {
// Null value should be allowed for add, set, and replace

ObjectNode objectNode = Utils.getSimpleObjectMapper().createObjectNode();
String id = UUID.randomUUID().toString();
String pkValue = "mypk";

String stringColumnName = "stringColumn";
String stringColumnName2 = "stringColumn2";
String newStringColumnName = "newStringColumn";

objectNode.put("id", id);
objectNode.put("mypk", pkValue);
objectNode.put(stringColumnName, UUID.randomUUID().toString());
objectNode.put(stringColumnName2, UUID.randomUUID().toString());

this.container.createItem(objectNode);

CosmosPatchOperations patchOperations = CosmosPatchOperations.create();
patchOperations.add("/" + newStringColumnName, null);
patchOperations.replace("/" + stringColumnName, null);
patchOperations.set("/" + stringColumnName2, null);

ObjectNode patchedItem = this.container.patchItem(id, new PartitionKey(pkValue), patchOperations, ObjectNode.class).getItem();
assertThat(patchedItem).isNotNull();
assert(patchedItem.get(newStringColumnName)).isNull();
assert(patchedItem.get(stringColumnName)).isNull();
assert(patchedItem.get(stringColumnName2)).isNull();
}

public static class ToDoActivity {

public String id;
Expand Down

0 comments on commit 9584e9c

Please sign in to comment.