@Test
public void getOrCreateForKey_whenNoExistingComponentsInDbForPortfolioAndSubPortfolio_shouldLoadUuidFromPortfolioTable() {
PortfolioDto portfolioDto = ComponentTesting.newPortfolioDto("uuid_ptf1", "ptf1", "Portfolio1", null);
- db.getDbClient().portfolioDao().insert(db.getSession(), portfolioDto);
+ db.getDbClient().portfolioDao().insertWithAudit(db.getSession(), portfolioDto);
PortfolioDto subPortfolio = ComponentTesting.newPortfolioDto("subPtf1", "sub_ptf_1", "portfolio", portfolioDto);
- db.getDbClient().portfolioDao().insert(db.getSession(), subPortfolio);
+ db.getDbClient().portfolioDao().insertWithAudit(db.getSession(), subPortfolio);
ComponentUuidFactory underTest = new ComponentUuidFactoryImpl(db.getDbClient(), db.getSession(), portfolioDto.getKey());
dbClient.branchDao().insert(db.getSession(), branchDto);
ComponentDto fileDto = getComponentDto(projectKey + ":" + mainFile, branchDto.getUuid());
- dbClient.componentDao().insertOnMainBranch(db.getSession(), fileDto);
+ dbClient.componentDao().insertWithAudit(db.getSession(), fileDto);
insertAnticipatedTransition(projectUuid, mainFile);
insertAnticipatedTransition(projectUuid, "file2.js");
PortfolioDto portfolio2 = ComponentTesting.newPortfolioDto("uuid2", "ptf2", "Portfolio 2", null);
PortfolioDto subPortfolio2 = ComponentTesting.newPortfolioDto("sub_uuid21", "sub_ptd2", "SubPortfolio 2", portfolio2);
Arrays.asList(portfolio1, subPortfolio1, subSubPortfolio1, portfolio2, subPortfolio2)
- .forEach(portfolio -> portfolioDao.insert(db.getSession(), portfolio));
+ .forEach(portfolio -> portfolioDao.insertWithAudit(db.getSession(), portfolio));
List<KeyWithUuidDto> keyWithUuidDtos = portfolioDao.selectUuidsByKey(db.getSession(), portfolio1.getKey());
.setUuid("uuid")
.setParentUuid(null)
.setRootUuid("root");
- assertThatThrownBy(() -> portfolioDao.insert(session, portfolio))
+ assertThatThrownBy(() -> portfolioDao.insertWithAudit(session, portfolio))
.isInstanceOf(IllegalArgumentException.class);
PortfolioDto portfolio2 = new PortfolioDto()
.setUuid("uuid")
.setParentUuid("parent")
.setRootUuid("uuid");
- assertThatThrownBy(() -> portfolioDao.insert(session, portfolio2))
+ assertThatThrownBy(() -> portfolioDao.insertWithAudit(session, portfolio2))
.isInstanceOf(IllegalArgumentException.class);
}
.setRootUuid(parent.getRootUuid())
.setParentUuid(parent.getUuid())
.setUuid(uuid);
- db.getDbClient().portfolioDao().insert(session, portfolio);
+ db.getDbClient().portfolioDao().insertWithAudit(session, portfolio);
session.commit();
return portfolio;
}
/*
INSERT / UPDATE
*/
- public void insert(DbSession session, ComponentDto item, boolean isMainBranch) {
+ public void insert(DbSession session, ComponentDto item, boolean shouldPersistAudit) {
mapper(session).insert(item);
- if (isMainBranch) {
+ if (shouldPersistAudit) {
auditPersister.addComponent(session, new ComponentNewValue(item));
}
}
- public void insertOnMainBranch(DbSession session, ComponentDto item) {
+ public void insertWithAudit(DbSession session, ComponentDto item) {
insert(session, item, true);
}
/*
* Modify portfolios
*/
- public void insert(DbSession dbSession, PortfolioDto portfolio) {
+ public void insertWithAudit(DbSession dbSession, PortfolioDto portfolio) {
+ insert(dbSession, portfolio, true);
+ }
+
+ public void insert(DbSession dbSession, PortfolioDto portfolio, boolean shouldPersistAudit) {
checkArgument(portfolio.isRoot() == (portfolio.getUuid().equals(portfolio.getRootUuid())));
mapper(dbSession).insert(portfolio);
- auditPersister.addComponent(dbSession, toComponentNewValue(portfolio));
+ if(shouldPersistAudit) {
+ auditPersister.addComponent(dbSession, toComponentNewValue(portfolio));
+ }
}
public void delete(DbSession dbSession, PortfolioDto portfolio) {
*/
package org.sonar.db.audit;
+import java.util.List;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.DbTester;
dbClient.auditDao().insert(dbSession, auditDto);
db.commit();
}
+
+ public final List<AuditDto> selectAll() {
+ return dbClient.auditDao().selectOlderThan(dbSession, Long.MAX_VALUE);
+ }
}
PortfolioDto portfolioDto = toPortfolioDto(componentDto, System2.INSTANCE.now());
portfolioPopulator.accept(portfolioDto);
- dbClient.portfolioDao().insert(dbSession, portfolioDto);
+ dbClient.portfolioDao().insertWithAudit(dbSession, portfolioDto);
db.commit();
return componentDto;
}
public void fails_with_BRE_if_component_is_not_root() {
String uuid = "uuid";
String key = "key";
- dbClient.componentDao().insertOnMainBranch(dbSession, new ComponentDto()
+ dbClient.componentDao().insertWithAudit(dbSession, new ComponentDto()
.setUuid(uuid)
.setUuidPath(uuid + ".")
.setBranchUuid("branchUuid")
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
-import org.sonar.api.config.Configuration;
import org.sonar.api.resources.Qualifiers;
import org.sonar.api.resources.Scopes;
import org.sonar.api.utils.System2;
import org.sonar.core.util.SequenceUuidFactory;
import org.sonar.db.DbSession;
import org.sonar.db.DbTester;
+import org.sonar.db.audit.AuditPersister;
import org.sonar.db.component.BranchDto;
import org.sonar.db.component.BranchType;
import org.sonar.db.component.ComponentDto;
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.ArgumentMatchers.argThat;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.sonar.api.resources.Qualifiers.APP;
private final System2 system2 = System2.INSTANCE;
+ private final AuditPersister auditPersister = mock();
+
@Rule
- public final DbTester db = DbTester.create(system2);
+ public final DbTester db = DbTester.create(system2, auditPersister);
@Rule
public final I18nRule i18n = new I18nRule().put("qualifier.TRK", "Project");
private final TestIndexers projectIndexers = new TestIndexers();
private final PermissionTemplateService permissionTemplateService = mock(PermissionTemplateService.class);
private final DefaultBranchNameResolver defaultBranchNameResolver = mock(DefaultBranchNameResolver.class);
- private final Configuration config = mock(Configuration.class);
public EsTester es = EsTester.createCustom(new FooIndexDefinition());
private final PermissionUpdater<UserPermissionChange> userPermissionUpdater = new PermissionUpdater(
new IndexersImpl(new PermissionIndexer(db.getDbClient(), es.client())),
verify(permissionTemplateService, never()).applyDefaultToNewComponent(any(), any(), any());
}
+ @Test
+ public void createWithoutCommit_whenInsertingPortfolio_shouldOnlyAddOneEntryToAuditLogs() {
+ String portfolioKey = "portfolio";
+ NewComponent portfolio = NewComponent.newComponentBuilder()
+ .setKey(portfolioKey)
+ .setName(portfolioKey)
+ .setQualifier(VIEW)
+ .build();
+ ComponentCreationParameters creationParameters = ComponentCreationParameters.builder()
+ .newComponent(portfolio)
+ .creationMethod(CreationMethod.LOCAL_API)
+ .build();
+
+ underTest.createWithoutCommit(db.getSession(), creationParameters);
+ db.commit();
+
+ verify(auditPersister, times(1)).addComponent(argThat(d -> d.equals(db.getSession())),
+ argThat(newValue -> newValue.getComponentKey().equals(portfolioKey)));
+ }
+
@Test
public void createWithoutCommit_whenProjectIsManagedAndPrivate_applyPublicPermissionsToCreator() {
UserDto userDto = db.users().insertUser();
import org.sonar.db.component.ProjectData;
import org.sonar.db.component.SnapshotDao;
import org.sonar.db.component.SnapshotDto;
-import org.sonar.db.project.ProjectDto;
import org.sonar.db.protobuf.DbFileSources;
import org.sonar.db.source.FileSourceDto;
import org.sonar.db.user.UserDto;
private ComponentDto insertFile(ComponentDto project) {
ComponentDto file = newFileDto(project);
- componentDao.insertOnMainBranch(db.getSession(), file);
+ componentDao.insertWithAudit(db.getSession(), file);
db.getSession().commit();
return file;
}
public void setUp() {
project = dbTester.components().insertPrivateProject(PROJECT_UUID);
file = ComponentTesting.newFileDto(project.getMainBranchComponent(), null, FILE_UUID).setKey(FILE_KEY);
- dbClient.componentDao().insertOnMainBranch(dbTester.getSession(), file);
+ dbClient.componentDao().insertWithAudit(dbTester.getSession(), file);
dbTester.getSession().commit();
}
}
} else if (isPortfolio(componentDto)) {
portfolioDto = toPortfolioDto(componentDto, now);
- dbClient.portfolioDao().insert(dbSession, portfolioDto);
+ dbClient.portfolioDao().insert(dbSession, portfolioDto, false);
permissionTemplateService.applyDefaultToNewComponent(dbSession, portfolioDto, componentCreationParameters.userUuid());
} else {
throw new IllegalArgumentException("Component " + componentDto + " is not a top level entity");