From 587334df210166a883dbe329c02f2503c810992f Mon Sep 17 00:00:00 2001 From: mohitbadve Date: Sat, 2 Nov 2024 23:02:02 -0500 Subject: [PATCH 1/2] Fix Non-Deterministic Test - testRole_InitAndSerialize --- .../apache/iotdb/db/auth/entity/RoleTest.java | 51 +++++++++++++++++-- 1 file changed, 48 insertions(+), 3 deletions(-) diff --git a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/auth/entity/RoleTest.java b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/auth/entity/RoleTest.java index 8b527eb235ebd..6bd4ba637e679 100644 --- a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/auth/entity/RoleTest.java +++ b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/auth/entity/RoleTest.java @@ -28,7 +28,14 @@ import org.junit.Assert; import org.junit.Test; +import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.regex.Matcher; +import java.util.regex.Pattern; public class RoleTest { @@ -39,13 +46,13 @@ public void testRole_InitAndSerialize() throws IllegalPathException { role.setPrivilegeList(Collections.singletonList(pathPrivilege)); role.addPathPrivilege(new PartialPath("root.ln"), 1, false); role.addPathPrivilege(new PartialPath("root.ln"), 2, true); - Assert.assertEquals( + assertEqualsDeserializedRole( "Role{name='role', pathPrivilegeList=[root.ln : WRITE_DATA" + " READ_SCHEMA_with_grant_option], systemPrivilegeSet=[]}", role.toString()); Role role1 = new Role("role1"); role1.deserialize(role.serialize()); - Assert.assertEquals( + assertEqualsDeserializedRole( "Role{name='role', pathPrivilegeList=[root.ln : " + "WRITE_DATA READ_SCHEMA_with_grant_option], systemPrivilegeSet=[]}", role1.toString()); @@ -62,7 +69,7 @@ public void testRole_InitAndSerialize() throws IllegalPathException { } } admin.getPathPrivilegeList().add(pathPri); - Assert.assertEquals( + assertEqualsDeserializedRole( "Role{name='root', pathPrivilegeList=[root.** : READ_DAT" + "A_with_grant_option WRITE_DATA_with_grant_option READ_SCHEMA_with" + "_grant_option WRITE_SCHEMA_with_grant_option], systemPrivilegeSet=[MANAGE_ROLE" @@ -72,4 +79,42 @@ public void testRole_InitAndSerialize() throws IllegalPathException { + "_TEMPLATE_with_grant_option , USE_MODEL_with_grant_option ]}", admin.toString()); } + + private void assertEqualsDeserializedRole(String expectedStr, String actualStr){ + Assert.assertEquals(extractName(expectedStr), extractName(actualStr)); + List expectedPathPrevilegeList = extractPathPrevilegeList(expectedStr); + List actualPathPrevilegeList = extractPathPrevilegeList(actualStr); + Assert.assertTrue(expectedPathPrevilegeList.size() == actualPathPrevilegeList.size() && expectedPathPrevilegeList.containsAll(actualPathPrevilegeList) && actualPathPrevilegeList.containsAll(expectedPathPrevilegeList)); + Set expectedSystemPrivilegeSet = extractSystemPrivilegeSet(expectedStr); + Set actualSystemPrivilegeSet = extractSystemPrivilegeSet(actualStr); + Assert.assertTrue(expectedSystemPrivilegeSet.size() == actualSystemPrivilegeSet.size() && expectedSystemPrivilegeSet.containsAll(actualSystemPrivilegeSet) && actualSystemPrivilegeSet.containsAll(expectedSystemPrivilegeSet)); + } + + private String extractName(String roleStr){ + Pattern namePattern = Pattern.compile("name='([^']+)'"); + Matcher nameMatcher = namePattern.matcher(roleStr); + return nameMatcher.find() ? nameMatcher.group(1): ""; + } + + private List extractPathPrevilegeList(String roleStr){ + Pattern pathPrivilegePattern = Pattern.compile("pathPrivilegeList=\\[([^]]+)\\]"); + Matcher pathPrivilegeMatcher = pathPrivilegePattern.matcher(roleStr); + List pathPrivilegeList = new ArrayList<>(); + if (pathPrivilegeMatcher.find()) { + String[] privileges = pathPrivilegeMatcher.group(1).split("\\s+"); + pathPrivilegeList.addAll(Arrays.asList(privileges)); + } + return pathPrivilegeList; + } + + private Set extractSystemPrivilegeSet(String roleStr){ + Pattern systemPrivilegePattern = Pattern.compile("systemPrivilegeSet=\\[([^]]+)\\]"); + Matcher systemPrivilegeMatcher = systemPrivilegePattern.matcher(roleStr); + Set systemPrivilegeSet = new HashSet<>(); + if (systemPrivilegeMatcher.find()) { + String[] privileges = systemPrivilegeMatcher.group(1).split(",\\s*"); + systemPrivilegeSet.addAll(Arrays.asList(privileges)); + } + return systemPrivilegeSet; + } } From 13a5d020965af0260fd85a22119aecda153192a8 Mon Sep 17 00:00:00 2001 From: mohitbadve Date: Sat, 2 Nov 2024 23:18:45 -0500 Subject: [PATCH 2/2] Fix Non-Deterministic Test - testRole_InitAndSerialize --- .../apache/iotdb/db/auth/entity/RoleTest.java | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/auth/entity/RoleTest.java b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/auth/entity/RoleTest.java index 6bd4ba637e679..749c59bbef655 100644 --- a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/auth/entity/RoleTest.java +++ b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/auth/entity/RoleTest.java @@ -80,23 +80,29 @@ public void testRole_InitAndSerialize() throws IllegalPathException { admin.toString()); } - private void assertEqualsDeserializedRole(String expectedStr, String actualStr){ + private void assertEqualsDeserializedRole(String expectedStr, String actualStr) { Assert.assertEquals(extractName(expectedStr), extractName(actualStr)); List expectedPathPrevilegeList = extractPathPrevilegeList(expectedStr); List actualPathPrevilegeList = extractPathPrevilegeList(actualStr); - Assert.assertTrue(expectedPathPrevilegeList.size() == actualPathPrevilegeList.size() && expectedPathPrevilegeList.containsAll(actualPathPrevilegeList) && actualPathPrevilegeList.containsAll(expectedPathPrevilegeList)); + Assert.assertTrue( + expectedPathPrevilegeList.size() == actualPathPrevilegeList.size() + && expectedPathPrevilegeList.containsAll(actualPathPrevilegeList) + && actualPathPrevilegeList.containsAll(expectedPathPrevilegeList)); Set expectedSystemPrivilegeSet = extractSystemPrivilegeSet(expectedStr); Set actualSystemPrivilegeSet = extractSystemPrivilegeSet(actualStr); - Assert.assertTrue(expectedSystemPrivilegeSet.size() == actualSystemPrivilegeSet.size() && expectedSystemPrivilegeSet.containsAll(actualSystemPrivilegeSet) && actualSystemPrivilegeSet.containsAll(expectedSystemPrivilegeSet)); + Assert.assertTrue( + expectedSystemPrivilegeSet.size() == actualSystemPrivilegeSet.size() + && expectedSystemPrivilegeSet.containsAll(actualSystemPrivilegeSet) + && actualSystemPrivilegeSet.containsAll(expectedSystemPrivilegeSet)); } - private String extractName(String roleStr){ + private String extractName(String roleStr) { Pattern namePattern = Pattern.compile("name='([^']+)'"); Matcher nameMatcher = namePattern.matcher(roleStr); - return nameMatcher.find() ? nameMatcher.group(1): ""; + return nameMatcher.find() ? nameMatcher.group(1) : ""; } - private List extractPathPrevilegeList(String roleStr){ + private List extractPathPrevilegeList(String roleStr) { Pattern pathPrivilegePattern = Pattern.compile("pathPrivilegeList=\\[([^]]+)\\]"); Matcher pathPrivilegeMatcher = pathPrivilegePattern.matcher(roleStr); List pathPrivilegeList = new ArrayList<>(); @@ -107,7 +113,7 @@ private List extractPathPrevilegeList(String roleStr){ return pathPrivilegeList; } - private Set extractSystemPrivilegeSet(String roleStr){ + private Set extractSystemPrivilegeSet(String roleStr) { Pattern systemPrivilegePattern = Pattern.compile("systemPrivilegeSet=\\[([^]]+)\\]"); Matcher systemPrivilegeMatcher = systemPrivilegePattern.matcher(roleStr); Set systemPrivilegeSet = new HashSet<>();