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); }
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); }
public void testDoPostExecutesAndWritesResponse() throws Exception { when(req.getHeaders("Authorization")).thenReturn(convertRawEnumerationToGeneric( generateOAuthHeader(ROBOT.getAddress()))); String operationId = "op1"; OperationRequest operation = new OperationRequest("wavelet.create", operationId); List<OperationRequest> operations = Collections.singletonList(operation); when(robotSerializer.deserializeOperations(anyString())).thenReturn(operations); String responseValue = "response value"; when(robotSerializer.serialize(any(), any(Type.class), any(ProtocolVersion.class))).thenReturn( responseValue); OperationService service = mock(OperationService.class); when(operationRegistry.getServiceFor(any(OperationType.class))).thenReturn(service); servlet.doPost(req, resp); verify(operationRegistry).getServiceFor(any(OperationType.class)); verify(service).execute(eq(operation), any(OperationContext.class), eq(ROBOT)); verify(resp).setStatus(HttpServletResponse.SC_OK); assertEquals("Response should have been written into the servlet", responseValue, outputWriter.toString()); }
public void testDoPostExecutesAndWritesResponse() throws Exception { String operationId = "op1"; OperationRequest operation = new OperationRequest("wavelet.create", operationId); List<OperationRequest> operations = Collections.singletonList(operation); when(robotSerializer.deserializeOperations(anyString())).thenReturn(operations); String responseValue = "response value"; when(robotSerializer.serialize(any(), any(Type.class), any(ProtocolVersion.class))).thenReturn( responseValue); Map<String, String[]> params = getOAuthParams(); when(req.getParameterMap()).thenReturn(params); OperationService service = mock(OperationService.class); when(operationRegistry.getServiceFor(any(OperationType.class))).thenReturn(service); servlet.doPost(req, resp); verify(validator).validateMessage(any(OAuthMessage.class), any(OAuthAccessor.class)); verify(operationRegistry).getServiceFor(any(OperationType.class)); verify(service).execute(eq(operation), any(OperationContext.class), eq(ALEX)); verify(resp).setStatus(HttpServletResponse.SC_OK); assertEquals("Response should have been written into the servlet", responseValue, stringWriter.toString()); }
public void testDoPostExecutesAndWritesResponse() throws Exception { String operationId = "op1"; OperationRequest operation = new OperationRequest("wavelet.create", operationId); List<OperationRequest> operations = Collections.singletonList(operation); when(robotSerializer.deserializeOperations(any())).thenReturn(operations); String responseValue = "response value"; when(robotSerializer.serialize(any(), any(Type.class), any(ProtocolVersion.class))).thenReturn( responseValue); Map<String, String[]> params = getOAuthParams(); when(req.getParameterMap()).thenReturn(params); OperationService service = mock(OperationService.class); when(operationRegistry.getServiceFor(any(OperationType.class))).thenReturn(service); servlet.doPost(req, resp); verify(validator).validateMessage(any(OAuthMessage.class), any(OAuthAccessor.class)); verify(operationRegistry).getServiceFor(any(OperationType.class)); verify(service).execute(eq(operation), any(OperationContext.class), eq(ALEX)); verify(resp).setStatus(HttpServletResponse.SC_OK); assertEquals("Response should have been written into the servlet", responseValue, stringWriter.toString()); }
public void testDoPostExecutesAndWritesResponse() throws Exception { when(req.getHeaders("Authorization")).thenReturn(convertRawEnumerationToGeneric( generateOAuthHeader(ROBOT.getAddress()))); String operationId = "op1"; OperationRequest operation = new OperationRequest("wavelet.create", operationId); List<OperationRequest> operations = Collections.singletonList(operation); when(robotSerializer.deserializeOperations(any())).thenReturn(operations); String responseValue = "response value"; when(robotSerializer.serialize(any(), any(Type.class), any(ProtocolVersion.class))).thenReturn( responseValue); OperationService service = mock(OperationService.class); when(operationRegistry.getServiceFor(any(OperationType.class))).thenReturn(service); servlet.doPost(req, resp); verify(operationRegistry).getServiceFor(any(OperationType.class)); verify(service).execute(eq(operation), any(OperationContext.class), eq(ROBOT)); verify(resp).setStatus(HttpServletResponse.SC_OK); assertEquals("Response should have been written into the servlet", responseValue, outputWriter.toString()); }
/** * Executes an {@link OperationRequest}. If the operation throws an * {@link InvalidRequestException} this exception will be used to construct an * error response in the {@link OperationContext}. * * @param operation the operation to be executed. If the operation contains * {@link ParamsProperty.PROXYING_FOR} - then it will be taken in * account. * @param operationRegistry the registry containing the operations that can be * performed. * @param context the context in which the operation is to be executed. * @param author the author of the operation. */ public static void executeOperation(OperationRequest operation, OperationServiceRegistry operationRegistry, OperationContext context, ParticipantId author) { try { OperationService service = operationRegistry.getServiceFor(OperationUtil.getOperationType(operation)); ParticipantId proxyParticipant = OperationUtil.computeParticipant(operation, author); service.execute(operation, context, proxyParticipant); } catch (InvalidRequestException e) { LOG.warning("Operation " + operation + " failed to execute", e); context.constructErrorResponse(operation, e.getMessage()); } }
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()); } }