Search in sources :

Example 86 with Operation

use of org.eclipse.che.api.promises.client.Operation in project che by eclipse.

the class TestServiceClientTest method initMocks.

@Before
public void initMocks() {
    MockitoAnnotations.initMocks(this);
    testServiceClient = spy(new TestServiceClient(appContext, asyncRequestFactory, dtoUnmarshallerFactory, dtoFactory, commandManager, execAgentCommandManager, promiseProvider, macroProcessor, commandConsoleFactory, processesPanelPresenter));
    doReturn(new PromiseMocker<TestResult>().getPromise()).when(testServiceClient).sendTests(anyString(), anyString(), anyMapOf(String.class, String.class));
    doAnswer(new FunctionAnswer<Executor.ExecutorBody<TestResult>, Promise<TestResult>>(executorBody -> {
        ExecutorPromiseMocker<TestResult> mocker = new ExecutorPromiseMocker<TestResult>(executorBody, (testResult, thisMocker) -> {
            thisMocker.applyOnThenOperation(testResult);
            return null;
        }, (promiseError, thisMocker) -> {
            thisMocker.applyOnCatchErrorOperation(promiseError);
            return null;
        });
        executorBody.apply(mocker.getResolveFunction(), mocker.getRejectFunction());
        return mocker.getPromise();
    })).when(testServiceClient).promiseFromExecutorBody(Matchers.<Executor.ExecutorBody<TestResult>>any());
    doAnswer(new FunctionAnswer<Throwable, PromiseError>(throwable -> {
        PromiseError promiseError = mock(PromiseError.class);
        when(promiseError.getCause()).thenReturn(throwable);
        return promiseError;
    })).when(testServiceClient).promiseFromThrowable(any(Throwable.class));
    when(appContext.getDevMachine()).thenReturn(devMachine);
    when(machine.getId()).thenReturn("DevMachineId");
    doAnswer(new FunctionAnswer<String, Promise<String>>(commandLine -> {
        String processedCommandLine = commandLine.replace("${current.project.path}", rootOfProjects + "/" + projectPath);
        return new PromiseMocker<String>().applyOnThenOperation(processedCommandLine).getPromise();
    })).when(macroProcessor).expandMacros(anyString());
    when(commandConsoleFactory.create(any(CommandImpl.class), any(Machine.class))).then(createCall -> {
        CommandOutputConsole commandOutputConsole = mock(CommandOutputConsole.class);
        when(commandOutputConsole.getProcessStartedOperation()).thenReturn(processStartedEvent -> {
            consoleEvents.add(processStartedEvent);
        });
        when(commandOutputConsole.getProcessDiedOperation()).thenReturn(processDiedEvent -> {
            consoleEvents.add(processDiedEvent);
        });
        when(commandOutputConsole.getStdErrOperation()).thenReturn(processStdErrEvent -> {
            consoleEvents.add(processStdErrEvent);
        });
        when(commandOutputConsole.getStdOutOperation()).thenReturn(processStdOutEvent -> {
            consoleEvents.add(processStdOutEvent);
        });
        return commandOutputConsole;
    });
    consoleEvents.clear();
    when(execAgentCommandManager.startProcess(anyString(), any(Command.class))).then(startProcessCall -> {
        @SuppressWarnings("unchecked") ExecAgentPromise<ProcessStartResponseDto> execAgentPromise = (ExecAgentPromise<ProcessStartResponseDto>) mock(ExecAgentPromise.class);
        class ProcessEventForward<DtoType> extends FunctionAnswer<Operation<DtoType>, ExecAgentPromise<ProcessStartResponseDto>> {

            public ProcessEventForward(Class<DtoType> dtoClass) {
                super(new java.util.function.Function<Operation<DtoType>, ExecAgentPromise<ProcessStartResponseDto>>() {

                    @Override
                    public ExecAgentPromise<ProcessStartResponseDto> apply(Operation<DtoType> op) {
                        operationsOnProcessEvents.put(dtoClass, op);
                        return execAgentPromise;
                    }
                });
            }
        }
        when(execAgentPromise.then(any())).then(new ProcessEventForward<>(ProcessStartResponseDto.class));
        when(execAgentPromise.thenIfProcessStartedEvent(any())).then(new ProcessEventForward<>(ProcessStartedEventDto.class));
        when(execAgentPromise.thenIfProcessDiedEvent(any())).then(new ProcessEventForward<>(ProcessDiedEventDto.class));
        when(execAgentPromise.thenIfProcessStdErrEvent(any())).then(new ProcessEventForward<>(ProcessStdErrEventDto.class));
        when(execAgentPromise.thenIfProcessStdOutEvent(any())).then(new ProcessEventForward<>(ProcessStdOutEventDto.class));
        return execAgentPromise;
    });
    operationsOnProcessEvents.clear();
}
Also used : Arrays(java.util.Arrays) AsyncRequestFactory(org.eclipse.che.ide.rest.AsyncRequestFactory) ProcessStartedEventDto(org.eclipse.che.api.machine.shared.dto.execagent.event.ProcessStartedEventDto) Executor(org.eclipse.che.api.promises.client.js.Executor) PromiseError(org.eclipse.che.api.promises.client.PromiseError) MockitoAnnotations(org.mockito.MockitoAnnotations) Arrays.asList(java.util.Arrays.asList) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Map(java.util.Map) Spy(org.mockito.Spy) Mockito.doReturn(org.mockito.Mockito.doReturn) Operation(org.eclipse.che.api.promises.client.Operation) CommandConsoleFactory(org.eclipse.che.ide.extension.machine.client.outputspanel.console.CommandConsoleFactory) DtoUnmarshallerFactory(org.eclipse.che.ide.rest.DtoUnmarshallerFactory) OperationException(org.eclipse.che.api.promises.client.OperationException) StatusNotification(org.eclipse.che.ide.api.notification.StatusNotification) DevMachine(org.eclipse.che.ide.api.machine.DevMachine) Collectors(java.util.stream.Collectors) Matchers.any(org.mockito.Matchers.any) List(java.util.List) ProcessStartResponseDto(org.eclipse.che.api.machine.shared.dto.execagent.ProcessStartResponseDto) Mockito.mock(org.mockito.Mockito.mock) Command(org.eclipse.che.api.core.model.machine.Command) ProcessStdOutEventDto(org.eclipse.che.api.machine.shared.dto.execagent.event.ProcessStdOutEventDto) CommandManager(org.eclipse.che.ide.api.command.CommandManager) GwtMockitoTestRunner(com.google.gwtmockito.GwtMockitoTestRunner) CommandOutputConsole(org.eclipse.che.ide.extension.machine.client.outputspanel.console.CommandOutputConsole) Matchers(org.mockito.Matchers) Mock(org.mockito.Mock) CommandImpl(org.eclipse.che.ide.api.command.CommandImpl) RunWith(org.junit.runner.RunWith) HashMap(java.util.HashMap) Mockito.spy(org.mockito.Mockito.spy) Matchers.anyString(org.mockito.Matchers.anyString) ProcessDiedEventDto(org.eclipse.che.api.machine.shared.dto.execagent.event.ProcessDiedEventDto) ArrayList(java.util.ArrayList) Promise(org.eclipse.che.api.promises.client.Promise) Matchers.anyMapOf(org.mockito.Matchers.anyMapOf) ProcessStdErrEventDto(org.eclipse.che.api.machine.shared.dto.execagent.event.ProcessStdErrEventDto) AppContext(org.eclipse.che.ide.api.app.AppContext) Before(org.junit.Before) DtoFactory(org.eclipse.che.ide.dto.DtoFactory) DtoWithPid(org.eclipse.che.api.machine.shared.dto.execagent.event.DtoWithPid) PromiseProvider(org.eclipse.che.api.promises.client.PromiseProvider) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) Machine(org.eclipse.che.api.core.model.machine.Machine) ExecAgentCommandManager(org.eclipse.che.ide.api.machine.ExecAgentCommandManager) Mockito.never(org.mockito.Mockito.never) ExecAgentPromise(org.eclipse.che.ide.api.machine.execagent.ExecAgentPromise) TestResult(org.eclipse.che.api.testing.shared.TestResult) Assert(org.junit.Assert) Collections(java.util.Collections) MacroProcessor(org.eclipse.che.ide.api.macro.MacroProcessor) ProcessesPanelPresenter(org.eclipse.che.ide.extension.machine.client.processes.panel.ProcessesPanelPresenter) ProcessStdErrEventDto(org.eclipse.che.api.machine.shared.dto.execagent.event.ProcessStdErrEventDto) Matchers.anyString(org.mockito.Matchers.anyString) Operation(org.eclipse.che.api.promises.client.Operation) DevMachine(org.eclipse.che.ide.api.machine.DevMachine) Machine(org.eclipse.che.api.core.model.machine.Machine) ExecAgentPromise(org.eclipse.che.ide.api.machine.execagent.ExecAgentPromise) Executor(org.eclipse.che.api.promises.client.js.Executor) CommandOutputConsole(org.eclipse.che.ide.extension.machine.client.outputspanel.console.CommandOutputConsole) ProcessStdOutEventDto(org.eclipse.che.api.machine.shared.dto.execagent.event.ProcessStdOutEventDto) CommandImpl(org.eclipse.che.ide.api.command.CommandImpl) TestResult(org.eclipse.che.api.testing.shared.TestResult) ProcessDiedEventDto(org.eclipse.che.api.machine.shared.dto.execagent.event.ProcessDiedEventDto) ProcessStartResponseDto(org.eclipse.che.api.machine.shared.dto.execagent.ProcessStartResponseDto) Promise(org.eclipse.che.api.promises.client.Promise) ExecAgentPromise(org.eclipse.che.ide.api.machine.execagent.ExecAgentPromise) PromiseError(org.eclipse.che.api.promises.client.PromiseError) Command(org.eclipse.che.api.core.model.machine.Command) ProcessStartedEventDto(org.eclipse.che.api.machine.shared.dto.execagent.event.ProcessStartedEventDto) Before(org.junit.Before)

Example 87 with Operation

use of org.eclipse.che.api.promises.client.Operation in project che by eclipse.

the class DebugConfigurationsManagerImplTest method testShouldApplyNewConfiguration.

@Test
public void testShouldApplyNewConfiguration() throws Exception {
    final String debugId = "debug";
    final String host = "localhost";
    final int port = 9000;
    when(debugConfiguration.getConnectionProperties()).thenReturn(ImmutableMap.of("prop", "value"));
    when(debugConfigurationType.getId()).thenReturn(debugId);
    when(debugConfiguration.getHost()).thenReturn(host);
    when(debugConfiguration.getPort()).thenReturn(port);
    when(debugConfiguration.getType()).thenReturn(debugConfigurationType);
    when(debuggerManager.getDebugger(debugId)).thenReturn(debugger);
    when(debugger.connect(anyMap())).thenReturn(mock(Promise.class));
    when(currentProjectPathMacro.expand()).thenReturn(mock(Promise.class));
    when(currentProjectPathMacro.getName()).thenReturn("key");
    debugConfigurationsManager.apply(debugConfiguration);
    ArgumentCaptor<Operation> operationArgumentCaptor = ArgumentCaptor.forClass(Operation.class);
    verify(currentProjectPathMacro.expand()).then(operationArgumentCaptor.capture());
    operationArgumentCaptor.getValue().apply("project path");
    verify(debuggerManager).setActiveDebugger(debugger);
    ArgumentCaptor<Map> properties = ArgumentCaptor.forClass(Map.class);
    verify(debugger).connect(properties.capture());
    Map<String, String> m = properties.getValue();
    assertEquals(m.get("HOST"), host);
    assertEquals(m.get("PORT"), String.valueOf(port));
    assertEquals(m.get("prop"), "value");
}
Also used : Promise(org.eclipse.che.api.promises.client.Promise) Matchers.anyString(org.mockito.Matchers.anyString) Operation(org.eclipse.che.api.promises.client.Operation) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) Matchers.anyMap(org.mockito.Matchers.anyMap) Test(org.junit.Test)

Example 88 with Operation

use of org.eclipse.che.api.promises.client.Operation in project che by eclipse.

the class AbstractDebugger method addBreakpoint.

@Override
public void addBreakpoint(final VirtualFile file, final int lineNumber) {
    if (isConnected()) {
        String fqn = pathToFqn(file);
        if (fqn == null) {
            return;
        }
        final String filePath = file.getLocation().toString();
        LocationDto locationDto = dtoFactory.createDto(LocationDto.class).withLineNumber(lineNumber + 1).withTarget(fqn).withResourcePath(filePath).withResourceProjectPath(getProject(file).getPath());
        BreakpointDto breakpointDto = dtoFactory.createDto(BreakpointDto.class).withLocation(locationDto).withEnabled(true);
        Promise<Void> promise = service.addBreakpoint(debugSessionDto.getId(), breakpointDto);
        promise.then(new Operation<Void>() {

            @Override
            public void apply(Void arg) throws OperationException {
                Breakpoint breakpoint = new Breakpoint(Breakpoint.Type.BREAKPOINT, lineNumber, filePath, file, true);
                for (DebuggerObserver observer : observers) {
                    observer.onBreakpointAdded(breakpoint);
                }
            }
        }).catchError(new Operation<PromiseError>() {

            @Override
            public void apply(PromiseError arg) throws OperationException {
                Log.error(AbstractDebugger.class, arg.getMessage());
            }
        });
    } else {
        Breakpoint breakpoint = new Breakpoint(Breakpoint.Type.BREAKPOINT, lineNumber, file.getLocation().toString(), file, false);
        for (DebuggerObserver observer : observers) {
            observer.onBreakpointAdded(breakpoint);
        }
    }
}
Also used : Breakpoint(org.eclipse.che.ide.api.debug.Breakpoint) Operation(org.eclipse.che.api.promises.client.Operation) JsPromiseError(org.eclipse.che.api.promises.client.js.JsPromiseError) PromiseError(org.eclipse.che.api.promises.client.PromiseError) BreakpointDto(org.eclipse.che.api.debug.shared.dto.BreakpointDto) DebuggerObserver(org.eclipse.che.ide.debug.DebuggerObserver) LocationDto(org.eclipse.che.api.debug.shared.dto.LocationDto) OperationException(org.eclipse.che.api.promises.client.OperationException)

Example 89 with Operation

use of org.eclipse.che.api.promises.client.Operation in project che by eclipse.

the class AbstractDebugger method setValue.

@Override
public void setValue(final Variable variable) {
    if (isConnected()) {
        Promise<Void> promise = service.setValue(debugSessionDto.getId(), asDto(variable));
        promise.then(new Operation<Void>() {

            @Override
            public void apply(Void arg) throws OperationException {
                for (DebuggerObserver observer : observers) {
                    observer.onValueChanged(variable.getVariablePath().getPath(), variable.getValue());
                }
            }
        }).catchError(new Operation<PromiseError>() {

            @Override
            public void apply(PromiseError arg) throws OperationException {
                Log.error(AbstractDebugger.class, arg.getMessage());
            }
        });
    }
}
Also used : JsPromiseError(org.eclipse.che.api.promises.client.js.JsPromiseError) PromiseError(org.eclipse.che.api.promises.client.PromiseError) Operation(org.eclipse.che.api.promises.client.Operation) DebuggerObserver(org.eclipse.che.ide.debug.DebuggerObserver) OperationException(org.eclipse.che.api.promises.client.OperationException)

Example 90 with Operation

use of org.eclipse.che.api.promises.client.Operation in project che by eclipse.

the class DebuggerPresenter method onDebuggerAttached.

@Override
public void onDebuggerAttached(final DebuggerDescriptor debuggerDescriptor, Promise<Void> connect) {
    final String address = debuggerDescriptor.getAddress();
    final StatusNotification notification = notificationManager.notify(constant.debuggerConnectingTitle(address), PROGRESS, FLOAT_MODE);
    connect.then(new Operation<Void>() {

        @Override
        public void apply(Void arg) throws OperationException {
            DebuggerPresenter.this.debuggerDescriptor = debuggerDescriptor;
            notification.setTitle(constant.debuggerConnectedTitle());
            notification.setContent(constant.debuggerConnectedDescription(address));
            notification.setStatus(SUCCESS);
            showAndUpdateView();
            showDebuggerPanel();
        }
    }).catchError(new Operation<PromiseError>() {

        @Override
        public void apply(PromiseError arg) throws OperationException {
            notification.setTitle(constant.failedToConnectToRemoteDebuggerDescription(address, arg.getMessage()));
            notification.setStatus(FAIL);
            notification.setDisplayMode(FLOAT_MODE);
        }
    });
}
Also used : PromiseError(org.eclipse.che.api.promises.client.PromiseError) StatusNotification(org.eclipse.che.ide.api.notification.StatusNotification) Operation(org.eclipse.che.api.promises.client.Operation) OperationException(org.eclipse.che.api.promises.client.OperationException)

Aggregations

Operation (org.eclipse.che.api.promises.client.Operation)126 OperationException (org.eclipse.che.api.promises.client.OperationException)116 PromiseError (org.eclipse.che.api.promises.client.PromiseError)110 Project (org.eclipse.che.ide.api.resources.Project)51 Resource (org.eclipse.che.ide.api.resources.Resource)45 Promise (org.eclipse.che.api.promises.client.Promise)21 StatusNotification (org.eclipse.che.ide.api.notification.StatusNotification)21 CLIOutputResponse (org.eclipse.che.plugin.svn.shared.CLIOutputResponse)21 List (java.util.List)17 VirtualFile (org.eclipse.che.ide.api.resources.VirtualFile)16 Path (org.eclipse.che.ide.resource.Path)14 GitOutputConsole (org.eclipse.che.ide.ext.git.client.outputconsole.GitOutputConsole)13 Optional (com.google.common.base.Optional)11 ArrayList (java.util.ArrayList)11 HashMap (java.util.HashMap)10 File (org.eclipse.che.ide.api.resources.File)10 Credentials (org.eclipse.che.ide.api.subversion.Credentials)10 EditorPartPresenter (org.eclipse.che.ide.api.editor.EditorPartPresenter)9 JsPromiseError (org.eclipse.che.api.promises.client.js.JsPromiseError)8 TestResult (org.eclipse.che.api.testing.shared.TestResult)7