Skip to content

Commit

Permalink
Fixes testUpdateBranchGrayRulesWithUpdateOnce (#4599)
Browse files Browse the repository at this point in the history
* Fixes testUpdateBranchGrayRulesWithUpdateOnce

* revise test fixing

* revise test fixing

* refactor test

Co-authored-by: zhewenf2 <[email protected]>
  • Loading branch information
ZhewenFu and zhewenfufufu authored Oct 28, 2022
1 parent 75ae5cc commit d849c02
Showing 1 changed file with 36 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
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.lang.reflect.Type;
import java.util.Set;
import java.util.Map;

import org.junit.Assert;
import org.junit.Test;
Expand All @@ -30,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 {

Expand Down Expand Up @@ -82,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(releaseHistory.getOperationContext().contains(rule.getRules()));
Assert.assertTrue(containRules(releaseHistory.getOperationContext(), rule.getRules()));
}

@Test
Expand Down Expand Up @@ -115,10 +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(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(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 containRules(String context, String rules) {
Type grayReleaseRuleItemsType = new TypeToken<Map<String, Set<GrayReleaseRuleItemDTO>>>() {
}.getType();
Map<String, Set<GrayReleaseRuleItemDTO>> contextRulesMap = new Gson().fromJson(context, grayReleaseRuleItemsType);
Set<GrayReleaseRuleItemDTO> ruleSet = GrayReleaseRuleItemTransformer.batchTransformFromJSON(rules);

for (GrayReleaseRuleItemDTO rule : ruleSet) {
boolean found = false;
loop: for (Set<GrayReleaseRuleItemDTO> contextRules : contextRulesMap.values()) {
for (GrayReleaseRuleItemDTO contextRule : contextRules) {
if (contextRule.toString().equals(rule.toString())) {
found = true;
break loop;
}
}
}
if (!found) {
return false;
}
}

return true;
}

@Test
Expand Down

0 comments on commit d849c02

Please sign in to comment.