/** * Takes a {@link SoapCall} object and invokes the method by reflection. * * @param soapCall contains the SOAP method, SOAP client, and args to be * called * @return the response from the SOAP web service * @throws Exception thrown if calling the operation on the remote service * fails */ protected Object invoke(SoapCall<T> soapCall) throws Exception { Method soapClientMethod = soapCall.getSoapClientMethod(); Object[] soapArgs = soapCall.getSoapArgs(); soapArgs = processSoapArguments(soapClientMethod, soapArgs); return soapClientMethod.invoke(soapCall.getSoapClient(), soapArgs); } }
@Test public void testGetSoapClientMethod() throws Exception { Method method = soapClientHandler.getSoapClientMethod(soapClient, SimilarMockSoapClient.class.getMethod( "identityCall", Object[].class, Object.class, Object.class)); assertEquals(MockSoapClient.class.getMethod("identityCall", Object[].class), method); }
@Test public void testInvoke_lotsOfArgs() throws Exception { Object firstArg = new Object(); Object[] args = new Object[] {firstArg, new Object[] {}, new Object(), new Object()}; when(soapCall.getSoapClientMethod()).thenReturn( MockSoapClient.class.getMethod("lotsOfArgsCall", Object.class, Object[].class, Object.class, Object.class)); when(soapCall.getSoapClient()).thenReturn(new MockSoapClient()); when(soapCall.getSoapArgs()).thenReturn(args); Object result = soapClientHandler.invoke(soapCall); assertSame(firstArg, result); }
@Test public void testInvoke_soapClientMethod() throws Throwable { Object returnValue = new Object(); RemoteCallReturn callReturn = new RemoteCallReturn.Builder().withReturnValue(returnValue).build(); Method identityCallMethod = MockSoapClient.class.getMethod("identityCall", Object[].class); Object[] arg = new String[] {"arg1", "arg2"}; Object[] args = new Object[] {arg}; SoapCall<Object> soapCall = new SoapCall<Object>(identityCallMethod, soapClient, args); when(soapClientHandler.getSoapClientMethod(soapClient, identityCallMethod)) .thenReturn(identityCallMethod); when(soapClientHandler.invokeSoapCall(soapCall)).thenReturn(callReturn); Object result = soapServiceClient.invoke(null, identityCallMethod, args); assertEquals(returnValue, result); }
@Test @SuppressWarnings("unchecked") public void testCallSoapClient() throws Throwable { RemoteCallReturn expectedRemoteCallReturn = new RemoteCallReturn.Builder().build(); SoapCall<Object> soapCall = Mockito.mock(SoapCall.class); when(soapClientHandler.invokeSoapCall(soapCall)).thenReturn(expectedRemoteCallReturn); RemoteCallReturn remoteCallReturn = soapServiceClient.callSoapClient(soapCall); assertSame(expectedRemoteCallReturn, remoteCallReturn); }
@Test public void testInvoke_serviceClientMethod() throws Throwable { String endpointAddress = "http://test.com"; soapServiceClient.invoke(null, SoapServiceClient.class.getMethod("setEndpointAddress", String.class), new Object[] {endpointAddress}); verify(soapClientHandler).setEndpointAddress(soapClient, endpointAddress); }
@Test @SuppressWarnings("unchecked") public void testCallSoapClient_exception() { RemoteCallReturn expectedRemoteCallReturn = new RemoteCallReturn.Builder().withException(MockSoapClient.EXCEPTION).build(); SoapCall<Object> soapCall = Mockito.mock(SoapCall.class); when(soapClientHandler.invokeSoapCall(soapCall)).thenReturn(expectedRemoteCallReturn); RemoteCallReturn testRemoteCallReturn = soapServiceClient.callSoapClient(soapCall); assertSame(MockSoapClient.EXCEPTION, testRemoteCallReturn.getException()); }
@Test public void testSetEndpointAddress() { String expectedEndpointAddress = "endpoint"; soapServiceClient.setEndpointAddress(expectedEndpointAddress); verify(soapClientHandler).setEndpointAddress(soapClient, expectedEndpointAddress); }
/** * Takes a {@link SoapCall} object and invokes the method by reflection. * * @param soapCall contains the SOAP method, SOAP client, and args to be * called * @return the response from the SOAP web service * @throws Exception thrown if calling the operation on the remote service * fails */ protected Object invoke(SoapCall<T> soapCall) throws Exception { Method soapClientMethod = soapCall.getSoapClientMethod(); Object[] soapArgs = soapCall.getSoapArgs(); soapArgs = processSoapArguments(soapClientMethod, soapArgs); return soapClientMethod.invoke(soapCall.getSoapClient(), soapArgs); } }
@Test public void testCall_identityCall() throws Throwable { Object[] args = new Object[] {1L, "2", 3.0d}; when(soapCall.getSoapClientMethod()).thenReturn( MockSoapClient.class.getMethod("identityCall", Object[].class)); when(soapCall.getSoapClient()).thenReturn(new MockSoapClient()); when(soapCall.getSoapArgs()).thenReturn(new Object[] {args}); Object result = soapClientHandler.invoke(soapCall); assertSame(args, result); }
@Test public void testInvoke_soapClientMethodNoSuchmethodException() throws Throwable { Method indentityCallMethod = MockSoapClient.class.getMethod("identityCall", Object[].class); Object[] args = new String[] {"arg1", "arg2"}; when(soapClientHandler.getSoapClientMethod(soapClient, indentityCallMethod)) .thenThrow(new NoSuchMethodException()); thrown.expect(NoSuchMethodException.class); soapServiceClient.invoke(null, indentityCallMethod, args); }
@Test public void testProcessArgs_emptyMethod() throws Exception { Object[] args = new Object[] {}; Object[] processedArgs = soapClientHandler.processSoapArguments(MockSoapClient.class.getMethod("emptyCall"), args); assertEquals(0, processedArgs.length); }
@Test public void testInvoke_emptyCall() throws Exception { Object[] args = null; when(soapCall.getSoapClientMethod()).thenReturn( MockSoapClient.class.getMethod("emptyCall")); when(soapCall.getSoapClient()).thenReturn(new MockSoapClient()); when(soapCall.getSoapArgs()).thenReturn(args); Object result = soapClientHandler.invoke(soapCall); assertNull(result); }
@Test public void testGetSoapClientMethod_incorrectReturnType() throws Exception { thrown.expect(NoSuchMethodException.class); soapClientHandler.getSoapClientMethod(soapClient, SimilarMockSoapClient.class.getMethod("voidCall", Object[].class)); }
@Test public void testProcessArgs_null() throws Exception { Object[] args = null; Object[] processedArgs = soapClientHandler.processSoapArguments(MockSoapClient.class.getMethod("emptyCall"), args); assertNull(processedArgs); }
@Test public void testCall_exception() throws Throwable { Object[] args = new Object[] {1L, "2", 3.0d}; MockSoapClient mockSoapClient = new MockSoapClient(); when(soapCall.getSoapClientMethod()).thenReturn( MockSoapClient.class.getMethod("throwException", Object[].class)); when(soapCall.getSoapClient()).thenReturn(mockSoapClient); when(soapCall.getSoapArgs()).thenReturn(new Object[] {args}); thrown.expect(InvocationTargetException.class); thrown.expectCause(Matchers.<Exception>is(MockSoapClient.EXCEPTION)); soapClientHandler.invoke(soapCall); }
@Test public void testGetSoapClientMethod_noMethod() throws Exception { thrown.expect(NoSuchMethodException.class); soapClientHandler.getSoapClientMethod(soapClient, SimilarMockSoapClient.class.getMethod( "noSuchMethod", Object[].class, Object.class, Object.class)); }
@Test public void testProcessArgs_tooMany() throws Exception { Object[] args = new Object[] {new Object[] {1.0d, "2", 3L}, "4"}; Object[] processedArgs = soapClientHandler.processSoapArguments(MockSoapClient.class.getMethod("emptyCall"), args); assertEquals(args[0], processedArgs[0]); assertEquals(args[1], processedArgs[1]); assertEquals(2, processedArgs.length); }
@Test public void testInvoke_voidCall() throws Exception { Object[] args = new Object[] {"2", 1}; when(soapCall.getSoapClientMethod()).thenReturn( MockSoapClient.class.getMethod("voidCall", Object[].class)); when(soapCall.getSoapClient()).thenReturn(new MockSoapClient()); when(soapCall.getSoapArgs()).thenReturn(new Object[] {args}); Object result = soapClientHandler.invoke(soapCall); assertNull(result); }
@Test public void testProcessArgs_emptyArgs() throws Exception { Object[] args = new Object[] {}; Object[] processedArgs = soapClientHandler.processSoapArguments(MockSoapClient.class.getMethod("lotsOfArgsCall", Object.class, Object[].class, Object.class, Object.class), args); assertNull(processedArgs[0]); assertNull(processedArgs[1]); assertNull(processedArgs[2]); assertNull(processedArgs[3]); assertEquals(4, processedArgs.length); }