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 testapplyOperationsExecutesAndSubmitsDelta() throws Exception { // Use a special operation service that generates a simple delta; OperationService service = new OperationService() { @Override public void execute( OperationRequest operation, OperationContext context, ParticipantId participant) throws InvalidRequestException { context.openWavelet(WAVE_ID, WAVELET_ID, ROBOT_PARTICIPANT).addParticipant(ALEX); } }; when(operationRegistry.getServiceFor(any(OperationType.class))).thenReturn(service); OperationRequest operation = new OperationRequest("wavelet.create", "op1"); List<OperationRequest> operations = Collections.singletonList(operation); applicator.applyOperations(operations, waveletData, hashedVersionZero, ACCOUNT); verify(operationRegistry).getServiceFor(any(OperationType.class)); verify(waveletProvider).submitRequest(eq(WAVELET_NAME), any(ProtocolWaveletDelta.class), any(WaveletProvider.SubmitRequestListener.class)); } }
public void testapplyOperationsExecutesAndSubmitsDelta() throws Exception { // Use a special operation service that generates a simple delta; OperationService service = new OperationService() { @Override public void execute( OperationRequest operation, OperationContext context, ParticipantId participant) throws InvalidRequestException { context.openWavelet(WAVE_ID, WAVELET_ID, ROBOT_PARTICIPANT).addParticipant(ALEX); } }; when(operationRegistry.getServiceFor(any(OperationType.class))).thenReturn(service); OperationRequest operation = new OperationRequest("wavelet.create", "op1"); List<OperationRequest> operations = Collections.singletonList(operation); applicator.applyOperations(operations, waveletData, hashedVersionZero, ACCOUNT); verify(operationRegistry).getServiceFor(any(OperationType.class)); verify(waveletProvider).submitRequest(eq(WAVELET_NAME), any(ProtocolWaveletDelta.class), any(WaveletProvider.SubmitRequestListener.class)); } }
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()); }
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(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 testExecuteOperationsSetsErrorOnInvalidRequestException() throws Exception { String operationId = "op1"; OperationRequest operation = new OperationRequest("wavelet.create", operationId); OperationService service = mock(OperationService.class, new ThrowsException(new InvalidRequestException(""))); when(operationRegistry.getServiceFor(any(OperationType.class))).thenReturn(service); OperationUtil.executeOperation(operation, operationRegistry, context, ALEX); assertTrue("Expected one response", context.getResponses().size() == 1); assertTrue("Expected an error response", context.getResponse(operationId).isError()); }
public void testExecuteOperationsSetsErrorOnInvalidRequestException() throws Exception { String operationId = "op1"; OperationRequest operation = new OperationRequest("wavelet.create", operationId); OperationService service = mock(OperationService.class, new ThrowsException(new InvalidRequestException(""))); when(operationRegistry.getServiceFor(any(OperationType.class))).thenReturn(service); OperationUtil.executeOperation(operation, operationRegistry, context, ALEX); assertTrue("Expected one response", context.getResponses().size() == 1); assertTrue("Expected an error response", context.getResponse(operationId).isError()); }
/** * 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()); } }