Search in sources :

Example 6 with InvalidBindingException

use of org.sonarlint.intellij.exception.InvalidBindingException in project sonarlint-intellij by SonarSource.

the class SonarLintProjectConfigurable method onSave.

/**
 * When we save the binding, we need to:
 * - Send a message for listeners interested in it
 * - If we are bound to a module, update it (even if we detected no changes)
 * - Clear all issues and submit an analysis on all open files
 */
private void onSave() {
    SonarLintProjectNotifications.get(project).reset();
    ProjectConfigurationListener projectListener = project.getMessageBus().syncPublisher(ProjectConfigurationListener.TOPIC);
    if (projectSettings.isBindingEnabled() && projectSettings.getProjectKey() != null && projectSettings.getServerId() != null) {
        ProjectBindingManager bindingManager = SonarLintUtils.get(project, ProjectBindingManager.class);
        try {
            SonarQubeServer server = bindingManager.getSonarQubeServer();
            ConnectedSonarLintEngine engine = bindingManager.getConnectedEngineSkipChecks();
            String moduleKey = projectSettings.getProjectKey();
            ServerUpdateTask task = new ServerUpdateTask(engine, server, Collections.singletonMap(moduleKey, Collections.singletonList(project)), true);
            ProgressManager.getInstance().run(task.asModal());
        } catch (InvalidBindingException e) {
        // nothing to do, SonarLintEngineManager should have already shown a warning
        }
    }
    projectListener.changed(projectSettings);
}
Also used : ServerUpdateTask(org.sonarlint.intellij.tasks.ServerUpdateTask) ProjectConfigurationListener(org.sonarlint.intellij.messages.ProjectConfigurationListener) InvalidBindingException(org.sonarlint.intellij.exception.InvalidBindingException) SonarQubeServer(org.sonarlint.intellij.config.global.SonarQubeServer) ProjectBindingManager(org.sonarlint.intellij.core.ProjectBindingManager) ConnectedSonarLintEngine(org.sonarsource.sonarlint.core.client.api.connected.ConnectedSonarLintEngine)

Example 7 with InvalidBindingException

use of org.sonarlint.intellij.exception.InvalidBindingException in project sonarlint-intellij by SonarSource.

the class ProjectBindingManager method getSonarQubeServer.

public synchronized SonarQubeServer getSonarQubeServer() throws InvalidBindingException {
    String serverId = projectSettings.getServerId();
    List<SonarQubeServer> servers = globalSettings.getSonarQubeServers();
    Optional<SonarQubeServer> server = servers.stream().filter(s -> s.getName().equals(serverId)).findAny();
    return server.orElseThrow(() -> new InvalidBindingException("SonarQube server configuration does not exist for server id: " + serverId));
}
Also used : SonarLintConsole(org.sonarlint.intellij.ui.SonarLintConsole) List(java.util.List) SonarLintProjectSettings(org.sonarlint.intellij.config.project.SonarLintProjectSettings) InvalidBindingException(org.sonarlint.intellij.exception.InvalidBindingException) SonarLintGlobalSettings(org.sonarlint.intellij.config.global.SonarLintGlobalSettings) SonarQubeServer(org.sonarlint.intellij.config.global.SonarQubeServer) Project(com.intellij.openapi.project.Project) Optional(java.util.Optional) Nullable(javax.annotation.Nullable) ConnectedSonarLintEngine(org.sonarsource.sonarlint.core.client.api.connected.ConnectedSonarLintEngine) AbstractProjectComponent(com.intellij.openapi.components.AbstractProjectComponent) InvalidBindingException(org.sonarlint.intellij.exception.InvalidBindingException) SonarQubeServer(org.sonarlint.intellij.config.global.SonarQubeServer)

Example 8 with InvalidBindingException

use of org.sonarlint.intellij.exception.InvalidBindingException in project sonarlint-intellij by SonarSource.

the class SonarLintEngineManager method getConnectedEngine.

public synchronized ConnectedSonarLintEngine getConnectedEngine(SonarLintProjectNotifications notifications, String serverId, String projectKey) throws InvalidBindingException {
    Preconditions.checkNotNull(notifications, "notifications");
    Preconditions.checkNotNull(serverId, "serverId");
    Preconditions.checkNotNull(projectKey, "projectKey");
    if (!configuredStorageIds.contains(serverId)) {
        notifications.notifyServerIdInvalid();
        throw new InvalidBindingException("Invalid server name: " + serverId);
    }
    ConnectedSonarLintEngine engine = getConnectedEngine(serverId);
    checkConnectedEngineStatus(engine, notifications, serverId, projectKey);
    return engine;
}
Also used : InvalidBindingException(org.sonarlint.intellij.exception.InvalidBindingException) ConnectedSonarLintEngine(org.sonarsource.sonarlint.core.client.api.connected.ConnectedSonarLintEngine)

Example 9 with InvalidBindingException

use of org.sonarlint.intellij.exception.InvalidBindingException in project sonarlint-intellij by SonarSource.

the class ServerIssueUpdaterTest method testDownloadAllServerIssues.

@Test
public void testDownloadAllServerIssues() throws InvalidBindingException {
    List<VirtualFile> files = new LinkedList<>();
    for (int i = 0; i < 10; i++) {
        VirtualFile file = mock(VirtualFile.class);
        String filename = "MyFile" + i + ".txt";
        when(file.getPath()).thenReturn(FileUtil.toSystemIndependentName(projectBaseDir.resolve(filename).toString()));
        files.add(file);
    }
    ServerIssue serverIssue = mock(ServerIssue.class);
    // mock creation of engine / server
    ConnectedSonarLintEngine engine = mock(ConnectedSonarLintEngine.class);
    when(bindingManager.getConnectedEngine()).thenReturn(engine);
    SonarQubeServer server = mock(SonarQubeServer.class);
    when(server.getHostUrl()).thenReturn("http://dummyserver:9000");
    when(bindingManager.getSonarQubeServer()).thenReturn(server);
    // mock issues fetched
    when(engine.getServerIssues(eq(PROJECT_KEY), anyString())).thenReturn(Collections.singletonList(serverIssue));
    // run
    settings.setBindingEnabled(true);
    updater.initComponent();
    updater.fetchAndMatchServerIssues(files, indicator, false);
    verify(issueManager, timeout(3000).times(10)).matchWithServerIssues(any(VirtualFile.class), argThat(issues -> issues.size() == 1));
    verify(engine).downloadServerIssues(any(ServerConfiguration.class), eq(PROJECT_KEY));
    verify(bindingManager).getConnectedEngine();
    verify(console, never()).error(anyString());
    verify(console, never()).error(anyString(), any(Throwable.class));
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) SonarTest(org.sonarlint.intellij.SonarTest) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) VirtualFile(com.intellij.openapi.vfs.VirtualFile) Mock(org.mockito.Mock) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) ServerIssue(org.sonarsource.sonarlint.core.client.api.connected.ServerIssue) Mockito.verifyZeroInteractions(org.mockito.Mockito.verifyZeroInteractions) MockitoAnnotations(org.mockito.MockitoAnnotations) Mockito.timeout(org.mockito.Mockito.timeout) InvalidBindingException(org.sonarlint.intellij.exception.InvalidBindingException) SonarQubeServer(org.sonarlint.intellij.config.global.SonarQubeServer) Project(com.intellij.openapi.project.Project) FileUtil(com.intellij.openapi.util.io.FileUtil) LinkedList(java.util.LinkedList) Path(java.nio.file.Path) ConnectedSonarLintEngine(org.sonarsource.sonarlint.core.client.api.connected.ConnectedSonarLintEngine) Before(org.junit.Before) IssueManager(org.sonarlint.intellij.issue.IssueManager) SonarLintProjectSettings(org.sonarlint.intellij.config.project.SonarLintProjectSettings) SonarApplication(org.sonarlint.intellij.SonarApplication) IOException(java.io.IOException) Test(org.junit.Test) ServerConfiguration(org.sonarsource.sonarlint.core.client.api.connected.ServerConfiguration) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) SonarLintConsole(org.sonarlint.intellij.ui.SonarLintConsole) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) List(java.util.List) Mockito.never(org.mockito.Mockito.never) Rule(org.junit.Rule) Collections(java.util.Collections) TemporaryFolder(org.junit.rules.TemporaryFolder) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Mockito.mock(org.mockito.Mockito.mock) ServerConfiguration(org.sonarsource.sonarlint.core.client.api.connected.ServerConfiguration) SonarQubeServer(org.sonarlint.intellij.config.global.SonarQubeServer) ServerIssue(org.sonarsource.sonarlint.core.client.api.connected.ServerIssue) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ConnectedSonarLintEngine(org.sonarsource.sonarlint.core.client.api.connected.ConnectedSonarLintEngine) LinkedList(java.util.LinkedList) SonarTest(org.sonarlint.intellij.SonarTest) Test(org.junit.Test)

Example 10 with InvalidBindingException

use of org.sonarlint.intellij.exception.InvalidBindingException in project sonarlint-intellij by SonarSource.

the class ServerIssueUpdaterTest method testServerIssueTracking.

@Test
public void testServerIssueTracking() throws InvalidBindingException {
    VirtualFile file = mock(VirtualFile.class);
    ServerIssue serverIssue = mock(ServerIssue.class);
    String filename = "MyFile.txt";
    when(file.getPath()).thenReturn(FileUtil.toSystemIndependentName(projectBaseDir.resolve(filename).toString()));
    // mock creation of engine / server
    ConnectedSonarLintEngine engine = mock(ConnectedSonarLintEngine.class);
    when(bindingManager.getConnectedEngine()).thenReturn(engine);
    SonarQubeServer server = mock(SonarQubeServer.class);
    when(server.getHostUrl()).thenReturn("http://dummyserver:9000");
    when(bindingManager.getSonarQubeServer()).thenReturn(server);
    // mock issues downloaded
    when(engine.downloadServerIssues(any(ServerConfiguration.class), eq(PROJECT_KEY), eq(filename))).thenReturn(Collections.singletonList(serverIssue));
    // run
    settings.setBindingEnabled(true);
    updater.initComponent();
    updater.fetchAndMatchServerIssues(Collections.singletonList(file), indicator, false);
    verify(issueManager, timeout(3000).times(1)).matchWithServerIssues(eq(file), argThat(issues -> issues.size() == 1));
    verify(bindingManager).getConnectedEngine();
    verify(console, never()).error(anyString());
    verify(console, never()).error(anyString(), any(Throwable.class));
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) SonarTest(org.sonarlint.intellij.SonarTest) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) VirtualFile(com.intellij.openapi.vfs.VirtualFile) Mock(org.mockito.Mock) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) ServerIssue(org.sonarsource.sonarlint.core.client.api.connected.ServerIssue) Mockito.verifyZeroInteractions(org.mockito.Mockito.verifyZeroInteractions) MockitoAnnotations(org.mockito.MockitoAnnotations) Mockito.timeout(org.mockito.Mockito.timeout) InvalidBindingException(org.sonarlint.intellij.exception.InvalidBindingException) SonarQubeServer(org.sonarlint.intellij.config.global.SonarQubeServer) Project(com.intellij.openapi.project.Project) FileUtil(com.intellij.openapi.util.io.FileUtil) LinkedList(java.util.LinkedList) Path(java.nio.file.Path) ConnectedSonarLintEngine(org.sonarsource.sonarlint.core.client.api.connected.ConnectedSonarLintEngine) Before(org.junit.Before) IssueManager(org.sonarlint.intellij.issue.IssueManager) SonarLintProjectSettings(org.sonarlint.intellij.config.project.SonarLintProjectSettings) SonarApplication(org.sonarlint.intellij.SonarApplication) IOException(java.io.IOException) Test(org.junit.Test) ServerConfiguration(org.sonarsource.sonarlint.core.client.api.connected.ServerConfiguration) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) SonarLintConsole(org.sonarlint.intellij.ui.SonarLintConsole) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) List(java.util.List) Mockito.never(org.mockito.Mockito.never) Rule(org.junit.Rule) Collections(java.util.Collections) TemporaryFolder(org.junit.rules.TemporaryFolder) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Mockito.mock(org.mockito.Mockito.mock) ServerConfiguration(org.sonarsource.sonarlint.core.client.api.connected.ServerConfiguration) SonarQubeServer(org.sonarlint.intellij.config.global.SonarQubeServer) ServerIssue(org.sonarsource.sonarlint.core.client.api.connected.ServerIssue) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ConnectedSonarLintEngine(org.sonarsource.sonarlint.core.client.api.connected.ConnectedSonarLintEngine) SonarTest(org.sonarlint.intellij.SonarTest) Test(org.junit.Test)

Aggregations

InvalidBindingException (org.sonarlint.intellij.exception.InvalidBindingException)10 ConnectedSonarLintEngine (org.sonarsource.sonarlint.core.client.api.connected.ConnectedSonarLintEngine)7 SonarQubeServer (org.sonarlint.intellij.config.global.SonarQubeServer)6 Project (com.intellij.openapi.project.Project)4 List (java.util.List)3 SonarLintProjectSettings (org.sonarlint.intellij.config.project.SonarLintProjectSettings)3 SonarLintConsole (org.sonarlint.intellij.ui.SonarLintConsole)3 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)2 FileUtil (com.intellij.openapi.util.io.FileUtil)2 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 IOException (java.io.IOException)2 Path (java.nio.file.Path)2 Collections (java.util.Collections)2 LinkedList (java.util.LinkedList)2 Nullable (javax.annotation.Nullable)2 Before (org.junit.Before)2 Rule (org.junit.Rule)2 Test (org.junit.Test)2 TemporaryFolder (org.junit.rules.TemporaryFolder)2 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)2