|
| 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.implementation.routing.Range; |
| 6 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 7 | +import com.fasterxml.jackson.databind.node.ObjectNode; |
| 8 | +import org.testng.annotations.Test; |
| 9 | + |
| 10 | +import java.util.Arrays; |
| 11 | +import java.util.List; |
| 12 | + |
| 13 | +import static org.testng.Assert.assertEquals; |
| 14 | +import static org.testng.Assert.assertNotNull; |
| 15 | +import static org.testng.Assert.assertTrue; |
| 16 | + |
| 17 | +/** |
| 18 | + * Tests for {@link PartitionKeyRange}, focused on the memory-saving "strip unused fields" behavior |
| 19 | + * applied when constructing from a Jackson {@link ObjectNode}. |
| 20 | + * |
| 21 | + * <p>The retained-field set is intentionally kept aligned with |
| 22 | + * <a href="https://github.com/Azure/azure-sdk-for-python/pull/46297">azure-sdk-for-python#46297</a> |
| 23 | + * (Python's {@code PKRange} namedtuple). These tests pin that contract.</p> |
| 24 | + */ |
| 25 | +public class PartitionKeyRangeTest { |
| 26 | + |
| 27 | + private static final ObjectMapper MAPPER = Utils.getSimpleObjectMapper(); |
| 28 | + |
| 29 | + /** Mirrors the JSON shape the Cosmos DB service returns for a partition key range. */ |
| 30 | + private static final String FULL_PK_RANGE_JSON = |
| 31 | + "{" |
| 32 | + + "\"_rid\":\"90t-ALzvP44CAAAAAAAAUA==\"," |
| 33 | + + "\"id\":\"0\"," |
| 34 | + + "\"_etag\":\"\\\"00001e02-0000-0800-0000-6a2c41690000\\\"\"," |
| 35 | + + "\"minInclusive\":\"\"," |
| 36 | + + "\"maxExclusive\":\"FF\"," |
| 37 | + + "\"ridPrefix\":0," |
| 38 | + + "\"_self\":\"dbs/90t-AA==/colls/90t-ALzvP44=/pkranges/90t-ALzvP44CAAAAAAAAUA==/\"," |
| 39 | + + "\"throughputFraction\":1," |
| 40 | + + "\"status\":\"online\"," |
| 41 | + + "\"parents\":[]," |
| 42 | + + "\"ownedArchivalPKRangeIds\":[]," |
| 43 | + + "\"_ts\":1781285225," |
| 44 | + + "\"lsn\":87," |
| 45 | + + "\"_lsn\":87" |
| 46 | + + "}"; |
| 47 | + |
| 48 | + /** |
| 49 | + * Allow-list kept in heap on every deserialized {@link PartitionKeyRange}. |
| 50 | + * |
| 51 | + * <p>Includes Python's {@code PKRange} namedtuple slots |
| 52 | + * ({@code id}, {@code minInclusive}, {@code maxExclusive}, {@code parents}, |
| 53 | + * {@code status}, {@code throughputFraction}) plus {@code _rid} — Java-specific because |
| 54 | + * {@code AddressResolver.isSameCollection} reads {@code getResourceId()} on a |
| 55 | + * {@code PartitionKeyRange} during retry target-change detection.</p> |
| 56 | + */ |
| 57 | + private static final List<String> KEPT_FIELDS = Arrays.asList( |
| 58 | + "id", "minInclusive", "maxExclusive", "parents", "status", "throughputFraction", "_rid"); |
| 59 | + |
| 60 | + /** All non-kept fields present in the full payload above; everything here must be stripped. */ |
| 61 | + private static final List<String> STRIPPED_FIELDS = Arrays.asList( |
| 62 | + "_etag", "ridPrefix", "_self", "ownedArchivalPKRangeIds", "_ts", "lsn", "_lsn"); |
| 63 | + |
| 64 | + private static ObjectNode fullPkRangeNode() throws Exception { |
| 65 | + return (ObjectNode) MAPPER.readTree(FULL_PK_RANGE_JSON); |
| 66 | + } |
| 67 | + |
| 68 | + @Test(groups = "unit") |
| 69 | + public void objectNodeConstructor_stripsEverythingNotOnAllowList() throws Exception { |
| 70 | + // Pin the allow-list: every field not on Python's PKRange namedtuple must be dropped. |
| 71 | + ObjectNode node = fullPkRangeNode(); |
| 72 | + PartitionKeyRange range = new PartitionKeyRange(node); |
| 73 | + |
| 74 | + for (String dropped : STRIPPED_FIELDS) { |
| 75 | + assertEquals(range.has(dropped), false, dropped + " must be stripped (not on allow-list)"); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + @Test(groups = "unit") |
| 80 | + public void objectNodeConstructor_preservesAllowListedFields() throws Exception { |
| 81 | + // Every field on the allow-list must survive the strip. |
| 82 | + ObjectNode node = fullPkRangeNode(); |
| 83 | + PartitionKeyRange range = new PartitionKeyRange(node); |
| 84 | + |
| 85 | + // Routing-map essentials. |
| 86 | + assertEquals(range.getId(), "0"); |
| 87 | + assertEquals(range.getMinInclusive(), ""); |
| 88 | + assertEquals(range.getMaxExclusive(), "FF"); |
| 89 | + assertNotNull(range.getParents()); |
| 90 | + assertEquals(range.getParents().size(), 0); |
| 91 | + |
| 92 | + // _rid is on the allow-list specifically so AddressResolver.isSameCollection |
| 93 | + // can call getResourceId() on a deserialized PartitionKeyRange during retry |
| 94 | + // target-change detection. Without this, ResourceId.parse(null) throws |
| 95 | + // "INVALID resource id null" on the first retry after a 410/Gone. |
| 96 | + assertEquals(range.getResourceId(), "90t-ALzvP44CAAAAAAAAUA=="); |
| 97 | + |
| 98 | + for (String kept : KEPT_FIELDS) { |
| 99 | + assertTrue(range.has(kept), kept + " must be preserved (on allow-list)"); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + @Test(groups = "unit") |
| 104 | + public void objectNodeConstructor_dropsUnknownFutureField() throws Exception { |
| 105 | + // Allow-list (not deny-list) semantics: a new server-side field tomorrow is dropped |
| 106 | + // by default so per-instance heap stays bounded against payload growth. Mirrors |
| 107 | + // Python's PKRange namedtuple, which has no slot for unknown fields. |
| 108 | + String json = "{" |
| 109 | + + "\"id\":\"0\",\"minInclusive\":\"\",\"maxExclusive\":\"FF\"," |
| 110 | + + "\"futureFieldA\":\"hello\"," |
| 111 | + + "\"futureFieldB\":{\"nested\":42}" |
| 112 | + + "}"; |
| 113 | + ObjectNode node = (ObjectNode) MAPPER.readTree(json); |
| 114 | + PartitionKeyRange range = new PartitionKeyRange(node); |
| 115 | + |
| 116 | + assertEquals(range.getId(), "0"); |
| 117 | + assertEquals(range.has("futureFieldA"), false, "unknown future field must be dropped by allow-list"); |
| 118 | + assertEquals(range.has("futureFieldB"), false, "unknown future nested field must be dropped by allow-list"); |
| 119 | + } |
| 120 | + |
| 121 | + @Test(groups = "unit") |
| 122 | + public void objectNodeConstructor_preservesNonEmptyParents() throws Exception { |
| 123 | + // Split-merge bookkeeping uses parents; verify it survives the strip. |
| 124 | + String json = "{" |
| 125 | + + "\"_rid\":\"X==\",\"id\":\"1\",\"_etag\":\"\\\"e\\\"\"," |
| 126 | + + "\"minInclusive\":\"\",\"maxExclusive\":\"FF\"," |
| 127 | + + "\"_self\":\"x/\",\"parents\":[\"0\"],\"_ts\":1,\"lsn\":1" |
| 128 | + + "}"; |
| 129 | + ObjectNode node = (ObjectNode) MAPPER.readTree(json); |
| 130 | + PartitionKeyRange range = new PartitionKeyRange(node); |
| 131 | + |
| 132 | + assertNotNull(range.getParents()); |
| 133 | + assertEquals(range.getParents().size(), 1); |
| 134 | + assertEquals(range.getParents().get(0), "0"); |
| 135 | + // Dropped fields still gone. _rid stays now that it's on the allow-list. |
| 136 | + assertEquals(range.has("_self"), false); |
| 137 | + assertEquals(range.has("lsn"), false); |
| 138 | + assertEquals(range.getResourceId(), "X=="); |
| 139 | + } |
| 140 | + |
| 141 | + @Test(groups = "unit") |
| 142 | + public void objectNodeConstructor_equalsAndHashCodeUnchanged() throws Exception { |
| 143 | + // PartitionKeyRange#equals / #hashCode use id/min/max only -- the slim instance must |
| 144 | + // remain value-equal to a manually-constructed instance with the same identity fields. |
| 145 | + ObjectNode node = fullPkRangeNode(); |
| 146 | + PartitionKeyRange slim = new PartitionKeyRange(node); |
| 147 | + PartitionKeyRange handBuilt = new PartitionKeyRange("0", "", "FF"); |
| 148 | + |
| 149 | + assertEquals(slim, handBuilt); |
| 150 | + assertEquals(slim.hashCode(), handBuilt.hashCode()); |
| 151 | + assertEquals(slim.toRange(), new Range<>("", "FF", true, false)); |
| 152 | + } |
| 153 | + |
| 154 | + @Test(groups = "unit") |
| 155 | + public void objectNodeConstructor_handlesNull() { |
| 156 | + // Defensive: a null ObjectNode argument must not throw; super(null) is the existing |
| 157 | + // pre-PR contract and must be preserved. |
| 158 | + new PartitionKeyRange((ObjectNode) null); |
| 159 | + } |
| 160 | + |
| 161 | + @Test(groups = "unit") |
| 162 | + public void deserializationFunnelStripsForFeedResponsePath() throws Exception { |
| 163 | + // JsonSerializable.instantiateFromObjectNodeAndType is the funnel every FeedResponse |
| 164 | + // page uses when deserializing pkranges. Confirm it routes through the |
| 165 | + // PartitionKeyRange(ObjectNode) ctor and therefore inherits the strip. |
| 166 | + ObjectNode node = fullPkRangeNode(); |
| 167 | + Object result = |
| 168 | + JsonSerializable.instantiateFromObjectNodeAndType(node, PartitionKeyRange.class); |
| 169 | + |
| 170 | + assertTrue(result instanceof PartitionKeyRange); |
| 171 | + PartitionKeyRange range = (PartitionKeyRange) result; |
| 172 | + for (String dropped : STRIPPED_FIELDS) { |
| 173 | + assertEquals(range.has(dropped), false, dropped + " must be stripped via FeedResponse funnel"); |
| 174 | + } |
| 175 | + assertEquals(range.getId(), "0"); |
| 176 | + } |
| 177 | +} |
0 commit comments