public void testExecuteOperationCallsExecute() throws Exception { String operationId = "op1"; OperationRequest operation = new OperationRequest("wavelet.create", operationId); OperationService service = mock(OperationService.class); when(operationRegistry.getServiceFor(any(OperationType.class))).thenReturn(service); OperationUtil.executeOperation(operation, operationRegistry, context, ALEX); verify(service).execute(operation, context, ALEX); }
@Override public String toString() { return String.format("{'method':'%s','id':'%s','waveId':'%s','waveletId':'%s','blipId':'%s'," + "'parameters':%s}", method, id, getWaveId(), getWaveletId(), getBlipId(), parameters); } }
@Override public JsonElement serialize(OperationRequest req, Type type, JsonSerializationContext ctx) { JsonObject object = new JsonObject(); object.addProperty(RequestProperty.METHOD.key(), operationNamespace + req.getMethod()); object.addProperty(RequestProperty.ID.key(), req.getId()); JsonObject parameters = new JsonObject(); for (Entry<ParamsProperty, Object> entry : req.getParams().entrySet()) { if (entry.getValue() != null) { parameters.add(entry.getKey().key(), ctx.serialize(entry.getValue())); } } object.add(RequestProperty.PARAMS.key(), parameters); return object; }
/** * Determines the protocol version of a given operation bundle by inspecting * the first operation in the bundle. If it is a {@code robot.notify} * operation, and contains {@code protocolVersion} parameter, then this method * will return the value of that parameter. Otherwise, this method will return * the default version. * * @param operationBundle the operation bundle to check. * @return the wire protocol version of the given operation bundle. */ private ProtocolVersion determineProtocolVersion(List<OperationRequest> operationBundle) { if (operationBundle.size() == 0) { return defaultProtocolVersion; } OperationRequest firstOperation = operationBundle.get(0); if (isRobotNotifyOperationMethod(firstOperation.getMethod())) { String versionString = (String) firstOperation.getParameter(ParamsProperty.PROTOCOL_VERSION); if (versionString != null) { return ProtocolVersion.fromVersionString(versionString); } } return defaultProtocolVersion; }
public void testDeserialize() throws Exception { String operation = "{'id':'op1','method':'wavelet.setTitle','params':{" + "'waveId':'1','waveletId':'2','waveletTitle':'Title','unknown':'value'}}"; JsonElement jsonElement = new JsonParser().parse(operation); JsonDeserializationContext mockContext = mock(JsonDeserializationContext.class); when(mockContext.deserialize(any(JsonElement.class), eq(String.class))).thenAnswer( new Answer<String>() { public String answer(InvocationOnMock invocation) { return ((JsonPrimitive) (invocation.getArguments()[0])).getAsString(); } }); OperationRequestGsonAdaptor adaptor = new OperationRequestGsonAdaptor(); OperationRequest result = adaptor.deserialize(jsonElement, null, mockContext); assertEquals("op1", result.getId()); assertEquals("wavelet.setTitle", result.getMethod()); assertEquals("1", result.getWaveId()); assertEquals("2", result.getWaveletId()); assertNull(result.getBlipId()); assertEquals(3, result.getParams().size()); assertEquals("Title", result.getParameter(ParamsProperty.WAVELET_TITLE)); } }
/** * Returns the wave id where this request should be invoked on. This might not * be applicable to all requests. * * @return the wave id. */ public String getWaveId() { return (String) getParameter(ParamsProperty.WAVE_ID); }
public void testReturnsEmptyResponse() throws Exception { // Type of operation doesn't matter in this case OperationRequest request = new OperationRequest("wavelet.fetch", "op1"); WaveletProvider waveletProvider = mock(WaveletProvider.class); EventDataConverter converter = mock(EventDataConverter.class); ConversationUtil conversationUtil = mock(ConversationUtil.class); OperationContextImpl context = new OperationContextImpl(waveletProvider, converter, conversationUtil); operationService.execute(request, context, BOB); JsonRpcResponse response = context.getResponse(request.getId()); assertFalse("Expected non error response", response.isError()); assertTrue("Empty Response must be set", response.getData().isEmpty()); } }
/** * @return the type of operation present in the request */ public static OperationType getOperationType(OperationRequest operation) { String methodName = operation.getMethod(); // TODO(ljvderijk): This might be removed after the deserialization is fixed if (methodName.startsWith("wave.")) { methodName = methodName.replaceFirst("^wave[.]", ""); } return OperationType.fromMethodName(methodName); }
@Override public OperationRequest deserialize(JsonElement json, Type type, JsonDeserializationContext ctx) throws JsonParseException { JsonObject jsonObject = json.getAsJsonObject(); JsonObject parameters = jsonObject.getAsJsonObject(RequestProperty.PARAMS.key()); OperationRequest request = new OperationRequest( jsonObject.get(RequestProperty.METHOD.key()).getAsString(), jsonObject.get(RequestProperty.ID.key()).getAsString(), getPropertyAsStringThenRemove(parameters, ParamsProperty.WAVE_ID), getPropertyAsStringThenRemove(parameters, ParamsProperty.WAVELET_ID), getPropertyAsStringThenRemove(parameters, ParamsProperty.BLIP_ID)); for (Entry<String, JsonElement> parameter : parameters.entrySet()) { ParamsProperty parameterType = ParamsProperty.fromKey(parameter.getKey()); if (parameterType != null) { Object object; if (parameterType == ParamsProperty.RAW_DELTAS) { object = ctx.deserialize(parameter.getValue(), GsonFactory.RAW_DELTAS_TYPE); } else { object = ctx.deserialize(parameter.getValue(), parameterType.clazz()); } request.addParameter(Parameter.of(parameterType, object)); } } return request; }
/** * Creates a new wavelet with an optional message. * * @param domain the domain to create the wavelet in. * @param participants the initial participants on this new wavelet. * @param message an optional payload that is returned with the corresponding * event. * @return an instance of {@link Wavelet} that represents the new wavelet. */ public Wavelet createWavelet(String domain, Set<String> participants, String message) { Wavelet newWavelet = newWavelet(domain, participants, this); OperationRequest operation = appendOperation(OperationType.ROBOT_CREATE_WAVELET, newWavelet, Parameter.of(ParamsProperty.WAVELET_DATA, newWavelet.serialize())); // Don't add the message if it's null or empty. if (message != null && !message.isEmpty()) { operation.addParameter(Parameter.of(ParamsProperty.MESSAGE, message)); } return newWavelet; }
public void testDeserialize() throws Exception { String operation = "{'id':'op1','method':'wavelet.setTitle','params':{" + "'waveId':'1','waveletId':'2','waveletTitle':'Title','unknown':'value'}}"; JsonElement jsonElement = new JsonParser().parse(operation); JsonDeserializationContext mockContext = mock(JsonDeserializationContext.class); when(mockContext.deserialize(any(JsonElement.class), eq(String.class))).thenAnswer( new Answer<String>() { public String answer(InvocationOnMock invocation) { return ((JsonPrimitive) (invocation.getArguments()[0])).getAsString(); } }); OperationRequestGsonAdaptor adaptor = new OperationRequestGsonAdaptor(); OperationRequest result = adaptor.deserialize(jsonElement, null, mockContext); assertEquals("op1", result.getId()); assertEquals("wavelet.setTitle", result.getMethod()); assertEquals("1", result.getWaveId()); assertEquals("2", result.getWaveletId()); assertNull(result.getBlipId()); assertEquals(3, result.getParams().size()); assertEquals("Title", result.getParameter(ParamsProperty.WAVELET_TITLE)); } }
/** * Returns the wavelet id where this request should be invoked on. This might * not be applicable to all requests. * * @return the wavelet id. */ public String getWaveletId() { return (String) getParameter(ParamsProperty.WAVELET_ID); }
/** * Determines the protocol version of a given operation bundle by inspecting * the first operation in the bundle. If it is a {@code robot.notify} * operation, and contains {@code protocolVersion} parameter, then this method * will return the value of that parameter. Otherwise, this method will return * the default version. * * @param operations the {@link OperationRequest}s to inspect. * @return the wire protocol version of the given operation bundle. */ public static ProtocolVersion getProtocolVersion(List<OperationRequest> operations) { if (operations.size() == 0) { return ProtocolVersion.DEFAULT; } OperationRequest firstOperation = operations.get(0); if (firstOperation.getMethod().equals(OperationType.ROBOT_NOTIFY.method())) { String versionString = (String) firstOperation.getParameter(ParamsProperty.PROTOCOL_VERSION); if (versionString != null) { return ProtocolVersion.fromVersionString(versionString); } } return ProtocolVersion.DEFAULT; }
public void testReturnsEmptyResponse() throws Exception { // Type of operation doesn't matter in this case OperationRequest request = new OperationRequest("wavelet.fetch", "op1"); WaveletProvider waveletProvider = mock(WaveletProvider.class); EventDataConverter converter = mock(EventDataConverter.class); ConversationUtil conversationUtil = mock(ConversationUtil.class); OperationContextImpl context = new OperationContextImpl(waveletProvider, converter, conversationUtil); operationService.execute(request, context, BOB); JsonRpcResponse response = context.getResponse(request.getId()); assertFalse("Expected non error response", response.isError()); assertTrue("Empty Response must be set", response.getData().isEmpty()); } }
public void testSubmitWith() throws Exception { OperationQueue queue = new OperationQueue(); queue.createWavelet("example.com", Collections.<String>emptySet()); OperationQueue queue2 = new OperationQueue(); queue2.setTitleOfWavelet(wavelet, "My title"); queue2.submitWith(queue); List<OperationRequest> ops = queue.getPendingOperations(); assertEquals(2, ops.size()); assertEquals(OperationType.ROBOT_CREATE_WAVELET.method(), ops.get(0).getMethod()); assertEquals(OperationType.WAVELET_SET_TITLE.method(), ops.get(1).getMethod()); assertEquals(queue.getPendingOperations(), queue2.getPendingOperations()); }
OperationRequest operation = new OperationRequest(opType.method(), String.format(OP_ID_FORMAT, nextOpId++), waveIdString, waveletIdString, blipId, parameters); operation.addParameter(Parameter.of(ParamsProperty.PROXYING_FOR, proxyForId));
op.addParameter(parameter); op.addParameter(Parameter.of(ParamsProperty.MODIFY_ACTION, new DocumentModifyAction( modifyHow, values, annotationName, elements, bundledAnnotations, useMarkup)));
public void testExecuteOperationCallsExecute() throws Exception { String operationId = "op1"; OperationRequest operation = new OperationRequest("wavelet.create", operationId); OperationService service = mock(OperationService.class); when(operationRegistry.getServiceFor(any(OperationType.class))).thenReturn(service); OperationUtil.executeOperation(operation, operationRegistry, context, ALEX); verify(service).execute(operation, context, ALEX); }
/** * Returns the blip id where this request should be invoked on. This might not * be applicable to all requests. * * @return the blip id. */ public String getBlipId() { return (String) getParameter(ParamsProperty.BLIP_ID); }