BundleService { public BundleMessagesRequestDto callExecutors(final DlmsConnectionManager conn, final DlmsDevice device, final BundleMessagesRequestDto bundleMessagesRequest) { final List<ActionDto> actionList = bundleMessagesRequest.getActionList(); for (final ActionDto actionDto : actionList) { if (actionDto.getResponse() == null) { final Class<? extends ActionRequestDto> actionRequestClass = actionDto.getRequest().getClass(); final CommandExecutor<?, ?> executor = this.bundleCommandExecutorMap .getCommandExecutor(actionRequestClass); final String executorName = executor == null ? "null" : executor.getClass().getSimpleName(); try { this.checkIfExecutorExists(actionRequestClass, executor); LOGGER.debug("**************************************************"); LOGGER.info("Calling executor in bundle {}", executorName); LOGGER.debug("**************************************************"); actionDto.setResponse(executor.executeBundleAction(conn, device, actionDto.getRequest())); } catch (final ConnectionException connectionException) { LOGGER.warn("A connection exception occurred while executing {}", executorName, connectionException); final List<ActionDto> remainingActionDtoList = actionList .subList(actionList.indexOf(actionDto), actionList.size()); for (final ActionDto remainingActionDto : remainingActionDtoList) { LOGGER.debug("Skipping: {}", remainingActionDto.getRequest().getClass().getSimpleName()); } actionDto.setResponse(null); throw connectionException; } catch (final Exception exception) { LOGGER.error("Error while executing bundle action for {} with {}", actionRequestClass.getName(), executorName, exception); final String responseMessage = executor == null ? "Unable to handle request" : "Error handling request with " + executorName; this.addFaultResponse(actionDto, exception, responseMessage, device); } } } return bundleMessagesRequest; }  BundleMessagesRequestDto callExecutors(final DlmsConnectionManager conn, final DlmsDevice device,
            final BundleMessagesRequestDto bundleMessagesRequest);  }
@Test public void testHappyFlow() { final List<ActionDto> actionDtoList = this.makeActions(); final BundleMessagesRequestDto dto = new BundleMessagesRequestDto(actionDtoList); final BundleMessagesRequestDto result = this.callExecutors(dto); this.assertResult(result); }
@Test public void testException() { final List<ActionDto> actionDtoList = this.makeActions(); final BundleMessagesRequestDto dto = new BundleMessagesRequestDto(actionDtoList); this.getStub(FindEventsRequestDto.class).failWith(new ProtocolAdapterException("simulate error")); final BundleMessagesRequestDto result = this.callExecutors(dto); this.assertResult(result); }
@Test public void testConnectionException() throws ProtocolAdapterException { final List<ActionDto> actionDtoList = this.makeActions(); final BundleMessagesRequestDto dto = new BundleMessagesRequestDto(actionDtoList); this.getStub(FindEventsRequestDto.class) .failWithRuntimeException(new ConnectionException("Connection Exception thrown!")); try { this.callExecutors(dto); fail("A ConnectionException should be thrown"); } catch (final ConnectionException connectionException) { assertThat(dto.getAllResponses().size()).isEqualTo(8); } this.getStub(FindEventsRequestDto.class).failWithRuntimeException(null); try { this.callExecutors(dto); assertThat(actionDtoList.size()).isEqualTo(dto.getAllResponses().size()); } catch (final ConnectionException connectionException) { fail("A ConnectionException should not have been thrown."); } }