2 * SonarQube, open source software quality management tool.
3 * Copyright (C) 2008-2014 SonarSource
4 * mailto:contact AT sonarsource DOT com
6 * SonarQube 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 * SonarQube 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.
21 package org.sonar.server.db.migrations.v50;
23 import com.google.common.collect.ImmutableSet;
24 import org.junit.After;
25 import org.junit.Before;
26 import org.junit.ClassRule;
27 import org.junit.Test;
28 import org.sonar.core.persistence.DbSession;
29 import org.sonar.core.persistence.DbTester;
30 import org.sonar.core.persistence.migration.v50.Component;
31 import org.sonar.core.persistence.migration.v50.Migration50Mapper;
32 import org.sonar.server.db.DbClient;
33 import org.sonar.server.db.migrations.DatabaseMigration;
35 import static org.assertj.core.api.Assertions.assertThat;
37 public class PopulateProjectsUuidColumnsMigrationTest {
40 public static DbTester db = new DbTester().schema(PopulateProjectsUuidColumnsMigrationTest.class, "schema.sql");
46 Migration50Mapper mapper;
48 DatabaseMigration migration;
51 public void setUp() throws Exception {
52 db.executeUpdateSql("truncate table projects");
53 db.executeUpdateSql("truncate table snapshots");
54 dbClient = new DbClient(db.database(), db.myBatis());
55 session = dbClient.openSession(false);
56 mapper = session.getMapper(Migration50Mapper.class);
57 migration = new PopulateProjectsUuidColumnsMigration(dbClient);
61 public void tearDown() throws Exception {
66 public void migrate_components() throws Exception {
67 db.prepareDbUnit(getClass(), "migrate_components.xml");
72 Component root = mapper.selectComponentByKey("org.struts:struts");
73 assertThat(root.getUuid()).isNotNull();
74 assertThat(root.getProjectUuid()).isEqualTo(root.getUuid());
75 assertThat(root.getModuleUuid()).isNull();
76 assertThat(root.getModuleUuidPath()).isEmpty();
78 Component module = mapper.selectComponentByKey("org.struts:struts-core");
79 assertThat(module.getUuid()).isNotNull();
80 assertThat(module.getProjectUuid()).isEqualTo(root.getUuid());
81 assertThat(module.getModuleUuid()).isEqualTo(root.getUuid());
82 assertThat(module.getModuleUuidPath()).isEqualTo(root.getUuid());
84 Component subModule = mapper.selectComponentByKey("org.struts:struts-db");
85 assertThat(subModule.getUuid()).isNotNull();
86 assertThat(subModule.getProjectUuid()).isEqualTo(root.getUuid());
87 assertThat(subModule.getModuleUuid()).isEqualTo(module.getUuid());
88 assertThat(subModule.getModuleUuidPath()).isEqualTo(root.getUuid() + "." + module.getUuid());
90 Component directory = mapper.selectComponentByKey("org.struts:struts-core:src/org/struts");
91 assertThat(directory.getUuid()).isNotNull();
92 assertThat(directory.getProjectUuid()).isEqualTo(root.getUuid());
93 assertThat(directory.getModuleUuid()).isEqualTo(subModule.getUuid());
94 assertThat(directory.getModuleUuidPath()).isEqualTo(root.getUuid() + "." + module.getUuid() + "." + subModule.getUuid());
96 Component file = mapper.selectComponentByKey("org.struts:struts-core:src/org/struts/RequestContext.java");
97 assertThat(file.getUuid()).isNotNull();
98 assertThat(file.getProjectUuid()).isEqualTo(root.getUuid());
99 assertThat(file.getModuleUuid()).isEqualTo(subModule.getUuid());
100 assertThat(file.getModuleUuidPath()).isEqualTo(root.getUuid() + "." + module.getUuid() + "." + subModule.getUuid());
102 // Verify that each generated uuid is unique
103 assertThat(ImmutableSet.of(root.getUuid(), module.getUuid(), subModule.getUuid(), directory.getUuid(), file.getUuid())).hasSize(5);
107 public void not_migrate_already_migrated_components() throws Exception {
108 db.prepareDbUnit(getClass(), "not_migrate_already_migrated_components.xml");
113 Component root = mapper.selectComponentByKey("org.struts:struts");
114 assertThat(root.getUuid()).isEqualTo("ABCD");
115 assertThat(root.getProjectUuid()).isEqualTo("ABCD");
116 assertThat(root.getModuleUuid()).isNull();
117 assertThat(root.getModuleUuidPath()).isEmpty();
119 Component module = mapper.selectComponentByKey("org.struts:struts-core");
120 assertThat(module.getUuid()).isEqualTo("BCDE");
121 assertThat(module.getProjectUuid()).isEqualTo("ABCD");
122 assertThat(module.getModuleUuid()).isEqualTo("ABCD");
123 assertThat(module.getModuleUuidPath()).isEqualTo("ABCD");
125 Component subModule = mapper.selectComponentByKey("org.struts:struts-db");
126 assertThat(subModule.getUuid()).isNotNull();
127 assertThat(subModule.getProjectUuid()).isEqualTo("ABCD");
128 assertThat(subModule.getModuleUuid()).isEqualTo("BCDE");
129 assertThat(subModule.getModuleUuidPath()).isEqualTo("ABCD.BCDE");
131 Component directory = mapper.selectComponentByKey("org.struts:struts-core:src/org/struts");
132 assertThat(directory.getUuid()).isNotNull();
133 assertThat(directory.getProjectUuid()).isEqualTo("ABCD");
134 assertThat(directory.getModuleUuid()).isEqualTo(subModule.getUuid());
135 assertThat(directory.getModuleUuidPath()).isEqualTo("ABCD.BCDE." + subModule.getUuid());
137 Component file = mapper.selectComponentByKey("org.struts:struts-core:src/org/struts/RequestContext.java");
138 assertThat(file.getUuid()).isNotNull();
139 assertThat(file.getProjectUuid()).isEqualTo("ABCD");
140 assertThat(file.getModuleUuid()).isEqualTo(subModule.getUuid());
141 assertThat(file.getModuleUuidPath()).isEqualTo("ABCD.BCDE." + subModule.getUuid());
143 Component removedFile = mapper.selectComponentByKey("org.struts:struts-core:src/org/struts/RequestContext2.java");
144 assertThat(removedFile.getUuid()).isEqualTo("DCBA");
145 assertThat(removedFile.getProjectUuid()).isEqualTo("ABCD");
146 assertThat(removedFile.getModuleUuid()).isEqualTo("BCDE");
147 assertThat(removedFile.getModuleUuidPath()).isEqualTo("ABCD.BCDE");
151 public void migrate_disable_components() throws Exception {
152 db.prepareDbUnit(getClass(), "migrate_disable_components.xml");
157 Component root = mapper.selectComponentByKey("org.struts:struts");
158 assertThat(root.getUuid()).isNotNull();
160 Component module = mapper.selectComponentByKey("org.struts:struts-core");
161 assertThat(module.getUuid()).isNotNull();
162 assertThat(module.getProjectUuid()).isEqualTo(root.getUuid());
163 // Module and module path will always be null for removed components
164 assertThat(module.getModuleUuid()).isNull();
165 assertThat(module.getModuleUuidPath()).isEmpty();
167 Component subModule = mapper.selectComponentByKey("org.struts:struts-db");
168 assertThat(subModule.getUuid()).isNotNull();
169 assertThat(subModule.getProjectUuid()).isEqualTo(root.getUuid());
170 // Module and module path will always be null for removed components
171 assertThat(subModule.getModuleUuid()).isNull();
172 assertThat(subModule.getModuleUuidPath()).isEmpty();
174 Component directory = mapper.selectComponentByKey("org.struts:struts-core:src/org/struts");
175 assertThat(directory.getUuid()).isNotNull();
176 assertThat(directory.getProjectUuid()).isEqualTo(root.getUuid());
177 // Module and module path will always be null for removed components
178 assertThat(directory.getModuleUuid()).isNull();
179 assertThat(directory.getModuleUuidPath()).isEmpty();
181 Component file = mapper.selectComponentByKey("org.struts:struts-core:src/org/struts/RequestContext.java");
182 assertThat(file.getUuid()).isNotNull();
183 assertThat(file.getProjectUuid()).isEqualTo(root.getUuid());
184 // Module and module path will always be null for removed components
185 assertThat(file.getModuleUuid()).isNull();
186 assertThat(file.getModuleUuidPath()).isEmpty();
190 public void migrate_provisioned_project() throws Exception {
191 db.prepareDbUnit(getClass(), "migrate_provisioned_project.xml");
196 Component root = mapper.selectComponentByKey("org.struts:struts");
197 assertThat(root.getUuid()).isNotNull();
198 assertThat(root.getProjectUuid()).isEqualTo(root.getUuid());
199 assertThat(root.getModuleUuid()).isNull();
200 assertThat(root.getModuleUuidPath()).isEmpty();
204 public void migrate_library() throws Exception {
205 db.prepareDbUnit(getClass(), "migrate_library.xml");
210 Component root = mapper.selectComponentByKey("org.hamcrest:hamcrest-library");
211 assertThat(root.getUuid()).isNotNull();
212 assertThat(root.getProjectUuid()).isEqualTo(root.getUuid());
213 assertThat(root.getModuleUuid()).isNull();
214 assertThat(root.getModuleUuidPath()).isEmpty();
218 public void migrate_view() throws Exception {
219 db.prepareDbUnit(getClass(), "migrate_view.xml");
224 Component view = mapper.selectComponentByKey("view");
225 assertThat(view.getUuid()).isNotNull();
226 assertThat(view.getProjectUuid()).isEqualTo(view.getUuid());
227 assertThat(view.getModuleUuid()).isNull();
228 assertThat(view.getModuleUuidPath()).isEmpty();
230 Component subView = mapper.selectComponentByKey("subView");
231 assertThat(subView.getUuid()).isNotNull();
232 assertThat(subView.getProjectUuid()).isEqualTo(view.getUuid());
233 assertThat(subView.getModuleUuid()).isEqualTo(view.getUuid());
234 assertThat(subView.getModuleUuidPath()).isEqualTo(view.getUuid());
236 Component techProject = mapper.selectComponentByKey("vieworg.struts:struts");
237 assertThat(techProject.getUuid()).isNotNull();
238 assertThat(techProject.getProjectUuid()).isEqualTo(view.getUuid());
239 assertThat(techProject.getModuleUuid()).isEqualTo(subView.getUuid());
240 assertThat(techProject.getModuleUuidPath()).isEqualTo(view.getUuid() + "." + subView.getUuid());
244 public void migrate_developer() throws Exception {
245 db.prepareDbUnit(getClass(), "migrate_developer.xml");
250 Component dev = mapper.selectComponentByKey("DEV:developer@company.net");
251 assertThat(dev.getUuid()).isNotNull();
252 assertThat(dev.getProjectUuid()).isEqualTo(dev.getUuid());
253 assertThat(dev.getModuleUuid()).isNull();
254 assertThat(dev.getModuleUuidPath()).isEmpty();
256 Component techDev = mapper.selectComponentByKey("DEV:developer@company.net:org.struts:struts");
257 assertThat(techDev.getUuid()).isNotNull();
258 assertThat(techDev.getProjectUuid()).isEqualTo(dev.getUuid());
259 assertThat(techDev.getModuleUuid()).isEqualTo(dev.getUuid());
260 assertThat(techDev.getModuleUuidPath()).isEqualTo(dev.getUuid());
264 public void migrate_components_without_uuid() throws Exception {
265 db.prepareDbUnit(getClass(), "migrate_components_without_uuid.xml");
270 // Root project migrated
271 Component root = mapper.selectComponentByKey("org.struts:struts");
272 assertThat(root.getUuid()).isNotNull();
273 assertThat(root.getProjectUuid()).isEqualTo(root.getUuid());
274 assertThat(root.getModuleUuid()).isNull();
275 assertThat(root.getModuleUuidPath()).isEmpty();
277 // Module with a snapshot having no islast=true
278 Component module = mapper.selectComponentByKey("org.struts:struts-core");
279 assertThat(module.getUuid()).isNotNull();
280 assertThat(module.getProjectUuid()).isEqualTo(module.getUuid());
281 assertThat(module.getModuleUuid()).isNull();
282 assertThat(module.getModuleUuidPath()).isEmpty();
284 // File linked on a no more existing project
285 Component file = mapper.selectComponentByKey("org.struts:struts-core:src/org/struts/RequestContext.java");
286 assertThat(file.getUuid()).isNotNull();
287 assertThat(file.getProjectUuid()).isEqualTo(file.getUuid());
288 assertThat(file.getModuleUuid()).isNull();
289 assertThat(file.getModuleUuidPath()).isEmpty();