@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; }
String opId = operation.getId(); JsonRpcResponse response = results.getResponses().get(opId); responses.addLast(response);
/** * Fetches multiple profiles using Data API. */ private FetchProfilesResult fetchProfilesFromService(ParticipantId user, List<String> addresses) { OperationQueue opQueue = new OperationQueue(); FetchProfilesRequest request = new FetchProfilesRequest(addresses); opQueue.fetchProfiles(request); OperationContextImpl context = new OperationContextImpl(waveletProvider, converterManager.getEventDataConverter(ProtocolVersion.DEFAULT), conversationUtil); OperationRequest operationRequest = opQueue.getPendingOperations().get(0); String opId = operationRequest.getId(); OperationUtil.executeOperation(operationRequest, operationRegistry, context, user); JsonRpcResponse jsonRpcResponse = context.getResponses().get(opId); FetchProfilesResult profileResults = (FetchProfilesResult) jsonRpcResponse.getData().get(ParamsProperty.FETCH_PROFILES_RESULT); return profileResults; }
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 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()); } }
/** * Performs search using Data API. */ protected SearchResult performSearch(SearchRequest searchRequest, ParticipantId user) { OperationQueue opQueue = new OperationQueue(); opQueue.search(searchRequest.getQuery(), searchRequest.getIndex(), searchRequest.getNumResults()); OperationContextImpl context = new OperationContextImpl(waveletProvider, converterManager.getEventDataConverter(ProtocolVersion.DEFAULT), conversationUtil); LOG.fine( "Performing query: " + searchRequest.getQuery() + " [" + searchRequest.getIndex() + ", " + (searchRequest.getIndex() + searchRequest.getNumResults()) + "]"); OperationRequest operationRequest = opQueue.getPendingOperations().get(0); String opId = operationRequest.getId(); OperationUtil.executeOperation(operationRequest, operationRegistry, context, user); JsonRpcResponse jsonRpcResponse = context.getResponses().get(opId); SearchResult searchResult = (SearchResult) jsonRpcResponse.getData().get(ParamsProperty.SEARCH_RESULTS); return searchResult; } }
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 testConstructResponse() { Map<ParamsProperty, Object> data = Maps.newHashMap(); data.put(ParamsProperty.PARTICIPANT_ID, USERNAME); operationContext.constructResponse(request, data); JsonRpcResponse response = operationContext.getResponse(request.getId()); assertFalse("Expected non-error response", response.isError()); assertEquals("Expected operation id not to change", OPERATION_ID, response.getId()); assertEquals("Expected payload not to change", data, response.getData()); }
public void testConstructResponse() { Map<ParamsProperty, Object> data = Maps.newHashMap(); data.put(ParamsProperty.PARTICIPANT_ID, USERNAME); operationContext.constructResponse(request, data); JsonRpcResponse response = operationContext.getResponse(request.getId()); assertFalse("Expected non-error response", response.isError()); assertEquals("Expected operation id not to change", OPERATION_ID, response.getId()); assertEquals("Expected payload not to change", data, response.getData()); }
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 testProcessEvent() throws Exception { // A randomly selected non-error event Event event = new WaveletBlipCreatedEvent(null, null, USERNAME, 0L, "root", "newBlip"); operationContext.processEvent(request, event); JsonRpcResponse response = operationContext.getResponse(request.getId()); assertFalse("Expected non-error response", response.isError()); assertEquals("Expected operation id not to change", OPERATION_ID, response.getId()); }
public void testProcessEvent() throws Exception { // A randomly selected non-error event Event event = new WaveletBlipCreatedEvent(null, null, USERNAME, 0L, "root", "newBlip"); operationContext.processEvent(request, event); JsonRpcResponse response = operationContext.getResponse(request.getId()); assertFalse("Expected non-error response", response.isError()); assertEquals("Expected operation id not to change", OPERATION_ID, response.getId()); }
public void testConstructErrorResponse() { operationContext.constructErrorResponse(request, ERROR_MESSAGE); JsonRpcResponse response = operationContext.getResponse(request.getId()); assertTrue("Expected error response", response.isError()); assertEquals("Expected provided error message", ERROR_MESSAGE, response.getErrorMessage()); assertEquals("Expected operation id not to change", OPERATION_ID, response.getId()); }
public void testConstructErrorResponse() { operationContext.constructErrorResponse(request, ERROR_MESSAGE); JsonRpcResponse response = operationContext.getResponse(request.getId()); assertTrue("Expected error response", response.isError()); assertEquals("Expected provided error message", ERROR_MESSAGE, response.getErrorMessage()); assertEquals("Expected operation id not to change", OPERATION_ID, response.getId()); }
public void testProcessErrorEvent() throws Exception { // A randomly selected non-error event Event event = new OperationErrorEvent(null, null, USERNAME, 0L, OPERATION_ID, ERROR_MESSAGE); operationContext.processEvent(request, event); JsonRpcResponse response = operationContext.getResponse(request.getId()); assertTrue("Expected error response", response.isError()); assertEquals("Expected provided error message", ERROR_MESSAGE, response.getErrorMessage()); assertEquals("Expected operation id not to change", OPERATION_ID, response.getId()); }
public void testProcessErrorEvent() throws Exception { // A randomly selected non-error event Event event = new OperationErrorEvent(null, null, USERNAME, 0L, OPERATION_ID, ERROR_MESSAGE); operationContext.processEvent(request, event); JsonRpcResponse response = operationContext.getResponse(request.getId()); assertTrue("Expected error response", response.isError()); assertEquals("Expected provided error message", ERROR_MESSAGE, response.getErrorMessage()); assertEquals("Expected operation id not to change", OPERATION_ID, response.getId()); }