CommandOrchestratorImpl implements CommandOrchestrator { @Override public void receive(final InboundCommand command) { txLogger.logTransactionState(txstatus -> log.info( "Transaction {} for InboundCommand: {}", txstatus, txLogger.printJsonObject(command))); this.receiveCommand( command, (meta) -> this.publisher.publishEvent(new InboundCommandStarted(meta)) ); } @Autowired  CommandOrchestratorImpl(
        final CommandRepository commandRepository,
        final ApplicationEventPublisher publisher,
        final BubbleCache bubbleCache,
        final ObjectMapper mapper,
        final Environment environment,
        final KermossTxLogger txLogger
    ); @Override void receive(final InboundCommand command); @Override void receive(final OutboundCommand command); @Override void receive(BaseTransactionCommand command); @Override void prepare(BaseTransactionCommand command); @Override void prepare(InboundCommand command); @Override boolean isInboundCommandExist(String refId); @Override boolean isInboundCommandWithStatusExist(String refId, InboundCommand.Status status); @Override Optional<P> retreive(String eventId, Class<P> target);  }
@Test public void testReceiveWhenCommandExistInDb() { final InboundCommand command = mock(InboundCommand.class); when(mockCommandRepository.save(any(AbstractCommand.class))).thenReturn(command); when(command.buildMeta()).thenReturn(mockCommandMeta); commandOrchestratorImplUnderTest.receive(command); verify(mockCommandRepository).save(same(command)); }
@Test public void testReceiveWhenCommandNotExistInDb() { final InboundCommand command = mock(InboundCommand.class); when(mockCommandRepository.exists(anyString())).thenReturn(true); when(mockCommandRepository.findOne(anyString())).thenReturn(command); when(command.buildMeta()).thenReturn(mockCommandMeta); commandOrchestratorImplUnderTest.receive(command); verify(mockCommandRepository).findOne(command.getId()); }
@Test public void testReceiveInboundCommand() { final InboundCommand command = mock(InboundCommand.class); when(mockCommandRepository.exists(anyString())).thenReturn(true); when(mockCommandRepository.findOne(anyString())).thenReturn(command); when(command.buildMeta()).thenReturn(mockCommandMeta); commandOrchestratorImplUnderTest.receive(command); verify(mockPublisher).publishEvent(any(InboundCommandStarted.class)); }
@Test public void testReceiveOutboundCommand() { final OutboundCommand command = mock(OutboundCommand.class); when(mockCommandRepository.exists(anyString())).thenReturn(true); when(mockCommandRepository.findOne(anyString())).thenReturn(command); when(command.buildMeta()).thenReturn(mockCommandMeta); when(mockEnvironment.getProperty(same("kermoss.serviceName"), anyString())).thenReturn("source"); commandOrchestratorImplUnderTest.receive(command); verify(mockPublisher).publishEvent(any(OutboundCommandStarted.class)); verify(command).setSource("source"); }
@Test public void testReceiveBaseTransactionCommand() throws JsonProcessingException { final BaseTransactionCommand command = mock(BaseTransactionCommand.class); final AbstractCommand abstractCommand = mock(AbstractCommand.class); ArgumentCaptor<OutboundCommand> commandArgumentCaptor = ArgumentCaptor.forClass(OutboundCommand.class); BubbleMessage message = mock(BubbleMessage.class); when(mockBubbleCache.getBubble(anyString())).thenReturn(Optional.of(message)); when(mockMapper.writeValueAsString(anyString())).thenReturn(PAYLOAD); when(abstractCommand.buildMeta()).thenReturn(mockCommandMeta); when(mockCommandRepository.save(any(AbstractCommand.class))).thenReturn(abstractCommand); when(mockEnvironment.getProperty(same("kermoss.serviceName"), anyString())).thenReturn("source"); commandOrchestratorImplUnderTest.receive(command); verify(mockCommandRepository).save(commandArgumentCaptor.capture()); OutboundCommand mappedCommand = commandArgumentCaptor.getValue(); verify(mockPublisher).publishEvent(any(OutboundCommandStarted.class)); Assert.assertEquals(mappedCommand.getPayload(), PAYLOAD); Assert.assertEquals(mappedCommand.getSource(), "source"); verify(mockPublisher).publishEvent(any(OutboundCommandStarted.class)); }