Search in sources :

Example 16 with TestResult

use of org.eclipse.che.api.testing.shared.TestResult in project che by eclipse.

the class RunClassTestAction method actionPerformed.

@Override
public void actionPerformed(ActionEvent e) {
    final StatusNotification notification = new StatusNotification("Running Tests...", PROGRESS, FLOAT_MODE);
    notificationManager.notify(notification);
    final Project project = appContext.getRootProject();
    EditorPartPresenter editorPart = editorAgent.getActiveEditor();
    final VirtualFile file = editorPart.getEditorInput().getFile();
    String fqn = JavaUtil.resolveFQN(file);
    Map<String, String> parameters = new HashMap<>();
    parameters.put("fqn", fqn);
    parameters.put("runClass", "true");
    parameters.put("updateClasspath", "true");
    Promise<TestResult> testResultPromise = service.getTestResult(project.getPath(), "testng", parameters);
    testResultPromise.then(new Operation<TestResult>() {

        @Override
        public void apply(TestResult result) throws OperationException {
            notification.setStatus(SUCCESS);
            if (result.isSuccess()) {
                notification.setTitle("Test runner executed successfully");
                notification.setContent("All tests are passed");
            } else {
                notification.setTitle("Test runner executed successfully with test failures.");
                notification.setContent(result.getFailureCount() + " test(s) failed.\n");
            }
            presenter.handleResponse(result);
        }
    }).catchError(new Operation<PromiseError>() {

        @Override
        public void apply(PromiseError exception) throws OperationException {
            final String errorMessage = (exception.getMessage() != null) ? exception.getMessage() : "Failed to run test cases";
            notification.setContent(errorMessage);
            notification.setStatus(FAIL);
        }
    });
}
Also used : VirtualFile(org.eclipse.che.ide.api.resources.VirtualFile) HashMap(java.util.HashMap) StatusNotification(org.eclipse.che.ide.api.notification.StatusNotification) TestResult(org.eclipse.che.api.testing.shared.TestResult) Operation(org.eclipse.che.api.promises.client.Operation) Project(org.eclipse.che.ide.api.resources.Project) PromiseError(org.eclipse.che.api.promises.client.PromiseError) EditorPartPresenter(org.eclipse.che.ide.api.editor.EditorPartPresenter) OperationException(org.eclipse.che.api.promises.client.OperationException)

Example 17 with TestResult

use of org.eclipse.che.api.testing.shared.TestResult 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 18 with TestResult

use of org.eclipse.che.api.testing.shared.TestResult in project che by eclipse.

the class TestingServiceTest method testSuccessfulTestExecution.

// Asking for run a test from known framework.
@Test
public void testSuccessfulTestExecution() throws Exception {
    //given
    String query = "projectPath=/sample-project&testFramework=" + TEST_FRAMEWORK_A;
    TestResult result = dto.createDto(TestResult.class);
    result.setSuccess(true);
    TestResult expected = dto.clone(result);
    //when
    when(testRunnerA.execute(anyMapOf(String.class, String.class))).thenReturn(result);
    //then
    Response response = given().when().get(SERVICE_PATH_RUN_ACTION + "/?" + query);
    assertEquals(response.getStatusCode(), 200);
    TestResult responseResult = dto.createDtoFromJson(response.getBody().asInputStream(), TestResult.class);
    assertEquals(responseResult, expected);
}
Also used : Response(com.jayway.restassured.response.Response) TestResult(org.eclipse.che.api.testing.shared.TestResult) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.testng.annotations.Test)

Aggregations

TestResult (org.eclipse.che.api.testing.shared.TestResult)18 OperationException (org.eclipse.che.api.promises.client.OperationException)9 PromiseError (org.eclipse.che.api.promises.client.PromiseError)9 Operation (org.eclipse.che.api.promises.client.Operation)7 StatusNotification (org.eclipse.che.ide.api.notification.StatusNotification)7 ArrayList (java.util.ArrayList)6 HashMap (java.util.HashMap)6 List (java.util.List)5 CommandImpl (org.eclipse.che.ide.api.command.CommandImpl)5 Matchers.anyString (org.mockito.Matchers.anyString)5 Failure (org.eclipse.che.api.testing.shared.Failure)4 Project (org.eclipse.che.ide.api.resources.Project)4 URLClassLoader (java.net.URLClassLoader)3 Test (org.junit.Test)3 PrintWriter (java.io.PrintWriter)2 StringWriter (java.io.StringWriter)2 Map (java.util.Map)2 Machine (org.eclipse.che.api.core.model.machine.Machine)2 DtoWithPid (org.eclipse.che.api.machine.shared.dto.execagent.event.DtoWithPid)2 Promise (org.eclipse.che.api.promises.client.Promise)2