Search in sources :

Example 1 with Selector

use of com.alibaba.nacos.api.selector.Selector in project nacos by alibaba.

the class LabelSelectorTest method testParseSelector.

@Test
public void testParseSelector() throws NacosException {
    Selector selector = selectorManager.parseSelector("label", "CONSUMER.label.A=PROVIDER.label.A &CONSUMER.label.B=PROVIDER.label.B");
    Assert.assertTrue(selector instanceof LabelSelector);
    LabelSelector labelSelector = (LabelSelector) selector;
    Assert.assertEquals(2, labelSelector.getLabels().size());
    Assert.assertTrue(labelSelector.getLabels().contains("A"));
    Assert.assertTrue(labelSelector.getLabels().contains("B"));
}
Also used : Selector(com.alibaba.nacos.api.selector.Selector) Test(org.junit.Test)

Example 2 with Selector

use of com.alibaba.nacos.api.selector.Selector in project nacos by alibaba.

the class SelectorManagerTest method testParseSelector.

@Test
public void testParseSelector() throws NacosException {
    Selector selector = selectorManager.parseSelector("mock", "key=value");
    Assert.assertTrue(selector instanceof MockSelector);
    Assert.assertEquals("mock", selector.getType());
}
Also used : Selector(com.alibaba.nacos.api.selector.Selector) Test(org.junit.Test)

Example 3 with Selector

use of com.alibaba.nacos.api.selector.Selector in project nacos by alibaba.

the class ServiceController method parseSelector.

private Selector parseSelector(String selectorJsonString) throws Exception {
    if (StringUtils.isBlank(selectorJsonString)) {
        return new NoneSelector();
    }
    JsonNode selectorJson = JacksonUtils.toObj(URLDecoder.decode(selectorJsonString, "UTF-8"));
    String type = Optional.ofNullable(selectorJson.get("type")).orElseThrow(() -> new NacosException(NacosException.INVALID_PARAM, "not match any type of selector!")).asText();
    String expression = Optional.ofNullable(selectorJson.get("expression")).map(JsonNode::asText).orElse(null);
    Selector selector = selectorManager.parseSelector(type, expression);
    if (Objects.isNull(selector)) {
        throw new NacosException(NacosException.INVALID_PARAM, "not match any type of selector!");
    }
    return selector;
}
Also used : JsonNode(com.fasterxml.jackson.databind.JsonNode) NacosException(com.alibaba.nacos.api.exception.NacosException) NoneSelector(com.alibaba.nacos.naming.selector.NoneSelector) Selector(com.alibaba.nacos.api.selector.Selector) NoneSelector(com.alibaba.nacos.naming.selector.NoneSelector)

Example 4 with Selector

use of com.alibaba.nacos.api.selector.Selector in project nacos by alibaba.

the class ServiceControllerV2 method parseSelector.

private Selector parseSelector(String selectorJsonString) throws Exception {
    if (StringUtils.isBlank(selectorJsonString)) {
        return new NoneSelector();
    }
    JsonNode selectorJson = JacksonUtils.toObj(URLDecoder.decode(selectorJsonString, "UTF-8"));
    String type = Optional.ofNullable(selectorJson.get("type")).orElseThrow(() -> new NacosException(NacosException.INVALID_PARAM, "not match any type of selector!")).asText();
    String expression = Optional.ofNullable(selectorJson.get("expression")).map(JsonNode::asText).orElse(null);
    Selector selector = selectorManager.parseSelector(type, expression);
    if (Objects.isNull(selector)) {
        throw new NacosException(NacosException.INVALID_PARAM, "not match any type of selector!");
    }
    return selector;
}
Also used : JsonNode(com.fasterxml.jackson.databind.JsonNode) NacosException(com.alibaba.nacos.api.exception.NacosException) NoneSelector(com.alibaba.nacos.naming.selector.NoneSelector) Selector(com.alibaba.nacos.api.selector.Selector) NoneSelector(com.alibaba.nacos.naming.selector.NoneSelector)

Example 5 with Selector

use of com.alibaba.nacos.api.selector.Selector in project nacos by alibaba.

the class UpgradeOpsController method parseSelector.

private Selector parseSelector(String selectorJsonString) throws Exception {
    if (StringUtils.isBlank(selectorJsonString)) {
        return new NoneSelector();
    }
    JsonNode selectorJson = JacksonUtils.toObj(URLDecoder.decode(selectorJsonString, "UTF-8"));
    String type = Optional.ofNullable(selectorJson.get("type")).orElseThrow(() -> new NacosException(NacosException.INVALID_PARAM, "not match any type of selector!")).asText();
    String expression = Optional.ofNullable(selectorJson.get("expression")).map(JsonNode::asText).orElse(null);
    Selector selector = selectorManager.parseSelector(type, expression);
    if (Objects.isNull(selector)) {
        throw new NacosException(NacosException.INVALID_PARAM, "not match any type of selector!");
    }
    return selector;
}
Also used : JsonNode(com.fasterxml.jackson.databind.JsonNode) NacosException(com.alibaba.nacos.api.exception.NacosException) NoneSelector(com.alibaba.nacos.naming.selector.NoneSelector) Selector(com.alibaba.nacos.api.selector.Selector) NoneSelector(com.alibaba.nacos.naming.selector.NoneSelector)

Aggregations

Selector (com.alibaba.nacos.api.selector.Selector)10 NacosException (com.alibaba.nacos.api.exception.NacosException)5 NoneSelector (com.alibaba.nacos.naming.selector.NoneSelector)5 Test (org.junit.Test)5 JsonNode (com.fasterxml.jackson.databind.JsonNode)3 LabelSelector (com.alibaba.nacos.naming.selector.LabelSelector)2 Instance (com.alibaba.nacos.api.naming.pojo.Instance)1 Serializer (com.alibaba.nacos.consistency.Serializer)1 Constructor (java.lang.reflect.Constructor)1