import org.sonar.ce.task.step.TestComputationStepContext;
import org.sonar.db.DbClient;
import org.sonar.db.DbTester;
+import org.sonar.db.audit.AuditPersister;
import org.sonar.db.component.ComponentDto;
import org.sonar.db.component.ComponentTesting;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
+import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.sonar.ce.task.projectanalysis.component.ViewAttributes.Type.APPLICATION;
import static org.sonar.ce.task.projectanalysis.component.ViewAttributes.Type.PORTFOLIO;
private static final String PROJECT_VIEW_1_NAME = "PV1_NAME";
private static final String PROJECT_VIEW_1_UUID = "PV1_UUID";
+ private final AuditPersister auditPersister = mock();
+
@Rule
- public DbTester dbTester = DbTester.create(System2.INSTANCE);
+ public DbTester dbTester = DbTester.create(System2.INSTANCE, auditPersister);
@Rule
public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
assertDtoIsProjectView1(pv1Dto, view, view, project);
}
+ @Test
+ public void execute_whenPortfolioProjectAndTechnicalProjectIsCreated_shouldStoreTwoAuditLogs() {
+ ComponentDto view = newViewDto();
+ ComponentDto project = ComponentTesting.newPrivateProjectDto();
+ persistComponents(view, project);
+
+ treeRootHolder.setRoot(
+ createViewBuilder(PORTFOLIO)
+ .addChildren(createProjectView1Builder(project, null).build())
+ .build());
+
+ underTest.execute(new TestComputationStepContext());
+
+ //We create audits for the portfolio and the project, not for the technical project
+ verify(auditPersister, times(2)).addComponent(any(), any());
+ }
+
@Test
public void update_copy_component_uuid_of_project_view() {
ComponentDto view = newViewDto();
@Override
public void visitProjectView(Component projectView, Path<ComponentDtoHolder> path) {
ComponentDto dto = createForProjectView(projectView, path);
- persistComponent(dto);
+ persistComponent(dto, false);
}
private ComponentDto persistComponent(ComponentDto componentDto) {
+ return persistComponent(componentDto, true);
+ }
+
+ private ComponentDto persistComponent(ComponentDto componentDto, boolean shouldPersistAudit) {
ComponentDto existingComponent = existingComponentDtosByUuids.remove(componentDto.uuid());
if (existingComponent == null) {
if (componentDto.qualifier().equals("APP") && componentDto.scope().equals("PRJ")) {
throw new IllegalStateException("Application should already exists: " + componentDto);
}
- dbClient.componentDao().insert(dbSession, componentDto, analysisMetadataHolder.getBranch().isMain());
+ dbClient.componentDao().insert(dbSession, componentDto, shouldPersistAudit);
return componentDto;
}
Optional<ComponentUpdateDto> update = compareForUpdate(existingComponent, componentDto);