Skip to content
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(containRule(releaseHistory.getOperationContext(), rule.getRules()));
}

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

Set<GrayReleaseRuleItemDTO> rules = GrayReleaseRuleItemTransformer.batchTransformFromJSON(rule);

for (Set<GrayReleaseRuleItemDTO> ruleSet : contextRules.values()) {
if (rules.toString().equals(ruleSet.toString()))
return true;
}
return false;
}

@Test
Expand Down