Skip to content
This repository has been archived by the owner on Dec 14, 2021. It is now read-only.

Commit

Permalink
Streamline PortfolioManagementUserListener and test
Browse files Browse the repository at this point in the history
- Change into an @service
- Use the CommandGateway i.o. the CommandBus
- Add todos noting futher work is necessary

#28
  • Loading branch information
smcvb committed Jun 1, 2018
1 parent d185ba7 commit 23dae93
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 42 deletions.
Expand Up @@ -16,38 +16,33 @@

package org.axonframework.samples.trader.orders.command;

import org.axonframework.commandhandling.CommandBus;
import org.axonframework.commandhandling.GenericCommandMessage;
import org.axonframework.commandhandling.gateway.CommandGateway;
import org.axonframework.eventhandling.EventHandler;
import org.axonframework.samples.trader.api.portfolio.CreatePortfolioCommand;
import org.axonframework.samples.trader.api.portfolio.PortfolioId;
import org.axonframework.samples.trader.api.users.UserCreatedEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

/**
* <p>Listener that is used to create a new portfolio for each new user that is created.</p>
*
* @author Jettro Coenradie
* Listener that is used to create a new portfolio for each new user that is created.
* TODO #28 might benefit from a cleaner approach still. Think about this
*/
@Component
@Service
public class PortfolioManagementUserListener {

private final static Logger logger = LoggerFactory.getLogger(PortfolioManagementUserListener.class);
private CommandBus commandBus;
private static final Logger logger = LoggerFactory.getLogger(PortfolioManagementUserListener.class);

@EventHandler
public void createNewPortfolioWhenUserIsCreated(UserCreatedEvent event) {
logger.debug("About to dispatch a new command to create a Portfolio for the new user {}",
event.getUserId());
CreatePortfolioCommand command = new CreatePortfolioCommand(new PortfolioId(), event.getUserId());
commandBus.dispatch(new GenericCommandMessage<>(command));
private final CommandGateway commandGateway;

public PortfolioManagementUserListener(CommandGateway commandGateway) {
this.commandGateway = commandGateway;
}

@Autowired
public void setCommandBus(CommandBus commandBus) {
this.commandBus = commandBus;
@EventHandler
public void on(UserCreatedEvent event) {
logger.debug("About to dispatch a new command to create a Portfolio for the new user {}", event.getUserId());
commandGateway.send(new CreatePortfolioCommand(new PortfolioId(), event.getUserId()));
}
}
Expand Up @@ -16,54 +16,49 @@

package org.axonframework.samples.trader.orders.command;

import org.axonframework.commandhandling.CommandBus;
import org.axonframework.commandhandling.GenericCommandMessage;
import org.axonframework.commandhandling.gateway.CommandGateway;
import org.axonframework.samples.trader.api.portfolio.CreatePortfolioCommand;
import org.axonframework.samples.trader.api.users.UserCreatedEvent;
import org.axonframework.samples.trader.api.users.UserId;
import org.junit.Test;
import org.mockito.ArgumentMatcher;
import org.mockito.Matchers;
import org.mockito.Mockito;

/**
* @author Jettro Coenradie
*/
import static org.mockito.Matchers.argThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

public class PortfolioManagementUserListenerTest {

private final CommandGateway commandGateway = mock(CommandGateway.class);

private final PortfolioManagementUserListener listener = new PortfolioManagementUserListener(commandGateway);

@Test
public void checkPortfolioCreationAfterUserCreated() {
CommandBus commandBus = Mockito.mock(CommandBus.class);
PortfolioManagementUserListener listener = new PortfolioManagementUserListener();
listener.setCommandBus(commandBus);

UserId userIdentifier = new UserId();
UserCreatedEvent event = new UserCreatedEvent(userIdentifier, "Test", "testuser", "testpassword");
UserId userId = new UserId();

listener.createNewPortfolioWhenUserIsCreated(event);
listener.on(new UserCreatedEvent(userId, "Test", "testuser", "testpassword"));

Mockito.verify(commandBus).dispatch(Matchers.argThat(new GenericCommandMessageMatcher(userIdentifier)));
verify(commandGateway).send(argThat(new CreatePortfolioCommandMatcher(userId)));
}

private class GenericCommandMessageMatcher extends ArgumentMatcher<GenericCommandMessage> {
// TODO #28 replace this by a direct command equals call. This requires instantiating the aggregate ids ourselves
private class CreatePortfolioCommandMatcher extends ArgumentMatcher<CreatePortfolioCommand> {

private UserId userId;

private GenericCommandMessageMatcher(UserId userId) {
private CreatePortfolioCommandMatcher(UserId userId) {
this.userId = userId;
}

@Override
public boolean matches(Object argument) {
if (!(argument instanceof GenericCommandMessage)) {
if (!(argument instanceof CreatePortfolioCommand)) {
return false;
}
if (!(((GenericCommandMessage) argument).getPayload() instanceof CreatePortfolioCommand)) {
return false;
}
CreatePortfolioCommand createPortfolioCommand = ((GenericCommandMessage<CreatePortfolioCommand>) argument).getPayload();

CreatePortfolioCommand createPortfolioCommand = (CreatePortfolioCommand) argument;
return createPortfolioCommand.getUserId().equals(userId);
}
}

}

0 comments on commit 23dae93

Please sign in to comment.