3 * Copyright (C) 2009-2016 SonarSource SA
4 * mailto:contact AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.server.computation.step;
22 import java.util.Locale;
23 import org.junit.Before;
24 import org.junit.Rule;
25 import org.junit.Test;
26 import org.junit.experimental.categories.Category;
27 import org.sonar.api.i18n.I18n;
28 import org.sonar.api.utils.System2;
29 import org.sonar.batch.protocol.Constants;
30 import org.sonar.batch.protocol.output.BatchReport;
31 import org.sonar.db.DbTester;
32 import org.sonar.server.computation.batch.BatchReportReaderRule;
33 import org.sonar.server.computation.batch.TreeRootHolderRule;
34 import org.sonar.server.computation.component.Component;
35 import org.sonar.server.computation.component.ReportComponent;
36 import org.sonar.server.computation.component.VisitException;
37 import org.sonar.test.DbTests;
39 import static org.assertj.core.api.Assertions.assertThat;
40 import static org.assertj.core.api.Assertions.failBecauseExceptionWasNotThrown;
41 import static org.mockito.Mockito.mock;
42 import static org.mockito.Mockito.when;
44 @Category(DbTests.class)
45 public class PersistProjectLinksStepTest extends BaseStepTest {
48 public DbTester dbTester = DbTester.create(System2.INSTANCE);
51 public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
54 public BatchReportReaderRule reportReader = new BatchReportReaderRule();
56 PersistProjectLinksStep step;
60 I18n i18n = mock(I18n.class);
61 when(i18n.message(Locale.ENGLISH, "project_links.homepage", null)).thenReturn("Home");
62 when(i18n.message(Locale.ENGLISH, "project_links.scm", null)).thenReturn("Sources");
63 when(i18n.message(Locale.ENGLISH, "project_links.scm_dev", null)).thenReturn("Developer connection");
64 when(i18n.message(Locale.ENGLISH, "project_links.ci", null)).thenReturn("Continuous integration");
65 when(i18n.message(Locale.ENGLISH, "project_links.issue", null)).thenReturn("Issues");
67 step = new PersistProjectLinksStep(dbTester.getDbClient(), i18n, treeRootHolder, reportReader);
71 protected ComputationStep step() {
76 public void add_links_on_project_and_module() {
77 dbTester.prepareDbUnit(getClass(), "empty.xml");
79 treeRootHolder.setRoot(ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").addChildren(
80 ReportComponent.builder(Component.Type.MODULE, 2).setUuid("BCDE").build())
83 // project and 1 module
84 reportReader.putComponent(BatchReport.Component.newBuilder()
86 .setType(Constants.ComponentType.PROJECT)
88 .addLink(BatchReport.ComponentLink.newBuilder().setType(Constants.ComponentLinkType.HOME).setHref("http://www.sonarqube.org").build())
89 .addLink(BatchReport.ComponentLink.newBuilder().setType(Constants.ComponentLinkType.SCM).setHref("https://github.com/SonarSource/sonar").build())
90 .addLink(BatchReport.ComponentLink.newBuilder().setType(Constants.ComponentLinkType.SCM_DEV).setHref("scm:git:git@github.com:SonarSource/sonar.git/sonar").build())
91 .addLink(BatchReport.ComponentLink.newBuilder().setType(Constants.ComponentLinkType.ISSUE).setHref("http://jira.sonarsource.com/").build())
92 .addLink(BatchReport.ComponentLink.newBuilder().setType(Constants.ComponentLinkType.CI).setHref("http://bamboo.ci.codehaus.org/browse/SONAR").build())
94 reportReader.putComponent(BatchReport.Component.newBuilder()
96 .setType(Constants.ComponentType.MODULE)
97 .addLink(BatchReport.ComponentLink.newBuilder().setType(Constants.ComponentLinkType.SCM).setHref("https://github.com/SonarSource/sonar/server").build())
102 dbTester.assertDbUnit(getClass(), "add_links_on_project_and_module-result.xml", "project_links");
106 public void nothing_to_do_when_link_already_exists() {
107 dbTester.prepareDbUnit(getClass(), "nothing_to_do_when_link_already_exists.xml");
109 treeRootHolder.setRoot(ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").build());
111 reportReader.putComponent(BatchReport.Component.newBuilder()
113 .setType(Constants.ComponentType.PROJECT)
114 .addLink(BatchReport.ComponentLink.newBuilder().setType(Constants.ComponentLinkType.HOME).setHref("http://www.sonarqube.org").build())
119 dbTester.assertDbUnit(getClass(), "nothing_to_do_when_link_already_exists.xml", "project_links");
123 public void do_not_add_links_on_file() {
124 dbTester.prepareDbUnit(getClass(), "empty.xml");
126 treeRootHolder.setRoot(ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").addChildren(
127 ReportComponent.builder(Component.Type.FILE, 2).setUuid("BCDE").build())
130 reportReader.putComponent(BatchReport.Component.newBuilder()
132 .setType(Constants.ComponentType.PROJECT)
135 reportReader.putComponent(BatchReport.Component.newBuilder()
137 .setType(Constants.ComponentType.FILE)
138 .addLink(BatchReport.ComponentLink.newBuilder().setType(Constants.ComponentLinkType.HOME).setHref("http://www.sonarqube.org").build())
143 assertThat(dbTester.countRowsOfTable("project_links")).isEqualTo(0);
147 public void update_link() {
148 dbTester.prepareDbUnit(getClass(), "update_link.xml");
150 treeRootHolder.setRoot(ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").build());
152 reportReader.putComponent(BatchReport.Component.newBuilder()
154 .setType(Constants.ComponentType.PROJECT)
155 .addLink(BatchReport.ComponentLink.newBuilder().setType(Constants.ComponentLinkType.HOME).setHref("http://www.sonarqube.org").build())
160 dbTester.assertDbUnit(getClass(), "update_link-result.xml", "project_links");
164 public void delete_link() {
165 dbTester.prepareDbUnit(getClass(), "delete_link.xml");
167 treeRootHolder.setRoot(ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").build());
169 reportReader.putComponent(BatchReport.Component.newBuilder()
171 .setType(Constants.ComponentType.PROJECT)
176 assertThat(dbTester.countRowsOfTable("project_links")).isEqualTo(0);
180 public void not_delete_custom_link() {
181 dbTester.prepareDbUnit(getClass(), "not_delete_custom_link.xml");
183 treeRootHolder.setRoot(ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").build());
185 reportReader.putComponent(BatchReport.Component.newBuilder()
187 .setType(Constants.ComponentType.PROJECT)
192 dbTester.assertDbUnit(getClass(), "not_delete_custom_link.xml", "project_links");
196 public void fail_when_trying_to_add_same_link_type_multiple_times() {
197 dbTester.prepareDbUnit(getClass(), "empty.xml");
199 treeRootHolder.setRoot(ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").build());
201 reportReader.putComponent(BatchReport.Component.newBuilder()
203 .setType(Constants.ComponentType.PROJECT)
204 .addLink(BatchReport.ComponentLink.newBuilder().setType(Constants.ComponentLinkType.HOME).setHref("http://www.sonarqube.org").build())
205 .addLink(BatchReport.ComponentLink.newBuilder().setType(Constants.ComponentLinkType.HOME).setHref("http://www.sonarqube.org").build())
210 failBecauseExceptionWasNotThrown(VisitException.class);
211 } catch (VisitException e) {
212 assertThat(e.getCause()).isInstanceOf(IllegalArgumentException.class);
213 assertThat(e.getCause()).hasMessage("Link of type 'homepage' has already been declared on component 'ABCD'");