diff --git a/src/test/java/org/gcube/common/clients/AsyncDelegateTest.java b/src/test/java/org/gcube/common/clients/AsyncDelegateTest.java index 0327c05..458b0b0 100644 --- a/src/test/java/org/gcube/common/clients/AsyncDelegateTest.java +++ b/src/test/java/org/gcube/common/clients/AsyncDelegateTest.java @@ -37,16 +37,16 @@ import org.mockito.stubbing.Answer; public class AsyncDelegateTest { AsyncProxyDelegate delegate; - + @Mock ProxyPlugin plugin; @Mock Object endpoint; - + @Mock Call call; - + @Mock Object value; @Mock Exception original; @Mock Exception converted; - + @Before @SuppressWarnings("all") public void setup() throws Exception { @@ -54,34 +54,34 @@ public class AsyncDelegateTest { //create subject-under-testing delegate =new AsyncProxyDelegate(mockDelegate(plugin,endpoint)); - + //common configuration staging: mocking a delegate is not that immediate.. when(plugin.name()).thenReturn("some service"); when(plugin.convert(original,delegate.config())).thenReturn(converted); - + } - + @Test public void asyncCallsReturnFutureValues() throws Exception { - + //stage call when(call.call(endpoint)).thenReturn(value); - + Future future = delegate.makeAsync(call); - + Object output = future.get(); - + assertEquals(value,output); - + assertFalse(future.isCancelled()); - + assertTrue(future.isDone()); } @Test public void asyncCallsTimeout() throws Exception { - + //stage call Answer slowly = new Answer() { @Override @@ -91,53 +91,48 @@ public class AsyncDelegateTest { } }; when(call.call(endpoint)).thenAnswer(slowly); - + Future future = delegate.makeAsync(call); - + try { future.get(100,TimeUnit.MILLISECONDS); fail(); } catch(TimeoutException e) {} - + } - + @Test public void asyncCallsExecuteInCallScope() throws Exception { - + final String scope = "a/b/c"; SecretManagerProvider.instance.set(new Secret() { - - @Override - public boolean isRefreshable() { - // TODO Auto-generated method stub - return false; - } - + + @Override public boolean isExpired() { // TODO Auto-generated method stub return false; } - + @Override public Owner getOwner() { // TODO Auto-generated method stub return null; } - + @Override public Map getHTTPAuthorizationHeaders() { // TODO Auto-generated method stub return null; } - + @Override public String getContext() { return scope; } }); - + //stage call Answer checkingScope= new Answer() { @Override @@ -146,22 +141,22 @@ public class AsyncDelegateTest { return value; } }; - + when(call.call(endpoint)).thenAnswer(checkingScope); - + Future future = delegate.makeAsync(call); - + future.get(); } - + @Test public void asyncCallReturnConvertedFaultsAsInnerCauses() throws Exception { - + //stage call when(call.call(endpoint)).thenThrow(original); - + Future future = delegate.makeAsync(call); - + try { future.get(); fail(); @@ -169,45 +164,40 @@ public class AsyncDelegateTest { catch(Exception fault) { assertEquals(converted,fault.getCause()); } - + } @Test public void asyncCallsAreInterrupted() throws Exception { - + final String scope = "a/b/c"; -SecretManagerProvider.instance.set(new Secret() { - - @Override - public boolean isRefreshable() { - // TODO Auto-generated method stub - return false; - } - + SecretManagerProvider.instance.set(new Secret() { + + @Override public boolean isExpired() { // TODO Auto-generated method stub return false; } - + @Override public Owner getOwner() { // TODO Auto-generated method stub return null; } - + @Override public Map getHTTPAuthorizationHeaders() { // TODO Auto-generated method stub return null; } - + @Override public String getContext() { return scope; } }); - + //stage call Answer slowly = new Answer() { @Override @@ -222,12 +212,12 @@ SecretManagerProvider.instance.set(new Secret() { new Thread() { public void run() { - + future.cancel(true); - + }; }.start(); - + try { future.get(); fail(); @@ -235,12 +225,12 @@ SecretManagerProvider.instance.set(new Secret() { catch(CancellationException fault) { assertTrue(future.isCancelled()); } - + } - + @Test public void callbacksGetResults() throws Exception { - + //stage call Answer answer = new Answer() { @Override @@ -248,9 +238,9 @@ SecretManagerProvider.instance.set(new Secret() { return value; } }; - + when(call.call(endpoint)).thenAnswer(answer); - + @SuppressWarnings("all") Callback callback = mock(Callback.class); @@ -258,16 +248,16 @@ SecretManagerProvider.instance.set(new Secret() { //make sure the callback has had time to arrive Thread.sleep(400); - + verify(callback).done(value); - + assertTrue(future.isDone()); - + } - + @Test public void callbacksGetTimeoutErrors() throws Exception { - + //stage call Answer slowly = new Answer() { @Override @@ -276,15 +266,15 @@ SecretManagerProvider.instance.set(new Secret() { return value; } }; - + when(call.call(endpoint)).thenAnswer(slowly); - + @SuppressWarnings("all") Callback callback = mock(Callback.class); when(callback.timeout()).thenReturn(100L); final CountDownLatch latch = new CountDownLatch(1); - + Answer unblock = new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { @@ -292,27 +282,27 @@ SecretManagerProvider.instance.set(new Secret() { return value; } }; - + doAnswer(unblock).when(callback).onFailure(any(TimeoutException.class)); - + Future future = delegate.makeAsync(call,callback); - + //makes sure callback has been invoked latch.await(5,SECONDS); - + assertTrue(future.isCancelled()); } - + @Test public void callbacksGetFaults() throws Exception { - + when(call.call(endpoint)).thenThrow(original); - + @SuppressWarnings("all") Callback callback = mock(Callback.class); - + final CountDownLatch latch = new CountDownLatch(1); - + Answer unblock = new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { @@ -323,14 +313,14 @@ SecretManagerProvider.instance.set(new Secret() { return null; } }; - + doAnswer(unblock).when(callback).onFailure(any(Throwable.class)); - + delegate.makeAsync(call,callback); - + //makes sure callback has been invoked latch.await(1,SECONDS); - + } - + }