import org.sonar.api.component.Component;
import org.sonar.api.component.RubyComponentService;
import org.sonar.api.i18n.I18n;
+import org.sonar.api.resources.Qualifiers;
import org.sonar.api.resources.Scopes;
import org.sonar.core.component.ComponentDto;
import org.sonar.core.component.ComponentKeys;
return componentService.getNullableByUuid(uuid);
}
+ @CheckForNull
public Long createComponent(String kee, String name, String qualifier) {
- ComponentDto component = (ComponentDto) resourceDao.findByKey(kee);
- if (component != null) {
- throw new BadRequestException(formatMessage("Could not create %s, key already exists: %s", qualifier, kee));
- }
- checkKeyFormat(qualifier, kee);
-
- String uuid = UUID.randomUUID().toString();
- resourceDao.insertOrUpdate(
- new ResourceDto()
- .setUuid(uuid)
- .setProjectUuid(uuid)
- .setKey(kee)
- .setDeprecatedKey(kee)
- .setName(name)
- .setLongName(name)
- .setScope(Scopes.PROJECT)
- .setQualifier(qualifier)
- .setCreatedAt(new Date()));
- component = (ComponentDto) resourceDao.findByKey(kee);
- if (component == null) {
- throw new BadRequestException(String.format("Component not created: %s", kee));
+ // Sub view should not be created with provisioning. Will be fixed by http://jira.sonarsource.com/browse/VIEWS-296
+ if (!Qualifiers.SUBVIEW.equals(qualifier)) {
+ ComponentDto component = (ComponentDto) resourceDao.findByKey(kee);
+ if (component != null) {
+ throw new BadRequestException(formatMessage("Could not create %s, key already exists: %s", qualifier, kee));
+ }
+ checkKeyFormat(qualifier, kee);
+
+ String uuid = UUID.randomUUID().toString();
+ resourceDao.insertOrUpdate(
+ new ResourceDto()
+ .setUuid(uuid)
+ .setProjectUuid(uuid)
+ .setKey(kee)
+ .setDeprecatedKey(kee)
+ .setName(name)
+ .setLongName(name)
+ .setScope(Scopes.PROJECT)
+ .setQualifier(qualifier)
+ .setCreatedAt(new Date()));
+ component = (ComponentDto) resourceDao.findByKey(kee);
+ if (component == null) {
+ throw new BadRequestException(String.format("Component not created: %s", kee));
+ }
+ resourceIndexerDao.indexResource(component.getId());
+ return component.getId();
}
- resourceIndexerDao.indexResource(component.getId());
- return component.getId();
+ return null;
}
public void updateComponent(Long id, String key, String name) {
}
@Test
- public void should_create_component_and_index_it() {
+ public void create_component_and_index_it() {
String componentKey = "new-project";
String componentName = "New Project";
String qualifier = Qualifiers.PROJECT;
verify(resourceIndexerDao).indexResource(componentId);
}
+ @Test
+ public void not_create_component_on_sub_views() {
+ String componentKey = "new-project";
+ String componentName = "New Project";
+ String qualifier = Qualifiers.SUBVIEW;
+ long componentId = Long.MAX_VALUE;
+ ComponentDto component = mock(ComponentDto.class);
+ when(component.getId()).thenReturn(componentId);
+ when(resourceDao.findByKey(componentKey)).thenReturn(null).thenReturn(component);
+
+ service.createComponent(componentKey, componentName, qualifier);
+
+ verify(resourceDao, never()).insertOrUpdate(any(ResourceDto.class));
+ verifyZeroInteractions(resourceIndexerDao);
+ }
+
@Test(expected = BadRequestException.class)
public void should_thow_if_create_fails() {
String componentKey = "new-project";
}
@Test
- public void not_migrate_view() throws Exception {
+ public void migrate_view() throws Exception {
db.prepareDbUnit(getClass(), "migrate_view.xml");
migration.execute();
session.commit();
- Component root = mapper.selectComponentByKey("view");
- assertThat(root.getUuid()).isNull();
- assertThat(root.getProjectUuid()).isNull();
- assertThat(root.getModuleUuid()).isNull();
- assertThat(root.getModuleUuidPath()).isNull();
+ Component view = mapper.selectComponentByKey("view");
+ assertThat(view.getUuid()).isNotNull();
+ assertThat(view.getProjectUuid()).isEqualTo(view.getUuid());
+ assertThat(view.getModuleUuid()).isNull();
+ assertThat(view.getModuleUuidPath()).isNull();
+
+ Component subView = mapper.selectComponentByKey("subView");
+ assertThat(subView.getUuid()).isNotNull();
+ assertThat(subView.getProjectUuid()).isEqualTo(view.getUuid());
+ assertThat(subView.getModuleUuid()).isNull();
+ assertThat(subView.getModuleUuidPath()).isEqualTo(view.getUuid());
+
+ Component techProject = mapper.selectComponentByKey("vieworg.struts:struts");
+ assertThat(techProject.getUuid()).isNotNull();
+ assertThat(techProject.getProjectUuid()).isEqualTo(view.getUuid());
+ assertThat(techProject.getModuleUuid()).isEqualTo(subView.getUuid());
+ assertThat(techProject.getModuleUuidPath()).isEqualTo(view.getUuid() + "." + subView.getUuid());
}
@Test
- public void not_migrate_developer() throws Exception {
+ public void migrate_developer() throws Exception {
db.prepareDbUnit(getClass(), "migrate_developer.xml");
migration.execute();
session.commit();
- Component root = mapper.selectComponentByKey("DEV:developer@company.net");
- assertThat(root.getUuid()).isNull();
- assertThat(root.getProjectUuid()).isNull();
- assertThat(root.getModuleUuid()).isNull();
- assertThat(root.getModuleUuidPath()).isNull();
- }
-
- @Test
- public void not_migrate_technical_projects() throws Exception {
- db.prepareDbUnit(getClass(), "not_migrate_technical_projects.xml");
-
- migration.execute();
-
- db.assertDbUnit(getClass(), "not_migrate_technical_projects.xml");
+ Component dev = mapper.selectComponentByKey("DEV:developer@company.net");
+ assertThat(dev.getUuid()).isNotNull();
+ assertThat(dev.getProjectUuid()).isEqualTo(dev.getUuid());
+ assertThat(dev.getModuleUuid()).isNull();
+ assertThat(dev.getModuleUuidPath()).isNull();
+
+ Component techDev = mapper.selectComponentByKey("DEV:developer@company.net:org.struts:struts");
+ assertThat(techDev.getUuid()).isNotNull();
+ assertThat(techDev.getProjectUuid()).isEqualTo(dev.getUuid());
+ assertThat(techDev.getModuleUuid()).isNull();
+ assertThat(techDev.getModuleUuidPath()).isEqualTo(dev.getUuid());
}
}
depth="[null]" scope="PRJ" qualifier="DEV" created_at="2008-12-02 13:58:00.00" build_date="2008-12-02 13:58:00.00"
version="[null]" path=""/>
+ <!-- technical project -->
+ <projects id="2" root_id="1" scope="PRJ" qualifier="DEV_PRJ" kee="DEV:developer@company.net:org.struts:struts" name="Struts"
+ uuid="[null]" project_uuid="[null]" module_uuid="[null]" module_uuid_path="[null]"
+ description="the description" long_name="Apache Struts"
+ enabled="[true]" language="[null]" copy_resource_id="10" person_id="[null]" path="[null]" deprecated_kee="[null]"
+ created_at="2014-06-18" authorization_updated_at="2014-06-18" />
+ <snapshots id="2" project_id="2" parent_snapshot_id="1" root_project_id="1" root_snapshot_id="1"
+ status="P" islast="[true]" purge_status="[null]"
+ period1_mode="[null]" period1_param="[null]" period1_date="[null]"
+ period2_mode="[null]" period2_param="[null]" period2_date="[null]"
+ period3_mode="[null]" period3_param="[null]" period3_date="[null]"
+ period4_mode="[null]" period4_param="[null]" period4_date="[null]"
+ period5_mode="[null]" period5_param="[null]" period5_date="[null]"
+ depth="1" scope="PRJ" qualifier="DEV_PRJ" created_at="2008-12-02 13:58:00.00" build_date="2008-12-02 13:58:00.00"
+ version="[null]" path="1."/>
+
</dataset>
depth="[null]" scope="PRJ" qualifier="VW" created_at="2008-12-02 13:58:00.00" build_date="2008-12-02 13:58:00.00"
version="[null]" path=""/>
+ <!-- sub view -->
+ <projects id="2" kee="subView" name="Sub View" long_name="Sub View" scope="PRJ" qualifier="SVW" root_id="1" description="[null]"
+ uuid="[null]" project_uuid="[null]" module_uuid="[null]" module_uuid_path="[null]"
+ enabled="[true]" language="java" copy_resource_id="[null]" person_id="[null]" path="[null]" deprecated_kee="[null]"
+ created_at="2014-09-01" authorization_updated_at="[null]"/>
+ <snapshots id="2" project_id="2" parent_snapshot_id="1" root_project_id="1" root_snapshot_id="1"
+ status="P" islast="[true]" purge_status="[null]"
+ period1_mode="[null]" period1_param="[null]" period1_date="[null]"
+ period2_mode="[null]" period2_param="[null]" period2_date="[null]"
+ period3_mode="[null]" period3_param="[null]" period3_date="[null]"
+ period4_mode="[null]" period4_param="[null]" period4_date="[null]"
+ period5_mode="[null]" period5_param="[null]" period5_date="[null]"
+ depth="[null]" scope="PRJ" qualifier="SVW" created_at="2008-12-02 13:58:00.00" build_date="2008-12-02 13:58:00.00"
+ version="[null]" path="1."/>
+
+ <!-- technical project -->
+ <projects id="3" root_id="1" scope="FIL" qualifier="TRK" kee="vieworg.struts:struts" name="Struts"
+ uuid="[null]" project_uuid="[null]" module_uuid="[null]" module_uuid_path="[null]"
+ description="the description" long_name="Apache Struts"
+ enabled="[true]" language="[null]" copy_resource_id="10" person_id="[null]" path="[null]" deprecated_kee="[null]"
+ created_at="2014-06-18" authorization_updated_at="2014-06-18" />
+ <snapshots id="3" project_id="3" parent_snapshot_id="2" root_project_id="1" root_snapshot_id="1"
+ status="P" islast="[true]" purge_status="[null]"
+ period1_mode="[null]" period1_param="[null]" period1_date="[null]"
+ period2_mode="[null]" period2_param="[null]" period2_date="[null]"
+ period3_mode="[null]" period3_param="[null]" period3_date="[null]"
+ period4_mode="[null]" period4_param="[null]" period4_date="[null]"
+ period5_mode="[null]" period5_param="[null]" period5_date="[null]"
+ depth="1" scope="FIL" qualifier="TRK" created_at="2008-12-02 13:58:00.00" build_date="2008-12-02 13:58:00.00"
+ version="[null]" path="1.2."/>
+
</dataset>
+++ /dev/null
-<dataset>
-
- <!-- technical project -->
- <projects id="1" root_id="[null]" scope="FIL" qualifier="TRK" kee="OS_PLUGINSorg.struts:struts" name="Struts"
- uuid="[null]" project_uuid="[null]" module_uuid="[null]" module_uuid_path="[null]"
- description="the description" long_name="Apache Struts"
- enabled="[true]" language="[null]" copy_resource_id="10" person_id="[null]" path="[null]" deprecated_kee="[null]"
- created_at="2014-06-18" authorization_updated_at="2014-06-18" />
- <snapshots id="1" project_id="1" parent_snapshot_id="10" root_project_id="1" root_snapshot_id="10"
- status="P" islast="[true]" purge_status="[null]"
- period1_mode="[null]" period1_param="[null]" period1_date="[null]"
- period2_mode="[null]" period2_param="[null]" period2_date="[null]"
- period3_mode="[null]" period3_param="[null]" period3_date="[null]"
- period4_mode="[null]" period4_param="[null]" period4_date="[null]"
- period5_mode="[null]" period5_param="[null]" period5_date="[null]"
- depth="1" scope="FIL" qualifier="TRK" created_at="2008-12-02 13:58:00.00" build_date="2008-12-02 13:58:00.00"
- version="[null]" path="10."/>
-
-</dataset>
+++ /dev/null
-<dataset>
-
- <!-- technical project -->
- <projects id="1" root_id="[null]" scope="FIL" qualifier="TRK" kee="OS_PLUGINSorg.struts:struts" name="Struts"
- uuid="[null]" project_uuid="[null]" module_uuid="[null]" module_uuid_path="[null]"
- description="the description" long_name="Apache Struts"
- enabled="[true]" language="[null]" copy_resource_id="10" person_id="[null]" path="[null]" deprecated_kee="[null]"
- created_at="2014-06-18" authorization_updated_at="2014-06-18" />
- <snapshots id="1" project_id="1" parent_snapshot_id="10" root_project_id="1" root_snapshot_id="10"
- status="P" islast="[true]" purge_status="[null]"
- period1_mode="[null]" period1_param="[null]" period1_date="[null]"
- period2_mode="[null]" period2_param="[null]" period2_date="[null]"
- period3_mode="[null]" period3_param="[null]" period3_date="[null]"
- period4_mode="[null]" period4_param="[null]" period4_date="[null]"
- period5_mode="[null]" period5_param="[null]" period5_date="[null]"
- depth="1" scope="FIL" qualifier="TRK" created_at="2008-12-02 13:58:00.00" build_date="2008-12-02 13:58:00.00"
- version="[null]" path="10."/>
-
-</dataset>
add_column 'projects', :module_uuid, :string, :limit => 50, :null => true
add_column 'projects', :module_uuid_path, :string, :limit => 4000, :null => true
- add_index 'projects', 'uuid', :name => 'projects_uuid'
+ add_index 'projects', 'uuid', :name => 'projects_uuid', :unique => true
end
end
" LEFT OUTER JOIN snapshots s ON s.project_id = p.id AND s.islast = ${_true} " +
" WHERE " +
" p.scope = 'PRJ' " +
- " AND p.root_id IS NULL " +
- " AND p.qualifier <> 'VW' AND p.qualifier <> 'DEV' ")
+ " AND p.root_id IS NULL ")
@Result(javaType = Component.class)
@Options(statementType = StatementType.PREPARED, resultSetType = ResultSetType.FORWARD_ONLY, fetchSize = 200)
List<Component> selectRootProjects();
private Long id;
private String uuid;
private String projectUuid;
+ private String moduleUuid;
+ private String moduleUuidPath;
private String key;
private String deprecatedKey;
private String name;
return this;
}
+ public String getModuleUuid() {
+ return moduleUuid;
+ }
+
+ public ResourceDto setModuleUuid(String moduleUuid) {
+ this.moduleUuid = moduleUuid;
+ return this;
+ }
+
+ public String getModuleUuidPath() {
+ return moduleUuidPath;
+ }
+
+ public ResourceDto setModuleUuidPath(String moduleUuidPath) {
+ this.moduleUuidPath = moduleUuidPath;
+ return this;
+ }
+
public String getName() {
return name;
}
<mapper namespace="org.sonar.core.resource.ResourceMapper">
- <sql id="resourceColumns">
- p.id,
- p.kee as key,
- p.path as path,
- p.deprecated_kee as deprecatedKey,
- p.name as name,
- p.long_name as longName,
- p.root_id as rootId,
- p.scope as scope,
- p.qualifier as qualifier,
- p.enabled as enabled,
- p.description as description,
- p.language as language,
- p.copy_resource_id as copyResourceId,
- p.person_id as personId,
- p.created_at as createdAt,
- p.authorization_updated_at as authorizationUpdatedAt
- </sql>
-
<resultMap id="snapshotResultMap" type="Snapshot">
<id property="id" column="id"/>
<result property="parentId" column="parent_snapshot_id"/>
<resultMap id="resourceResultMap" type="Resource">
<id property="id" column="id"/>
<result property="key" column="kee"/>
+ <result property="uuid" column="uuid"/>
+ <result property="projectUuid" column="project_uuid"/>
+ <result property="moduleUuid" column="module_uuid"/>
+ <result property="moduleUuidPath" column="module_uuid_path"/>
<result property="deprecatedKey" column="deprecated_kee"/>
<result property="path" column="path"/>
<result property="name" column="name"/>
ResourceDto resource = dao.getResource(1L);
+ assertThat(resource.getUuid()).isEqualTo("ABCD");
+ assertThat(resource.getProjectUuid()).isEqualTo("ABCD");
assertThat(resource.getPath()).isNull();
assertThat(resource.getName()).isEqualTo("Struts");
assertThat(resource.getLongName()).isEqualTo("Apache Struts");
assertThat(file.getPath()).isEqualTo("src/org/struts/RequestContext.java");
}
+ @Test
+ public void get_uuid() {
+ setupData("fixture");
+
+ ResourceDto file = dao.getResource(4L);
+ assertThat(file.getUuid()).isEqualTo("CDEF");
+ assertThat(file.getProjectUuid()).isEqualTo("ABCD");
+ assertThat(file.getModuleUuid()).isEqualTo("BCDE");
+ assertThat(file.getModuleUuidPath()).isEqualTo("ABCD.BCDE");
+ }
+
@Test
public void getResource_not_found() {
setupData("fixture");
setupData("shouldInsertAuthorAndDeveloper");
String login = "developer@company.net";
- ResourceDto resourceDto = new ResourceDto().setName(login).setQualifier("DEV");
+ ResourceDto resourceDto = new ResourceDto().setName(login).setQualifier("DEV").setUuid("ABCD").setProjectUuid("ABCD");
dao.insertAuthorAndDeveloper(login, resourceDto);
checkTables("shouldInsertAuthorAndDeveloper",
<!-- root project -->
<projects id="1" root_id="[null]" scope="PRJ" qualifier="TRK" kee="org.struts:struts" name="Struts"
+ uuid="ABCD" project_uuid="ABCD" module_uuid="[null]" module_uuid_path="[null]"
description="the description" long_name="Apache Struts"
enabled="[true]" language="java" copy_resource_id="[null]" person_id="[null]" path="[null]"
created_at="2008-12-02" authorization_updated_at="2014-09-03"/>
<!-- module -->
<projects id="2" root_id="1" kee="org.struts:struts-core" name="Struts Core"
+ uuid="BCDE" project_uuid="ABCD" module_uuid="[null]" module_uuid_path="ABCD"
scope="PRJ" qualifier="BRC" long_name="Struts Core"
description="[null]" enabled="[true]" language="java" copy_resource_id="[null]" person_id="[null]"
created_at="2008-12-02" authorization_updated_at="[null]"/>
<!-- directory -->
<projects long_name="org.struts" id="3" scope="DIR" qualifier="DIR" kee="org.struts:struts-core:src/org/struts"
+ uuid="CDEF" project_uuid="ABCD" module_uuid="BCDE" module_uuid_path="ABCD.BCDE"
name="src/org/struts" root_id="2"
description="[null]"
enabled="[true]" language="java" copy_resource_id="[null]" person_id="[null]" path="src/org/struts"
<!-- file -->
<projects long_name="org.struts.RequestContext" id="4" scope="FIL" qualifier="FIL" kee="org.struts:struts-core:src/org/struts/RequestContext.java"
+ uuid="CDEF" project_uuid="ABCD" module_uuid="BCDE" module_uuid_path="ABCD.BCDE"
name="RequestContext.java" root_id="2"
description="[null]"
enabled="[true]" language="java" copy_resource_id="[null]" person_id="[null]" path="src/org/struts/RequestContext.java"
<dataset>
- <projects id="1" name="developer@company.net" qualifier="DEV" uuid="[null]" project_uuid="[null]" module_uuid="[null]" module_uuid_path="[null]"/>
+ <projects id="1" name="developer@company.net" qualifier="DEV" uuid="ABCD" project_uuid="ABCD" module_uuid="[null]" module_uuid_path="[null]"/>
<authors id="1" person_id="1" login="developer@company.net" />
</dataset>