@SuppressWarnings("unchecked") @Override public Object handle(Message<?> message) throws Exception { Callable<Object> messageHandling; if (message instanceof CommandMessage) { messageHandling = () -> handle((CommandMessage) message); } else if (message instanceof EventMessage) { messageHandling = () -> handle((EventMessage) message); } else { throw new IllegalArgumentException("Unsupported message type: " + message.getClass()); } return executeWithResult(messageHandling); }
@Test public void testCommandsAreRoutedToCorrectEntity() throws Exception { AggregateModel<Book> bookAggregateModel = AnnotatedAggregateMetaModelFactory.inspectAggregate(Book.class); EventBus mockEventBus = SimpleEventBus.builder().build(); mockEventBus.subscribe(m -> m.forEach(i -> System.out.println(i.getPayloadType().getName()))); AnnotatedAggregate<Book> bookAggregate = AnnotatedAggregate.initialize((Book) null, bookAggregateModel, mockEventBus); bookAggregate.handle(command(new CreateBookCommand("book1"))); bookAggregate.handle(command(new CreatePageCommand("book1"))); bookAggregate.handle(command(new CreateParagraphCommand("book1", 0))); bookAggregate.handle(command(new CreateParagraphCommand("book1", 0))); bookAggregate.handle(command(new UpdateParagraphCommand("book1", 0, 0, "Hello world"))); bookAggregate.handle(command(new UpdateParagraphCommand("book1", 0, 1, "Hello world2"))); assertEquals("Hello world", bookAggregate.getAggregateRoot().getPages().get(0).getParagraphs().get(0).getText()); assertEquals("Hello world2", bookAggregate.getAggregateRoot().getPages().get(0).getParagraphs().get(1).getText()); }
@SuppressWarnings("unchecked") @Override public Object handle(Message<?> message) throws Exception { Callable<Object> messageHandling; if (message instanceof CommandMessage) { messageHandling = () -> handle((CommandMessage) message); } else if (message instanceof EventMessage) { messageHandling = () -> handle((EventMessage) message); } else { throw new IllegalArgumentException("Unsupported message type: " + message.getClass()); } return executeWithResult(messageHandling); }