Search in sources :

Example 1 with KeyValueResource

use of com.alibaba.csp.sentinel.cluster.server.envoy.rls.rule.EnvoyRlsRule.KeyValueResource in project Sentinel by alibaba.

the class EnvoySentinelRuleConverterTest method testConvertToSentinelFlowRules.

@Test
public void testConvertToSentinelFlowRules() {
    String domain = "testConvertToSentinelFlowRules";
    EnvoyRlsRule rlsRule = new EnvoyRlsRule();
    rlsRule.setDomain(domain);
    List<ResourceDescriptor> descriptors = new ArrayList<>();
    ResourceDescriptor d1 = new ResourceDescriptor();
    d1.setCount(10d);
    d1.setResources(Collections.singleton(new KeyValueResource("k1", "v1")));
    descriptors.add(d1);
    ResourceDescriptor d2 = new ResourceDescriptor();
    d2.setCount(20d);
    d2.setResources(new HashSet<>(Arrays.asList(new KeyValueResource("k2", "v2"), new KeyValueResource("k3", "v3"))));
    descriptors.add(d2);
    rlsRule.setDescriptors(descriptors);
    List<FlowRule> rules = EnvoySentinelRuleConverter.toSentinelFlowRules(rlsRule);
    final String expectedK1 = domain + SEPARATOR + "k1" + SEPARATOR + "v1";
    FlowRule r1 = rules.stream().filter(e -> e.getResource().equals(expectedK1)).findAny().orElseThrow(() -> new AssertionError("the converted rule does not exist, expected key: " + expectedK1));
    assertEquals(10d, r1.getCount(), 0.01);
    final String expectedK2 = domain + SEPARATOR + "k2" + SEPARATOR + "v2" + SEPARATOR + "k3" + SEPARATOR + "v3";
    FlowRule r2 = rules.stream().filter(e -> e.getResource().equals(expectedK2)).findAny().orElseThrow(() -> new AssertionError("the converted rule does not exist, expected key: " + expectedK2));
    assertEquals(20d, r2.getCount(), 0.01);
}
Also used : KeyValueResource(com.alibaba.csp.sentinel.cluster.server.envoy.rls.rule.EnvoyRlsRule.KeyValueResource) ArrayList(java.util.ArrayList) FlowRule(com.alibaba.csp.sentinel.slots.block.flow.FlowRule) ResourceDescriptor(com.alibaba.csp.sentinel.cluster.server.envoy.rls.rule.EnvoyRlsRule.ResourceDescriptor) Test(org.junit.Test)

Aggregations

KeyValueResource (com.alibaba.csp.sentinel.cluster.server.envoy.rls.rule.EnvoyRlsRule.KeyValueResource)1 ResourceDescriptor (com.alibaba.csp.sentinel.cluster.server.envoy.rls.rule.EnvoyRlsRule.ResourceDescriptor)1 FlowRule (com.alibaba.csp.sentinel.slots.block.flow.FlowRule)1 ArrayList (java.util.ArrayList)1 Test (org.junit.Test)1