its.ConnectedModeRequirementsTest.java Source code

Java tutorial

Introduction

Here is the source code for its.ConnectedModeRequirementsTest.java

Source

/*
 * SonarLint Core - ITs - Tests
 * Copyright (C) 2009-2017 SonarSource SA
 * mailto:info AT sonarsource DOT com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package its;

import static org.assertj.core.api.Assertions.assertThat;

import java.io.IOException;
import java.nio.file.Path;

import org.apache.commons.io.FileUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;
import org.sonar.wsclient.user.UserParameters;
import org.sonarsource.sonarlint.core.ConnectedSonarLintEngineImpl;
import org.sonarsource.sonarlint.core.WsHelperImpl;
import org.sonarsource.sonarlint.core.client.api.connected.ConnectedGlobalConfiguration;
import org.sonarsource.sonarlint.core.client.api.connected.ConnectedSonarLintEngine;
import org.sonarsource.sonarlint.core.client.api.connected.ConnectedSonarLintEngine.State;
import org.sonarsource.sonarlint.core.client.api.connected.LoadedAnalyzer;
import org.sonarsource.sonarlint.core.client.api.connected.ServerConfiguration;
import org.sonarsource.sonarlint.core.client.api.connected.UpdateResult;
import org.sonarsource.sonarlint.core.client.api.connected.ValidationResult;

import com.sonar.orchestrator.Orchestrator;

public class ConnectedModeRequirementsTest extends AbstractConnectedTest {

    @ClassRule
    public static Orchestrator ORCHESTRATOR = Orchestrator.builderEnv()
            .setOrchestratorProperty("javaVersion", "3.7").addPlugin("java").build();

    @ClassRule
    public static TemporaryFolder temp = new TemporaryFolder();

    @Rule
    public ExpectedException exception = ExpectedException.none();

    private static Path sonarUserHome;

    private ConnectedSonarLintEngine engine;

    @BeforeClass
    public static void prepare() throws Exception {
        sonarUserHome = temp.newFolder().toPath();

        ORCHESTRATOR.getServer().adminWsClient().userClient().create(UserParameters.create().login(SONARLINT_USER)
                .password(SONARLINT_PWD).passwordConfirmation(SONARLINT_PWD).name("SonarLint"));
    }

    @Before
    public void start() {
        FileUtils.deleteQuietly(sonarUserHome.toFile());
        engine = new ConnectedSonarLintEngineImpl(ConnectedGlobalConfiguration.builder().setServerId("orchestrator")
                .setSonarLintUserHome(sonarUserHome).setLogOutput((msg, level) -> System.out.println(msg)).build());
        assertThat(engine.getGlobalStorageStatus()).isNull();
        assertThat(engine.getState()).isEqualTo(State.NEVER_UPDATED);
    }

    @After
    public void stop() {
        try {
            engine.stop(true);
        } catch (Exception e) {
            // Ignore
        }
    }

    @Test
    public void checkMinimalPluginVersionDuringGlobalUpdate() throws IOException {
        UpdateResult update = engine.update(config(), null);
        assertThat(update.status().getLastUpdateDate()).isNotNull();
        assertThat(engine.getLoadedAnalyzers().stream().map(LoadedAnalyzer::key)).doesNotContain("java");
    }

    @Test
    public void dontCheckMinimalPluginVersionWhenValidatingConnection() throws IOException {
        ValidationResult result = new WsHelperImpl().validateConnection(config());
        assertThat(result.success()).isTrue();
    }

    private ServerConfiguration config() {
        return ServerConfiguration.builder().url(ORCHESTRATOR.getServer().getUrl()).userAgent("SonarLint ITs")
                .credentials(SONARLINT_USER, SONARLINT_PWD).build();
    }
}