### Question:
AbstractNiFiControllerServicesRestClient implements NiFiControllerServicesRestClient { protected ControllerServiceDTO updateStateByIdWithRetries(@Nonnull final String id, @Nonnull final String state, final int retries, final int timeout, @Nonnull final TimeUnit timeUnit) { ControllerServiceDTO controllerService = new ControllerServiceDTO(); controllerService.setId(id); controllerService.setState(state); controllerService = update(controllerService); for (int count = 0; isPendingState(controllerService.getState(), state) && count < retries; ++count) { log.debug("Waiting for controller service {} to exit pending state {}. Try {} of {}.", id, controllerService.getState(), count + 1, retries); Uninterruptibles.sleepUninterruptibly(timeout, timeUnit); controllerService = findById(id).orElseThrow(() -> new NifiComponentNotFoundException(id, NifiConstants.NIFI_COMPONENT_TYPE.CONTROLLER_SERVICE, null)); } if (state.equals(controllerService.getState())) { return controllerService; } else { String msg = id; try { final List<BulletinDTO> bulletins = getClient().getBulletins(id); if (!bulletins.isEmpty()) { msg = bulletins.get(0).getMessage(); } } catch (final ClientErrorException e) { } throw new NifiClientRuntimeException("Timeout waiting for controller service to be " + state + ": " + msg); } }  @Nonnull @Override Future<Optional<ControllerServiceDTO>> disableAndDeleteAsync(@Nonnull final String id); @Override ControllerServiceDTO updateStateById(@Nonnull final String id, @Nonnull final State state); @Nonnull @Override ControllerServiceDTO updateServiceAndReferencingComponents(@Nonnull final ControllerServiceDTO controllerService);  }### Answer:
@Test public void updateStateByIdWithRetries() { final ControllerServiceDTO response1 = new ControllerServiceDTO(); response1.setState("ENABLING"); final ControllerServiceDTO response2 = new ControllerServiceDTO(); response2.setState("ENABLED"); final AbstractNiFiControllerServicesRestClient client = Mockito.mock(AbstractNiFiControllerServicesRestClient.class, Mockito.CALLS_REAL_METHODS); Mockito.when(client.update(Mockito.any())).thenReturn(response1); Mockito.when(client.findById(Mockito.anyString())).thenReturn(Optional.of(response2)); Assert.assertEquals(response2, client.updateStateByIdWithRetries("MYID", "ENABLED", 1, 0, TimeUnit.NANOSECONDS)); }
@Test(expected = NifiClientRuntimeException.class) public void updateStateByIdWithTimeout() { final NiFiRestClient nifiClient = Mockito.mock(NiFiRestClient.class); Mockito.when(nifiClient.getBulletins("MYID")).thenReturn(Collections.emptyList()); final ControllerServiceDTO response = new ControllerServiceDTO(); response.setState("ENABLING"); final AbstractNiFiControllerServicesRestClient client = Mockito.mock(AbstractNiFiControllerServicesRestClient.class, Mockito.CALLS_REAL_METHODS); Mockito.when(client.update(Mockito.any())).thenReturn(response); Mockito.when(client.findById(Mockito.anyString())).thenReturn(Optional.of(response)); Mockito.when(client.getClient()).thenReturn(nifiClient); client.updateStateByIdWithRetries("MYID", "ENABLED", 1, 0, TimeUnit.NANOSECONDS); }