From fcfbed22e3c14f67c70787a837d5a5c4e8dd1338 Mon Sep 17 00:00:00 2001 From: zhewenf2 Date: Sun, 9 Oct 2022 22:22:41 -0500 Subject: [PATCH 1/4] Fixes testUpdateBranchGrayRulesWithUpdateOnce --- .../service/NamespaceBranchServiceTest.java | 54 ++++++++++--------- 1 file changed, 29 insertions(+), 25 deletions(-) diff --git a/apollo-biz/src/test/java/com/ctrip/framework/apollo/biz/service/NamespaceBranchServiceTest.java b/apollo-biz/src/test/java/com/ctrip/framework/apollo/biz/service/NamespaceBranchServiceTest.java index abbe40ef70d..485c1374ead 100644 --- a/apollo-biz/src/test/java/com/ctrip/framework/apollo/biz/service/NamespaceBranchServiceTest.java +++ b/apollo-biz/src/test/java/com/ctrip/framework/apollo/biz/service/NamespaceBranchServiceTest.java @@ -64,17 +64,16 @@ public void testUpdateBranchGrayRulesWithUpdateOnce() { namespaceBranchService.updateBranchGrayRules(testApp, testCluster, testNamespace, testBranchName, rule); - GrayReleaseRule - activeRule = - namespaceBranchService.findBranchGrayRules(testApp, testCluster, testNamespace, testBranchName); + GrayReleaseRule activeRule = namespaceBranchService.findBranchGrayRules(testApp, testCluster, testNamespace, + testBranchName); Assert.assertNotNull(activeRule); Assert.assertEquals(rule.getAppId(), activeRule.getAppId()); Assert.assertEquals(rule.getRules(), activeRule.getRules()); Assert.assertEquals(Long.valueOf(0), activeRule.getReleaseId()); - Page releaseHistories = releaseHistoryService.findReleaseHistoriesByNamespace - (testApp, testCluster, testNamespace, pageable); + Page releaseHistories = releaseHistoryService.findReleaseHistoriesByNamespace(testApp, testCluster, + testNamespace, pageable); ReleaseHistory releaseHistory = releaseHistories.getContent().get(0); @@ -82,7 +81,15 @@ public void testUpdateBranchGrayRulesWithUpdateOnce() { Assert.assertEquals(ReleaseOperation.APPLY_GRAY_RULES, releaseHistory.getOperation()); Assert.assertEquals(0, releaseHistory.getReleaseId()); Assert.assertEquals(0, releaseHistory.getPreviousReleaseId()); - Assert.assertTrue(releaseHistory.getOperationContext().contains(rule.getRules())); + Assert.assertTrue(containRule(releaseHistory.getOperationContext(), rule.getRules())); + } + + private boolean containRule(String context, String rule) { + String[] rules = rule.substring(2, rule.length() - 2).split(","); + for (int i = 0; i < rules.length; i++) + if (!context.contains(rules[i])) + return false; + return true; } @Test @@ -94,20 +101,20 @@ public void testUpdateBranchGrayRulesWithUpdateTwice() { namespaceBranchService.updateBranchGrayRules(testApp, testCluster, testNamespace, testBranchName, firstRule); GrayReleaseRule secondRule = instanceGrayReleaseRule(); - secondRule.setRules("[{\"clientAppId\":\"branch-test\",\"clientIpList\":[\"10.38.57.112\"],\"clientLabelList\":[\"branch-test\"]}]"); + secondRule.setRules( + "[{\"clientAppId\":\"branch-test\",\"clientIpList\":[\"10.38.57.112\"],\"clientLabelList\":[\"branch-test\"]}]"); namespaceBranchService.updateBranchGrayRules(testApp, testCluster, testNamespace, testBranchName, secondRule); - GrayReleaseRule - activeRule = - namespaceBranchService.findBranchGrayRules(testApp, testCluster, testNamespace, testBranchName); + GrayReleaseRule activeRule = namespaceBranchService.findBranchGrayRules(testApp, testCluster, testNamespace, + testBranchName); Assert.assertNotNull(secondRule); Assert.assertEquals(secondRule.getAppId(), activeRule.getAppId()); Assert.assertEquals(secondRule.getRules(), activeRule.getRules()); Assert.assertEquals(Long.valueOf(0), activeRule.getReleaseId()); - Page releaseHistories = releaseHistoryService.findReleaseHistoriesByNamespace - (testApp, testCluster, testNamespace, pageable); + Page releaseHistories = releaseHistoryService.findReleaseHistoriesByNamespace(testApp, testCluster, + testNamespace, pageable); ReleaseHistory firstReleaseHistory = releaseHistories.getContent().get(1); ReleaseHistory secondReleaseHistory = releaseHistories.getContent().get(0); @@ -130,9 +137,8 @@ public void testUpdateRulesReleaseIdWithOldRuleNotExist() { namespaceBranchService .updateRulesReleaseId(testApp, testCluster, testNamespace, testBranchName, latestReleaseId, operator); - GrayReleaseRule - activeRule = - namespaceBranchService.findBranchGrayRules(testApp, testCluster, testNamespace, testBranchName); + GrayReleaseRule activeRule = namespaceBranchService.findBranchGrayRules(testApp, testCluster, testNamespace, + testBranchName); Assert.assertNull(activeRule); } @@ -150,9 +156,8 @@ public void testUpdateRulesReleaseIdWithOldRuleExist() { namespaceBranchService .updateRulesReleaseId(testApp, testCluster, testNamespace, testBranchName, latestReleaseId, operator); - GrayReleaseRule - activeRule = - namespaceBranchService.findBranchGrayRules(testApp, testCluster, testNamespace, testBranchName); + GrayReleaseRule activeRule = namespaceBranchService.findBranchGrayRules(testApp, testCluster, testNamespace, + testBranchName); Assert.assertNotNull(activeRule); Assert.assertEquals(Long.valueOf(latestReleaseId), activeRule.getReleaseId()); @@ -160,7 +165,6 @@ public void testUpdateRulesReleaseIdWithOldRuleExist() { Assert.assertEquals(NamespaceBranchStatus.ACTIVE, activeRule.getBranchStatus()); } - @Test @Sql(scripts = "/sql/namespace-branch-test.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD) @Sql(scripts = "/sql/clean.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD) @@ -169,19 +173,21 @@ public void testDeleteBranch() { GrayReleaseRule rule = instanceGrayReleaseRule(); namespaceBranchService.updateBranchGrayRules(testApp, testCluster, testNamespace, testBranchName, rule); - namespaceBranchService.deleteBranch(testApp, testCluster, testNamespace, testBranchName, NamespaceBranchStatus.DELETED, operator); + namespaceBranchService.deleteBranch(testApp, testCluster, testNamespace, testBranchName, + NamespaceBranchStatus.DELETED, operator); Namespace branch = namespaceBranchService.findBranch(testApp, testCluster, testNamespace); Assert.assertNull(branch); - GrayReleaseRule latestRule = namespaceBranchService.findBranchGrayRules(testApp, testCluster, testNamespace, testBranchName); + GrayReleaseRule latestRule = namespaceBranchService.findBranchGrayRules(testApp, testCluster, testNamespace, + testBranchName); Assert.assertNotNull(latestRule); Assert.assertEquals(NamespaceBranchStatus.DELETED, latestRule.getBranchStatus()); Assert.assertEquals("[]", latestRule.getRules()); - Page releaseHistories = releaseHistoryService.findReleaseHistoriesByNamespace - (testApp, testCluster, testNamespace, pageable); + Page releaseHistories = releaseHistoryService.findReleaseHistoriesByNamespace(testApp, testCluster, + testNamespace, pageable); ReleaseHistory firstReleaseHistory = releaseHistories.getContent().get(1); ReleaseHistory secondReleaseHistory = releaseHistories.getContent().get(0); @@ -192,7 +198,6 @@ public void testDeleteBranch() { } - private GrayReleaseRule instanceGrayReleaseRule() { GrayReleaseRule rule = new GrayReleaseRule(); rule.setAppId(testApp); @@ -204,5 +209,4 @@ private GrayReleaseRule instanceGrayReleaseRule() { return rule; } - } From e0499a8092c5d6b2fc4b930656b62e835c33ab0f Mon Sep 17 00:00:00 2001 From: zhewenf2 Date: Sun, 23 Oct 2022 00:59:16 -0500 Subject: [PATCH 2/4] revise test fixing --- .../service/NamespaceBranchServiceTest.java | 71 ++++++++++--------- 1 file changed, 38 insertions(+), 33 deletions(-) diff --git a/apollo-biz/src/test/java/com/ctrip/framework/apollo/biz/service/NamespaceBranchServiceTest.java b/apollo-biz/src/test/java/com/ctrip/framework/apollo/biz/service/NamespaceBranchServiceTest.java index 485c1374ead..e4e8e74dd7c 100644 --- a/apollo-biz/src/test/java/com/ctrip/framework/apollo/biz/service/NamespaceBranchServiceTest.java +++ b/apollo-biz/src/test/java/com/ctrip/framework/apollo/biz/service/NamespaceBranchServiceTest.java @@ -22,6 +22,9 @@ import com.ctrip.framework.apollo.biz.entity.ReleaseHistory; import com.ctrip.framework.apollo.common.constants.NamespaceBranchStatus; import com.ctrip.framework.apollo.common.constants.ReleaseOperation; +import com.ctrip.framework.apollo.common.utils.GrayReleaseRuleItemTransformer; +import com.ctrip.framework.apollo.common.dto.GrayReleaseRuleItemDTO; +import java.util.Set; import org.junit.Assert; import org.junit.Test; @@ -64,16 +67,17 @@ public void testUpdateBranchGrayRulesWithUpdateOnce() { namespaceBranchService.updateBranchGrayRules(testApp, testCluster, testNamespace, testBranchName, rule); - GrayReleaseRule activeRule = namespaceBranchService.findBranchGrayRules(testApp, testCluster, testNamespace, - testBranchName); + GrayReleaseRule + activeRule = + namespaceBranchService.findBranchGrayRules(testApp, testCluster, testNamespace, testBranchName); Assert.assertNotNull(activeRule); Assert.assertEquals(rule.getAppId(), activeRule.getAppId()); Assert.assertEquals(rule.getRules(), activeRule.getRules()); Assert.assertEquals(Long.valueOf(0), activeRule.getReleaseId()); - Page releaseHistories = releaseHistoryService.findReleaseHistoriesByNamespace(testApp, testCluster, - testNamespace, pageable); + Page releaseHistories = releaseHistoryService.findReleaseHistoriesByNamespace + (testApp, testCluster, testNamespace, pageable); ReleaseHistory releaseHistory = releaseHistories.getContent().get(0); @@ -81,15 +85,7 @@ public void testUpdateBranchGrayRulesWithUpdateOnce() { Assert.assertEquals(ReleaseOperation.APPLY_GRAY_RULES, releaseHistory.getOperation()); Assert.assertEquals(0, releaseHistory.getReleaseId()); Assert.assertEquals(0, releaseHistory.getPreviousReleaseId()); - Assert.assertTrue(containRule(releaseHistory.getOperationContext(), rule.getRules())); - } - - private boolean containRule(String context, String rule) { - String[] rules = rule.substring(2, rule.length() - 2).split(","); - for (int i = 0; i < rules.length; i++) - if (!context.contains(rules[i])) - return false; - return true; + Assert.assertTrue(containRule(releaseHistory.getOperationContext(),rule.getRules())); } @Test @@ -101,20 +97,20 @@ public void testUpdateBranchGrayRulesWithUpdateTwice() { namespaceBranchService.updateBranchGrayRules(testApp, testCluster, testNamespace, testBranchName, firstRule); GrayReleaseRule secondRule = instanceGrayReleaseRule(); - secondRule.setRules( - "[{\"clientAppId\":\"branch-test\",\"clientIpList\":[\"10.38.57.112\"],\"clientLabelList\":[\"branch-test\"]}]"); + secondRule.setRules("[{\"clientAppId\":\"branch-test\",\"clientIpList\":[\"10.38.57.112\"],\"clientLabelList\":[\"branch-test\"]}]"); namespaceBranchService.updateBranchGrayRules(testApp, testCluster, testNamespace, testBranchName, secondRule); - GrayReleaseRule activeRule = namespaceBranchService.findBranchGrayRules(testApp, testCluster, testNamespace, - testBranchName); + GrayReleaseRule + activeRule = + namespaceBranchService.findBranchGrayRules(testApp, testCluster, testNamespace, testBranchName); Assert.assertNotNull(secondRule); Assert.assertEquals(secondRule.getAppId(), activeRule.getAppId()); Assert.assertEquals(secondRule.getRules(), activeRule.getRules()); Assert.assertEquals(Long.valueOf(0), activeRule.getReleaseId()); - Page releaseHistories = releaseHistoryService.findReleaseHistoriesByNamespace(testApp, testCluster, - testNamespace, pageable); + Page releaseHistories = releaseHistoryService.findReleaseHistoriesByNamespace + (testApp, testCluster, testNamespace, pageable); ReleaseHistory firstReleaseHistory = releaseHistories.getContent().get(1); ReleaseHistory secondReleaseHistory = releaseHistories.getContent().get(0); @@ -122,10 +118,16 @@ public void testUpdateBranchGrayRulesWithUpdateTwice() { Assert.assertEquals(2, releaseHistories.getTotalElements()); Assert.assertEquals(ReleaseOperation.APPLY_GRAY_RULES, firstReleaseHistory.getOperation()); Assert.assertEquals(ReleaseOperation.APPLY_GRAY_RULES, secondReleaseHistory.getOperation()); - Assert.assertTrue(firstReleaseHistory.getOperationContext().contains(firstRule.getRules())); - Assert.assertFalse(firstReleaseHistory.getOperationContext().contains(secondRule.getRules())); - Assert.assertTrue(secondReleaseHistory.getOperationContext().contains(firstRule.getRules())); - Assert.assertTrue(secondReleaseHistory.getOperationContext().contains(secondRule.getRules())); + Assert.assertTrue(containRule(firstReleaseHistory.getOperationContext(),firstRule.getRules())); + Assert.assertFalse(containRule(firstReleaseHistory.getOperationContext(),secondRule.getRules())); + Assert.assertTrue(containRule(secondReleaseHistory.getOperationContext(),firstRule.getRules())); + Assert.assertTrue(containRule(secondReleaseHistory.getOperationContext(),secondRule.getRules())); + } + + private boolean containRule(String context, String rule){ + Set contextRules = GrayReleaseRuleItemTransformer.batchTransformFromJSON(context.substring(context.indexOf("["),context.lastIndexOf("]")+1)); + Set rules = GrayReleaseRuleItemTransformer.batchTransformFromJSON(rule); + return contextRules.iterator().next().toString().equals(rules.iterator().next().toString()); } @Test @@ -137,8 +139,9 @@ public void testUpdateRulesReleaseIdWithOldRuleNotExist() { namespaceBranchService .updateRulesReleaseId(testApp, testCluster, testNamespace, testBranchName, latestReleaseId, operator); - GrayReleaseRule activeRule = namespaceBranchService.findBranchGrayRules(testApp, testCluster, testNamespace, - testBranchName); + GrayReleaseRule + activeRule = + namespaceBranchService.findBranchGrayRules(testApp, testCluster, testNamespace, testBranchName); Assert.assertNull(activeRule); } @@ -156,8 +159,9 @@ public void testUpdateRulesReleaseIdWithOldRuleExist() { namespaceBranchService .updateRulesReleaseId(testApp, testCluster, testNamespace, testBranchName, latestReleaseId, operator); - GrayReleaseRule activeRule = namespaceBranchService.findBranchGrayRules(testApp, testCluster, testNamespace, - testBranchName); + GrayReleaseRule + activeRule = + namespaceBranchService.findBranchGrayRules(testApp, testCluster, testNamespace, testBranchName); Assert.assertNotNull(activeRule); Assert.assertEquals(Long.valueOf(latestReleaseId), activeRule.getReleaseId()); @@ -165,6 +169,7 @@ public void testUpdateRulesReleaseIdWithOldRuleExist() { Assert.assertEquals(NamespaceBranchStatus.ACTIVE, activeRule.getBranchStatus()); } + @Test @Sql(scripts = "/sql/namespace-branch-test.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD) @Sql(scripts = "/sql/clean.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD) @@ -173,21 +178,19 @@ public void testDeleteBranch() { GrayReleaseRule rule = instanceGrayReleaseRule(); namespaceBranchService.updateBranchGrayRules(testApp, testCluster, testNamespace, testBranchName, rule); - namespaceBranchService.deleteBranch(testApp, testCluster, testNamespace, testBranchName, - NamespaceBranchStatus.DELETED, operator); + namespaceBranchService.deleteBranch(testApp, testCluster, testNamespace, testBranchName, NamespaceBranchStatus.DELETED, operator); Namespace branch = namespaceBranchService.findBranch(testApp, testCluster, testNamespace); Assert.assertNull(branch); - GrayReleaseRule latestRule = namespaceBranchService.findBranchGrayRules(testApp, testCluster, testNamespace, - testBranchName); + GrayReleaseRule latestRule = namespaceBranchService.findBranchGrayRules(testApp, testCluster, testNamespace, testBranchName); Assert.assertNotNull(latestRule); Assert.assertEquals(NamespaceBranchStatus.DELETED, latestRule.getBranchStatus()); Assert.assertEquals("[]", latestRule.getRules()); - Page releaseHistories = releaseHistoryService.findReleaseHistoriesByNamespace(testApp, testCluster, - testNamespace, pageable); + Page releaseHistories = releaseHistoryService.findReleaseHistoriesByNamespace + (testApp, testCluster, testNamespace, pageable); ReleaseHistory firstReleaseHistory = releaseHistories.getContent().get(1); ReleaseHistory secondReleaseHistory = releaseHistories.getContent().get(0); @@ -198,6 +201,7 @@ public void testDeleteBranch() { } + private GrayReleaseRule instanceGrayReleaseRule() { GrayReleaseRule rule = new GrayReleaseRule(); rule.setAppId(testApp); @@ -209,4 +213,5 @@ private GrayReleaseRule instanceGrayReleaseRule() { return rule; } + } From 5630b2aee8469a08389b718b6d18ee662b982d0f Mon Sep 17 00:00:00 2001 From: zhewenf2 Date: Sun, 23 Oct 2022 20:05:41 -0500 Subject: [PATCH 3/4] revise test fixing --- .../service/NamespaceBranchServiceTest.java | 28 +++++++++++++------ 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/apollo-biz/src/test/java/com/ctrip/framework/apollo/biz/service/NamespaceBranchServiceTest.java b/apollo-biz/src/test/java/com/ctrip/framework/apollo/biz/service/NamespaceBranchServiceTest.java index e4e8e74dd7c..7fe02b65be3 100644 --- a/apollo-biz/src/test/java/com/ctrip/framework/apollo/biz/service/NamespaceBranchServiceTest.java +++ b/apollo-biz/src/test/java/com/ctrip/framework/apollo/biz/service/NamespaceBranchServiceTest.java @@ -24,7 +24,9 @@ import com.ctrip.framework.apollo.common.constants.ReleaseOperation; import com.ctrip.framework.apollo.common.utils.GrayReleaseRuleItemTransformer; import com.ctrip.framework.apollo.common.dto.GrayReleaseRuleItemDTO; +import java.lang.reflect.Type; import java.util.Set; +import java.util.Map; import org.junit.Assert; import org.junit.Test; @@ -33,6 +35,8 @@ import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; import org.springframework.test.context.jdbc.Sql; +import com.google.gson.Gson; +import com.google.gson.reflect.TypeToken; public class NamespaceBranchServiceTest extends AbstractIntegrationTest { @@ -85,7 +89,7 @@ public void testUpdateBranchGrayRulesWithUpdateOnce() { Assert.assertEquals(ReleaseOperation.APPLY_GRAY_RULES, releaseHistory.getOperation()); Assert.assertEquals(0, releaseHistory.getReleaseId()); Assert.assertEquals(0, releaseHistory.getPreviousReleaseId()); - Assert.assertTrue(containRule(releaseHistory.getOperationContext(),rule.getRules())); + Assert.assertTrue(containRule(releaseHistory.getOperationContext(), rule.getRules())); } @Test @@ -118,16 +122,24 @@ public void testUpdateBranchGrayRulesWithUpdateTwice() { Assert.assertEquals(2, releaseHistories.getTotalElements()); Assert.assertEquals(ReleaseOperation.APPLY_GRAY_RULES, firstReleaseHistory.getOperation()); Assert.assertEquals(ReleaseOperation.APPLY_GRAY_RULES, secondReleaseHistory.getOperation()); - Assert.assertTrue(containRule(firstReleaseHistory.getOperationContext(),firstRule.getRules())); - Assert.assertFalse(containRule(firstReleaseHistory.getOperationContext(),secondRule.getRules())); - Assert.assertTrue(containRule(secondReleaseHistory.getOperationContext(),firstRule.getRules())); - Assert.assertTrue(containRule(secondReleaseHistory.getOperationContext(),secondRule.getRules())); + Assert.assertTrue(containRule(firstReleaseHistory.getOperationContext(), firstRule.getRules())); + Assert.assertFalse(containRule(firstReleaseHistory.getOperationContext(), secondRule.getRules())); + Assert.assertTrue(containRule(secondReleaseHistory.getOperationContext(), firstRule.getRules())); + Assert.assertTrue(containRule(secondReleaseHistory.getOperationContext(), secondRule.getRules())); } - private boolean containRule(String context, String rule){ - Set contextRules = GrayReleaseRuleItemTransformer.batchTransformFromJSON(context.substring(context.indexOf("["),context.lastIndexOf("]")+1)); + private boolean containRule(String context, String rule) { + Type grayReleaseRuleItemsType = new TypeToken>>() { + }.getType(); + Map> contextRules = new Gson().fromJson(context, grayReleaseRuleItemsType); + Set rules = GrayReleaseRuleItemTransformer.batchTransformFromJSON(rule); - return contextRules.iterator().next().toString().equals(rules.iterator().next().toString()); + + for (Set ruleSet : contextRules.values()) { + if (rules.toString().equals(ruleSet.toString())) + return true; + } + return false; } @Test From b3d0e267858870dca6d6f5d060fa912ae3dbd22d Mon Sep 17 00:00:00 2001 From: zhewenf2 Date: Thu, 27 Oct 2022 00:15:15 -0500 Subject: [PATCH 4/4] refactor test --- .../service/NamespaceBranchServiceTest.java | 38 ++++++++++++------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/apollo-biz/src/test/java/com/ctrip/framework/apollo/biz/service/NamespaceBranchServiceTest.java b/apollo-biz/src/test/java/com/ctrip/framework/apollo/biz/service/NamespaceBranchServiceTest.java index 7fe02b65be3..b6b56a5f8f8 100644 --- a/apollo-biz/src/test/java/com/ctrip/framework/apollo/biz/service/NamespaceBranchServiceTest.java +++ b/apollo-biz/src/test/java/com/ctrip/framework/apollo/biz/service/NamespaceBranchServiceTest.java @@ -89,7 +89,7 @@ public void testUpdateBranchGrayRulesWithUpdateOnce() { Assert.assertEquals(ReleaseOperation.APPLY_GRAY_RULES, releaseHistory.getOperation()); Assert.assertEquals(0, releaseHistory.getReleaseId()); Assert.assertEquals(0, releaseHistory.getPreviousReleaseId()); - Assert.assertTrue(containRule(releaseHistory.getOperationContext(), rule.getRules())); + Assert.assertTrue(containRules(releaseHistory.getOperationContext(), rule.getRules())); } @Test @@ -122,24 +122,34 @@ public void testUpdateBranchGrayRulesWithUpdateTwice() { Assert.assertEquals(2, releaseHistories.getTotalElements()); Assert.assertEquals(ReleaseOperation.APPLY_GRAY_RULES, firstReleaseHistory.getOperation()); Assert.assertEquals(ReleaseOperation.APPLY_GRAY_RULES, secondReleaseHistory.getOperation()); - Assert.assertTrue(containRule(firstReleaseHistory.getOperationContext(), firstRule.getRules())); - Assert.assertFalse(containRule(firstReleaseHistory.getOperationContext(), secondRule.getRules())); - Assert.assertTrue(containRule(secondReleaseHistory.getOperationContext(), firstRule.getRules())); - Assert.assertTrue(containRule(secondReleaseHistory.getOperationContext(), secondRule.getRules())); + Assert.assertTrue(containRules(firstReleaseHistory.getOperationContext(), firstRule.getRules())); + Assert.assertFalse(containRules(firstReleaseHistory.getOperationContext(), secondRule.getRules())); + Assert.assertTrue(containRules(secondReleaseHistory.getOperationContext(), firstRule.getRules())); + Assert.assertTrue(containRules(secondReleaseHistory.getOperationContext(), secondRule.getRules())); } - private boolean containRule(String context, String rule) { + private boolean containRules(String context, String rules) { Type grayReleaseRuleItemsType = new TypeToken>>() { }.getType(); - Map> contextRules = new Gson().fromJson(context, grayReleaseRuleItemsType); - - Set rules = GrayReleaseRuleItemTransformer.batchTransformFromJSON(rule); - - for (Set ruleSet : contextRules.values()) { - if (rules.toString().equals(ruleSet.toString())) - return true; + Map> contextRulesMap = new Gson().fromJson(context, grayReleaseRuleItemsType); + Set ruleSet = GrayReleaseRuleItemTransformer.batchTransformFromJSON(rules); + + for (GrayReleaseRuleItemDTO rule : ruleSet) { + boolean found = false; + loop: for (Set contextRules : contextRulesMap.values()) { + for (GrayReleaseRuleItemDTO contextRule : contextRules) { + if (contextRule.toString().equals(rule.toString())) { + found = true; + break loop; + } + } + } + if (!found) { + return false; + } } - return false; + + return true; } @Test