aboutsummaryrefslogtreecommitdiffstats
path: root/it/it-tests/src/test/java/batch/IssuesModeTest.java
blob: 651546c0535006a2b4b9e1aa7d4d79968d25d79c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/*
 * Copyright (C) 2009-2014 SonarSource SA
 * All rights reserved
 * mailto:contact AT sonarsource DOT com
 */
package batch;

import com.sonar.orchestrator.build.BuildFailureException;

import util.ItUtils;
import com.google.common.collect.Maps;
import com.sonar.orchestrator.Orchestrator;
import com.sonar.orchestrator.build.BuildResult;
import com.sonar.orchestrator.build.SonarRunner;
import com.sonar.orchestrator.build.SonarRunnerInstaller;
import com.sonar.orchestrator.config.FileSystem;
import com.sonar.orchestrator.locator.FileLocation;
import com.sonar.orchestrator.version.Version;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import static org.junit.Assert.*;
import static org.assertj.core.api.Assertions.assertThat;
import org.apache.commons.lang.ObjectUtils;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.junit.Before;
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.SonarClient;
import org.sonar.wsclient.issue.Issue;
import org.sonar.wsclient.issue.IssueQuery;
import org.sonar.wsclient.issue.Issues;
import org.sonar.wsclient.services.Resource;
import org.sonar.wsclient.services.ResourceQuery;
import org.sonar.wsclient.user.UserParameters;

public class IssuesModeTest {

  @ClassRule
  public static Orchestrator orchestrator = Orchestrator.builderEnv()
    .setSonarVersion("DEV")
    .addPlugin(ItUtils.xooPlugin())
    .setContext("/")

    .addPlugin(ItUtils.pluginArtifact("access-secured-props-plugin"))
    .build();

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

  @Rule
  public TemporaryFolder temp = new TemporaryFolder();

  @Before
  public void deleteData() throws IOException {
    orchestrator.resetData();
  }

  @Test
  public void issuesAnalysisOnNewProject() throws IOException {
    restoreProfile("one-issue-per-line.xml");
    orchestrator.getServer().provisionProject("sample", "xoo-sample");
    orchestrator.getServer().associateProjectToQualityProfile("sample", "xoo", "one-issue-per-line");
    SonarRunner runner = configureRunnerIssues("shared/xoo-sample");
    orchestrator.executeBuild(runner);
  }
  
  @Test
  public void invalidIncrementalMode() throws IOException {
    restoreProfile("one-issue-per-line.xml");
    orchestrator.getServer().provisionProject("sample", "xoo-sample");
    orchestrator.getServer().associateProjectToQualityProfile("sample", "xoo", "one-issue-per-line");
    SonarRunner runner = configureRunner("shared/xoo-sample");
    runner.setProperty("sonar.analysis.mode", "incremental");
    
    thrown.expect(BuildFailureException.class);
    BuildResult res = orchestrator.executeBuild(runner);
    
    assertThat(res.getLogs()).contains("Invalid analysis mode: incremental. This mode was removed in SonarQube 5.2");
  }

  // SONAR-5715
  @Test
  public void test_issues_mode_on_project_with_space_in_filename() throws IOException {
    restoreProfile("with-many-rules.xml");
    orchestrator.getServer().provisionProject("sample", "xoo-sample-with-spaces");
    orchestrator.getServer().associateProjectToQualityProfile("sample", "xoo", "with-many-rules");

    SonarRunner runner = configureRunner("batch/xoo-sample-with-spaces/v2");
    BuildResult result = orchestrator.executeBuild(runner);
    assertThat(getResource("sample:my sources/main/xoo/sample/My Sample.xoo")).isNotNull();

    runner = configureRunnerIssues("batch/xoo-sample-with-spaces/v2");
    result = orchestrator.executeBuild(runner);
    // Analysis is not persisted in database
    Resource project = getResource("com.sonarsource.it.samples:simple-sample");
    assertThat(project).isNull();
    assertThat(result.getLogs()).contains("Issues");
    assertThat(result.getLogs()).contains("ANALYSIS SUCCESSFUL");
  }

  @Test
  public void should_not_fail_on_resources_that_have_existed_before() throws IOException {
    restoreProfile("with-many-rules.xml");
    orchestrator.getServer().provisionProject("sample", "xoo-history");
    orchestrator.getServer().associateProjectToQualityProfile("sample", "xoo", "with-many-rules");

    // First real scan with source
    SonarRunner runner = configureRunner("shared/xoo-history-v2");
    BuildResult result = orchestrator.executeBuild(runner);
    assertThat(getResource("sample:src/main/xoo/sample/ClassAdded.xoo")).isNotNull();

    // Second scan should remove ClassAdded.xoo
    runner = configureRunner("shared/xoo-history-v1");
    result = orchestrator.executeBuild(runner);
    assertThat(getResource("sample:src/main/xoo/sample/ClassAdded.xoo")).isNull();

    // Re-add ClassAdded.xoo in local workspace
    runner = configureRunnerIssues("shared/xoo-history-v2");
    result = orchestrator.executeBuild(runner);

    assertThat(getResource("sample:src/main/xoo/sample/ClassAdded.xoo")).isNull();
    assertThat(result.getLogs()).contains("Issues");
    assertThat(result.getLogs()).contains("ANALYSIS SUCCESSFUL");
  }

  @Test
  public void should_fail_if_plugin_access_secured_properties() throws IOException {
    // Test access from task (ie BatchSettings)
    SonarRunner runner = configureRunnerIssues("shared/xoo-sample",
      "accessSecuredFromTask", "true");
    BuildResult result = orchestrator.executeBuildQuietly(runner);

    assertThat(result.getLogs()).contains("Access to the secured property 'foo.bar.secured' is not possible in issues mode. "
      + "The SonarQube plugin which requires this property must be deactivated in issues mode.");

    // Test access from sensor (ie ModuleSettings)
    runner = configureRunnerIssues("shared/xoo-sample",
      "accessSecuredFromSensor", "true");
    result = orchestrator.executeBuildQuietly(runner);

    assertThat(result.getLogs()).contains("Access to the secured property 'foo.bar.secured' is not possible in issues mode. "
      + "The SonarQube plugin which requires this property must be deactivated in issues mode.");
  }

  // SONAR-4602
  @Test
  public void no_issues_mode_cache_after_new_analysis() throws Exception {
    restoreProfile("one-issue-per-line.xml");
    restoreProfile("empty.xml");
    orchestrator.getServer().provisionProject("sample", "xoo-sample");

    // First run (publish mode)
    orchestrator.getServer().associateProjectToQualityProfile("sample", "xoo", "empty");
    SonarRunner runner = configureRunner("shared/xoo-sample");
    orchestrator.executeBuild(runner);

    // First run issues mode
    orchestrator.getServer().associateProjectToQualityProfile("sample", "xoo", "one-issue-per-line");
    runner = configureRunnerIssues("shared/xoo-sample");
    BuildResult result = orchestrator.executeBuild(runner);

    // As many new issue as lines
    assertThat(ItUtils.countIssuesInJsonReport(result, true)).isEqualTo(17);

    // Second run (publish mode) should invalidate cache
    runner = configureRunner("shared/xoo-sample");
    orchestrator.executeBuild(runner);

    // Second run issues mode
    runner = configureRunnerIssues("shared/xoo-sample", "sonar.report.export.path", "sonar-report.json");
    result = orchestrator.executeBuild(runner);

    // No new issue this time
    assertThat(ItUtils.countIssuesInJsonReport(result, true)).isEqualTo(0);
  }

  // SONAR-4602
  @Test
  public void no_issues_mode_cache_after_profile_change() throws Exception {
    restoreProfile("one-issue-per-line-empty.xml");
    orchestrator.getServer().provisionProject("sample", "xoo-sample");
    orchestrator.getServer().associateProjectToQualityProfile("sample", "xoo", "one-issue-per-line");

    // First run (publish mode)
    SonarRunner runner = configureRunner("shared/xoo-sample");
    orchestrator.executeBuild(runner);

    // First issues mode
    runner = configureRunnerIssues("shared/xoo-sample");
    BuildResult result = orchestrator.executeBuild(runner);

    // No new issues
    assertThat(ItUtils.countIssuesInJsonReport(result, true)).isEqualTo(0);

    // Modification of QP should invalidate cache
    restoreProfile("/one-issue-per-line.xml");

    // Second issues mode
    runner = configureRunnerIssues("shared/xoo-sample", "sonar.report.export.path", "sonar-report.json");
    result = orchestrator.executeBuild(runner);

    // As many new issue as lines
    assertThat(ItUtils.countIssuesInJsonReport(result, true)).isEqualTo(17);
  }

  // SONAR-4602
  @Test
  public void no_issues_mode_cache_after_issue_change() throws Exception {
    restoreProfile("one-issue-per-line.xml");
    orchestrator.getServer().provisionProject("sample", "xoo-sample");
    orchestrator.getServer().associateProjectToQualityProfile("sample", "xoo", "one-issue-per-line");

    // First run (publish mode)
    SonarRunner runner = configureRunner("shared/xoo-sample");
    orchestrator.executeBuild(runner);

    // First issues mode
    runner = configureRunnerIssues("shared/xoo-sample");
    BuildResult result = orchestrator.executeBuild(runner);

    // 17 issues
    assertThat(ItUtils.countIssuesInJsonReport(result, false)).isEqualTo(17);

    // Flag one issue as false positive
    JSONObject obj = ItUtils.getJSONReport(result);
    String key = ((JSONObject) ((JSONArray) obj.get("issues")).get(0)).get("key").toString();
    orchestrator.getServer().adminWsClient().issueClient().doTransition(key, "falsepositive");

    // Second issues mode
    runner = configureRunnerIssues("shared/xoo-sample");
    result = orchestrator.executeBuild(runner);

    // False positive is not returned
    assertThat(ItUtils.countIssuesInJsonReport(result, false)).isEqualTo(16);
  }

  // SONAR-6522
  @Test
  public void load_user_name_in_json_report() throws Exception {
    restoreProfile("one-issue-per-line.xml");
    orchestrator.getServer().provisionProject("sample", "xoo-sample");
    orchestrator.getServer().associateProjectToQualityProfile("sample", "xoo", "one-issue-per-line");

    // First run (publish mode)
    SonarRunner runner = configureRunner("shared/xoo-sample");
    orchestrator.executeBuild(runner);

    SonarClient client = orchestrator.getServer().adminWsClient();

    Issues issues = client.issueClient().find(IssueQuery.create());
    Issue issue = issues.list().get(0);

    UserParameters creationParameters = UserParameters.create().login("julien").name("Julien H")
      .password("password").passwordConfirmation("password");
    client.userClient().create(creationParameters);

    // Assign issue
    client.issueClient().assign(issue.key(), "julien");

    // Issues
    runner = configureRunnerIssues("shared/xoo-sample");
    BuildResult result = orchestrator.executeBuild(runner);

    JSONObject obj = ItUtils.getJSONReport(result);

    Map<String, String> userNameByLogin = Maps.newHashMap();
    final JSONArray users = (JSONArray) obj.get("users");
    if (users != null) {
      for (Object user : users) {
        String login = ObjectUtils.toString(((JSONObject) user).get("login"));
        String name = ObjectUtils.toString(((JSONObject) user).get("name"));
        userNameByLogin.put(login, name);
      }
    }
    assertThat(userNameByLogin.get("julien")).isEqualTo("Julien H");

    for (Object issueJson : (JSONArray) obj.get("issues")) {
      JSONObject jsonObject = (JSONObject) issueJson;
      if (issue.key().equals(jsonObject.get("key"))) {
        assertThat(jsonObject.get("assignee")).isEqualTo("julien");
        return;
      }
    }
    fail("Issue not found");
  }

  @Test
  public void concurrent_issue_mode_on_existing_project() throws Exception {
    restoreProfile("one-issue-per-line.xml");
    orchestrator.getServer().provisionProject("sample", "xoo-sample");
    orchestrator.getServer().associateProjectToQualityProfile("sample", "xoo", "one-issue-per-line");

    SonarRunner runner = configureRunner("shared/xoo-sample");
    orchestrator.executeBuild(runner);

    runConcurrentIssues();
  }

  private void runConcurrentIssues() throws InterruptedException, ExecutionException {
    // Install sonar-runner in advance to avoid concurrent unzip issues
    FileSystem fileSystem = orchestrator.getConfiguration().fileSystem();
    new SonarRunnerInstaller(fileSystem).install(Version.create(SonarRunner.DEFAULT_RUNNER_VERSION), fileSystem.workspace());
    final int nThreads = 3;
    ExecutorService executorService = Executors.newFixedThreadPool(nThreads);
    List<Callable<BuildResult>> tasks = new ArrayList<>();
    for (int i = 0; i < nThreads; i++) {
      tasks.add(new Callable<BuildResult>() {

        public BuildResult call() throws Exception {
          SonarRunner runner = configureRunnerIssues("shared/xoo-sample");
          return orchestrator.executeBuild(runner);
        }
      });
    }

    boolean expectedError = false;
    for (Future<BuildResult> result : executorService.invokeAll(tasks)) {
      try {
      result.get();
      } catch(ExecutionException e) {
        if(e.getCause() instanceof BuildFailureException) {
          BuildFailureException bfe = (BuildFailureException) e.getCause();
          assertThat(bfe.getResult().getLogs()).contains("Another SonarQube analysis is already in progress for this project");
          expectedError = true;
        }
      }
    }
    if(!expectedError) {
      fail("At least one of the threads should have failed");
    }
  }

  private void restoreProfile(String fileName) {
    orchestrator.getServer().restoreProfile(FileLocation.ofClasspath("/batch/IssuesModeTest/" + fileName));
  }

  private Resource getResource(String key) {
    return orchestrator.getServer().getWsClient().find(ResourceQuery.createForMetrics(key, "lines"));
  }

  private SonarRunner configureRunnerIssues(String projectDir, String... props) throws IOException {
    SonarRunner runner = SonarRunner.create(ItUtils.projectDir(projectDir),
      "sonar.working.directory", temp.newFolder().getAbsolutePath(),
      "sonar.analysis.mode", "issues",
      "sonar.report.export.path", "sonar-report.json",
      "sonar.userHome", temp.newFolder().getAbsolutePath());
    runner.setProperties(props);
    return runner;
  }

  private SonarRunner configureRunner(String projectDir, String... props) throws IOException {
    SonarRunner runner = SonarRunner.create(ItUtils.projectDir(projectDir),
      "sonar.working.directory", temp.newFolder().getAbsolutePath(),
      "sonar.report.export.path", "sonar-report.json",
      "sonar.userHome", temp.newFolder().getAbsolutePath());
    runner.setProperties(props);
    return runner;
  }

}