Search in sources :

Example 51 with MetaData

use of org.apache.shenyu.common.dto.MetaData in project incubator-shenyu by apache.

the class SofaPlugin method doExecute.

@Override
protected Mono<Void> doExecute(final ServerWebExchange exchange, final ShenyuPluginChain chain, final SelectorData selector, final RuleData rule) {
    String param = exchange.getAttribute(Constants.PARAM_TRANSFORM);
    ShenyuContext shenyuContext = exchange.getAttribute(Constants.CONTEXT);
    assert shenyuContext != null;
    MetaData metaData = exchange.getAttribute(Constants.META_DATA);
    if (!checkMetaData(metaData)) {
        assert metaData != null;
        LOG.error(" path is :{}, meta data have error.... {}", shenyuContext.getPath(), metaData);
        exchange.getResponse().setStatusCode(HttpStatus.INTERNAL_SERVER_ERROR);
        Object error = ShenyuResultWrap.error(exchange, ShenyuResultEnum.META_DATA_ERROR, null);
        return WebFluxResultUtils.result(exchange, error);
    }
    if (StringUtils.isNoneBlank(metaData.getParameterTypes()) && StringUtils.isBlank(param)) {
        exchange.getResponse().setStatusCode(HttpStatus.INTERNAL_SERVER_ERROR);
        Object error = ShenyuResultWrap.error(exchange, ShenyuResultEnum.SOFA_HAVE_BODY_PARAM, null);
        return WebFluxResultUtils.result(exchange, error);
    }
    Map<String, Map<String, String>> rpcContext = exchange.getAttribute(Constants.GENERAL_CONTEXT);
    Optional.ofNullable(rpcContext).map(context -> context.get(PluginEnum.SOFA.getName())).ifPresent(context -> RpcInvokeContext.getContext().putAllRequestBaggage(context));
    final Mono<Object> result = sofaProxyService.genericInvoker(param, metaData, exchange);
    return result.then(chain.execute(exchange));
}
Also used : ShenyuContext(org.apache.shenyu.plugin.api.context.ShenyuContext) Constants(org.apache.shenyu.common.constant.Constants) PluginEnum(org.apache.shenyu.common.enums.PluginEnum) Logger(org.slf4j.Logger) SofaProxyService(org.apache.shenyu.plugin.sofa.proxy.SofaProxyService) ShenyuPluginChain(org.apache.shenyu.plugin.api.ShenyuPluginChain) WebFluxResultUtils(org.apache.shenyu.plugin.api.utils.WebFluxResultUtils) LoggerFactory(org.slf4j.LoggerFactory) RuleData(org.apache.shenyu.common.dto.RuleData) Mono(reactor.core.publisher.Mono) StringUtils(org.apache.commons.lang3.StringUtils) RpcTypeEnum(org.apache.shenyu.common.enums.RpcTypeEnum) ServerWebExchange(org.springframework.web.server.ServerWebExchange) HttpStatus(org.springframework.http.HttpStatus) ShenyuResultEnum(org.apache.shenyu.plugin.api.result.ShenyuResultEnum) ShenyuResultWrap(org.apache.shenyu.plugin.api.result.ShenyuResultWrap) AbstractShenyuPlugin(org.apache.shenyu.plugin.base.AbstractShenyuPlugin) Map(java.util.Map) RpcInvokeContext(com.alipay.sofa.rpc.context.RpcInvokeContext) Optional(java.util.Optional) MetaData(org.apache.shenyu.common.dto.MetaData) SelectorData(org.apache.shenyu.common.dto.SelectorData) ShenyuContext(org.apache.shenyu.plugin.api.context.ShenyuContext) MetaData(org.apache.shenyu.common.dto.MetaData) Map(java.util.Map)

Example 52 with MetaData

use of org.apache.shenyu.common.dto.MetaData in project incubator-shenyu by apache.

the class ApplicationConfigCacheTest method testInitPrx.

@Test
public void testInitPrx() {
    final MetaData metaData = new MetaData("id", "127.0.0.1:8080", "contextPath", "path6", RpcTypeEnum.TARS.getName(), "serviceName6", "method1", "parameterTypes", "{\"methodInfo\":[{\"methodName\":\"method1\",\"params\":[{\"left\":\"int\",\"right\":\"param1\"}," + "{\"left\":\"java.lang.Integer\",\"right\":\"param2\"}],\"returnType\":\"java.lang.String\"}]}", false);
    assertThrows(NullPointerException.class, () -> {
        applicationConfigCacheUnderTest.initPrx(metaData);
        final TarsInvokePrxList result = applicationConfigCacheUnderTest.get("path6");
        assertEquals("promise_method1", result.getMethod().getName());
    });
}
Also used : MetaData(org.apache.shenyu.common.dto.MetaData) TarsInvokePrxList(org.apache.shenyu.plugin.tars.proxy.TarsInvokePrxList) Test(org.junit.jupiter.api.Test)

Example 53 with MetaData

use of org.apache.shenyu.common.dto.MetaData in project incubator-shenyu by apache.

the class PrxInfoUtilTest method testGetPrxName.

@Test
public void testGetPrxName() {
    final MetaData metaData = new MetaData("id", "appName", "contextPath", "/path", "rpcType", "serviceName", "methodName", "parameterTypes", "rpcExt", false);
    final String result = PrxInfoUtil.getPrxName(metaData);
    assertEquals("pathmethodNamePrx", result);
}
Also used : MetaData(org.apache.shenyu.common.dto.MetaData) Test(org.junit.jupiter.api.Test)

Example 54 with MetaData

use of org.apache.shenyu.common.dto.MetaData in project incubator-shenyu by apache.

the class ZookeeperSyncDataServiceTest method setUp.

@BeforeEach
public void setUp() {
    zkClient = mock(ZkClient.class);
    // mock plugin data & method
    PluginData pluginData = PluginData.builder().name(MOCK_PLUGIN_NAME).enabled(Boolean.FALSE).build();
    when(zkClient.exists(anyString())).thenReturn(Boolean.FALSE);
    when(zkClient.readData(MOCK_PLUGIN_PATH)).thenReturn(GsonUtils.getInstance().toJson(pluginData));
    when(zkClient.getChildren(MOCK_PLUGIN_PARENT_PATH)).thenReturn(Lists.newArrayList(MOCK_PLUGIN_NAME));
    // mock selector data & method
    SelectorData selectorData = SelectorData.builder().name(MOCK_SELECTOR_NAME).enabled(Boolean.FALSE).build();
    when(zkClient.readData(MOCK_SELECTOR_PATH)).thenReturn(GsonUtils.getInstance().toJson(selectorData));
    when(zkClient.getChildren(MOCK_SELECTOR_PARENT_PATH)).thenReturn(Lists.newArrayList(MOCK_SELECTOR_NAME));
    // mock rule data & method
    RuleData ruleData = RuleData.builder().name(MOCK_RULE_NAME).enabled(Boolean.FALSE).build();
    when(zkClient.readData(MOCK_RULE_PATH)).thenReturn(GsonUtils.getInstance().toJson(ruleData));
    when(zkClient.getChildren(MOCK_RULE_PARENT_PATH)).thenReturn(Lists.newArrayList(MOCK_RULE_NAME));
    // mock auth data & method
    AppAuthData appAuthData = AppAuthData.builder().appKey(MOCK_APP_AUTH_KEY).enabled(Boolean.FALSE).build();
    when(zkClient.readData(MOCK_APP_AUTH_PATH)).thenReturn(GsonUtils.getInstance().toJson(appAuthData));
    when(zkClient.getChildren(MOCK_APP_AUTH_PARENT_PATH)).thenReturn(Lists.newArrayList(MOCK_APP_AUTH_KEY));
    // mock meta data & method
    MetaData metaData = MetaData.builder().id(MOCK_META_DATA_ID).enabled(Boolean.FALSE).build();
    when(zkClient.readData(MOCK_META_DATA_PATH)).thenReturn(GsonUtils.getInstance().toJson(metaData));
    when(zkClient.getChildren(MOCK_META_DATA_PARENT_PATH)).thenReturn(Lists.newArrayList(MOCK_META_DATA_ID));
}
Also used : ZkClient(org.I0Itec.zkclient.ZkClient) RuleData(org.apache.shenyu.common.dto.RuleData) PluginData(org.apache.shenyu.common.dto.PluginData) AppAuthData(org.apache.shenyu.common.dto.AppAuthData) MetaData(org.apache.shenyu.common.dto.MetaData) SelectorData(org.apache.shenyu.common.dto.SelectorData) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 55 with MetaData

use of org.apache.shenyu.common.dto.MetaData in project incubator-shenyu by apache.

the class ZookeeperSyncDataServiceTest method testWatchMetaDataWhenDataDeleted.

@Test
public void testWatchMetaDataWhenDataDeleted() throws Exception {
    final List<MetaData> unSubscribeList = new ArrayList<>(1);
    MetaDataSubscriber metaDataSubscriber = new MetaDataSubscriber() {

        @Override
        public void onSubscribe(final MetaData metaData) {
        }

        @Override
        public void unSubscribe(final MetaData metaData) {
            unSubscribeList.add(metaData);
        }
    };
    syncDataService = new ZookeeperSyncDataService(zkClient, null, Lists.newArrayList(metaDataSubscriber), Collections.emptyList());
    ArgumentCaptor<IZkDataListener> captor = ArgumentCaptor.forClass(IZkDataListener.class);
    verify(zkClient).subscribeDataChanges(eq(MOCK_META_DATA_PATH), captor.capture());
    captor.getValue().handleDataDeleted(MOCK_META_DATA_PATH);
    assertThat(unSubscribeList.size(), is(1));
    assertThat(unSubscribeList.get(0).getPath(), is(MOCK_META_DATA_ID));
}
Also used : MetaDataSubscriber(org.apache.shenyu.sync.data.api.MetaDataSubscriber) MetaData(org.apache.shenyu.common.dto.MetaData) ArrayList(java.util.ArrayList) IZkDataListener(org.I0Itec.zkclient.IZkDataListener) Test(org.junit.jupiter.api.Test)

Aggregations

MetaData (org.apache.shenyu.common.dto.MetaData)70 Test (org.junit.jupiter.api.Test)31 BeforeEach (org.junit.jupiter.api.BeforeEach)14 RuleData (org.apache.shenyu.common.dto.RuleData)11 SelectorData (org.apache.shenyu.common.dto.SelectorData)10 ShenyuContext (org.apache.shenyu.plugin.api.context.ShenyuContext)9 ArrayList (java.util.ArrayList)8 MetaDataSubscriber (org.apache.shenyu.sync.data.api.MetaDataSubscriber)8 ServerWebExchange (org.springframework.web.server.ServerWebExchange)7 Objects (java.util.Objects)6 ShenyuException (org.apache.shenyu.common.exception.ShenyuException)6 Logger (org.slf4j.Logger)6 LoggerFactory (org.slf4j.LoggerFactory)6 Optional (java.util.Optional)5 Constants (org.apache.shenyu.common.constant.Constants)5 ShenyuPluginChain (org.apache.shenyu.plugin.api.ShenyuPluginChain)5 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 ResultEnum (org.apache.shenyu.common.enums.ResultEnum)4 RpcTypeEnum (org.apache.shenyu.common.enums.RpcTypeEnum)4 GsonUtils (org.apache.shenyu.common.utils.GsonUtils)4