Search in sources :

Example 1 with MockResult

use of org.apache.dubbo.mock.api.MockResult in project dubbo-admin by apache.

the class MockRuleServiceImpl method getMockData.

@Override
public MockResult getMockData(MockContext mockContext) {
    QueryWrapper<MockRule> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq("service_name", mockContext.getServiceName());
    queryWrapper.eq("method_name", mockContext.getMethodName());
    MockRule mockRule = mockRuleMapper.selectOne(queryWrapper);
    MockResult mockResult = new MockResult();
    if (Objects.isNull(mockRule)) {
        return mockResult;
    }
    mockResult.setEnable(mockRule.getEnable());
    mockResult.setContent(mockRule.getRule());
    return mockResult;
}
Also used : MockResult(org.apache.dubbo.mock.api.MockResult) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) MockRule(org.apache.dubbo.admin.model.domain.MockRule)

Example 2 with MockResult

use of org.apache.dubbo.mock.api.MockResult in project dubbo-spi-extensions by apache.

the class AdminMockFilter method invoke.

@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
    // check if open the admin mock config, global config.
    if (!ENABLE_ADMIN_MOCK) {
        return invoker.invoke(invocation);
    }
    String interfaceName = invocation.getTargetServiceUniqueName();
    String methodName = invocation.getMethodName();
    Object[] params = solveParams(invocation.getArguments());
    // check if the MockService's invoker, then request.
    if (Objects.equals(interfaceName, MockService.class.getName())) {
        return invoker.invoke(invocation);
    }
    MockService mockService = getMockService();
    if (Objects.isNull(mockService)) {
        log.warn("[Admin Mock] cloud not find MockService, will ignore this mock.");
        return invoker.invoke(invocation);
    }
    // parse the result from MockService, build the real method's return value.
    MockContext mockContext = MockContext.newMockContext().serviceName(interfaceName).methodName(methodName).arguments(params).build();
    MockResult mockResult = mockService.mock(mockContext);
    if (!mockResult.getEnable()) {
        return invoker.invoke(invocation);
    }
    // parse the return data.
    Class<?> returnType = ((RpcInvocation) invocation).getReturnType();
    ResultContext resultContext = ResultContext.newResultContext().data(mockResult.getContent()).targetType(returnType).serviceName(interfaceName).methodName(methodName).build();
    Object data = typeHandler.handleResult(resultContext);
    AppResponse appResponse = new AppResponse(data);
    CompletableFuture<AppResponse> appResponseFuture = new CompletableFuture<>();
    appResponseFuture.complete(appResponse);
    return new AsyncRpcResult(appResponseFuture, invocation);
}
Also used : RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) ResultContext(org.apache.dubbo.mock.handler.ResultContext) MockContext(org.apache.dubbo.mock.api.MockContext) MockResult(org.apache.dubbo.mock.api.MockResult) CompletableFuture(java.util.concurrent.CompletableFuture) AppResponse(org.apache.dubbo.rpc.AppResponse) MockService(org.apache.dubbo.mock.api.MockService) AsyncRpcResult(org.apache.dubbo.rpc.AsyncRpcResult)

Example 3 with MockResult

use of org.apache.dubbo.mock.api.MockResult in project incubator-dubbo-ops by apache.

the class MockRuleServiceImpl method getMockData.

@Override
public MockResult getMockData(MockContext mockContext) {
    QueryWrapper<MockRule> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq("service_name", mockContext.getServiceName());
    queryWrapper.eq("method_name", mockContext.getMethodName());
    MockRule mockRule = mockRuleMapper.selectOne(queryWrapper);
    MockResult mockResult = new MockResult();
    if (Objects.isNull(mockRule)) {
        return mockResult;
    }
    mockResult.setEnable(mockRule.getEnable());
    mockResult.setContent(mockRule.getRule());
    return mockResult;
}
Also used : MockResult(org.apache.dubbo.mock.api.MockResult) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) MockRule(org.apache.dubbo.admin.model.domain.MockRule)

Aggregations

MockResult (org.apache.dubbo.mock.api.MockResult)3 QueryWrapper (com.baomidou.mybatisplus.core.conditions.query.QueryWrapper)2 MockRule (org.apache.dubbo.admin.model.domain.MockRule)2 CompletableFuture (java.util.concurrent.CompletableFuture)1 MockContext (org.apache.dubbo.mock.api.MockContext)1 MockService (org.apache.dubbo.mock.api.MockService)1 ResultContext (org.apache.dubbo.mock.handler.ResultContext)1 AppResponse (org.apache.dubbo.rpc.AppResponse)1 AsyncRpcResult (org.apache.dubbo.rpc.AsyncRpcResult)1 RpcInvocation (org.apache.dubbo.rpc.RpcInvocation)1