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()); }
@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); }
/** * Shows the page that signals that a robot was successfully registered a * robot. It will show the robot's token and token secret to use for the * Active API. * * @param robotAccount the newly registered robot account. */ private void onRegisterSuccess(HttpServletRequest req, HttpServletResponse resp, RobotAccountData robotAccount) throws IOException { RobotRegistrationSuccessPage.write(resp.getWriter(), new GxpContext(req.getLocale()), robotAccount.getId().getAddress(), robotAccount.getConsumerSecret(), analyticsAccount); resp.setContentType("text/html"); resp.setStatus(HttpServletResponse.SC_OK); } }
/** * 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 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()); }
/** * Handles an {@link OperationResults} by submitting the deltas it generates * and sending off any events to the robot. Note that currently no events are * send off to the robot. * * @param results the results of the operations performed * @param account the account for which to handle results of robot operations. */ private void handleResults(OperationResults results, RobotAccountData account) { OperationUtil.submitDeltas(results, waveletProvider, LOGGING_REQUEST_LISTENER); // TODO(ljvderijk): In theory we should be sending off all events that are // generated by the operations. Currently not done in production. We should // make it possible though. boolean notifyOnError = account.getCapabilities().getCapabilitiesMap().containsKey(EventType.OPERATION_ERROR); } }
/** * Executes operations in the given context. * * @param context the context to perform the operations in. * @param operations the operations to perform. * @param account the account for which to execute robot operations. */ private void executeOperations( OperationContext context, List<OperationRequest> operations, RobotAccountData account) { for (OperationRequest operation : operations) { // Get the operation of the author taking into account the proxying for // field. OperationUtil.executeOperation(operation, operationRegistry, context, account.getId()); } }
@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); }
new OAuthConsumer(null, participant.getAddress(), account.asRobot().getConsumerSecret(), oauthServiceProvider); OAuthAccessor accessor = new OAuthAccessor(consumer);
public void testUnregisterSucceeds() throws PersistenceException, RobotRegistrationException { when(accountStore.getAccount(ROBOT_ID)).thenReturn(accountData); AccountData unregisteredAccountData = registrar.unregister(ROBOT_ID); assertTrue(unregisteredAccountData.equals(accountData)); verify(accountData).isRobot(); verify(accountStore).removeAccount(ROBOT_ID); }
/** * Constructs a new Robot which is characterized by its {@link RobotName}. * * @param robotName the name of the {@link Robot}. * @param account the {@link RobotAccountData} belonging to this * {@link Robot}. * @param gateway the gateway this robot belongs to. * @param connector the {@link RobotConnector} to make connections to * {@link Robot}s. * @param converterManager used to convert to Robot API objects. * @param waveletProvider used to access wavelets and submit deltas. * @param eventGenerator used to generate events * @param operationApplicator used to apply the robot operations returned by a * robot. */ Robot(RobotName robotName, RobotAccountData account, RobotsGateway gateway, RobotConnector connector, EventDataConverterManager converterManager, WaveletProvider waveletProvider, EventGenerator eventGenerator, RobotOperationApplicator operationApplicator) { Preconditions.checkArgument(account.isVerified(), "Account must be verified"); this.robotName = robotName; this.gateway = gateway; this.connector = connector; this.converterManager = converterManager; this.eventGenerator = eventGenerator; this.operationApplicator = operationApplicator; setAccount(account); }
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()); }
setupOAuth(account.getId().getAddress(), account.getConsumerSecret(), rpcUrl); Wavelet newWelcomeWavelet = newWave(getWaveDomain(), Sets.newHashSet(id.getAddress())); if (welcomeWaveId != null) {
public void testFetchCapabilities() throws Exception { when(connection.get(TEST_CAPABILITIES_ENDPOINT)).thenReturn(CAPABILITIES_XML); RobotAccountData accountData = connector.fetchCapabilities(ROBOT_ACCOUNT, ""); RobotCapabilities capabilities = accountData.getCapabilities(); assertEquals("Expected capabilities hash as specified in the xml", CAPABILITIES_HASH, capabilities.getCapabilitiesHash()); assertEquals("Expected protocol version as specified in the xml", ProtocolVersion.V2_2, capabilities.getProtocolVersion()); Map<EventType, Capability> capabilitiesMap = capabilities.getCapabilitiesMap(); assertTrue("Expected capabilities as specified in the xml", capabilitiesMap.size() == 2); assertTrue("Expected capabilities as specified in the xml", capabilitiesMap.containsKey(EventType.WAVELET_SELF_ADDED)); assertTrue("Expected capabilities as specified in the xml", capabilitiesMap.containsKey(EventType.OPERATION_ERROR)); // Only one connection should be made verify(connection).get(TEST_CAPABILITIES_ENDPOINT); } }
/** * Sets the account for this Robot. The address of the account must match the * address in the {@link RobotName}. * * @param account the account to set. */ void setAccount(RobotAccountData account) { Preconditions.checkArgument(robotName.toEmailAddress().equals(account.getId().getAddress()), String.format("The given RobotAccountData doesn't match the RobotName. %s != %s", account.getId(), robotName.toEmailAddress())); this.account = account; }
/** * 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 testUnregisterSucceeds() throws PersistenceException, RobotRegistrationException { when(accountStore.getAccount(ROBOT_ID)).thenReturn(accountData); AccountData unregisteredAccountData = registrar.unregister(ROBOT_ID); assertTrue(unregisteredAccountData.equals(accountData)); verify(accountData).isRobot(); verify(accountStore).removeAccount(ROBOT_ID); }
if (robotAccount.isVerified()) { Robot robot = getOrCreateRobot(robotName, robotAccount); updateRobot(robot, wavelet, deltas);