@Test public void testRandomExecutionFailure() throws IOException { bindOperations(); HttpServletRequest request = mock(HttpServletRequest.class); when(request.getScheme()).thenReturn("http"); HttpServletResponse response = mock(HttpServletResponse.class); IdentityTokenResolver tokenResolver = mock(IdentityTokenResolver.class); HttpCommand mockedCommand = mock(HttpCommand.class); RequestTimer mockTimer = mock(RequestTimer.class); commandProcessor.setExecutionVenue(new RandomFailureEV(1)); when(mockedCommand.getRequest()).thenReturn(request); when(mockedCommand.getResponse()).thenReturn(response); when(mockedCommand.getIdentityTokenResolver()).thenReturn(tokenResolver); when(mockedCommand.getStatus()).thenReturn(TransportCommand.CommandStatus.InProgress); when(mockedCommand.getTimer()).thenReturn(mockTimer); Executor executor = new Executor() { @Override public void execute(Runnable command) { command.run(); } }; commandProcessor.setExecutor(executor); String body="{ \"method\": \"" + SERVICE_NAME + "/v1.0/" + OP_NAME + "\", \"params\": [\"hello\", 321], \"id\": \"1\"}"; TestInputStream tis = new TestInputStream(new ByteArrayInputStream(body.getBytes("UTF-8"))); when(request.getInputStream()).thenReturn(tis); TestOutputStream tos = new TestOutputStream(); when(response.getOutputStream()).thenReturn(tos); commandProcessor.process(mockedCommand); String written = tos.getCapturedOutputStream(); Map result = objectMapper.readValue(written, Map.class); Map error = (Map)result.get("error"); assertTrue(error.containsKey("code")); assertEquals(-32603, error.get("code")); verify(logger).logAccess(eq(mockedCommand), isA(ExecutionContext.class), anyLong(), anyLong(), any(MediaType.class), any(MediaType.class), any(ResponseCode.class)); verifyTracerCalls(true); }
@Override public void process(HttpCommand command) { boolean traceStarted = false; incrementCommandsProcessed(); DehydratedExecutionContext ctx = null; try { validateCommand(command); final CommandResolver<HttpCommand> resolver = createCommandResolver(command, tracer); traceStarted = true; ctx = resolver.resolveExecutionContext(); final TimeConstraints realTimeConstraints = DefaultTimeConstraints.rebaseFromNewStartTime(ctx.getRequestTime(), readRawTimeConstraints(command.getRequest())); final DehydratedExecutionContext finalCtx = ctx; ExecutionCommand resolveCommand = new ExecutionCommand() { @Override public OperationKey getOperationKey() { return IDENTITY_RESOLUTION_OPDEF.getOperationKey(); } @Override public Object[] getArgs() { return new Object[0]; } @Override public void onResult(ExecutionResult executionResult) { Iterable<ExecutionCommand> batchCalls = resolver.resolveExecutionCommands(); if (executionResult.isFault()) { for (ExecutionCommand exec : batchCalls) { exec.onResult(executionResult); } } else { ExecutionContext context = ExecutionContextFactory.resolveExecutionContext(finalCtx, finalCtx.getIdentity()); for (ExecutionCommand exec : resolver.resolveExecutionCommands()) { ExecutionContext subContext = createCallContext(context); tracer.start(subContext.getRequestUUID(), exec.getOperationKey()); ExecutionCommand newCommand = createTraceableSubCommand(exec, tracer, subContext.getRequestUUID()); executeCommand(newCommand, context); } } } @Override public TimeConstraints getTimeConstraints() { return realTimeConstraints; } }; executeCommand(resolveCommand, ctx); } catch (CougarException ex) { writeErrorResponse(command, ctx, ex, traceStarted); } catch (Throwable ex) { writeErrorResponse(command, ctx, new CougarServiceException(ServerFaultCode.ServiceRuntimeException, ex.getMessage()), traceStarted); } }
JsonRpcTransportCommandProcessor extends AbstractHttpCommandProcessor<Void> { @Override public void process(HttpCommand command) { boolean traceStarted = false; incrementCommandsProcessed(); DehydratedExecutionContext ctx = null; try { validateCommand(command); final CommandResolver<HttpCommand> resolver = createCommandResolver(command, tracer); traceStarted = true; ctx = resolver.resolveExecutionContext(); final TimeConstraints realTimeConstraints = DefaultTimeConstraints.rebaseFromNewStartTime(ctx.getRequestTime(), readRawTimeConstraints(command.getRequest())); final DehydratedExecutionContext finalCtx = ctx; ExecutionCommand resolveCommand = new ExecutionCommand() { @Override public OperationKey getOperationKey() { return IDENTITY_RESOLUTION_OPDEF.getOperationKey(); } @Override public Object[] getArgs() { return new Object[0]; } @Override public void onResult(ExecutionResult executionResult) { Iterable<ExecutionCommand> batchCalls = resolver.resolveExecutionCommands(); if (executionResult.isFault()) { for (ExecutionCommand exec : batchCalls) { exec.onResult(executionResult); } } else { ExecutionContext context = ExecutionContextFactory.resolveExecutionContext(finalCtx, finalCtx.getIdentity()); for (ExecutionCommand exec : resolver.resolveExecutionCommands()) { ExecutionContext subContext = createCallContext(context); tracer.start(subContext.getRequestUUID(), exec.getOperationKey()); ExecutionCommand newCommand = createTraceableSubCommand(exec, tracer, subContext.getRequestUUID()); executeCommand(newCommand, context); } } } @Override public TimeConstraints getTimeConstraints() { return realTimeConstraints; } }; executeCommand(resolveCommand, ctx); } catch (CougarException ex) { writeErrorResponse(command, ctx, ex, traceStarted); } catch (Throwable ex) { writeErrorResponse(command, ctx, new CougarServiceException(ServerFaultCode.ServiceRuntimeException, ex.getMessage()), traceStarted); } } }
JsonRpcTransportCommandProcessor extends AbstractHttpCommandProcessor<Void> { @Override public void process(HttpCommand command) { boolean traceStarted = false; incrementCommandsProcessed(); DehydratedExecutionContext ctx = null; try { validateCommand(command); final CommandResolver<HttpCommand> resolver = createCommandResolver(command, tracer); traceStarted = true; ctx = resolver.resolveExecutionContext(); final TimeConstraints realTimeConstraints = DefaultTimeConstraints.rebaseFromNewStartTime(ctx.getRequestTime(), readRawTimeConstraints(command.getRequest())); final DehydratedExecutionContext finalCtx = ctx; ExecutionCommand resolveCommand = new ExecutionCommand() { @Override public OperationKey getOperationKey() { return IDENTITY_RESOLUTION_OPDEF.getOperationKey(); } @Override public Object[] getArgs() { return new Object[0]; } @Override public void onResult(ExecutionResult executionResult) { Iterable<ExecutionCommand> batchCalls = resolver.resolveExecutionCommands(); if (executionResult.isFault()) { for (ExecutionCommand exec : batchCalls) { exec.onResult(executionResult); } } else { ExecutionContext context = ExecutionContextFactory.resolveExecutionContext(finalCtx, finalCtx.getIdentity()); for (ExecutionCommand exec : resolver.resolveExecutionCommands()) { ExecutionContext subContext = createCallContext(context); tracer.start(subContext.getRequestUUID(), exec.getOperationKey()); ExecutionCommand newCommand = createTraceableSubCommand(exec, tracer, subContext.getRequestUUID()); executeCommand(newCommand, context); } } } @Override public TimeConstraints getTimeConstraints() { return realTimeConstraints; } }; executeCommand(resolveCommand, ctx); } catch (CougarException ex) { writeErrorResponse(command, ctx, ex, traceStarted); } catch (Throwable ex) { writeErrorResponse(command, ctx, new CougarServiceException(ServerFaultCode.ServiceRuntimeException, ex.getMessage()), traceStarted); } } JsonRpcTransportCommandProcessor(DehydratedExecutionContextResolution contextResolution, String requestTimeoutHeader, ObjectMapper mapper);  JsonRpcTransportCommandProcessor(DehydratedExecutionContextResolution contextResolution, String requestTimeoutHeader, JSONBindingFactory jsonBindingFactory); }
JsonRpcTransportCommandProcessor extends AbstractHttpCommandProcessor<Void> { @Override public void process(HttpCommand command) { boolean traceStarted = false; incrementCommandsProcessed(); DehydratedExecutionContext ctx = null; try { validateCommand(command); final CommandResolver<HttpCommand> resolver = createCommandResolver(command, tracer); traceStarted = true; ctx = resolver.resolveExecutionContext(); final TimeConstraints realTimeConstraints = DefaultTimeConstraints.rebaseFromNewStartTime(ctx.getRequestTime(), readRawTimeConstraints(command.getRequest())); final DehydratedExecutionContext finalCtx = ctx; ExecutionCommand resolveCommand = new ExecutionCommand() { @Override public OperationKey getOperationKey() { return IDENTITY_RESOLUTION_OPDEF.getOperationKey(); } @Override public Object[] getArgs() { return new Object[0]; } @Override public void onResult(ExecutionResult executionResult) { Iterable<ExecutionCommand> batchCalls = resolver.resolveExecutionCommands(); if (executionResult.isFault()) { for (ExecutionCommand exec : batchCalls) { exec.onResult(executionResult); } } else { ExecutionContext context = ExecutionContextFactory.resolveExecutionContext(finalCtx, finalCtx.getIdentity()); for (ExecutionCommand exec : resolver.resolveExecutionCommands()) { ExecutionContext subContext = createCallContext(context); tracer.start(subContext.getRequestUUID(), exec.getOperationKey()); ExecutionCommand newCommand = createTraceableSubCommand(exec, tracer, subContext.getRequestUUID()); executeCommand(newCommand, context); } } } @Override public TimeConstraints getTimeConstraints() { return realTimeConstraints; } }; executeCommand(resolveCommand, ctx); } catch (CougarException ex) { writeErrorResponse(command, ctx, ex, traceStarted); } catch (Throwable ex) { writeErrorResponse(command, ctx, new CougarServiceException(ServerFaultCode.ServiceRuntimeException, ex.getMessage()), traceStarted); } } JsonRpcTransportCommandProcessor(DehydratedExecutionContextResolution contextResolution, String requestTimeoutHeader, ObjectMapper mapper);  JsonRpcTransportCommandProcessor(DehydratedExecutionContextResolution contextResolution, String requestTimeoutHeader, JSONBindingFactory jsonBindingFactory); @Override void onCougarStart(); boolean bindOperation(OperationDefinition operationDefinition); @Override void process(HttpCommand command); @Override void writeErrorResponse(HttpCommand command, DehydratedExecutionContext context, CougarException error, boolean traceStarted); boolean writeResponseIfComplete(HttpCommand command, DehydratedExecutionContext context, boolean isBatch, List<JsonRpcRequest> requests, List<JsonRpcResponse> responses, long bytesRead, Tracer tracer); }
JsonRpcTransportCommandProcessor extends AbstractHttpCommandProcessor<Void> { @Override public void process(HttpCommand command) { boolean traceStarted = false; incrementCommandsProcessed(); DehydratedExecutionContext ctx = null; try { validateCommand(command); final CommandResolver<HttpCommand> resolver = createCommandResolver(command, tracer); traceStarted = true; ctx = resolver.resolveExecutionContext(); final TimeConstraints realTimeConstraints = DefaultTimeConstraints.rebaseFromNewStartTime(ctx.getRequestTime(), readRawTimeConstraints(command.getRequest())); final DehydratedExecutionContext finalCtx = ctx; ExecutionCommand resolveCommand = new ExecutionCommand() { @Override public OperationKey getOperationKey() { return IDENTITY_RESOLUTION_OPDEF.getOperationKey(); } @Override public Object[] getArgs() { return new Object[0]; } @Override public void onResult(ExecutionResult executionResult) { Iterable<ExecutionCommand> batchCalls = resolver.resolveExecutionCommands(); if (executionResult.isFault()) { for (ExecutionCommand exec : batchCalls) { exec.onResult(executionResult); } } else { ExecutionContext context = ExecutionContextFactory.resolveExecutionContext(finalCtx, finalCtx.getIdentity()); for (ExecutionCommand exec : resolver.resolveExecutionCommands()) { ExecutionContext subContext = createCallContext(context); tracer.start(subContext.getRequestUUID(), exec.getOperationKey()); ExecutionCommand newCommand = createTraceableSubCommand(exec, tracer, subContext.getRequestUUID()); executeCommand(newCommand, context); } } } @Override public TimeConstraints getTimeConstraints() { return realTimeConstraints; } }; executeCommand(resolveCommand, ctx); } catch (CougarException ex) { writeErrorResponse(command, ctx, ex, traceStarted); } catch (Throwable ex) { writeErrorResponse(command, ctx, new CougarServiceException(ServerFaultCode.ServiceRuntimeException, ex.getMessage()), traceStarted); } } JsonRpcTransportCommandProcessor(DehydratedExecutionContextResolution contextResolution, String requestTimeoutHeader, ObjectMapper mapper);  JsonRpcTransportCommandProcessor(DehydratedExecutionContextResolution contextResolution, String requestTimeoutHeader, JSONBindingFactory jsonBindingFactory); @Override void onCougarStart(); boolean bindOperation(OperationDefinition operationDefinition); @Override void process(HttpCommand command); @Override void writeErrorResponse(HttpCommand command, DehydratedExecutionContext context, CougarException error, boolean traceStarted); boolean writeResponseIfComplete(HttpCommand command, DehydratedExecutionContext context, boolean isBatch, List<JsonRpcRequest> requests, List<JsonRpcResponse> responses, long bytesRead, Tracer tracer);  }