MockClusterInvoker implements Invoker<T> { public Result invoke(Invocation invocation) throws RpcException { Result result = null; String value = directory.getUrl().getMethodParameter(invocation.getMethodName(), Constants.MOCK_KEY, Boolean.FALSE.toString()).trim(); if (value.length() == 0 || value.equalsIgnoreCase("false")){ result = this.invoker.invoke(invocation); } else if (value.startsWith("force")) { if (logger.isWarnEnabled()) { logger.info("force-mock: " + invocation.getMethodName() + " force-mock enabled , url : " + directory.getUrl()); } result = doMockInvoke(invocation, null); } else { try { result = this.invoker.invoke(invocation); }catch (RpcException e) { if (e.isBiz()) { throw e; } else { if (logger.isWarnEnabled()) { logger.info("fail-mock: " + invocation.getMethodName() + " fail-mock enabled , url : " + directory.getUrl(), e); } result = doMockInvoke(invocation, e); } } } return result; } MockClusterInvoker(Directory<T> directory, Invoker<T> invoker); URL getUrl(); boolean isAvailable(); void destroy(); Class<T> getInterface(); Result invoke(Invocation invocation); @Override String toString();  }
@Test public void testMockInvokerInvoke_normal(){ URL url = URL.valueOf("remote: url = url.addParameter(Constants.MOCK_KEY, "fail" ); Invoker<IHelloService> cluster = getClusterInvoker(url); URL mockUrl = URL.valueOf("mock: +"?getSomething.mock=return aa"); Protocol protocol = new MockProtocol(); Invoker<IHelloService> mInvoker1 = protocol.refer(IHelloService.class, mockUrl); invokers.add(mInvoker1); RpcInvocation invocation = new RpcInvocation(); invocation.setMethodName("getSomething"); Result ret = cluster.invoke(invocation); Assert.assertEquals("something", ret.getValue()); invocation = new RpcInvocation(); invocation.setMethodName("sayHello"); ret = cluster.invoke(invocation); Assert.assertEquals(null, ret.getValue()); }
@Test public void testMockInvokerInvoke_failmock(){ URL url = URL.valueOf("remote: .addParameter(Constants.MOCK_KEY, "fail:return null" ) .addParameter("invoke_return_error", "true" ); Invoker<IHelloService> cluster = getClusterInvoker(url); URL mockUrl = URL.valueOf("mock: +"?getSomething.mock=return aa").addParameters(url.getParameters()); Protocol protocol = new MockProtocol(); Invoker<IHelloService> mInvoker1 = protocol.refer(IHelloService.class, mockUrl); invokers.add(mInvoker1); RpcInvocation invocation = new RpcInvocation(); invocation.setMethodName("getSomething"); Result ret = cluster.invoke(invocation); Assert.assertEquals("aa", ret.getValue()); invocation = new RpcInvocation(); invocation.setMethodName("getSomething2"); ret = cluster.invoke(invocation); Assert.assertEquals(null, ret.getValue()); invocation = new RpcInvocation(); invocation.setMethodName("sayHello"); ret = cluster.invoke(invocation); Assert.assertEquals(null, ret.getValue()); }
@Test public void testMockInvokerInvoke_forcemock(){ URL url = URL.valueOf("remote: url = url.addParameter(Constants.MOCK_KEY, "force:return null" ); Invoker<IHelloService> cluster = getClusterInvoker(url); URL mockUrl = URL.valueOf("mock: +"?getSomething.mock=return aa&getSomething3xx.mock=return xx") .addParameters(url.getParameters()); Protocol protocol = new MockProtocol(); Invoker<IHelloService> mInvoker1 = protocol.refer(IHelloService.class, mockUrl); invokers.add(mInvoker1); RpcInvocation invocation = new RpcInvocation(); invocation.setMethodName("getSomething"); Result ret = cluster.invoke(invocation); Assert.assertEquals("aa", ret.getValue()); invocation = new RpcInvocation(); invocation.setMethodName("getSomething2"); ret = cluster.invoke(invocation); Assert.assertEquals(null, ret.getValue()); invocation = new RpcInvocation(); invocation.setMethodName("sayHello"); ret = cluster.invoke(invocation); Assert.assertEquals(null, ret.getValue()); }
@Test public void testMockInvokerInvoke_forcemock_defaultreturn(){ URL url = URL.valueOf("remote: url = url.addParameter(Constants.MOCK_KEY, "force" ); Invoker<IHelloService> cluster = getClusterInvoker(url); URL mockUrl = URL.valueOf("mock: +"?getSomething.mock=return aa&getSomething3xx.mock=return xx&sayHello.mock=return ") .addParameters(url.getParameters()); Protocol protocol = new MockProtocol(); Invoker<IHelloService> mInvoker1 = protocol.refer(IHelloService.class, mockUrl); invokers.add(mInvoker1); RpcInvocation invocation = new RpcInvocation(); invocation.setMethodName("sayHello"); Result ret = cluster.invoke(invocation); Assert.assertEquals(null, ret.getValue()); }
@Test public void testMockInvokerFromOverride_Invoke_Fock_someMethods(){ URL url = URL.valueOf("remote: .addParameter("getSomething.mock","fail:return x") .addParameter("getSomething2.mock","force:return y"); Invoker<IHelloService> cluster = getClusterInvoker(url); RpcInvocation invocation = new RpcInvocation(); invocation.setMethodName("getSomething"); Result ret = cluster.invoke(invocation); Assert.assertEquals("something", ret.getValue()); invocation = new RpcInvocation(); invocation.setMethodName("getSomething2"); ret = cluster.invoke(invocation); Assert.assertEquals("y", ret.getValue()); invocation = new RpcInvocation(); invocation.setMethodName("getSomething3"); ret = cluster.invoke(invocation); Assert.assertEquals("something3", ret.getValue()); invocation = new RpcInvocation(); invocation.setMethodName("sayHello"); ret = cluster.invoke(invocation); Assert.assertEquals(null, ret.getValue()); }
@Test public void testMockInvokerFromOverride_Invoke_Fock_WithOutDefault(){ URL url = URL.valueOf("remote: .addParameter("getSomething.mock","fail:return x") .addParameter("getSomething2.mock","force:return y") .addParameter("invoke_return_error", "true" ); Invoker<IHelloService> cluster = getClusterInvoker(url); RpcInvocation invocation = new RpcInvocation(); invocation.setMethodName("getSomething"); Result ret = cluster.invoke(invocation); Assert.assertEquals("x", ret.getValue()); invocation = new RpcInvocation(); invocation.setMethodName("getSomething2"); ret = cluster.invoke(invocation); Assert.assertEquals("y", ret.getValue()); invocation = new RpcInvocation(); invocation.setMethodName("getSomething3"); try { ret = cluster.invoke(invocation); Assert.fail(); }catch (RpcException e) { } }
@Test public void testMockInvokerFromOverride_Invoke_Fock_WithDefault(){ URL url = URL.valueOf("remote: .addParameter("mock","fail:return null") .addParameter("getSomething.mock","fail:return x") .addParameter("getSomething2.mock","force:return y") .addParameter("invoke_return_error", "true" ); Invoker<IHelloService> cluster = getClusterInvoker(url); RpcInvocation invocation = new RpcInvocation(); invocation.setMethodName("getSomething"); Result ret = cluster.invoke(invocation); Assert.assertEquals("x", ret.getValue()); invocation = new RpcInvocation(); invocation.setMethodName("getSomething2"); ret = cluster.invoke(invocation); Assert.assertEquals("y", ret.getValue()); invocation = new RpcInvocation(); invocation.setMethodName("getSomething3"); ret = cluster.invoke(invocation); Assert.assertEquals(null, ret.getValue()); invocation = new RpcInvocation(); invocation.setMethodName("sayHello"); ret = cluster.invoke(invocation); Assert.assertEquals(null, ret.getValue()); }
@Test public void testMockInvokerFromOverride_Invoke_Fock_WithFailDefault(){ URL url = URL.valueOf("remote: .addParameter("mock","fail:return z") .addParameter("getSomething.mock","fail:return x") .addParameter("getSomething2.mock","force:return y") .addParameter("invoke_return_error", "true" ); Invoker<IHelloService> cluster = getClusterInvoker(url); RpcInvocation invocation = new RpcInvocation(); invocation.setMethodName("getSomething"); Result ret = cluster.invoke(invocation); Assert.assertEquals("x", ret.getValue()); invocation = new RpcInvocation(); invocation.setMethodName("getSomething2"); ret = cluster.invoke(invocation); Assert.assertEquals("y", ret.getValue()); invocation = new RpcInvocation(); invocation.setMethodName("getSomething3"); ret = cluster.invoke(invocation); Assert.assertEquals("z", ret.getValue()); invocation = new RpcInvocation(); invocation.setMethodName("sayHello"); ret = cluster.invoke(invocation); Assert.assertEquals("z", ret.getValue()); }
@Test public void testMockInvokerFromOverride_Invoke_Fock_WithForceDefault(){ URL url = URL.valueOf("remote: .addParameter("mock","force:return z") .addParameter("getSomething.mock","fail:return x") .addParameter("getSomething2.mock","force:return y") .addParameter("invoke_return_error", "true" ); Invoker<IHelloService> cluster = getClusterInvoker(url); RpcInvocation invocation = new RpcInvocation(); invocation.setMethodName("getSomething"); Result ret = cluster.invoke(invocation); Assert.assertEquals("x", ret.getValue()); invocation = new RpcInvocation(); invocation.setMethodName("getSomething2"); ret = cluster.invoke(invocation); Assert.assertEquals("y", ret.getValue()); invocation = new RpcInvocation(); invocation.setMethodName("getSomething3"); ret = cluster.invoke(invocation); Assert.assertEquals("z", ret.getValue()); invocation = new RpcInvocation(); invocation.setMethodName("sayHello"); ret = cluster.invoke(invocation); Assert.assertEquals("z", ret.getValue()); }
@Test public void testMockInvokerFromOverride_Invoke_Fock_Default(){ URL url = URL.valueOf("remote: .addParameter("mock","fail:return x") .addParameter("invoke_return_error", "true" ); Invoker<IHelloService> cluster = getClusterInvoker(url); RpcInvocation invocation = new RpcInvocation(); invocation.setMethodName("getSomething"); Result ret = cluster.invoke(invocation); Assert.assertEquals("x", ret.getValue()); invocation = new RpcInvocation(); invocation.setMethodName("getSomething2"); ret = cluster.invoke(invocation); Assert.assertEquals("x", ret.getValue()); invocation = new RpcInvocation(); invocation.setMethodName("sayHello"); ret = cluster.invoke(invocation); Assert.assertEquals("x", ret.getValue()); }
@Test public void testMockInvokerFromOverride_Invoke_checkCompatible_return(){ URL url = URL.valueOf("remote: .addParameter("getSomething.mock","return x") .addParameter("invoke_return_error", "true" ); Invoker<IHelloService> cluster = getClusterInvoker(url); RpcInvocation invocation = new RpcInvocation(); invocation.setMethodName("getSomething"); Result ret = cluster.invoke(invocation); Assert.assertEquals("x", ret.getValue()); invocation = new RpcInvocation(); invocation.setMethodName("getSomething3"); try{ ret = cluster.invoke(invocation); Assert.fail("fail invoke"); }catch(RpcException e){ } }
@Test public void testMockInvokerFromOverride_Invoke_checkCompatible_ImplMock(){ URL url = URL.valueOf("remote: .addParameter("mock","true") .addParameter("invoke_return_error", "true" ); Invoker<IHelloService> cluster = getClusterInvoker(url); RpcInvocation invocation = new RpcInvocation(); invocation.setMethodName("getSomething"); Result ret = cluster.invoke(invocation); Assert.assertEquals("somethingmock", ret.getValue()); }
@Test public void testMockInvokerFromOverride_Invoke_checkCompatible_ImplMock2(){ URL url = URL.valueOf("remote: .addParameter("mock","fail") .addParameter("invoke_return_error", "true" ); Invoker<IHelloService> cluster = getClusterInvoker(url); RpcInvocation invocation = new RpcInvocation(); invocation.setMethodName("getSomething"); Result ret = cluster.invoke(invocation); Assert.assertEquals("somethingmock", ret.getValue()); }
@Test public void testMockInvokerFromOverride_Invoke_checkCompatible_ImplMock3(){ URL url = URL.valueOf("remote: .addParameter("mock","force"); Invoker<IHelloService> cluster = getClusterInvoker(url); RpcInvocation invocation = new RpcInvocation(); invocation.setMethodName("getSomething"); Result ret = cluster.invoke(invocation); Assert.assertEquals("somethingmock", ret.getValue()); }
@Test public void testMockInvokerFromOverride_Invoke_check_String(){ URL url = URL.valueOf("remote: .addParameter("getSomething.mock","force:return 1688") .addParameter("invoke_return_error", "true" ); Invoker<IHelloService> cluster = getClusterInvoker(url); RpcInvocation invocation = new RpcInvocation(); invocation.setMethodName("getSomething"); Result ret = cluster.invoke(invocation); Assert.assertTrue("result type must be String but was : " + ret.getValue().getClass(), ret.getValue() instanceof String); Assert.assertEquals("1688", (String)ret.getValue()); }
@Test public void testMockInvokerFromOverride_Invoke_check_int(){ URL url = URL.valueOf("remote: .addParameter("getInt1.mock","force:return 1688") .addParameter("invoke_return_error", "true" ); Invoker<IHelloService> cluster = getClusterInvoker(url); RpcInvocation invocation = new RpcInvocation(); invocation.setMethodName("getInt1"); Result ret = cluster.invoke(invocation); Assert.assertTrue("result type must be integer but was : " + ret.getValue().getClass(), ret.getValue() instanceof Integer); Assert.assertEquals(new Integer(1688), (Integer)ret.getValue()); }
@SuppressWarnings("unchecked") @Test public void testMockInvokerFromOverride_Invoke_check_ListString_empty(){ URL url = URL.valueOf("remote: .addParameter("getListString.mock","force:return empty") .addParameter("invoke_return_error", "true" ); Invoker<IHelloService> cluster = getClusterInvoker(url); RpcInvocation invocation = new RpcInvocation(); invocation.setMethodName("getListString"); Result ret = cluster.invoke(invocation); Assert.assertEquals(0, ((List<String>)ret.getValue()).size()); }
@SuppressWarnings("unchecked") @Test public void testMockInvokerFromOverride_Invoke_check_ListString(){ URL url = URL.valueOf("remote: .addParameter("getListString.mock","force:return [\"hi\",\"hi2\"]") .addParameter("invoke_return_error", "true" ); Invoker<IHelloService> cluster = getClusterInvoker(url); RpcInvocation invocation = new RpcInvocation(); invocation.setMethodName("getListString"); Result ret = cluster.invoke(invocation); List<String> rl = (List<String>)ret.getValue(); Assert.assertEquals(2, rl.size()); Assert.assertEquals("hi", rl.get(0)); }
@SuppressWarnings("unchecked") @Test public void testMockInvokerFromOverride_Invoke_check_ListPojo_empty(){ URL url = URL.valueOf("remote: .addParameter("getUsers.mock","force:return empty") .addParameter("invoke_return_error", "true" ); Invoker<IHelloService> cluster = getClusterInvoker(url); RpcInvocation invocation = new RpcInvocation(); invocation.setMethodName("getUsers"); Result ret = cluster.invoke(invocation); Assert.assertEquals(0, ((List<User>)ret.getValue()).size()); }
@SuppressWarnings("unchecked") @Test public void testMockInvokerFromOverride_Invoke_check_ListPojo(){ URL url = URL.valueOf("remote: .addParameter("getUsers.mock","force:return [{id:1, name:\"hi1\"}, {id:2, name:\"hi2\"}]") .addParameter("invoke_return_error", "true" ); Invoker<IHelloService> cluster = getClusterInvoker(url); RpcInvocation invocation = new RpcInvocation(); invocation.setMethodName("getUsers"); Result ret = cluster.invoke(invocation); List<User> rl = (List<User>)ret.getValue(); System.out.println(rl); Assert.assertEquals(2, rl.size()); Assert.assertEquals("hi1", ((User)rl.get(0)).getName()); }
@Test public void testMockInvokerFromOverride_Invoke_check_ListPojo_error(){ URL url = URL.valueOf("remote: .addParameter("getUsers.mock","force:return [{id:x, name:\"hi1\"}]") .addParameter("invoke_return_error", "true" ); Invoker<IHelloService> cluster = getClusterInvoker(url); RpcInvocation invocation = new RpcInvocation(); invocation.setMethodName("getUsers"); try{ cluster.invoke(invocation); }catch (RpcException e) { } }
@Test public void testMockInvokerFromOverride_Invoke_force_throw(){ URL url = URL.valueOf("remote: .addParameter("getBoolean2.mock","force:throw ") .addParameter("invoke_return_error", "true" ); Invoker<IHelloService> cluster = getClusterInvoker(url); RpcInvocation invocation = new RpcInvocation(); invocation.setMethodName("getBoolean2"); try { cluster.invoke(invocation); Assert.fail(); } catch (RpcException e) { Assert.assertFalse("not custem exception", e.isBiz()); } }
@Test public void testMockInvokerFromOverride_Invoke_force_throwCustemException() throws Throwable{ URL url = URL.valueOf("remote: .addParameter("getBoolean2.mock","force:throw com.alibaba.dubbo.rpc.cluster.support.wrapper.MyMockException") .addParameter("invoke_return_error", "true" ); Invoker<IHelloService> cluster = getClusterInvoker(url); RpcInvocation invocation = new RpcInvocation(); invocation.setMethodName("getBoolean2"); try { cluster.invoke(invocation).recreate(); Assert.fail(); } catch (MyMockException e) { } }
@Test public void testMockInvokerFromOverride_Invoke_force_throwCustemExceptionNotFound(){ URL url = URL.valueOf("remote: .addParameter("getBoolean2.mock","force:throw java.lang.RuntimeException2") .addParameter("invoke_return_error", "true" ); Invoker<IHelloService> cluster = getClusterInvoker(url); RpcInvocation invocation = new RpcInvocation(); invocation.setMethodName("getBoolean2"); try { cluster.invoke(invocation); Assert.fail(); } catch (Exception e) { Assert.assertTrue(e.getCause() instanceof IllegalStateException); } }
@Test public void testMockInvokerFromOverride_Invoke_mock_false(){ URL url = URL.valueOf("remote: .addParameter("mock","false") .addParameter("invoke_return_error", "true" ); Invoker<IHelloService> cluster = getClusterInvoker(url); RpcInvocation invocation = new RpcInvocation(); invocation.setMethodName("getBoolean2"); try { cluster.invoke(invocation); Assert.fail(); } catch (RpcException e) { Assert.assertTrue(e.isTimeout()); } }