@Inject public RobotRegistrarImpl(AccountStore accountStore, TokenGenerator tokenGenerator) { this.accountStore = accountStore; this.tokenGenerator = tokenGenerator; addRegistrationListener(REGISTRATION_EVENTS_LOGGER); }
@Override public RobotAccountData registerOrUpdate(ParticipantId robotId, String location) throws RobotRegistrationException, PersistenceException { Preconditions.checkNotNull(robotId); Preconditions.checkNotNull(location); Preconditions.checkArgument(!location.isEmpty()); AccountData account = accountStore.getAccount(robotId); if (account != null) { throwExceptionIfNotRobot(account); RobotAccountData robotAccount = account.asRobot(); if (robotAccount.getUrl().equals(location)) { return robotAccount; } else { removeRobotAccount(robotAccount); } } return registerRobot(robotId, location); }
@Override public RobotAccountData unregister(ParticipantId robotId) throws RobotRegistrationException, PersistenceException { Preconditions.checkNotNull(robotId); AccountData accountData = accountStore.getAccount(robotId); if (accountData == null) { return null; } throwExceptionIfNotRobot(accountData); RobotAccountData robotAccount = accountData.asRobot(); removeRobotAccount(robotAccount); return robotAccount; }
@Override protected void setUp() throws Exception { accountStore = mock(AccountStore.class); tokenGenerator = mock(TokenGenerator.class); accountData = mock(RobotAccountData.class); when(accountData.isRobot()).thenReturn(true); when(accountData.asRobot()).thenReturn(accountData); when(accountData.getUrl()).thenReturn(LOCATION); when(accountData.getId()).thenReturn(ROBOT_ID); when(tokenGenerator.generateToken(anyInt())).thenReturn(CONSUMER_TOKEN); registrar = new RobotRegistrarImpl(accountStore, tokenGenerator); }
@Override public RobotAccountData registerNew(ParticipantId robotId, String location) throws RobotRegistrationException, PersistenceException { Preconditions.checkNotNull(robotId); Preconditions.checkNotNull(location); Preconditions.checkArgument(!location.isEmpty()); if (accountStore.getAccount(robotId) != null) { throw new RobotRegistrationException(robotId.getAddress() + " is already in use, please choose another one."); } return registerRobot(robotId, location); }
/** * Adds the robot to the account store and notifies the listeners. */ private RobotAccountData registerRobot(ParticipantId robotId, String location) throws RobotRegistrationException, PersistenceException { String robotLocation = computeValidateRobotUrl(location); RobotAccountData robotAccount = new RobotAccountDataImpl(robotId, robotLocation, tokenGenerator.generateToken(TOKEN_LENGTH), null, true); accountStore.putAccount(robotAccount); for (Listener listener : listeners) { listener.onRegistrationSuccess(robotAccount); } return robotAccount; }
@Override protected void setUp() throws Exception { accountStore = mock(AccountStore.class); tokenGenerator = mock(TokenGenerator.class); accountData = mock(RobotAccountData.class); when(accountData.isRobot()).thenReturn(true); when(accountData.asRobot()).thenReturn(accountData); when(accountData.getUrl()).thenReturn(LOCATION); when(accountData.getId()).thenReturn(ROBOT_ID); when(tokenGenerator.generateToken(anyInt())).thenReturn(CONSUMER_TOKEN); registrar = new RobotRegistrarImpl(accountStore, tokenGenerator); }