BundleService { public BundleMessagesRequestDto callExecutors(final DlmsConnectionHolder 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 DlmsConnectionHolder conn, final DlmsDevice device,
            final BundleMessagesRequestDto bundleMessagesRequest);  }
@Test public void testHappyFlow() throws ProtocolAdapterException { final List<ActionDto> actionDtoList = this.makeActions(); final BundleMessagesRequestDto dto = new BundleMessagesRequestDto(actionDtoList); final BundleMessagesRequestDto result = this.callExecutors(dto); Assert.assertTrue(result != null); 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); Assert.fail("A ConnectionException should be thrown"); } catch (final ConnectionException connectionException) { Assert.assertEquals(dto.getAllResponses().size(), 8); } this.getStub(FindEventsRequestDto.class).failWithRuntimeException(null); try { this.callExecutors(dto); Assert.assertEquals(dto.getAllResponses().size(), actionDtoList.size()); } catch (final ConnectionException connectionException) { Assert.fail("A ConnectionException should not have been thrown."); } }