rpcServerUrl = jsonObj.get(RPC_SERVER_URL_TAG).getAsString(); EventMessageBundle result = new EventMessageBundle(robotAddress, rpcServerUrl); result.setProxyingFor(jsonObj.get(PROXYING_FOR_TAG).getAsString()); operationQueue = new OperationQueue(new ArrayList<OperationRequest>(), result.getProxyingFor()); } else { operationQueue = new OperationQueue(); result.setWaveletData(waveletData); Map<String, Blip> blips = new HashMap<String, Blip>(); Map<String, BlipThread> threads = new HashMap<String, BlipThread>(); Wavelet wavelet = Wavelet.deserialize(operationQueue, blips, threads, waveletData); wavelet.setRobotAddress(robotAddress); result.setWavelet(wavelet); result.setBlipData(blipDatas); for(Entry<String, BlipData> entry : blipDatas.entrySet()) { blips.put(entry.getKey(), Blip.deserialize(operationQueue, wavelet, entry.getValue())); result.setEvents(events); return result;
@SuppressWarnings("unchecked") public void testProcessSendsBundleAndCallsOperationsApplicator() throws Exception { EventMessageBundle messages = new EventMessageBundle(ROBOT_NAME.toEmailAddress(), ""); messages.addEvent(new DocumentChangedEvent(null, null, ALEX.getAddress(), 0L, "b+1234")); when(eventGenerator.generateEvents( any(WaveletAndDeltas.class), anyMap(), any(EventDataConverter.class))).thenReturn(messages); OperationRequest op = new OperationRequest("wavelet.fetch", "op1"); List<OperationRequest> ops = Collections.singletonList(op); when(connector.sendMessageBundle( any(EventMessageBundle.class), eq(robot), any(ProtocolVersion.class))).thenReturn(ops); enqueueEmptyWavelet(); robot.run(); verify(connector).sendMessageBundle( any(EventMessageBundle.class), eq(robot), any(ProtocolVersion.class)); verify(operationApplicator).applyOperations( eq(ops), any(ReadableWaveletData.class), any(HashedVersion.class), eq(INITIALIZED_ACCOUNT)); }
@Override public JsonElement serialize(EventMessageBundle src, Type typeOfSrc, JsonSerializationContext context) { JsonObject result = new JsonObject(); JsonArray events = new JsonArray(); for (Event event : src.getEvents()) { try { events.add(EventSerializer.serialize(event, context)); } catch (EventSerializationException e) { throw new JsonParseException(e); } } result.add(EVENTS_TAG, events); result.add(WAVELET_TAG, context.serialize(src.getWaveletData())); result.add(BLIPS_TAG, context.serialize(src.getBlipData())); result.add(THREADS_TAG, context.serialize(src.getThreads())); result.addProperty(ROBOT_ADDRESS_TAG, src.getRobotAddress()); String proxyingFor = src.getProxyingFor(); if (proxyingFor != null && !proxyingFor.isEmpty()) { result.addProperty(PROXYING_FOR_TAG, proxyingFor); } String rpcServerUrl = src.getRpcServerUrl(); if (rpcServerUrl != null && !rpcServerUrl.isEmpty()) { result.addProperty(RPC_SERVER_URL_TAG, rpcServerUrl); } return result; }
/** * Adds a single {@link ConversationBlip} to the {@link EventMessageBundle}. * * @param eventMessageBundle to add the blip to * @param blip {@link ConversationBlip} to add * @param wavelet {@link Wavelet} that the blip is based on * @param eventDataConverter {@link EventDataConverter} used for conversion */ private static void addBlipToEventMessages(EventMessageBundle eventMessageBundle, ConversationBlip blip, Wavelet wavelet, EventDataConverter eventDataConverter) { if (blip != null && !eventMessageBundle.hasBlipId(blip.getId())) { eventMessageBundle.addBlip( blip.getId(), eventDataConverter.toBlipData(blip, wavelet, eventMessageBundle)); } }
/** * Maps a wavelet and its conversation to a new {@link EventMessageBundle}. * * @param converter to convert to API objects. * @param participant the participant who the bundle is for. * @param wavelet the wavelet to put in the bundle. * @param conversation the conversation to put in the bundle. */ private EventMessageBundle mapWaveletToMessageBundle(EventDataConverter converter, ParticipantId participant, Wavelet wavelet, Conversation conversation) { EventMessageBundle messages = new EventMessageBundle(participant.getAddress(), ""); WaveletData waveletData = converter.toWaveletData(wavelet, conversation, messages); messages.setWaveletData(waveletData); ContextResolver.addAllBlipsToEventMessages(messages, conversation, wavelet, converter); return messages; }
/** * Checks whether events of the given types were put in the bundle. */ private void checkEventTypeWasGenerated(EventMessageBundle messages, EventType... types) { Set<EventType> eventsTypeSet = Sets.newHashSet(); for (EventType eventType : types) { eventsTypeSet.add(eventType); } for (Event event : messages.getEvents()) { if (eventsTypeSet.contains(event.getType())) { eventsTypeSet.remove(event.getType()); } } if (eventsTypeSet.size() != 0) { fail("Event of type " + eventsTypeSet.iterator().next() + " has not been generated"); } }
"blip1"); EventMessageBundle bundle = new EventMessageBundle("Foo", "http://gmodules.com/api/rpc"); bundle.addEvent(event1); bundle.addEvent(event2); bundle.addEvent(event3); bundle.setWaveletData(waveletData); String json = new GsonFactory().create().toJson(bundle);
public void testResolveContext() throws Exception { eventMessages.requireBlip( conversation.getRootThread().getFirstBlip().getId(), Lists.newArrayList(Context.CHILDREN)); ContextResolver.resolveContext( eventMessages, wavelet, conversation, new EventDataConverterV21()); Set<String> blips = eventMessages.getBlipData().keySet(); assertEquals(2, blips.size()); for (ConversationBlip blip : conversation.getRootThread().getBlips()) { assertTrue(blips.contains(blip.getId())); } }
@Override public EventMessageBundle createInstance(Type type) { return new EventMessageBundle("", "http://opensocial.example.com"); }
public void testToBlipData() throws Exception { Wavelet wavelet = mock(Wavelet.class); when(wavelet.getWaveId()).thenReturn(WAVE_ID); when(wavelet.getId()).thenReturn(WAVELET_ID); ConversationBlip blip = conversation.getRootThread().getFirstBlip(); String replyThreadId = blip.addReplyThread(3).getId(); EventDataConverterV22 converter = new EventDataConverterV22(); EventMessageBundle eventMessageBundle = new EventMessageBundle(null, null); BlipData blipData = converter.toBlipData(blip, wavelet, eventMessageBundle); assertEquals(blip.getThread().getId(), blipData.getThreadId()); assertEquals(Lists.newArrayList(replyThreadId), blipData.getReplyThreadIds()); Map<String, BlipThread> threads = eventMessageBundle.getThreads(); assertEquals(1, threads.size()); assertEquals(1, threads.get(replyThreadId).getLocation()); }
@Override public void execute(OperationRequest operation, OperationContext context, ParticipantId participant) throws InvalidRequestException { if (OperationUtil.<Boolean>getOptionalParameter(operation, ParamsProperty.RETURN_WAVELET_IDS, false)) { ImmutableSet<WaveletId> waveletIds = context.getVisibleWaveletIds(operation, participant); Map<ParamsProperty, Object> data = ImmutableMap.<ParamsProperty, Object>of(ParamsProperty.WAVELET_IDS, waveletIds); context.constructResponse(operation, data); } else { OpBasedWavelet wavelet = context.openWavelet(operation, participant); ObservableConversation conversation = context.openConversation(operation, participant).getRoot(); EventMessageBundle messages = mapWaveletToMessageBundle(context.getConverter(), participant, wavelet, conversation); String rootBlipId = ConversationUtil.getRootBlipId(conversation); String message = OperationUtil.getOptionalParameter(operation, ParamsProperty.MESSAGE); WaveletFetchedEvent event = new WaveletFetchedEvent(null, null, participant.getAddress(), System.currentTimeMillis(), message, rootBlipId, messages.getWaveletData(), messages.getBlipData(), messages.getThreads()); context.processEvent(operation, event); } }
contexts = capability.getContexts(); messages.requireBlip(blipId, contexts); messages.addEvent(event);
EventMessageBundle messages = new EventMessageBundle(robotName.toEmailAddress(), ""); ObservableWaveletData snapshot = WaveletDataUtil.copyWavelet(waveletAndDeltas.getSnapshotBeforeDeltas()); messages.setProxyingFor(robotName.getProxyFor()); if (messages.getEvents().isEmpty()) {
eventMessageBundle.setWaveletData(waveletData); for (Map.Entry<String, Set<Context>> entry : eventMessageBundle.getRequiredBlips().entrySet()) { Set<Context> contextSet = entry.getValue(); ConversationBlip requiredBlip = conversation.getBlip(entry.getKey());
int location, Wavelet wavelet) { String threadId = thread.getId(); if (eventMessageBundle.hasThreadId(threadId)) { blipIds.add(blip.getId()); eventMessageBundle.addThread(threadId, new BlipThread(thread.getId(), location, blipIds, null));
/** * Checks whether events of the given types were put in the bundle. */ private void checkEventTypeWasGenerated(EventMessageBundle messages, EventType... types) { Set<EventType> eventsTypeSet = Sets.newHashSet(); for (EventType eventType : types) { eventsTypeSet.add(eventType); } for (Event event : messages.getEvents()) { if (eventsTypeSet.contains(event.getType())) { eventsTypeSet.remove(event.getType()); } } if (eventsTypeSet.size() != 0) { fail("Event of type " + eventsTypeSet.iterator().next() + " has not been generated"); } }
"blip1"); EventMessageBundle bundle = new EventMessageBundle("Foo", "http://gmodules.com/api/rpc"); bundle.addEvent(event1); bundle.addEvent(event2); bundle.addEvent(event3); bundle.setWaveletData(waveletData); String json = new GsonFactory().create().toJson(bundle);
public void testResolveContext() throws Exception { eventMessages.requireBlip( conversation.getRootThread().getFirstBlip().getId(), Lists.newArrayList(Context.CHILDREN)); ContextResolver.resolveContext( eventMessages, wavelet, conversation, new EventDataConverterV21()); Set<String> blips = eventMessages.getBlipData().keySet(); assertEquals(2, blips.size()); for (ConversationBlip blip : conversation.getRootThread().getBlips()) { assertTrue(blips.contains(blip.getId())); } }
@Override protected void setUp() throws Exception { super.setUp(); Blips.init(); conversation = makeConversation(); wavelet = createMockWavelet(conversation); eventMessages = new EventMessageBundle("foo@appspot.com", "http://opensocial.example.com"); }
public void testToBlipData() throws Exception { Wavelet wavelet = mock(Wavelet.class); when(wavelet.getWaveId()).thenReturn(WAVE_ID); when(wavelet.getId()).thenReturn(WAVELET_ID); ConversationBlip blip = conversation.getRootThread().getFirstBlip(); String replyThreadId = blip.addReplyThread(3).getId(); EventDataConverterV22 converter = new EventDataConverterV22(); EventMessageBundle eventMessageBundle = new EventMessageBundle(null, null); BlipData blipData = converter.toBlipData(blip, wavelet, eventMessageBundle); assertEquals(blip.getThread().getId(), blipData.getThreadId()); assertEquals(Lists.newArrayList(replyThreadId), blipData.getReplyThreadIds()); Map<String, BlipThread> threads = eventMessageBundle.getThreads(); assertEquals(1, threads.size()); assertEquals(1, threads.get(replyThreadId).getLocation()); }