DefaultPrepareCommandTransporter extends AbstractCommandTransporter<OutboundCommandPrepared> { @Override @Transactional @EventListener public void onEvent(final OutboundCommandPrepared event) { log.info("calling DefaultPrepareCommandTransporter by event {}", event.getClass().getName()); CommandTransporterStrategy commandTransporterStrategy = this.clientStrategy.get(); this.commandRepository.findOutboundCommandOpt(event.getMeta().getCommandId()).ifPresent(outboundCommand -> { if(outboundCommand.getStatus().equals(OutboundCommand.Status.PREPARED)) { final String source = environment.getProperty( "kermoss.serviceName", environment.getProperty("spring.application.name") ); outboundCommand.setSource(source); if(!commandTransporterStrategy.transportPrepareCommand(transform(outboundCommand))) { log.info("Failure Transport Prepared Command over HTTP: {}", txLogger.printJsonObject(outboundCommand)); throw new PreparedCommandTransportException(outboundCommand.getId()); } log.info("Success Transport Prepared Command over HTTP: {}", txLogger.printJsonObject(outboundCommand)); OutboundCommand command = OutboundCommand.builder() .subject(outboundCommand.getSubject()) .gTX(outboundCommand.getGTX()) .pGTX(outboundCommand.getPGTX()) .lTX(outboundCommand.getLTX()) .fLTX(outboundCommand.getFLTX()) .destination(outboundCommand.getDestination()) .trace(outboundCommand.getTraceId()) .build(); log.info("Changing OutboundCommand status from Prepared to Started: {}", txLogger.printJsonObject(outboundCommand)); commandOrchestrator.receive(command); txLogger.logTransactionState(txstatus -> log.info( "Transaction {} for Prepared OutboundCommand: {}", txstatus, txLogger.printJsonObject(outboundCommand))); } }); } @Autowired  DefaultPrepareCommandTransporter(
    	final ApplicationEventPublisher publisher,
        final Environment environment,
        final CommandRepository commandRepository,
        final CommandOrchestrator commandOrchestrator,
        final KermossTxLogger txLogger,
        final HttpClientStrategy clientStrategy
    ); @Override @Transactional @EventListener void onEvent(final OutboundCommandPrepared event);  }
@Test public void testOnEventWhenPostIs2xx() { final OutboundCommandPrepared event = mock(OutboundCommandPrepared.class); final CommandMeta meta = mock(CommandMeta.class); final OutboundCommand command = mock(OutboundCommand.class); final ResponseEntity entity = mock(ResponseEntity.class); CommonSetup(event, meta, command); CommandTransporterStrategy strategy = mock(CommandTransporterStrategy.class); when(clientStrategy.get()).thenReturn(strategy); when(strategy.transportPrepareCommand(any())).thenReturn(true); when(mockRestTemplate.exchange(anyString(), any(), any(), same(TransporterCommand.class))).thenReturn(entity); when(entity.getStatusCode()).thenReturn(HttpStatus.OK); when(txLogger.printJsonObject(any())).thenReturn("Command String"); prepareRestCommandTransporter.onEvent(event); verifyTheCommonResults(command); verify(mockCommandOrchestrator).receive(any(OutboundCommand.class)); }
@Test(expected = PreparedCommandTransportException.class) public void testOnEventWhenPostIsNot2xx() { final OutboundCommandPrepared event = mock(OutboundCommandPrepared.class); final CommandMeta meta = mock(CommandMeta.class); final OutboundCommand command = mock(OutboundCommand.class); CommandTransporterStrategy strategy = mock(CommandTransporterStrategy.class); when(clientStrategy.get()).thenReturn(strategy); final ResponseEntity entity = mock(ResponseEntity.class); CommonSetup(event, meta, command); when(strategy.transportPrepareCommand(any())).thenReturn(false); when(mockRestTemplate.exchange(anyString(), any(), any(), same(TransporterCommand.class))).thenReturn(entity); when(entity.getStatusCode()).thenReturn(HttpStatus.INTERNAL_SERVER_ERROR); prepareRestCommandTransporter.onEvent(event); verifyTheCommonResults(command); verify(mockCommandOrchestrator, never()).receive(any(OutboundCommand.class)); }
@Test public void testOnEventWhenCommandStatusNotPrepared() { final OutboundCommandPrepared event = mock(OutboundCommandPrepared.class); final CommandMeta meta = mock(CommandMeta.class); final OutboundCommand command = mock(OutboundCommand.class); CommonSetup(event, meta, command); when(command.getStatus()).thenReturn(OutboundCommand.Status.STARTED); prepareRestCommandTransporter.onEvent(event); verify(mockEnvironment, never()).getProperty(anyString()); verify(command, never()).setSource(anyString()); verify(mockCommandOrchestrator, never()).receive(any(OutboundCommand.class)); }
@Test public void testOnEventWhenCommandNotFoundInDb() { final OutboundCommandPrepared event = mock(OutboundCommandPrepared.class); final CommandMeta meta = mock(CommandMeta.class); final OutboundCommand command = mock(OutboundCommand.class); when(event.getMeta()).thenReturn(meta); when(mockCommandRepository.findOutboundCommandOpt(anyString())).thenReturn(Optional.empty()); prepareRestCommandTransporter.onEvent(event); verify(mockEnvironment, never()).getProperty(anyString()); verify(command, never()).setSource(anyString()); verify(mockCommandOrchestrator, never()).receive(any(OutboundCommand.class)); }