@Override public void onUnregistrationSuccess(RobotAccountData account) { log.info("Unregistered robot: " + account.getId().getAddress() + " at " + account.getUrl()); } };
@Override public void onRegistrationSuccess(RobotAccountData account) { log.info("Registered robot: " + account.getId().getAddress() + " at " + account.getUrl()); }
@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 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 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); }
private DBObject robotToObject(RobotAccountData account) { return new BasicDBObject() .append(ROBOT_URL_FIELD, account.getUrl()) .append(ROBOT_SECRET_FIELD, account.getConsumerSecret()) .append(ROBOT_CAPABILITIES_FIELD, capabilitiesToObject(account.getCapabilities())) .append(ROBOT_VERIFIED_FIELD, account.isVerified()); }
/** * Returns a new {@link RobotAccountData} updated with the new capabilities * using the given {@link RobotAccountData}. * * @param account The {@link RobotAccountData} to update the capabilities for. * @param activeApiUrl the url of the Active Robot API. * @throws CapabilityFetchException if the capabilities couldn't be retrieved * or parsed. */ public RobotAccountData fetchCapabilities(RobotAccountData account, String activeApiUrl) throws CapabilityFetchException { RobotCapabilitiesParser parser = new RobotCapabilitiesParser( account.getUrl() + Robot.CAPABILITIES_URL, connection, activeApiUrl); RobotCapabilities capabilities = new RobotCapabilities( parser.getCapabilities(), parser.getCapabilitiesHash(), parser.getProtocolVersion()); return new RobotAccountDataImpl(account.getId(), account.getUrl(), account.getConsumerSecret(), capabilities, account.isVerified()); } }
public void testRegisterNewSucceeds() throws PersistenceException, RobotRegistrationException { RobotAccountData resultAccountData = registrar.registerNew(ROBOT_ID, LOCATION); verify(accountStore, atLeastOnce()).getAccount(ROBOT_ID); verify(accountStore).putAccount(any(RobotAccountData.class)); verify(tokenGenerator).generateToken(anyInt()); assertTrue(resultAccountData.isRobot()); RobotAccountData robotAccountData = resultAccountData.asRobot(); // Remove the last '/'. assertEquals(LOCATION.substring(0, LOCATION.length() - 1), robotAccountData.getUrl()); assertEquals(ROBOT_ID, robotAccountData.getId()); assertEquals(CONSUMER_TOKEN, robotAccountData.getConsumerSecret()); }
public void testRegisterNewSucceeds() throws PersistenceException, RobotRegistrationException { RobotAccountData resultAccountData = registrar.registerNew(ROBOT_ID, LOCATION); verify(accountStore, atLeastOnce()).getAccount(ROBOT_ID); verify(accountStore).putAccount(any(RobotAccountData.class)); verify(tokenGenerator).generateToken(anyInt()); assertTrue(resultAccountData.isRobot()); RobotAccountData robotAccountData = resultAccountData.asRobot(); // Remove the last '/'. assertEquals(LOCATION.substring(0, LOCATION.length() - 1), robotAccountData.getUrl()); assertEquals(ROBOT_ID, robotAccountData.getId()); assertEquals(CONSUMER_TOKEN, robotAccountData.getConsumerSecret()); }
private static ProtoRobotAccountData serialize(RobotAccountData account) { Preconditions.checkNotNull(account, "account is null"); ProtoRobotAccountData.Builder builder = ProtoRobotAccountData.newBuilder(); builder.setUrl(account.getUrl()); builder.setConsumerSecret(account.getConsumerSecret()); builder.setIsVerified(account.isVerified()); if (account.getCapabilities() != null) { builder.setRobotCapabilities(serialize(account.getCapabilities())); } return builder.build(); }
public void testReRegisterSucceedsOnExistingRobotAccount() throws PersistenceException, RobotRegistrationException { when(accountStore.getAccount(ROBOT_ID)).thenReturn(accountData); AccountData unregisteredAccountData = registrar.registerOrUpdate(ROBOT_ID, OTHER_LOCATION); verify(accountStore).removeAccount(ROBOT_ID); verify(accountStore).putAccount(any(RobotAccountData.class)); assertTrue(unregisteredAccountData.isRobot()); RobotAccountData robotAccountData = unregisteredAccountData.asRobot(); // Remove the last '/'. assertEquals(OTHER_LOCATION.substring(0, OTHER_LOCATION.length() - 1), robotAccountData.getUrl()); assertEquals(ROBOT_ID, robotAccountData.getId()); assertEquals(CONSUMER_TOKEN, robotAccountData.getConsumerSecret()); }
/** * Synchronously sends an {@link EventMessageBundle} off to a robot and hands * back the response in the form of a list of {@link OperationRequest}s. * * @param bundle the bundle to send to the robot. * @param robot the {@link RobotAccountData} of the robot. * @param version the version that we should speak to the robot. * @returns list of {@link OperationRequest}s that the robot wants to have * executed. */ public List<OperationRequest> sendMessageBundle( EventMessageBundle bundle, Robot robot, ProtocolVersion version) { String serializedBundle = serializer.serialize(bundle, version); String robotUrl = robot.getAccount().getUrl() + Robot.RPC_URL; LOG.info("Sending: " + serializedBundle + " to " + robotUrl); try { String response = connection.postJson(robotUrl, serializedBundle); LOG.info("Received: " + response + " from " + robotUrl); return serializer.deserializeOperations(response); } catch (RobotConnectionException e) { LOG.info("Failed to receive a response from " + robotUrl, e); } catch (InvalidRequestException e) { LOG.info("Failed to deserialize passive API response", e); } // Return an empty list and let the caller ignore the failure return Collections.emptyList(); }
public void testReRegisterSucceedsOnExistingRobotAccount() throws PersistenceException, RobotRegistrationException { when(accountStore.getAccount(ROBOT_ID)).thenReturn(accountData); AccountData unregisteredAccountData = registrar.registerOrUpdate(ROBOT_ID, OTHER_LOCATION); verify(accountStore).removeAccount(ROBOT_ID); verify(accountStore).putAccount(any(RobotAccountData.class)); assertTrue(unregisteredAccountData.isRobot()); RobotAccountData robotAccountData = unregisteredAccountData.asRobot(); // Remove the last '/'. assertEquals(OTHER_LOCATION.substring(0, OTHER_LOCATION.length() - 1), robotAccountData.getUrl()); assertEquals(ROBOT_ID, robotAccountData.getId()); assertEquals(CONSUMER_TOKEN, robotAccountData.getConsumerSecret()); }