private <ReqT, RespT, MetaT> void mockOperationResult( OperationCallable<ReqT, RespT, MetaT> callable, ReqT request, RespT response) { OperationSnapshot operationSnapshot = FakeOperationSnapshot.newBuilder() .setDone(true) .setErrorCode(GrpcStatusCode.of(Code.OK)) .setName("fake-name") .setResponse(response) .build(); OperationFuture<RespT, MetaT> operationFuture = OperationFutures.immediateOperationFuture(operationSnapshot); Mockito.when(callable.futureCall(request)).thenReturn(operationFuture); }
@Test(expected = IllegalArgumentException.class) public void testNotDone() { OperationFutures.immediateOperationFuture( FakeOperationSnapshot.newBuilder() .setName("required") .setDone(false) .setErrorCode(FakeStatusCode.of(StatusCode.Code.OK)) .build()); }
@Test public void testFailed() throws Exception { OperationFuture<String, Integer> future = OperationFutures.<String, Integer>immediateOperationFuture( FakeOperationSnapshot.newBuilder() .setName("myName") .setDone(true) .setMetadata(42) .setErrorCode(FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT)) .build()); assertThat(future.getName()).isEqualTo("myName"); assertThat(future.getMetadata().get()).isEqualTo(42); try { future.get(); fail(); } catch (ExecutionException e) { assertThat(e.getCause()).isInstanceOf(ApiException.class); } }
@Test public void testCompleted() throws Exception { OperationFuture<String, Integer> future = OperationFutures.<String, Integer>immediateOperationFuture( FakeOperationSnapshot.newBuilder() .setName("myName") .setDone(true) .setResponse("theResponse") .setMetadata(42) .setErrorCode(FakeStatusCode.of(StatusCode.Code.OK)) .build()); assertThat(future.getName()).isEqualTo("myName"); assertThat(future.get()).isEqualTo("theResponse"); assertThat(future.getMetadata().get()).isEqualTo(42); }