CommandOrchestratorImpl implements CommandOrchestrator { @Override public <P> Optional<P> retreive(String eventId, Class<P> target){ Optional<P> payload = Optional.empty(); Optional<BubbleMessage> bubbleMessage = (bubbleCache.getBubble(eventId)); if (bubbleMessage.isPresent()){ AbstractCommand c = commandRepository.findOne(bubbleMessage.get().getCommandId()); try { payload = Optional.of(mapper.readValue(c.getPayload(), target)); } catch (Exception e) { throw new CommandUnmarchelingException(e.getMessage()); } } return payload; } @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 testRetreiveWhenCommandExistInDbAndBubbleMessage() throws IOException { final String eventId = "1234"; final TestData someData = new TestData("test user", "20"); final AbstractCommand command = mock(AbstractCommand.class); String payload = "{\"name\":\"test user\", \"age\":\"20\"}"; BubbleMessage message = mock(BubbleMessage.class); when(command.getPayload()).thenReturn(payload); when(mockBubbleCache.getBubble(eventId)).thenReturn(Optional.of(message)); when(mockCommandRepository.findOne(anyString())).thenReturn(command); when(mockMapper.readValue(payload, TestData.class)).thenReturn(someData); final Optional<TestData> result = commandOrchestratorImplUnderTest.retreive(eventId, TestData.class); assertEquals( Optional.of(someData), result); }
@Test public void testRetreiveWhenCommandNotExistInBubbleMessage() throws IOException { final String eventId = "1234"; final TestData someData = new TestData("test user", "20"); final AbstractCommand command = mock(AbstractCommand.class); String payload = "{\"name\":\"test user\", \"age\":\"20\"}"; BubbleMessage message = mock(BubbleMessage.class); when(mockBubbleCache.getBubble(eventId)).thenReturn(Optional.empty()); final Optional<TestData> result = commandOrchestratorImplUnderTest.retreive(eventId, TestData.class); assertEquals( Optional.empty(), result); }
@Test(expected = CommandUnmarchelingException.class) public void testRetreiveWhenCommandNotExistInDb() throws IOException { final String eventId = "1234"; BubbleMessage message = mock(BubbleMessage.class); when(mockBubbleCache.getBubble(eventId)).thenReturn(Optional.of(message)); when(mockCommandRepository.findOne(anyString())).thenReturn(null); final Optional<TestData> result = commandOrchestratorImplUnderTest.retreive(eventId, TestData.class); }
@Test(expected = CommandUnmarchelingException.class) public void testRetreiveWhenJAcksonFireException() throws IOException { final String eventId = "1234"; final AbstractCommand command = mock(AbstractCommand.class); BubbleMessage message = mock(BubbleMessage.class); when(command.getPayload()).thenReturn(""); when(mockBubbleCache.getBubble(eventId)).thenReturn(Optional.of(message)); when(mockCommandRepository.findOne(anyString())).thenReturn(command); when(mockMapper.readValue("", TestData.class)).thenThrow(JsonProcessingException.class); final Optional<TestData> result = commandOrchestratorImplUnderTest.retreive(eventId, TestData.class); }