vyommani commented on code in PR #592: URL: https://github.com/apache/ranger/pull/592#discussion_r2157438973
########## agents-common/src/test/java/org/apache/ranger/plugin/util/TestServiceTags.java: ########## @@ -112,6 +115,61 @@ public void testDedup_DupTagsWithAttr_MultipleCalls() { assertEquals(0, svcTags.dedupTags()); } + @Test + public void testDedupTags_DuplicateTagWithHigherId() { + // Create ServiceTags with duplicate tags + RangerTag[] tags = { + new RangerTag("PII", Collections.singletonMap("type", "email")), + new RangerTag("PII", Collections.singletonMap("type", "email")), + new RangerTag("PCI", Collections.emptyMap()), + new RangerTag("PCI", Collections.emptyMap()), + new RangerTag("PII", Collections.singletonMap("type", "email")) + }; + + ServiceTags svcTags = createServiceTags(tags, RESOURCES); + assertEquals(5, svcTags.getTags().size()); + // Should remove 3 duplicates (2 PII, 1 PCI) + assertEquals(3, svcTags.dedupTags()); + + // Verify: 2 tags remain (one PII, one PCI) + assertEquals(2, svcTags.getTags().size()); + // Find retained PII tag ID + Long piiTagId = svcTags.getTags().entrySet().stream() + .filter(e -> e.getValue().getType().equals("PII")) + .map(Map.Entry::getKey) + .findFirst() + .orElseThrow(() -> new AssertionError("PII tag not found")); + RangerTag cachedTag = svcTags.getTags().get(piiTagId); + + // Verify resource mappings + assertTrue(svcTags.getResourceToTagIds().values().stream() + .allMatch(tagIds -> tagIds.contains(piiTagId))); + + // Add a new PII tag with higher tag ID + ServiceTags svcTags1 = new ServiceTags(svcTags); + svcTags1.getTags().remove(piiTagId); + RangerTag newTag = new RangerTag("PII", Collections.singletonMap("type", "email")); + long newTagId = 23L; + newTag.setId(newTagId); + svcTags1.getTags().put(newTagId, newTag); + svcTags1.getResourceToTagIds().get(1L).add(newTagId); + + assertEquals(0, svcTags1.dedupTags()); + assertEquals(2, svcTags1.getTags().size()); + assertEquals(cachedTag, svcTags1.getTags().get(piiTagId)); + assertFalse(svcTags1.getTags().containsKey(newTagId)); Review Comment: The assertion assertEquals(cachedTag, svcTags1.getTags().get(piiTagId)) holds true because when svcTag1.dedupTags() is called at line 157, it updates the tag ID of the newly added tag (with a higher ID) to match the cached tag ID (piiTagId). Although piiTagId was removed from svcTags1 at line 150, the dedupTags() method effectively restores it by updating the new tag's ID to the cached ID, allowing the assertion to pass. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: dev-unsubscr...@ranger.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org