]> source.dussan.org Git - sonarqube.git/blob
611137e3e469b7867e84d1fcf70576a5a1eb4fc2
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2016 SonarSource SA
4  * mailto:contact AT sonarsource DOT com
5  *
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.
10  *
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.
15  *
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.
19  */
20 package org.sonar.server.computation.step;
21
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;
38
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;
43
44 @Category(DbTests.class)
45 public class PersistProjectLinksStepTest extends BaseStepTest {
46
47   @Rule
48   public DbTester dbTester = DbTester.create(System2.INSTANCE);
49
50   @Rule
51   public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
52
53   @Rule
54   public BatchReportReaderRule reportReader = new BatchReportReaderRule();
55
56   PersistProjectLinksStep step;
57
58   @Before
59   public void setup() {
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");
66
67     step = new PersistProjectLinksStep(dbTester.getDbClient(), i18n, treeRootHolder, reportReader);
68   }
69
70   @Override
71   protected ComputationStep step() {
72     return step;
73   }
74
75   @Test
76   public void add_links_on_project_and_module() {
77     dbTester.prepareDbUnit(getClass(), "empty.xml");
78
79     treeRootHolder.setRoot(ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").addChildren(
80       ReportComponent.builder(Component.Type.MODULE, 2).setUuid("BCDE").build())
81       .build());
82
83     // project and 1 module
84     reportReader.putComponent(BatchReport.Component.newBuilder()
85       .setRef(1)
86       .setType(Constants.ComponentType.PROJECT)
87       .addChildRef(2)
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())
93       .build());
94     reportReader.putComponent(BatchReport.Component.newBuilder()
95       .setRef(2)
96       .setType(Constants.ComponentType.MODULE)
97       .addLink(BatchReport.ComponentLink.newBuilder().setType(Constants.ComponentLinkType.SCM).setHref("https://github.com/SonarSource/sonar/server").build())
98       .build());
99
100     step.execute();
101
102     dbTester.assertDbUnit(getClass(), "add_links_on_project_and_module-result.xml", "project_links");
103   }
104
105   @Test
106   public void nothing_to_do_when_link_already_exists() {
107     dbTester.prepareDbUnit(getClass(), "nothing_to_do_when_link_already_exists.xml");
108
109     treeRootHolder.setRoot(ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").build());
110
111     reportReader.putComponent(BatchReport.Component.newBuilder()
112       .setRef(1)
113       .setType(Constants.ComponentType.PROJECT)
114       .addLink(BatchReport.ComponentLink.newBuilder().setType(Constants.ComponentLinkType.HOME).setHref("http://www.sonarqube.org").build())
115       .build());
116
117     step.execute();
118
119     dbTester.assertDbUnit(getClass(), "nothing_to_do_when_link_already_exists.xml", "project_links");
120   }
121
122   @Test
123   public void do_not_add_links_on_file() {
124     dbTester.prepareDbUnit(getClass(), "empty.xml");
125
126     treeRootHolder.setRoot(ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").addChildren(
127       ReportComponent.builder(Component.Type.FILE, 2).setUuid("BCDE").build())
128       .build());
129
130     reportReader.putComponent(BatchReport.Component.newBuilder()
131       .setRef(1)
132       .setType(Constants.ComponentType.PROJECT)
133       .addChildRef(2)
134       .build());
135     reportReader.putComponent(BatchReport.Component.newBuilder()
136       .setRef(2)
137       .setType(Constants.ComponentType.FILE)
138       .addLink(BatchReport.ComponentLink.newBuilder().setType(Constants.ComponentLinkType.HOME).setHref("http://www.sonarqube.org").build())
139       .build());
140
141     step.execute();
142
143     assertThat(dbTester.countRowsOfTable("project_links")).isEqualTo(0);
144   }
145
146   @Test
147   public void update_link() {
148     dbTester.prepareDbUnit(getClass(), "update_link.xml");
149
150     treeRootHolder.setRoot(ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").build());
151
152     reportReader.putComponent(BatchReport.Component.newBuilder()
153       .setRef(1)
154       .setType(Constants.ComponentType.PROJECT)
155       .addLink(BatchReport.ComponentLink.newBuilder().setType(Constants.ComponentLinkType.HOME).setHref("http://www.sonarqube.org").build())
156       .build());
157
158     step.execute();
159
160     dbTester.assertDbUnit(getClass(), "update_link-result.xml", "project_links");
161   }
162
163   @Test
164   public void delete_link() {
165     dbTester.prepareDbUnit(getClass(), "delete_link.xml");
166
167     treeRootHolder.setRoot(ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").build());
168
169     reportReader.putComponent(BatchReport.Component.newBuilder()
170       .setRef(1)
171       .setType(Constants.ComponentType.PROJECT)
172       .build());
173
174     step.execute();
175
176     assertThat(dbTester.countRowsOfTable("project_links")).isEqualTo(0);
177   }
178
179   @Test
180   public void not_delete_custom_link() {
181     dbTester.prepareDbUnit(getClass(), "not_delete_custom_link.xml");
182
183     treeRootHolder.setRoot(ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").build());
184
185     reportReader.putComponent(BatchReport.Component.newBuilder()
186       .setRef(1)
187       .setType(Constants.ComponentType.PROJECT)
188       .build());
189
190     step.execute();
191
192     dbTester.assertDbUnit(getClass(), "not_delete_custom_link.xml", "project_links");
193   }
194
195   @Test
196   public void fail_when_trying_to_add_same_link_type_multiple_times() {
197     dbTester.prepareDbUnit(getClass(), "empty.xml");
198
199     treeRootHolder.setRoot(ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").build());
200
201     reportReader.putComponent(BatchReport.Component.newBuilder()
202       .setRef(1)
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())
206       .build());
207
208     try {
209       step.execute();
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'");
214     }
215   }
216 }