Skip to content

Commit

Permalink
SONAR-6259 Fix persistence of projects.root_id
Browse files Browse the repository at this point in the history
  • Loading branch information
julienlancelot committed May 22, 2015
1 parent 851e8b5 commit 9970fdc
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 13 deletions.
Expand Up @@ -62,46 +62,50 @@ public void execute(ComputationContext context) {
Map<String, ComponentDto> componentDtosByKey = componentDtosByKey(components); Map<String, ComponentDto> componentDtosByKey = componentDtosByKey(components);
int rootComponentRef = context.getReportMetadata().getRootComponentRef(); int rootComponentRef = context.getReportMetadata().getRootComponentRef();
ComponentContext componentContext = new ComponentContext(context, session, componentDtosByKey); ComponentContext componentContext = new ComponentContext(context, session, componentDtosByKey);
recursivelyProcessComponent(componentContext, rootComponentRef, null); recursivelyProcessComponent(componentContext, rootComponentRef, null, null);
session.commit(); session.commit();
} finally { } finally {
session.close(); session.close();
} }
} }


private void recursivelyProcessComponent(ComponentContext componentContext, int componentRef, @Nullable ComponentDto moduleParent) { private void recursivelyProcessComponent(ComponentContext componentContext, int componentRef, @Nullable ComponentDto parentModule, @Nullable ComponentDto project) {
BatchReportReader reportReader = componentContext.context.getReportReader(); BatchReportReader reportReader = componentContext.context.getReportReader();
BatchReport.Component reportComponent = reportReader.readComponent(componentRef); BatchReport.Component reportComponent = reportReader.readComponent(componentRef);
ComponentDto componentDto = processComponent(componentContext, reportComponent, moduleParent); ComponentDto componentDto = processComponent(componentContext, reportComponent, parentModule, project);
dbComponentsRefCache.addComponent(componentRef, new DbComponentsRefCache.DbComponent(componentDto.getId(), componentDto.getKey(), componentDto.uuid())); dbComponentsRefCache.addComponent(componentRef, new DbComponentsRefCache.DbComponent(componentDto.getId(), componentDto.getKey(), componentDto.uuid()));


for (Integer childRef : reportComponent.getChildRefList()) { for (Integer childRef : reportComponent.getChildRefList()) {
// If current component is not a module or a project, we need to keep the parent reference to the nearest module // If current component is not a module or a project, we need to keep the parent reference to the nearest module
ComponentDto nextModuleParent = !reportComponent.getType().equals(Constants.ComponentType.PROJECT) && !reportComponent.getType().equals(Constants.ComponentType.MODULE) ? ComponentDto nextParent = !reportComponent.getType().equals(Constants.ComponentType.PROJECT) && !reportComponent.getType().equals(Constants.ComponentType.MODULE) ?
moduleParent : componentDto; parentModule : componentDto;
recursivelyProcessComponent(componentContext, childRef, nextModuleParent); // Keep reference to the project
ComponentDto nextProject = reportComponent.getType().equals(Constants.ComponentType.PROJECT) ? componentDto : project;
recursivelyProcessComponent(componentContext, childRef, nextParent, nextProject);
} }
} }


private ComponentDto processComponent(ComponentContext componentContext, BatchReport.Component reportComponent, @Nullable ComponentDto moduleParent) { private ComponentDto processComponent(ComponentContext componentContext, BatchReport.Component reportComponent, @Nullable ComponentDto parentModule,
@Nullable ComponentDto project) {
ComputeComponentsRefCache.ComputeComponent cacheComputeComponent = computeComponentsRefCache.getByRef(reportComponent.getRef()); ComputeComponentsRefCache.ComputeComponent cacheComputeComponent = computeComponentsRefCache.getByRef(reportComponent.getRef());
String componentKey = cacheComputeComponent.getKey(); String componentKey = cacheComputeComponent.getKey();
String componentUuid = cacheComputeComponent.getUuid(); String componentUuid = cacheComputeComponent.getUuid();
ComponentDto existingComponent = componentContext.componentDtosByKey.get(componentKey); ComponentDto existingComponent = componentContext.componentDtosByKey.get(componentKey);
if (existingComponent == null) { if (existingComponent == null) {
ComponentDto component = createComponent(reportComponent, componentKey, componentUuid, moduleParent); ComponentDto component = createComponent(reportComponent, componentKey, componentUuid, parentModule, project);
dbClient.componentDao().insert(componentContext.dbSession, component); dbClient.componentDao().insert(componentContext.dbSession, component);
return component; return component;
} else { } else {
ComponentDto component = createComponent(reportComponent, componentKey, existingComponent.uuid(), moduleParent); ComponentDto component = createComponent(reportComponent, componentKey, existingComponent.uuid(), parentModule, project);
if (updateComponent(existingComponent, component)) { if (updateComponent(existingComponent, component)) {
dbClient.componentDao().update(componentContext.dbSession, existingComponent); dbClient.componentDao().update(componentContext.dbSession, existingComponent);
} }
return existingComponent; return existingComponent;
} }
} }


private ComponentDto createComponent(BatchReport.Component reportComponent, String componentKey, String uuid, @Nullable ComponentDto parentModule) { private ComponentDto createComponent(BatchReport.Component reportComponent, String componentKey, String uuid, @Nullable ComponentDto parentModule,
@Nullable ComponentDto project) {
ComponentDto component = new ComponentDto(); ComponentDto component = new ComponentDto();
component.setUuid(uuid); component.setUuid(uuid);
component.setKey(componentKey); component.setKey(componentKey);
Expand All @@ -125,8 +129,8 @@ private ComponentDto createComponent(BatchReport.Component reportComponent, Stri
component.setLanguage(reportComponent.getLanguage()); component.setLanguage(reportComponent.getLanguage());
} }
} }
if (parentModule != null) { if (parentModule != null && project != null) {
component.setParentProjectId(parentModule.getId()); component.setParentProjectId(component.scope().equals(Scopes.PROJECT) ? project.getId() : parentModule.getId());
component.setProjectUuid(parentModule.projectUuid()); component.setProjectUuid(parentModule.projectUuid());
component.setModuleUuid(parentModule.uuid()); component.setModuleUuid(parentModule.uuid());
component.setModuleUuidPath(reportComponent.getType().equals(Constants.ComponentType.MODULE) ? component.setModuleUuidPath(reportComponent.getType().equals(Constants.ComponentType.MODULE) ?
Expand Down
Expand Up @@ -353,6 +353,79 @@ public void persist_only_new_components() throws Exception {
assertThat(file.parentProjectId()).isEqualTo(module.getId()); assertThat(file.parentProjectId()).isEqualTo(module.getId());
} }


@Test
public void compute_parent_project_id_from_first_module() throws Exception {
computeComponentsRefCache.addComponent(1, new ComputeComponentsRefCache.ComputeComponent("PROJECT_KEY", "ABCD"));
computeComponentsRefCache.addComponent(2, new ComputeComponentsRefCache.ComputeComponent("MODULE_KEY", "BCDE"));
computeComponentsRefCache.addComponent(3, new ComputeComponentsRefCache.ComputeComponent("SUB_MODULE_1_KEY", "CDEF"));
computeComponentsRefCache.addComponent(4, new ComputeComponentsRefCache.ComputeComponent("SUB_MODULE_2_KEY", "DEFG"));
computeComponentsRefCache.addComponent(5, new ComputeComponentsRefCache.ComputeComponent("SUB_MODULE_2_KEY:src/main/java/dir", "EFGH"));

File reportDir = temp.newFolder();
BatchReportWriter writer = new BatchReportWriter(reportDir);
writer.writeMetadata(BatchReport.Metadata.newBuilder()
.setRootComponentRef(1)
.build());

writer.writeComponent(BatchReport.Component.newBuilder()
.setRef(1)
.setType(Constants.ComponentType.PROJECT)
.setKey("PROJECT_KEY")
.setName("Project")
.addChildRef(2)
.build());
writer.writeComponent(BatchReport.Component.newBuilder()
.setRef(2)
.setType(Constants.ComponentType.MODULE)
.setKey("MODULE_KEY")
.setName("Module")
.addChildRef(3)
.build());
writer.writeComponent(BatchReport.Component.newBuilder()
.setRef(3)
.setType(Constants.ComponentType.MODULE)
.setKey("SUB_MODULE_1_KEY")
.setName("Sub Module 1")
.addChildRef(4)
.build());
writer.writeComponent(BatchReport.Component.newBuilder()
.setRef(4)
.setType(Constants.ComponentType.MODULE)
.setKey("SUB_MODULE_2_KEY")
.setName("Sub Module 2")
.addChildRef(5)
.build());
writer.writeComponent(BatchReport.Component.newBuilder()
.setRef(5)
.setType(Constants.ComponentType.DIRECTORY)
.setPath("src/main/java/dir")
.build());

sut.execute(new ComputationContext(new BatchReportReader(reportDir), ComponentTesting.newProjectDto()));

assertThat(dbTester.countRowsOfTable("projects")).isEqualTo(5);

ComponentDto project = dbClient.componentDao().selectNullableByKey(session, "PROJECT_KEY");
assertThat(project).isNotNull();
assertThat(project.parentProjectId()).isNull();

ComponentDto module = dbClient.componentDao().selectNullableByKey(session, "MODULE_KEY");
assertThat(module).isNotNull();
assertThat(module.parentProjectId()).isEqualTo(project.getId());

ComponentDto subModule1 = dbClient.componentDao().selectNullableByKey(session, "SUB_MODULE_1_KEY");
assertThat(subModule1).isNotNull();
assertThat(subModule1.parentProjectId()).isEqualTo(project.getId());

ComponentDto subModule2 = dbClient.componentDao().selectNullableByKey(session, "SUB_MODULE_2_KEY");
assertThat(subModule2).isNotNull();
assertThat(subModule2.parentProjectId()).isEqualTo(project.getId());

ComponentDto directory = dbClient.componentDao().selectNullableByKey(session, "SUB_MODULE_2_KEY:src/main/java/dir");
assertThat(directory).isNotNull();
assertThat(directory.parentProjectId()).isEqualTo(subModule2.getId());
}

@Test @Test
public void nothing_to_persist() throws Exception { public void nothing_to_persist() throws Exception {
computeComponentsRefCache.addComponent(1, new ComputeComponentsRefCache.ComputeComponent("PROJECT_KEY", "ABCD")); computeComponentsRefCache.addComponent(1, new ComputeComponentsRefCache.ComputeComponent("PROJECT_KEY", "ABCD"));
Expand Down Expand Up @@ -561,7 +634,7 @@ public void update_module_uuid_when_moving_a_module() throws Exception {
assertThat(moduleBReloaded.moduleUuid()).isEqualTo(moduleAreloaded.uuid()); assertThat(moduleBReloaded.moduleUuid()).isEqualTo(moduleAreloaded.uuid());
assertThat(moduleBReloaded.moduleUuidPath()).isEqualTo(moduleAreloaded.moduleUuidPath() + moduleBReloaded.uuid() + "."); assertThat(moduleBReloaded.moduleUuidPath()).isEqualTo(moduleAreloaded.moduleUuidPath() + moduleBReloaded.uuid() + ".");
assertThat(moduleBReloaded.projectUuid()).isEqualTo(project.uuid()); assertThat(moduleBReloaded.projectUuid()).isEqualTo(project.uuid());
assertThat(moduleBReloaded.parentProjectId()).isEqualTo(moduleAreloaded.getId()); assertThat(moduleBReloaded.parentProjectId()).isEqualTo(project.getId());


ComponentDto directoryReloaded = dbClient.componentDao().selectNullableByKey(session, "MODULE_B:src/main/java/dir"); ComponentDto directoryReloaded = dbClient.componentDao().selectNullableByKey(session, "MODULE_B:src/main/java/dir");
assertThat(directoryReloaded).isNotNull(); assertThat(directoryReloaded).isNotNull();
Expand Down

0 comments on commit 9970fdc

Please sign in to comment.