/** * @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 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 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; }
/** * 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 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()); }
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()); }
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)); } }
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)); } }
public void testBlindWavelet() throws Exception { AbstractRobot robot = new MockRobot(); Wavelet blindWavelet = robot.blindWavelet(WAVE_1, WAVELET_1); assertEquals(0, blindWavelet.getOperationQueue().getPendingOperations().size()); blindWavelet.getParticipants().add("foo@test.com"); blindWavelet.reply("\n"); assertEquals(2, blindWavelet.getOperationQueue().getPendingOperations().size()); assertEquals(OperationType.WAVELET_ADD_PARTICIPANT_NEWSYNTAX.method(), blindWavelet.getOperationQueue().getPendingOperations().get(0).getMethod()); assertEquals(OperationType.WAVELET_APPEND_BLIP.method(), blindWavelet.getOperationQueue().getPendingOperations().get(1).getMethod()); }
public void testBlindWavelet() throws Exception { AbstractRobot robot = new MockRobot(); Wavelet blindWavelet = robot.blindWavelet(WAVE_1, WAVELET_1); assertEquals(0, blindWavelet.getOperationQueue().getPendingOperations().size()); blindWavelet.getParticipants().add("foo@test.com"); blindWavelet.reply("\n"); assertEquals(2, blindWavelet.getOperationQueue().getPendingOperations().size()); assertEquals(OperationType.WAVELET_ADD_PARTICIPANT_NEWSYNTAX.method(), blindWavelet.getOperationQueue().getPendingOperations().get(0).getMethod()); assertEquals(OperationType.WAVELET_APPEND_BLIP.method(), blindWavelet.getOperationQueue().getPendingOperations().get(1).getMethod()); }
public void testProxyFor() throws Exception { OperationQueue queue = new OperationQueue(); queue.createWavelet("google.com", Collections.<String>emptySet()); OperationQueue proxyingForQueue = queue.proxyFor("foo"); assertEquals(1, proxyingForQueue.getPendingOperations().size()); proxyingForQueue.setTitleOfWavelet(wavelet, "My title"); List<OperationRequest> ops = proxyingForQueue.getPendingOperations(); assertEquals(2, ops.size()); assertEquals(OperationType.ROBOT_CREATE_WAVELET.method(), ops.get(0).getMethod()); assertNull(ops.get(0).getParameter(ParamsProperty.PROXYING_FOR)); assertEquals(OperationType.WAVELET_SET_TITLE.method(), ops.get(1).getMethod()); assertEquals("foo", ops.get(1).getParameter(ParamsProperty.PROXYING_FOR)); OperationQueue nonProxyingForQueue = proxyingForQueue.proxyFor(null); nonProxyingForQueue.fetchWavelet(WaveId.of("example.com", "wave2"), WaveletId.of("example.com", "wavelet2")); ops = nonProxyingForQueue.getPendingOperations(); assertEquals(3, ops.size()); assertNull(ops.get(2).getParameter(ParamsProperty.PROXYING_FOR)); }
public void testProxyFor() throws Exception { OperationQueue queue = new OperationQueue(); queue.createWavelet("google.com", Collections.<String>emptySet()); OperationQueue proxyingForQueue = queue.proxyFor("foo"); assertEquals(1, proxyingForQueue.getPendingOperations().size()); proxyingForQueue.setTitleOfWavelet(wavelet, "My title"); List<OperationRequest> ops = proxyingForQueue.getPendingOperations(); assertEquals(2, ops.size()); assertEquals(OperationType.ROBOT_CREATE_WAVELET.method(), ops.get(0).getMethod()); assertNull(ops.get(0).getParameter(ParamsProperty.PROXYING_FOR)); assertEquals(OperationType.WAVELET_SET_TITLE.method(), ops.get(1).getMethod()); assertEquals("foo", ops.get(1).getParameter(ParamsProperty.PROXYING_FOR)); OperationQueue nonProxyingForQueue = proxyingForQueue.proxyFor(null); nonProxyingForQueue.fetchWavelet(WaveId.of("example.com", "wave2"), WaveletId.of("example.com", "wavelet2")); ops = nonProxyingForQueue.getPendingOperations(); assertEquals(3, ops.size()); assertNull(ops.get(2).getParameter(ParamsProperty.PROXYING_FOR)); }
public void testProxiedBlindWavelet() throws Exception { AbstractRobot robot = new MockRobot(); Wavelet blindWavelet = robot.blindWavelet(WAVE_1, WAVELET_1, "proxyid"); assertEquals(0, blindWavelet.getOperationQueue().getPendingOperations().size()); blindWavelet.reply("\n"); List<OperationRequest> ops = blindWavelet.getOperationQueue().getPendingOperations(); assertEquals(1, ops.size()); assertEquals(OperationType.WAVELET_APPEND_BLIP.method(), ops.get(0).getMethod()); assertEquals("proxyid", ops.get(0).getParameter(ParamsProperty.PROXYING_FOR)); // Assert that proxy id should be valid. try { robot.blindWavelet(WAVE_1, WAVELET_1, "foo@bar.com"); fail("Should have failed since proxy id is not valid."); } catch (IllegalArgumentException e) { // Expected. } }
public void testProxiedBlindWavelet() throws Exception { AbstractRobot robot = new MockRobot(); Wavelet blindWavelet = robot.blindWavelet(WAVE_1, WAVELET_1, "proxyid"); assertEquals(0, blindWavelet.getOperationQueue().getPendingOperations().size()); blindWavelet.reply("\n"); List<OperationRequest> ops = blindWavelet.getOperationQueue().getPendingOperations(); assertEquals(1, ops.size()); assertEquals(OperationType.WAVELET_APPEND_BLIP.method(), ops.get(0).getMethod()); assertEquals("proxyid", ops.get(0).getParameter(ParamsProperty.PROXYING_FOR)); // Assert that proxy id should be valid. try { robot.blindWavelet(WAVE_1, WAVELET_1, "foo@bar.com"); fail("Should have failed since proxy id is not valid."); } catch (IllegalArgumentException e) { // Expected. } }