|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | +package com.azure.cosmos.implementation; |
| 4 | + |
| 5 | +import com.azure.cosmos.ConsistencyLevel; |
| 6 | +import com.azure.cosmos.CosmosAsyncClient; |
| 7 | +import com.azure.cosmos.CosmosAsyncContainer; |
| 8 | +import com.azure.cosmos.CosmosClientBuilder; |
| 9 | +import com.azure.cosmos.models.CosmosItemRequestOptions; |
| 10 | +import com.azure.cosmos.models.CosmosItemResponse; |
| 11 | +import com.azure.cosmos.models.CosmosPatchOperations; |
| 12 | +import com.azure.cosmos.models.PartitionKey; |
| 13 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 14 | +import com.fasterxml.jackson.databind.node.ObjectNode; |
| 15 | +import org.slf4j.Logger; |
| 16 | +import org.slf4j.LoggerFactory; |
| 17 | +import org.testng.annotations.Test; |
| 18 | + |
| 19 | +import java.util.UUID; |
| 20 | + |
| 21 | +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; |
| 22 | + |
| 23 | +public class ThinClientE2ETest extends com.azure.cosmos.rx.TestSuiteBase { |
| 24 | + @Test(groups = {"thinclient"}) |
| 25 | + public void testThinClientDocumentPointOperations() { |
| 26 | + CosmosAsyncClient client = null; |
| 27 | + try { |
| 28 | + System.setProperty("COSMOS.THINCLIENT_ENABLED", "true"); |
| 29 | + System.setProperty("COSMOS.HTTP2_ENABLED", "true"); |
| 30 | + |
| 31 | + String thinclientTestEndpoint = System.getProperty("COSMOS.THINCLIENT_ENDPOINT"); |
| 32 | + String thinclientTestKey = System.getProperty("COSMOS.THINCLIENT_KEY"); |
| 33 | + |
| 34 | + client = new CosmosClientBuilder() |
| 35 | + .endpoint(thinclientTestEndpoint) |
| 36 | + .key(thinclientTestKey) |
| 37 | + .gatewayMode() |
| 38 | + .consistencyLevel(ConsistencyLevel.SESSION) |
| 39 | + .buildAsyncClient(); |
| 40 | + |
| 41 | + CosmosAsyncContainer container = client.getDatabase("updatedd-thin-client-test-db").getContainer("thin-client-test-container-1"); |
| 42 | + ObjectMapper mapper = new ObjectMapper(); |
| 43 | + ObjectNode doc = mapper.createObjectNode(); |
| 44 | + String idValue = UUID.randomUUID().toString(); |
| 45 | + doc.put("id", idValue); |
| 46 | + doc.put("pk", idValue); |
| 47 | + |
| 48 | + // create |
| 49 | + CosmosItemResponse<ObjectNode> createResponse = container.createItem(doc).block(); |
| 50 | + assertThat(createResponse.getStatusCode()).isEqualTo(201); |
| 51 | + assertThat(createResponse.getRequestCharge()).isGreaterThan(0.0); |
| 52 | + |
| 53 | + // read |
| 54 | + CosmosItemResponse<ObjectNode> readResponse = container.readItem(idValue, new PartitionKey(idValue), ObjectNode.class).block(); |
| 55 | + assertThat(readResponse.getStatusCode()).isEqualTo(200); |
| 56 | + assertThat(readResponse.getRequestCharge()).isGreaterThan(0.0); |
| 57 | + |
| 58 | + ObjectNode doc2 = mapper.createObjectNode(); |
| 59 | + String idValue2 = UUID.randomUUID().toString(); |
| 60 | + doc2.put("id", idValue2); |
| 61 | + doc2.put("pk", idValue); |
| 62 | + |
| 63 | + // replace |
| 64 | + CosmosItemResponse<ObjectNode> replaceResponse = container.replaceItem(doc2, idValue, new PartitionKey(idValue)).block(); |
| 65 | + assertThat(replaceResponse.getStatusCode()).isEqualTo(200); |
| 66 | + assertThat(replaceResponse.getRequestCharge()).isGreaterThan(0.0); |
| 67 | + CosmosItemResponse<ObjectNode> readAfterReplaceResponse = container.readItem(idValue2, new PartitionKey(idValue), ObjectNode.class).block(); |
| 68 | + assertThat(readAfterReplaceResponse.getStatusCode()).isEqualTo(200); |
| 69 | + ObjectNode replacedItemFromRead = readAfterReplaceResponse.getItem(); |
| 70 | + assertThat(replacedItemFromRead.get("id").asText()).isEqualTo(idValue2); |
| 71 | + assertThat(replacedItemFromRead.get("pk").asText()).isEqualTo(idValue); |
| 72 | + |
| 73 | + ObjectNode doc3 = mapper.createObjectNode(); |
| 74 | + doc3.put("id", idValue2); |
| 75 | + doc3.put("pk", idValue); |
| 76 | + doc3.put("newField", "newValue"); |
| 77 | + |
| 78 | + // upsert |
| 79 | + CosmosItemResponse<ObjectNode> upsertResponse = container.upsertItem(doc3, new PartitionKey(idValue), new CosmosItemRequestOptions()).block(); |
| 80 | + assertThat(upsertResponse.getStatusCode()).isEqualTo(200); |
| 81 | + assertThat(upsertResponse.getRequestCharge()).isGreaterThan(0.0); |
| 82 | + CosmosItemResponse<ObjectNode> readAfterUpsertResponse = container.readItem(idValue2, new PartitionKey(idValue), ObjectNode.class).block(); |
| 83 | + ObjectNode upsertedItemFromRead = readAfterUpsertResponse.getItem(); |
| 84 | + assertThat(upsertedItemFromRead.get("id").asText()).isEqualTo(idValue2); |
| 85 | + assertThat(upsertedItemFromRead.get("pk").asText()).isEqualTo(idValue); |
| 86 | + assertThat(upsertedItemFromRead.get("newField").asText()).isEqualTo("newValue"); |
| 87 | + |
| 88 | + // patch |
| 89 | + CosmosPatchOperations patchOperations = CosmosPatchOperations.create(); |
| 90 | + patchOperations.add("/anotherNewField", "anotherNewValue"); |
| 91 | + patchOperations.replace("/newField", "patchedNewField"); |
| 92 | + CosmosItemResponse<ObjectNode> patchResponse = container.patchItem(idValue2, new PartitionKey(idValue), patchOperations, ObjectNode.class).block(); |
| 93 | + assertThat(patchResponse.getStatusCode()).isEqualTo(200); |
| 94 | + assertThat(patchResponse.getRequestCharge()).isGreaterThan(0.0); |
| 95 | + CosmosItemResponse<ObjectNode> readAfterPatchResponse = container.readItem(idValue2, new PartitionKey(idValue), ObjectNode.class).block(); |
| 96 | + ObjectNode patchedItemFromRead = readAfterPatchResponse.getItem(); |
| 97 | + assertThat(patchedItemFromRead.get("id").asText()).isEqualTo(idValue2); |
| 98 | + assertThat(patchedItemFromRead.get("pk").asText()).isEqualTo(idValue); |
| 99 | + assertThat(patchedItemFromRead.get("newField").asText()).isEqualTo("patchedNewField"); |
| 100 | + assertThat(patchedItemFromRead.get("anotherNewField").asText()).isEqualTo("anotherNewValue"); |
| 101 | + |
| 102 | + // delete |
| 103 | + CosmosItemResponse<Object> deleteResponse = container.deleteItem(idValue2, new PartitionKey(idValue)).block(); |
| 104 | + assertThat(deleteResponse.getStatusCode()).isEqualTo(204); |
| 105 | + assertThat(deleteResponse.getRequestCharge()).isGreaterThan(0.0); |
| 106 | + } finally { |
| 107 | + System.clearProperty("COSMOS.THINCLIENT_ENABLED"); |
| 108 | + System.clearProperty("COSMOS.HTTP2_ENABLED"); |
| 109 | + client.close(); |
| 110 | + } |
| 111 | + } |
| 112 | +} |
0 commit comments