SfcGeniusTsaWriter { public CompletableFuture<Void> removeTerminatingServiceAction(BigInteger dpnId) { RemoveTerminatingServiceActionsInput input = new RemoveTerminatingServiceActionsInputBuilder() .setDpnId(dpnId) .setServiceId(SfcGeniusConstants.SFC_VNID) .build(); return CompletableFuture.supplyAsync(() -> { try { RpcResult<Void> result = itmRpcService.removeTerminatingServiceActions(input).get(); if (!result.isSuccessful()) { throw new SfcGeniusRuntimeException( new RuntimeException( "Could not remove terminating service action from data plane node" + dpnId, result.getErrors().stream().findFirst().map(RpcError::getCause).orElse(null))); } } catch (InterruptedException | ExecutionException e) { throw new SfcGeniusRuntimeException( new RuntimeException( "Could not remove terminating service action from data plane node" + dpnId, e)); } return null; }, executor); } SfcGeniusTsaWriter(ItmRpcService itmRpcService, Executor executor); CompletableFuture<Void> createTerminatingServiceAction(BigInteger dpnId); CompletableFuture<Void> removeTerminatingServiceAction(BigInteger dpnId);  }
@Test public void removeTerminatingServiceAction() throws Exception { BigInteger dpnid = BigInteger.valueOf(37); when(itmRpcService.removeTerminatingServiceActions(any())) .thenReturn(CompletableFuture.completedFuture(rpcResult)); when(rpcResult.isSuccessful()).thenReturn(true); SfcGeniusTsaWriter writer = new SfcGeniusTsaWriter(itmRpcService, executor); CompletableFuture<Void> completableFuture = writer.removeTerminatingServiceAction(dpnid); verify(executor).execute(runnableCaptor.capture()); runnableCaptor.getValue().run(); verify(itmRpcService).removeTerminatingServiceActions(removeInputCaptor.capture()); RemoveTerminatingServiceActionsInput input = removeInputCaptor.getValue(); assertThat(completableFuture.isDone(), is(true)); assertThat(dpnid, is(input.getDpnId())); assertThat(SfcGeniusConstants.SFC_VNID, is(input.getServiceId())); assertThat(completableFuture.isCompletedExceptionally(), is(false)); assertThat(completableFuture.isCancelled(), is(false)); }
@Test public void removeTerminatingServiceActionUnsuccessful() throws Exception { BigInteger dpnid = BigInteger.ZERO; Throwable throwable = new Throwable(); when(itmRpcService.removeTerminatingServiceActions(any())) .thenReturn(CompletableFuture.completedFuture(rpcResult)); when(rpcResult.isSuccessful()).thenReturn(false); when(rpcResult.getErrors()).thenReturn(Collections.singletonList(rpcError)); when(rpcError.getCause()).thenReturn(throwable); SfcGeniusTsaWriter writer = new SfcGeniusTsaWriter(itmRpcService, executor); CompletableFuture<Void> completableFuture = writer.removeTerminatingServiceAction(dpnid); verify(executor).execute(runnableCaptor.capture()); runnableCaptor.getValue().run(); assertThat(completableFuture.isCompletedExceptionally(), is(true)); try { completableFuture.join(); } catch (Exception e) { assertThat(e.getCause(), is(instanceOf(SfcGeniusRuntimeException.class))); assertThat(e.getCause().getCause(), is(instanceOf(RuntimeException.class))); assertThat(e.getCause().getCause().getCause(), sameInstance(throwable)); } }
@Test public void removeTerminatingServiceActionException() throws Exception { BigInteger dpnid = BigInteger.ZERO; CompletableFuture<RpcResult<Void>> future = new CompletableFuture<>(); Throwable throwable = new Throwable(); future.completeExceptionally(throwable); when(itmRpcService.removeTerminatingServiceActions(any())).thenReturn(future); SfcGeniusTsaWriter writer = new SfcGeniusTsaWriter(itmRpcService, executor); CompletableFuture<Void> completableFuture = writer.removeTerminatingServiceAction(dpnid); verify(executor).execute(runnableCaptor.capture()); runnableCaptor.getValue().run(); assertThat(completableFuture.isCompletedExceptionally(), is(true)); try { completableFuture.join(); } catch (Exception e) { assertThat(e.getCause(), is(instanceOf(SfcGeniusRuntimeException.class))); assertThat(e.getCause().getCause(), is(instanceOf(RuntimeException.class))); assertThat(e.getCause().getCause().getCause(), is(instanceOf(ExecutionException.class))); assertThat(e.getCause().getCause().getCause().getCause(), sameInstance(throwable)); } }