aboutsummaryrefslogtreecommitdiffstats
path: root/it/it-tests/src/test/java/it/duplication/CrossProjectDuplicationsOnRemoveFileTest.java
blob: 3884cbd59c2e87f73193f2367d6e5a2c5a306688 (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
/*
 * SonarQube Integration Tests :: Tests
 * Copyright (C) 2009-2016 SonarSource SA
 * mailto:contact 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 it.duplication;

import com.sonar.orchestrator.Orchestrator;
import com.sonar.orchestrator.locator.FileLocation;
import com.sonar.orchestrator.selenium.Selenese;
import it.Category4Suite;
import org.apache.commons.io.IOUtils;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;
import org.sonar.wsclient.services.ResourceQuery;
import util.selenium.SeleneseTest;

import static org.assertj.core.api.Assertions.assertThat;
import static org.skyscreamer.jsonassert.JSONAssert.assertEquals;
import static util.ItUtils.runProjectAnalysis;

public class CrossProjectDuplicationsOnRemoveFileTest {

  static final String ORIGIN_PROJECT = "origin-project";
  static final String DUPLICATE_PROJECT = "duplicate-project";
  static final String DUPLICATE_FILE = DUPLICATE_PROJECT + ":src/main/xoo/sample/File1.xoo";

  @ClassRule
  public static Orchestrator orchestrator = Category4Suite.ORCHESTRATOR;

  @BeforeClass
  public static void analyzeProjects() {
    orchestrator.resetData();
    orchestrator.getServer().restoreProfile(FileLocation.ofClasspath("/duplication/xoo-duplication-profile.xml"));

    analyzeProject(ORIGIN_PROJECT, "duplications/cross-project/origin");
    analyzeProject(DUPLICATE_PROJECT, "duplications/cross-project/duplicate");

    // Remove origin project
    orchestrator.getServer().adminWsClient().post("api/projects/bulk_delete", "keys", ORIGIN_PROJECT);
    assertThat(orchestrator.getServer().getAdminWsClient().find(ResourceQuery.create(ORIGIN_PROJECT))).isNull();
  }

  @Test
  public void duplications_show_ws_does_not_contain_key_of_deleted_file() throws Exception {
    String duplication = orchestrator.getServer().adminWsClient().get("api/duplications/show", "key", DUPLICATE_FILE);

    assertEquals(IOUtils.toString(CrossProjectDuplicationsTest.class.getResourceAsStream(
      "/duplication/CrossProjectDuplicationsOnRemoveFileTest/duplications_on_removed_file-expected.json"), "UTF-8"),
      duplication, false);

    // Only one file should be reference, so the reference 2 on origin-project must not exist
    assertThat(duplication).doesNotContain("\"2\"");
    assertThat(duplication).doesNotContain("origin-project");
  }

  /**
   * SONAR-3277
   */
  @Test
  public void display_message_in_viewer_when_duplications_with_deleted_files_are_found() throws Exception {
    // TODO stas, please replace this IT by a medium test
    new SeleneseTest(
      Selenese.builder().setHtmlTestsInClasspath("duplications-on-deleted-project",
        "/duplication/CrossProjectDuplicationsOnRemoveFileTest/duplications-with-deleted-project.html")
        .build())
      .runOn(orchestrator);
  }

  private static void analyzeProject(String projectKey, String path) {
    orchestrator.getServer().provisionProject(projectKey, projectKey);
    orchestrator.getServer().associateProjectToQualityProfile(projectKey, "xoo", "xoo-duplication-profile");

    runProjectAnalysis(orchestrator, path,
      "sonar.cpd.cross_project", "true",
      "sonar.projectKey", projectKey,
      "sonar.projectName", projectKey);
  }

}