CommandProcessor implements RestProcessor { void execute( final Request request, CommandCallback callback ) throws CommandException { ResourceHostInfo resourceHostInfo; try { resourceHostInfo = getResourceHostInfo( request.getId() ); if ( !resourceHostInfo.getId().equals( request.getId() ) ) { if ( hostRegistry.getContainerHostInfoById( request.getId() ).getState() != ContainerHostState.RUNNING ) { throw new CommandException( "Container is not running" ); } } } catch ( HostDisconnectedException e ) { throw new CommandException( e ); } CommandProcess commandProcess = createCommandProcess( callback, request, resourceHostInfo.getId() ); boolean queued = commands.put( request.getCommandId(), commandProcess, ( request.getTimeout() + EXTRA_TIMEOUT_SEC ) * 1000L, new CommandProcessExpiryCallback() ); if ( !queued ) { throw new CommandException( "Command id is null " ); } try { commandProcess.start(); String command = jsonUtil.to( new RequestWrapper( request ) ); if ( !request.getCommand().startsWith( "subutai file" ) ) { LOG.debug( String.format( "Sending:%n%s", command ) ); } } catch ( Exception e ) { LOG.error( "Error sending request", e ); remove( request ); commandProcess.stop(); throw new CommandException( e ); } } CommandProcessor( final HostRegistry hostRegistry, final IdentityManager identityManager ); void addListener( HeartbeatListener listener ); void removeListener( HeartbeatListener listener ); void dispose(); @Override void handleHeartbeat( final HeartBeat heartBeat ); @Override Set<String> getRequests( final String hostId ); @Override void handleResponse( final Response response );  }
@Test public void testExecute() throws Exception { doReturn( commandProcess ).when( commandProcessor ).createCommandProcess( callback, request, HOST_ID ); doThrow( new HostDisconnectedException( "" ) ).when( commandProcessor ).getResourceHostInfo( HOST_ID ); try { commandProcessor.execute( request, callback ); fail( "Expected CommandException" ); } catch ( CommandException e ) { } doReturn( resourceHostInfo ).when( commandProcessor ).getResourceHostInfo( HOST_ID ); try { commandProcessor.execute( request, callback ); fail( "Expected CommandException" ); } catch ( CommandException e ) { } }