Browse Source

SONAR-12689 migrated tags, updated test framework

tags/8.2.0.32929
Michal Duda 4 years ago
parent
commit
1ad6b11311
51 changed files with 587 additions and 334 deletions
  1. 8
    10
      server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/LoadReportAnalysisMetadataHolderStep.java
  2. 15
    2
      server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/ReportPersistComponentsStepTest.java
  3. 0
    9
      server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentDao.java
  4. 0
    29
      server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentDto.java
  5. 0
    2
      server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentMapper.java
  6. 1
    1
      server/sonar-db-dao/src/main/java/org/sonar/db/component/DbTagsReader.java
  7. 5
    4
      server/sonar-db-dao/src/main/java/org/sonar/db/measure/ProjectMeasuresIndexerIterator.java
  8. 4
    0
      server/sonar-db-dao/src/main/java/org/sonar/db/project/ProjectDao.java
  9. 3
    1
      server/sonar-db-dao/src/main/java/org/sonar/db/project/ProjectMapper.java
  10. 10
    0
      server/sonar-db-dao/src/main/java/org/sonar/db/project/ProjectQuery.java
  11. 0
    10
      server/sonar-db-dao/src/main/resources/org/sonar/db/component/ComponentMapper.xml
  12. 7
    0
      server/sonar-db-dao/src/main/resources/org/sonar/db/project/ProjectMapper.xml
  13. 0
    1
      server/sonar-db-dao/src/schema/schema-sq.ddl
  14. 21
    26
      server/sonar-db-dao/src/test/java/org/sonar/db/component/ComponentDaoTest.java
  15. 12
    4
      server/sonar-db-dao/src/test/java/org/sonar/db/measure/ProjectMeasuresIndexerIteratorTest.java
  16. 1
    1
      server/sonar-db-dao/src/test/java/org/sonar/db/purge/PurgeMapperTest.java
  17. 12
    12
      server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/QualityProfileDaoTest.java
  18. 130
    98
      server/sonar-db-dao/src/testFixtures/java/org/sonar/db/component/ComponentDbTester.java
  19. 4
    0
      server/sonar-db-dao/src/testFixtures/java/org/sonar/db/component/SnapshotTesting.java
  20. 1
    0
      server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v81/DbVersion81.java
  21. 40
    0
      server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v81/DropTagsColumnFromComponentsTable.java
  22. 1
    1
      server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v81/DbVersion81Test.java
  23. 56
    0
      server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v81/DropTagsColumnFromComponentsTableTest.java
  24. 47
    0
      server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v81/DropTagsColumnFromComponentsTableTest/schema.sql
  25. 1
    0
      server/sonar-server-common/src/main/java/org/sonar/server/es/ProjectIndexers.java
  26. 3
    1
      server/sonar-server-common/src/main/java/org/sonar/server/project/Project.java
  27. 13
    6
      server/sonar-server-common/src/test/java/org/sonar/server/measure/index/ProjectMeasuresIndexerTest.java
  28. 2
    2
      server/sonar-server-common/src/test/java/org/sonar/server/view/index/ViewIndexerTest.java
  29. 3
    3
      server/sonar-webserver-es/src/test/java/org/sonar/server/issue/index/IssueQueryFactoryTest.java
  30. 1
    1
      server/sonar-webserver-es/src/test/java/org/sonar/server/permission/index/PermissionIndexerDaoTest.java
  31. 42
    11
      server/sonar-webserver-webapi/src/main/java/org/sonar/server/component/ws/ComponentDtoToWsComponent.java
  32. 17
    15
      server/sonar-webserver-webapi/src/main/java/org/sonar/server/component/ws/SearchProjectsAction.java
  33. 34
    5
      server/sonar-webserver-webapi/src/main/java/org/sonar/server/component/ws/ShowAction.java
  34. 19
    7
      server/sonar-webserver-webapi/src/main/java/org/sonar/server/component/ws/TreeAction.java
  35. 0
    4
      server/sonar-webserver-webapi/src/main/java/org/sonar/server/projecttag/ws/SetAction.java
  36. 1
    1
      server/sonar-webserver-webapi/src/test/java/org/sonar/server/ce/ws/ActivityActionTest.java
  37. 5
    6
      server/sonar-webserver-webapi/src/test/java/org/sonar/server/ce/ws/AnalysisStatusActionTest.java
  38. 2
    4
      server/sonar-webserver-webapi/src/test/java/org/sonar/server/component/ComponentServiceUpdateKeyTest.java
  39. 1
    1
      server/sonar-webserver-webapi/src/test/java/org/sonar/server/component/ws/ComponentDtoToWsComponentTest.java
  40. 33
    26
      server/sonar-webserver-webapi/src/test/java/org/sonar/server/component/ws/SearchProjectsActionTest.java
  41. 8
    6
      server/sonar-webserver-webapi/src/test/java/org/sonar/server/component/ws/ShowActionTest.java
  42. 1
    1
      server/sonar-webserver-webapi/src/test/java/org/sonar/server/component/ws/SuggestionsActionTest.java
  43. 1
    1
      server/sonar-webserver-webapi/src/test/java/org/sonar/server/component/ws/TreeActionTest.java
  44. 6
    6
      server/sonar-webserver-webapi/src/test/java/org/sonar/server/issue/ws/SearchActionComponentsTest.java
  45. 3
    3
      server/sonar-webserver-webapi/src/test/java/org/sonar/server/permission/PermissionTemplateServiceTest.java
  46. 0
    1
      server/sonar-webserver-webapi/src/test/java/org/sonar/server/project/ws/UpdateKeyActionTest.java
  47. 1
    1
      server/sonar-webserver-webapi/src/test/java/org/sonar/server/project/ws/UpdateVisibilityActionTest.java
  48. 7
    7
      server/sonar-webserver-webapi/src/test/java/org/sonar/server/projectanalysis/ws/SearchActionTest.java
  49. 3
    2
      server/sonar-webserver-webapi/src/test/java/org/sonar/server/projecttag/ws/SetActionTest.java
  50. 1
    1
      server/sonar-webserver-webapi/src/test/java/org/sonar/server/ui/ws/ComponentActionTest.java
  51. 1
    1
      server/sonar-webserver-webapi/src/test/java/org/sonar/server/user/ws/CurrentActionTest.java

+ 8
- 10
server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/LoadReportAnalysisMetadataHolderStep.java View File

@@ -37,8 +37,8 @@ import org.sonar.core.platform.PluginRepository;
import org.sonar.core.util.stream.MoreCollectors;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.component.ComponentDto;
import org.sonar.db.organization.OrganizationDto;
import org.sonar.db.project.ProjectDto;
import org.sonar.db.qualityprofile.QProfileDto;
import org.sonar.scanner.protocol.output.ScannerReport;
import org.sonar.scanner.protocol.output.ScannerReport.Metadata.Plugin;
@@ -108,12 +108,13 @@ public class LoadReportAnalysisMetadataHolderStep implements ComputationStep {
"Compute Engine task main component key is null. Project with UUID %s must have been deleted since report was uploaded. Can not proceed.",
mainComponent.getUuid())));
CeTask.Component component = mandatoryComponent(ceTask.getComponent());
String componentKey = component.getKey()
.orElseThrow(() -> MessageException.of(format(
if (!component.getKey().isPresent()) {
throw MessageException.of(format(
"Compute Engine task component key is null. Project with UUID %s must have been deleted since report was uploaded. Can not proceed.",
component.getUuid())));
ComponentDto dto = toProject(reportMetadata.getProjectKey());
component.getUuid()));
}

ProjectDto dto = toProject(reportMetadata.getProjectKey());
analysisMetadata.setProject(Project.from(dto));
return () -> {
if (!mainComponentKey.equals(reportMetadata.getProjectKey())) {
@@ -125,9 +126,6 @@ public class LoadReportAnalysisMetadataHolderStep implements ComputationStep {
if (!dto.getOrganizationUuid().equals(organization.getUuid())) {
throw MessageException.of(format("Project is not in the expected organization: %s", organization.getKey()));
}
if (componentKey.equals(mainComponentKey) && dto.getMainBranchProjectUuid() != null) {
throw MessageException.of("Component should not reference a branch");
}
};
}

@@ -222,9 +220,9 @@ public class LoadReportAnalysisMetadataHolderStep implements ComputationStep {
return Organization.from(organizationDto.get());
}

private ComponentDto toProject(String projectKey) {
private ProjectDto toProject(String projectKey) {
try (DbSession dbSession = dbClient.openSession(false)) {
Optional<ComponentDto> opt = dbClient.componentDao().selectByKey(dbSession, projectKey);
Optional<ProjectDto> opt = dbClient.projectDao().selectProjectByKey(dbSession, projectKey);
checkState(opt.isPresent(), "Project with key '%s' can't be found", projectKey);
return opt.get();
}

+ 15
- 2
server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/ReportPersistComponentsStepTest.java View File

@@ -640,20 +640,33 @@ public class ReportPersistComponentsStepTest extends BaseStepTest {
return builder(PROJECT, 1).setUuid(project.uuid()).setKey(project.getDbKey()).setName(project.name());
}

private ComponentDto prepareProject(Consumer<ComponentDto>... populators) {
private ComponentDto prepareProject() {
return prepareProject(defaults());
}

private ComponentDto prepareProject(Consumer<ComponentDto> populators) {
ComponentDto dto = db.components().insertPrivateProject(db.organizations().insert(), populators);
analysisMetadataHolder.setProject(Project.from(dto));
analysisMetadataHolder.setBranch(new DefaultBranchImpl());
return dto;
}

private ComponentDto prepareBranch(String branchName, Consumer<ComponentDto>... populators) {
private ComponentDto prepareBranch(String branchName) {
return prepareBranch(branchName, defaults());
}

private ComponentDto prepareBranch(String branchName, Consumer<ComponentDto> populators) {
ComponentDto dto = db.components().insertPrivateProject(db.organizations().insert(), populators);
analysisMetadataHolder.setProject(Project.from(dto));
analysisMetadataHolder.setBranch(new TestBranch(branchName));
return dto;
}

private static <T> Consumer<T> defaults() {
return t -> {
};
}

private static class TestBranch implements Branch {
private final String name;


+ 0
- 9
server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentDao.java View File

@@ -25,12 +25,10 @@ import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Stream;
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
@@ -43,15 +41,12 @@ import org.sonar.db.RowNotFoundException;
import static com.google.common.base.Preconditions.checkArgument;
import static java.util.Collections.emptyList;
import static java.util.Objects.requireNonNull;
import static org.apache.commons.lang.StringUtils.isBlank;
import static org.sonar.core.util.stream.MoreCollectors.toList;
import static org.sonar.core.util.stream.MoreCollectors.toSet;
import static org.sonar.db.DaoUtils.buildLikeValue;
import static org.sonar.db.DatabaseUtils.checkThatNotTooManyConditions;
import static org.sonar.db.DatabaseUtils.executeLargeInputs;
import static org.sonar.db.DatabaseUtils.executeLargeInputsIntoSet;
import static org.sonar.db.DatabaseUtils.executeLargeUpdates;
import static org.sonar.db.WildcardPosition.BEFORE_AND_AFTER;
import static org.sonar.db.component.ComponentDto.generateBranchKey;
import static org.sonar.db.component.ComponentDto.generatePullRequestKey;

@@ -375,10 +370,6 @@ public class ComponentDao implements Dao {
mapper(session).update(component);
}

public void updateTags(DbSession session, ComponentDto component) {
mapper(session).updateTags(component);
}

public void updateBEnabledToFalse(DbSession session, Collection<String> uuids) {
executeLargeUpdates(uuids, mapper(session)::updateBEnabledToFalse);
}

+ 0
- 29
server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentDto.java View File

@@ -19,7 +19,6 @@
*/
package org.sonar.db.component;

import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
import com.google.common.base.Strings;
import java.util.Date;
@@ -38,7 +37,6 @@ import static org.sonar.db.DaoUtils.buildLikeValue;
import static org.sonar.db.component.ComponentValidator.checkComponentKey;
import static org.sonar.db.component.ComponentValidator.checkComponentLongName;
import static org.sonar.db.component.ComponentValidator.checkComponentName;
import static org.sonar.db.component.DbTagsReader.readDbTags;

public class ComponentDto {

@@ -56,9 +54,6 @@ public class ComponentDto {
public static final String UUID_PATH_OF_ROOT = UUID_PATH_SEPARATOR;
private static final Splitter UUID_PATH_SPLITTER = Splitter.on(UUID_PATH_SEPARATOR).omitEmptyStrings();

static final char TAGS_SEPARATOR = ',';
private static final Joiner TAGS_JOINER = Joiner.on(TAGS_SEPARATOR).skipNulls();

/**
* ID generated by database. Do not use.
*/
@@ -151,7 +146,6 @@ public class ComponentDto {
private String longName;
private String language;
private String description;
private String tags;
private boolean enabled = true;
private boolean isPrivate = false;

@@ -407,28 +401,6 @@ public class ComponentDto {
return moduleUuid == null && Scopes.PROJECT.equals(scope);
}

public List<String> getTags() {
return readDbTags(tags);
}

public ComponentDto setTags(List<String> tags) {
setTagsString(TAGS_JOINER.join(tags));
return this;
}

/**
* Used by MyBatis
*/
@CheckForNull
public String getTagsString() {
return tags;
}

public ComponentDto setTagsString(@Nullable String tags) {
this.tags = tags;
return this;
}

public boolean isPrivate() {
return isPrivate;
}
@@ -501,7 +473,6 @@ public class ComponentDto {
copy.longName = longName;
copy.language = language;
copy.description = description;
copy.tags = tags;
copy.enabled = enabled;
copy.isPrivate = isPrivate;
copy.createdAt = createdAt;

+ 0
- 2
server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentMapper.java View File

@@ -156,8 +156,6 @@ public interface ComponentMapper {

void delete(long componentId);

void updateTags(ComponentDto component);

List<KeyWithUuidDto> selectAllSiblingComponentKeysHavingOpenIssues(@Param("referenceBranchUuid") String referenceBranchUuid,
@Param("currentBranchUuid") String currentBranchUuid);


+ 1
- 1
server/sonar-db-dao/src/main/java/org/sonar/db/component/DbTagsReader.java View File

@@ -24,9 +24,9 @@ import java.util.List;
import javax.annotation.Nullable;

import static com.google.common.base.Strings.nullToEmpty;
import static org.sonar.db.component.ComponentDto.TAGS_SEPARATOR;

public class DbTagsReader {
private static final char TAGS_SEPARATOR = ',';
private static final Splitter TAGS_SPLITTER = Splitter.on(TAGS_SEPARATOR).trimResults().omitEmptyStrings();

private DbTagsReader() {

+ 5
- 4
server/sonar-db-dao/src/main/java/org/sonar/db/measure/ProjectMeasuresIndexerIterator.java View File

@@ -40,7 +40,6 @@ import javax.annotation.Nullable;
import org.apache.commons.lang.StringUtils;
import org.sonar.api.measures.CoreMetrics;
import org.sonar.api.resources.Qualifiers;
import org.sonar.api.resources.Scopes;
import org.sonar.core.util.CloseableIterator;
import org.sonar.db.DatabaseUtils;
import org.sonar.db.DbSession;
@@ -72,8 +71,9 @@ public class ProjectMeasuresIndexerIterator extends CloseableIterator<ProjectMea
// TODO filter on enabled projects
private static final String SQL_PROJECTS = "SELECT p.organization_uuid, p.uuid, p.kee, p.name, s.created_at, p.tags " +
"FROM projects p " +
"LEFT OUTER JOIN components cp on p.uuid = cp.uuid " +
"LEFT OUTER JOIN snapshots s ON s.component_uuid=p.uuid AND s.islast=? " +
"WHERE p.qualifier=?";
"WHERE cp.enabled=? AND p.qualifier=?";

private static final String PROJECT_FILTER = " AND p.uuid=?";

@@ -131,9 +131,10 @@ public class ProjectMeasuresIndexerIterator extends CloseableIterator<ProjectMea
}
PreparedStatement stmt = session.getConnection().prepareStatement(sql.toString());
stmt.setBoolean(1, true);
stmt.setString(2, Qualifiers.PROJECT);
stmt.setBoolean(2, true);
stmt.setString(3, Qualifiers.PROJECT);
if (projectUuid != null) {
stmt.setString(3, projectUuid);
stmt.setString(4, projectUuid);
}
return stmt;
} catch (SQLException e) {

+ 4
- 0
server/sonar-db-dao/src/main/java/org/sonar/db/project/ProjectDao.java View File

@@ -74,6 +74,10 @@ public class ProjectDao implements Dao {
return mapper(session).selectByUuids(uuids);
}

public void updateKey(DbSession session, String uuid, String newKey) {
mapper(session).updateKey(uuid, newKey);
}

public void updateTags(DbSession session, ProjectDto project) {
mapper(session).updateTags(project);
}

+ 3
- 1
server/sonar-db-dao/src/main/java/org/sonar/db/project/ProjectMapper.java View File

@@ -44,7 +44,9 @@ public interface ProjectMapper {

List<ProjectDto> selectByUuids(@Param("uuids") Collection<String> uuids);

List<ProjectDto> selectByOrganizationUuid(String organizationUuid);
List<ProjectDto> selectByOrganizationUuid(@Param("organizationUuid") String organizationUuid);

void updateKey(@Param("uuid") String uuid, @Param("newKey") String newKey);

void updateTags(ProjectDto project);


+ 10
- 0
server/sonar-db-dao/src/main/java/org/sonar/db/project/ProjectQuery.java View File

@@ -129,6 +129,7 @@ public class ProjectQuery {
private Boolean isPrivate;
private Set<String> projectUuids;
private Set<String> projectKeys;
private Set<String> qualifiers;
private Long analyzedBefore;
private Long anyBranchAnalyzedBefore;
private Long anyBranchAnalyzedAfter;
@@ -158,6 +159,15 @@ public class ProjectQuery {
return this;
}

public Set<String> getQualifiers() {
return qualifiers;
}

public ProjectQuery.Builder setQualifiers(Set<String> qualifiers) {
this.qualifiers = qualifiers;
return this;
}

public ProjectQuery.Builder setPrivate(@Nullable Boolean isPrivate) {
this.isPrivate = isPrivate;
return this;

+ 0
- 10
server/sonar-db-dao/src/main/resources/org/sonar/db/component/ComponentMapper.xml View File

@@ -15,7 +15,6 @@
p.name as name,
p.long_name as longName,
p.description as description,
p.tags as tagsString,
p.qualifier as qualifier,
p.scope as scope,
p.language as language,
@@ -583,7 +582,6 @@
language,
description,
private,
tags,
root_uuid,
path,
copy_component_uuid,
@@ -618,7 +616,6 @@
#{language,jdbcType=VARCHAR},
#{description,jdbcType=VARCHAR},
#{isPrivate,jdbcType=BOOLEAN},
#{tagsString, jdbcType=VARCHAR},
#{rootUuid,jdbcType=VARCHAR},
#{path,jdbcType=VARCHAR},
#{copyComponentUuid,jdbcType=VARCHAR},
@@ -639,13 +636,6 @@
)
</insert>

<update id="updateTags" parameterType="Component" useGeneratedKeys="false">
update components set
tags = #{tagsString,jdbcType=VARCHAR}
where
uuid = #{uuid,jdbcType=VARCHAR}
</update>

<update id="update" parameterType="org.sonar.db.component.ComponentUpdateDto" useGeneratedKeys="false">
update components set
b_changed = #{bChanged,jdbcType=BOOLEAN},

+ 7
- 0
server/sonar-db-dao/src/main/resources/org/sonar/db/project/ProjectMapper.xml View File

@@ -141,4 +141,11 @@
uuid = #{uuid,jdbcType=VARCHAR}
</update>

<update id="updateKey" parameterType="String">
update projects set
kee = #{newKey,jdbcType=VARCHAR}
where
uuid = #{uuid,jdbcType=VARCHAR}
</update>

</mapper>

+ 0
- 1
server/sonar-db-dao/src/schema/schema-sq.ddl View File

@@ -186,7 +186,6 @@ CREATE TABLE "COMPONENTS"(
"MODULE_UUID" VARCHAR(50),
"MODULE_UUID_PATH" VARCHAR(1500),
"AUTHORIZATION_UPDATED_AT" BIGINT,
"TAGS" VARCHAR(500),
"MAIN_BRANCH_PROJECT_UUID" VARCHAR(50),
"B_CHANGED" BOOLEAN,
"B_NAME" VARCHAR(500),

+ 21
- 26
server/sonar-db-dao/src/test/java/org/sonar/db/component/ComponentDaoTest.java View File

@@ -56,10 +56,8 @@ import org.sonar.db.organization.OrganizationDto;
import org.sonar.db.source.FileSourceDto;

import static com.google.common.collect.ImmutableSet.of;
import static com.google.common.collect.Lists.newArrayList;
import static com.google.common.collect.Sets.newHashSet;
import static java.util.Arrays.asList;
import static java.util.Collections.emptyList;
import static java.util.Collections.emptySet;
import static java.util.Collections.singleton;
import static java.util.Collections.singletonList;
@@ -326,15 +324,15 @@ public class ComponentDaoTest {
assertThat(underTest.selectByKeysAndBranches(db.getSession(), ImmutableMap.of(
projectBranch.getKey(), projectBranch.getBranch(),
applicationBranch.getKey(), applicationBranch.getBranch())))
.extracting(ComponentDto::getKey, ComponentDto::getBranch)
.containsExactlyInAnyOrder(
tuple(projectBranch.getKey(), "my_branch"),
tuple(applicationBranch.getKey(), "my_branch"));
.extracting(ComponentDto::getKey, ComponentDto::getBranch)
.containsExactlyInAnyOrder(
tuple(projectBranch.getKey(), "my_branch"),
tuple(applicationBranch.getKey(), "my_branch"));
assertThat(underTest.selectByKeysAndBranches(db.getSession(), ImmutableMap.of(
projectBranch.getKey(), "unknown",
"unknown", projectBranch.getBranch())))
.extracting(ComponentDto::getDbKey)
.isEmpty();
.extracting(ComponentDto::getDbKey)
.isEmpty();
assertThat(underTest.selectByKeysAndBranches(db.getSession(), Collections.emptyMap())).isEmpty();
}

@@ -733,7 +731,7 @@ public class ComponentDaoTest {
db.components().insertView(organization, "IJKL");
ComponentDto view = db.components().insertView(organization, "EFGH");
db.components().insertSubView(view, dto -> dto.setUuid("FGHI"));
ComponentDto application = db.components().insertApplication(organization);
ComponentDto application = db.components().insertPublicApplication(organization);

assertThat(underTest.selectAllViewsAndSubViews(dbSession)).extracting(UuidWithProjectUuidDto::getUuid)
.containsExactlyInAnyOrder("ABCD", "EFGH", "FGHI", "IJKL", application.uuid());
@@ -759,7 +757,7 @@ public class ComponentDaoTest {

@DataProvider
public static Object[][] oneOrMoreProjects() {
return new Object[][]{
return new Object[][] {
{1},
{1 + new Random().nextInt(10)}
};
@@ -988,7 +986,7 @@ public class ComponentDaoTest {

@DataProvider
public static Object[][] portfolioOrApplicationRootViewQualifier() {
return new Object[][]{
return new Object[][] {
{Qualifiers.VIEW},
{Qualifiers.APP},
};
@@ -1003,8 +1001,11 @@ public class ComponentDaoTest {
return lowestView;
}

@SafeVarargs
private final ComponentDto insertView(OrganizationDto organization, String rootViewQualifier, Consumer<ComponentDto>... dtoPopulators) {
private ComponentDto insertView(OrganizationDto organization, String rootViewQualifier) {
return insertView(organization, rootViewQualifier, defaults());
}

private ComponentDto insertView(OrganizationDto organization, String rootViewQualifier, Consumer<ComponentDto> dtoPopulators) {
ComponentDbTester tester = db.components();
if (rootViewQualifier.equals(Qualifiers.VIEW)) {
return random.nextBoolean() ? tester.insertPublicPortfolio(organization, dtoPopulators) : tester.insertPrivatePortfolio(organization, dtoPopulators);
@@ -1076,7 +1077,7 @@ public class ComponentDaoTest {
ComponentDto file = db.components().insertComponent(newFileDto(module, directory));
ComponentDto project2 = db.components().insertPrivateProject(organization);
ComponentDto view = db.components().insertView(organization);
ComponentDto application = db.components().insertApplication(organization);
ComponentDto application = db.components().insertPublicApplication(organization);
OrganizationDto otherOrganization = db.organizations().insert();
ComponentDto projectOnOtherOrganization = db.components().insertPrivateProject(otherOrganization);

@@ -1189,8 +1190,7 @@ public class ComponentDaoTest {
.containsExactly(
project1.uuid(),
project2.uuid(),
project3.uuid()
);
project3.uuid());
}

@Test
@@ -1399,16 +1399,6 @@ public class ComponentDaoTest {
"from components where uuid='" + uuid + "'");
}

@Test
public void update_tags() {
ComponentDto project = db.components().insertPrivateProject(p -> p.setTags(emptyList()));

underTest.updateTags(dbSession, project.setTags(newArrayList("finance", "toto", "tutu")));
dbSession.commit();

assertThat(underTest.selectOrFailByKey(dbSession, project.getDbKey()).getTags()).containsOnly("finance", "toto", "tutu");
}

@Test
public void delete() {
ComponentDto project1 = db.components().insertPrivateProject(db.getDefaultOrganization(), (t) -> t.setDbKey("PROJECT_1"));
@@ -2002,4 +1992,9 @@ public class ComponentDaoTest {
db.measures().insertLiveMeasure(componentDto, metric, m -> m.setValue(value));
}

private static <T> Consumer<T> defaults() {
return t -> {
};
}

}

+ 12
- 4
server/sonar-db-dao/src/test/java/org/sonar/db/measure/ProjectMeasuresIndexerIteratorTest.java View File

@@ -36,7 +36,9 @@ import org.sonar.db.component.SnapshotDto;
import org.sonar.db.measure.ProjectMeasuresIndexerIterator.ProjectMeasures;
import org.sonar.db.metric.MetricDto;
import org.sonar.db.organization.OrganizationDto;
import org.sonar.db.project.ProjectDto;

import static com.google.common.collect.Lists.newArrayList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.entry;
import static org.sonar.api.measures.Metric.Level.ERROR;
@@ -62,7 +64,11 @@ public class ProjectMeasuresIndexerIteratorTest {
@Test
public void return_project_measure() {
OrganizationDto organization = dbTester.organizations().insert();
ComponentDto project = dbTester.components().insertPrivateProject(organization, p -> p.setDbKey("Project-Key").setName("Project Name").setTagsString("platform,java"));
ComponentDto project = dbTester.components().insertPrivateProject(organization,
c -> c.setDbKey("Project-Key").setName("Project Name"),
p -> p.setTags(newArrayList("platform", "java")));
ProjectDto projectDto = dbTester.components().getProjectDto(project);

SnapshotDto analysis = dbTester.components().insertSnapshot(project);
MetricDto metric1 = dbTester.measures().insertMetric(m -> m.setValueType(INT.name()).setKey("ncloc"));
MetricDto metric2 = dbTester.measures().insertMetric(m -> m.setValueType(INT.name()).setKey("coverage"));
@@ -85,7 +91,9 @@ public class ProjectMeasuresIndexerIteratorTest {
@Test
public void return_project_measure_having_leak() {
OrganizationDto organization = dbTester.organizations().insert();
ComponentDto project = dbTester.components().insertPrivateProject(organization, p -> p.setDbKey("Project-Key").setName("Project Name").setTagsString("platform,java"));
ComponentDto project = dbTester.components().insertPrivateProject(organization,
c -> c.setDbKey("Project-Key").setName("Project Name"),
p -> p.setTagsString("platform,java"));
MetricDto metric = dbTester.measures().insertMetric(m -> m.setValueType(INT.name()).setKey("new_lines"));
dbTester.measures().insertLiveMeasure(project, metric, m -> m.setVariation(10d));

@@ -207,7 +215,7 @@ public class ProjectMeasuresIndexerIteratorTest {

@Test
public void return_project_without_analysis() {
ComponentDto project = dbTester.components().insertPrivateProject(ComponentTesting.newPrivateProjectDto(dbTester.organizations().insert()));
ComponentDto project = dbTester.components().insertPrivateProject(dbTester.organizations().insert());
dbClient.snapshotDao().insert(dbSession, newAnalysis(project).setLast(false));
dbSession.commit();

@@ -220,7 +228,7 @@ public class ProjectMeasuresIndexerIteratorTest {

@Test
@Ignore
//TODO
// TODO
public void does_not_return_non_active_projects() {
OrganizationDto organization = dbTester.organizations().insert();
// Disabled project

+ 1
- 1
server/sonar-db-dao/src/test/java/org/sonar/db/purge/PurgeMapperTest.java View File

@@ -94,7 +94,7 @@ public class PurgeMapperTest {

@Test
public void selectRootAndModulesOrSubviewsByProjectUuid_returns_application_with_specified_uuid() {
ComponentDto view = db.components().insertApplication(db.getDefaultOrganization());
ComponentDto view = db.components().insertPublicApplication(db.getDefaultOrganization());

assertThat(purgeMapper.selectRootAndModulesOrSubviewsByProjectUuid(view.uuid()))
.extracting(IdUuidPair::getUuid)

+ 12
- 12
server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/QualityProfileDaoTest.java View File

@@ -743,11 +743,11 @@ public class QualityProfileDaoTest {

@Test
public void select_selected_projects() {
ComponentDto project1 = db.components().insertPrivateProject(t -> t.setName("Project1 name"), t -> t.setOrganizationUuid(organization.getUuid()));
ComponentDto project2 = db.components().insertPrivateProject(t -> t.setName("Project2 name"), t -> t.setOrganizationUuid(organization.getUuid()));
ComponentDto project3 = db.components().insertPrivateProject(t -> t.setName("Project3 name"), t -> t.setOrganizationUuid(organization.getUuid()));
ComponentDto project1 = db.components().insertPrivateProject(t -> t.setName("Project1 name").setOrganizationUuid(organization.getUuid()));
ComponentDto project2 = db.components().insertPrivateProject(t -> t.setName("Project2 name").setOrganizationUuid(organization.getUuid()));
ComponentDto project3 = db.components().insertPrivateProject(t -> t.setName("Project3 name").setOrganizationUuid(organization.getUuid()));
OrganizationDto organization2 = db.organizations().insert();
ComponentDto project4 = db.components().insertPrivateProject(t -> t.setName("Project4 name"), t -> t.setOrganizationUuid(organization2.getUuid()));
ComponentDto project4 = db.components().insertPrivateProject(t -> t.setName("Project4 name").setOrganizationUuid(organization2.getUuid()));
ComponentDto branch = db.components().insertProjectBranch(project1, t -> t.setKey("branch"));

QProfileDto profile1 = newQualityProfileDto();
@@ -772,11 +772,11 @@ public class QualityProfileDaoTest {

@Test
public void select_deselected_projects() {
ComponentDto project1 = db.components().insertPrivateProject(t -> t.setName("Project1 name"), t -> t.setOrganizationUuid(organization.getUuid()));
ComponentDto project2 = db.components().insertPrivateProject(t -> t.setName("Project2 name"), t -> t.setOrganizationUuid(organization.getUuid()));
ComponentDto project3 = db.components().insertPrivateProject(t -> t.setName("Project3 name"), t -> t.setOrganizationUuid(organization.getUuid()));
ComponentDto project1 = db.components().insertPrivateProject(t -> t.setName("Project1 name").setOrganizationUuid(organization.getUuid()));
ComponentDto project2 = db.components().insertPrivateProject(t -> t.setName("Project2 name").setOrganizationUuid(organization.getUuid()));
ComponentDto project3 = db.components().insertPrivateProject(t -> t.setName("Project3 name").setOrganizationUuid(organization.getUuid()));
OrganizationDto organization2 = db.organizations().insert();
ComponentDto project4 = db.components().insertPrivateProject(t -> t.setName("Project4 name"), t -> t.setOrganizationUuid(organization2.getUuid()));
ComponentDto project4 = db.components().insertPrivateProject(t -> t.setName("Project4 name").setOrganizationUuid(organization2.getUuid()));
ComponentDto branch = db.components().insertProjectBranch(project1, t -> t.setKey("branch"));

QProfileDto profile1 = newQualityProfileDto();
@@ -800,11 +800,11 @@ public class QualityProfileDaoTest {

@Test
public void select_project_associations() {
ComponentDto project1 = db.components().insertPrivateProject(t -> t.setName("Project1 name"), t -> t.setOrganizationUuid(organization.getUuid()));
ComponentDto project2 = db.components().insertPrivateProject(t -> t.setName("Project2 name"), t -> t.setOrganizationUuid(organization.getUuid()));
ComponentDto project3 = db.components().insertPrivateProject(t -> t.setName("Project3 name"), t -> t.setOrganizationUuid(organization.getUuid()));
ComponentDto project1 = db.components().insertPrivateProject(t -> t.setName("Project1 name").setOrganizationUuid(organization.getUuid()));
ComponentDto project2 = db.components().insertPrivateProject(t -> t.setName("Project2 name").setOrganizationUuid(organization.getUuid()));
ComponentDto project3 = db.components().insertPrivateProject(t -> t.setName("Project3 name").setOrganizationUuid(organization.getUuid()));
OrganizationDto organization2 = db.organizations().insert();
ComponentDto project4 = db.components().insertPrivateProject(t -> t.setName("Project4 name"), t -> t.setOrganizationUuid(organization2.getUuid()));
ComponentDto project4 = db.components().insertPrivateProject(t -> t.setName("Project4 name").setOrganizationUuid(organization2.getUuid()));
ComponentDto branch = db.components().insertProjectBranch(project1, t -> t.setKey("branch"));

QProfileDto profile1 = newQualityProfileDto();

+ 130
- 98
server/sonar-db-dao/src/testFixtures/java/org/sonar/db/component/ComponentDbTester.java View File

@@ -47,7 +47,7 @@ public class ComponentDbTester {
}

public SnapshotDto insertProjectAndSnapshot(ComponentDto component) {
insertComponentAndBranchAndProject(component, null, noExtraConfiguration());
insertComponentAndBranchAndProject(component, null, defaults(), defaults(), defaults());
return insertSnapshot(component);
}

@@ -57,55 +57,72 @@ public class ComponentDbTester {
}

public ComponentDto insertComponent(ComponentDto component) {
return insertComponentImpl(component, null, noExtraConfiguration());
return insertComponentImpl(component, null, defaults());
}

public ComponentDto insertPrivateProject() {
return insertComponentAndBranchAndProject(ComponentTesting.newPrivateProjectDto(db.getDefaultOrganization()), true, noExtraConfiguration());
return insertComponentAndBranchAndProject(ComponentTesting.newPrivateProjectDto(db.getDefaultOrganization()), true,
defaults(), defaults(), defaults());
}

public ProjectDto getProjectDto(ComponentDto project) {
return db.getDbClient().projectDao().selectByUuid(dbSession, project.uuid()).get();
return db.getDbClient().projectDao().selectByUuid(dbSession, project.uuid())
.orElseThrow(() -> new IllegalStateException("Project has invalid configuration"));
}

public ComponentDto insertPrivateProject(ComponentDto componentDto) {
return insertComponentAndBranchAndProject(componentDto, true, noExtraConfiguration());
return insertComponentAndBranchAndProject(componentDto, true);
}

public ComponentDto insertPublicProject() {
return insertComponentAndBranchAndProject(ComponentTesting.newPublicProjectDto(db.getDefaultOrganization()), false, noExtraConfiguration());
return insertComponentAndBranchAndProject(ComponentTesting.newPublicProjectDto(db.getDefaultOrganization()), false);
}

public ComponentDto insertPublicProject(ComponentDto componentDto) {
return insertComponentAndBranchAndProject(componentDto, false, noExtraConfiguration());
return insertComponentAndBranchAndProject(componentDto, false);
}

@SafeVarargs
public final ComponentDto insertPrivateProject(Consumer<ComponentDto>... dtoPopulators) {
return insertComponentAndBranchAndProject(ComponentTesting.newPrivateProjectDto(db.getDefaultOrganization()), true, null, dtoPopulators);
public final ComponentDto insertPrivateProject(Consumer<ComponentDto> dtoPopulator) {
return insertComponentAndBranchAndProject(ComponentTesting.newPrivateProjectDto(db.getDefaultOrganization()), true, defaults(),
dtoPopulator);
}

@SafeVarargs
public final ComponentDto insertPublicProject(Consumer<ComponentDto>... dtoPopulators) {
return insertComponentAndBranchAndProject(ComponentTesting.newPublicProjectDto(db.getDefaultOrganization()), false, null, dtoPopulators);
public final ComponentDto insertPrivateProject(Consumer<ComponentDto> componentDtoPopulator, Consumer<ProjectDto> projectDtoPopulator) {
return insertComponentAndBranchAndProject(ComponentTesting.newPrivateProjectDto(db.getDefaultOrganization()),
true, defaults(), componentDtoPopulator, projectDtoPopulator);
}

@SafeVarargs
public final ComponentDto insertPrivateProject(OrganizationDto organizationDto, Consumer<ComponentDto>... dtoPopulators) {
return insertComponentAndBranchAndProject(ComponentTesting.newPrivateProjectDto(organizationDto), true, null, dtoPopulators);
public final ComponentDto insertPublicProject(Consumer<ComponentDto> dtoPopulator) {
return insertComponentAndBranchAndProject(ComponentTesting.newPublicProjectDto(db.getDefaultOrganization()), false, defaults(),
dtoPopulator);
}

@SafeVarargs
public final ComponentDto insertPublicProject(OrganizationDto organizationDto, Consumer<ComponentDto>... dtoPopulators) {
return insertComponentAndBranchAndProject(ComponentTesting.newPublicProjectDto(organizationDto), false, null, dtoPopulators);
public final ComponentDto insertPrivateProject(OrganizationDto organizationDto, Consumer<ComponentDto> componentDtoPopulator) {
return insertPrivateProject(organizationDto, componentDtoPopulator, defaults());
}

public final ComponentDto insertPrivateProject(OrganizationDto organizationDto, Consumer<ComponentDto> componentDtoPopulator,
Consumer<ProjectDto> projectDtoPopulator) {
return insertComponentAndBranchAndProject(ComponentTesting.newPrivateProjectDto(organizationDto), true, defaults(),
componentDtoPopulator, projectDtoPopulator);
}

public final ComponentDto insertPublicProject(OrganizationDto organizationDto, Consumer<ComponentDto> componentDtoPopulator) {
return insertPublicProject(organizationDto, componentDtoPopulator, defaults());
}

public final ComponentDto insertPublicProject(OrganizationDto organizationDto, Consumer<ComponentDto> componentDtoPopulator,
Consumer<ProjectDto> projectDtoPopulator) {
return insertComponentAndBranchAndProject(ComponentTesting.newPublicProjectDto(organizationDto), false, defaults(), componentDtoPopulator,
projectDtoPopulator);
}

public ComponentDto insertPrivateProject(OrganizationDto organizationDto) {
return insertComponentAndBranchAndProject(ComponentTesting.newPrivateProjectDto(organizationDto), true, noExtraConfiguration());
return insertComponentAndBranchAndProject(ComponentTesting.newPrivateProjectDto(organizationDto), true);
}

public ComponentDto insertPublicProject(OrganizationDto organizationDto) {
return insertComponentAndBranchAndProject(ComponentTesting.newPublicProjectDto(organizationDto), false, noExtraConfiguration());
return insertComponentAndBranchAndProject(ComponentTesting.newPublicProjectDto(organizationDto), false);
}

public ProjectDto insertPublicProjectDto() {
@@ -123,9 +140,8 @@ public class ComponentDbTester {
return getProjectDto(componentDto);
}

@SafeVarargs
public final ProjectDto insertPublicProjectDto(OrganizationDto organization, Consumer<ComponentDto>... dtoPopulators) {
ComponentDto componentDto = insertPublicProject(organization, dtoPopulators);
public final ProjectDto insertPublicProjectDto(OrganizationDto organization, Consumer<ComponentDto> dtoPopulator) {
ComponentDto componentDto = insertPublicProject(organization, dtoPopulator);
return getProjectDto(componentDto);
}

@@ -134,47 +150,52 @@ public class ComponentDbTester {
return getProjectDto(componentDto);
}

@SafeVarargs
public final ProjectDto insertPrivateProjectDto(Consumer<ComponentDto>... dtoPopulators) {
ComponentDto componentDto = insertPrivateProject(dtoPopulators);
public final ProjectDto insertPrivateProjectDto(Consumer<ComponentDto> componentDtoPopulator) {
return insertPrivateProjectDto(componentDtoPopulator, defaults());
}

public final ProjectDto insertPrivateProjectDto(Consumer<ComponentDto> componentDtoPopulator, Consumer<ProjectDto> projectDtoPopulator) {
ComponentDto componentDto = insertPrivateProject(componentDtoPopulator, projectDtoPopulator);
return getProjectDto(componentDto);
}

public ProjectDto insertPrivateProjectDto(OrganizationDto organization, Consumer<BranchDto> branchConsumer) {
ComponentDto componentDto = insertPrivateProjectWithCustomBranch(organization, branchConsumer);
ComponentDto componentDto = insertPrivateProjectWithCustomBranch(organization, branchConsumer, defaults());
return getProjectDto(componentDto);
}

public ComponentDto insertPrivateProject(OrganizationDto organizationDto, String uuid) {
return insertComponentAndBranchAndProject(ComponentTesting.newPrivateProjectDto(organizationDto, uuid), true, noExtraConfiguration());
return insertComponentAndBranchAndProject(ComponentTesting.newPrivateProjectDto(organizationDto, uuid), true);
}

public ComponentDto insertPublicProject(OrganizationDto organizationDto, String uuid) {
return insertComponentAndBranchAndProject(ComponentTesting.newPublicProjectDto(organizationDto, uuid), false, noExtraConfiguration());
return insertComponentAndBranchAndProject(ComponentTesting.newPublicProjectDto(organizationDto, uuid), false);
}

@SafeVarargs
public final ComponentDto insertPrivateProject(OrganizationDto organizationDto, String uuid, Consumer<ComponentDto>... dtoPopulators) {
return insertComponentAndBranchAndProject(ComponentTesting.newPrivateProjectDto(organizationDto, uuid), true, null, dtoPopulators);
public final ComponentDto insertPrivateProject(OrganizationDto organizationDto, String uuid, Consumer<ComponentDto> dtoPopulator) {
return insertComponentAndBranchAndProject(ComponentTesting.newPrivateProjectDto(organizationDto, uuid), true, defaults(), dtoPopulator);
}

public final ComponentDto insertPrivateProjectWithCustomBranch(OrganizationDto organizationDto, Consumer<BranchDto> branchPopulator) {
return insertPrivateProjectWithCustomBranch(organizationDto, branchPopulator, defaults());
}

@SafeVarargs
public final ComponentDto insertPrivateProjectWithCustomBranch(OrganizationDto organizationDto, Consumer<BranchDto> branchPopulator,
Consumer<ComponentDto>... componentPopulator) {
Consumer<ComponentDto> componentPopulator) {
return insertComponentAndBranchAndProject(ComponentTesting.newPrivateProjectDto(organizationDto), true, branchPopulator, componentPopulator);
}

/**
* @see #insertPublicPortfolio(OrganizationDto, Consumer[])
* @see #insertPublicPortfolio(org.sonar.db.organization.OrganizationDto, java.util.function.Consumer)
* @deprecated since 6.6
*/
@Deprecated
public ComponentDto insertView() {
return insertComponentImpl(ComponentTesting.newView(db.getDefaultOrganization()), false, noExtraConfiguration());
return insertComponentImpl(ComponentTesting.newView(db.getDefaultOrganization()), false);
}

/**
* @see #insertPublicPortfolio(OrganizationDto, Consumer[])
* @see #insertPublicPortfolio(org.sonar.db.organization.OrganizationDto, java.util.function.Consumer)
* @deprecated since 6.6
*/
public ComponentDto insertView(Consumer<ComponentDto> dtoPopulator) {
@@ -182,15 +203,15 @@ public class ComponentDbTester {
}

/**
* @see #insertPublicPortfolio(OrganizationDto, Consumer[])
* @see #insertPublicPortfolio(org.sonar.db.organization.OrganizationDto, java.util.function.Consumer)
* @deprecated since 6.6
*/
public ComponentDto insertView(OrganizationDto organizationDto) {
return insertComponentImpl(ComponentTesting.newView(organizationDto), false, noExtraConfiguration());
return insertComponentImpl(ComponentTesting.newView(organizationDto), false, defaults());
}

/**
* @see #insertPublicPortfolio(OrganizationDto, Consumer[])
* @see #insertPublicPortfolio(org.sonar.db.organization.OrganizationDto, java.util.function.Consumer)
* @deprecated since 6.6
*/
public ComponentDto insertView(OrganizationDto organizationDto, Consumer<ComponentDto> dtoPopulator) {
@@ -198,101 +219,105 @@ public class ComponentDbTester {
}

/**
* @see #insertPublicPortfolio(OrganizationDto, Consumer[])
* @see #insertPublicPortfolio(org.sonar.db.organization.OrganizationDto, java.util.function.Consumer)
* @deprecated since 6.6
*/
public ComponentDto insertView(String uuid) {
return insertComponentImpl(ComponentTesting.newView(db.getDefaultOrganization(), uuid), false, noExtraConfiguration());
return insertComponentImpl(ComponentTesting.newView(db.getDefaultOrganization(), uuid), false, defaults());
}

/**
* @see #insertPublicPortfolio(OrganizationDto, Consumer[])
* @see #insertPublicPortfolio(org.sonar.db.organization.OrganizationDto, java.util.function.Consumer)
* @deprecated since 6.6
*/
public ComponentDto insertView(OrganizationDto organizationDto, String uuid) {
return insertComponentImpl(ComponentTesting.newView(organizationDto, uuid), false, noExtraConfiguration());
return insertComponentImpl(ComponentTesting.newView(organizationDto, uuid), false, defaults());
}

@SafeVarargs
public final ComponentDto insertPublicPortfolio(OrganizationDto organization, Consumer<ComponentDto>... dtoPopulators) {
return insertComponentImpl(ComponentTesting.newView(organization).setPrivate(false), false, dtoPopulators);
public final ComponentDto insertPublicPortfolio(OrganizationDto organization) {
return insertPublicPortfolio(organization, defaults());
}

@SafeVarargs
public final ComponentDto insertPrivatePortfolio(Consumer<ComponentDto>... dtoPopulators) {
return insertComponentImpl(ComponentTesting.newView(db.getDefaultOrganization()).setPrivate(true), true, dtoPopulators);
public final ComponentDto insertPublicPortfolio(OrganizationDto organization, Consumer<ComponentDto> dtoPopulator) {
return insertComponentImpl(ComponentTesting.newView(organization).setPrivate(false), false, dtoPopulator);
}

@SafeVarargs
public final ComponentDto insertPrivatePortfolio(OrganizationDto organization, Consumer<ComponentDto>... dtoPopulators) {
return insertComponentImpl(ComponentTesting.newView(organization).setPrivate(true), true, dtoPopulators);
public final ComponentDto insertPrivatePortfolio() {
return insertPrivatePortfolio(db.getDefaultOrganization());
}

@SafeVarargs
public final ComponentDto insertPublicApplication(Consumer<ComponentDto>... dtoPopulators) {
return insertComponentImpl(ComponentTesting.newApplication(db.getDefaultOrganization()).setPrivate(false), false, dtoPopulators);
public final ComponentDto insertPrivatePortfolio(OrganizationDto organization) {
return insertPrivatePortfolio(organization, defaults());
}

@SafeVarargs
public final ComponentDto insertPublicApplication(OrganizationDto organization, Consumer<ComponentDto>... dtoPopulators) {
return insertComponentAndBranchAndProject(ComponentTesting.newApplication(organization).setPrivate(false), false, b -> {
}, dtoPopulators);
public final ComponentDto insertPrivatePortfolio(OrganizationDto organization, Consumer<ComponentDto> dtoPopulator) {
return insertComponentImpl(ComponentTesting.newView(organization).setPrivate(true), true, dtoPopulator);
}

@SafeVarargs
public final ComponentDto insertPrivateApplication(Consumer<ComponentDto>... dtoPopulators) {
return insertComponentImpl(ComponentTesting.newApplication(db.getDefaultOrganization()).setPrivate(true), true, dtoPopulators);
public final ComponentDto insertPublicApplication() {
return insertPublicApplication(db.getDefaultOrganization());
}

@SafeVarargs
public final ComponentDto insertPrivateApplication(OrganizationDto organization, Consumer<ComponentDto>... dtoPopulators) {
return insertComponentAndBranchAndProject(ComponentTesting.newApplication(organization).setPrivate(true), true, b -> {
}, dtoPopulators);
public final ComponentDto insertPublicApplication(OrganizationDto organization) {
return insertPublicApplication(organization, defaults());
}

/**
* @see #insertPublicApplication(OrganizationDto, Consumer[])
* @deprecated since 6.6
*/
@SafeVarargs
public final ComponentDto insertApplication(OrganizationDto organizationDto, Consumer<ComponentDto>... dtoPopulators) {
return insertComponentAndBranchAndProject(ComponentTesting.newApplication(organizationDto), false, b -> {
}, dtoPopulators);
public final ComponentDto insertPublicApplication(OrganizationDto organization, Consumer<ComponentDto> dtoPopulator) {
return insertComponentAndBranchAndProject(ComponentTesting.newApplication(organization).setPrivate(false), false, defaults(), dtoPopulator);
}

@SafeVarargs
public final ComponentDto insertSubView(ComponentDto view, Consumer<ComponentDto>... dtoPopulators) {
return insertComponentAndBranchAndProject(ComponentTesting.newSubView(view), view.isPrivate(), null, dtoPopulators);
public final ComponentDto insertPrivateApplication() {
return insertPrivateApplication(db.getDefaultOrganization());
}
public final ComponentDto insertPrivateApplication(OrganizationDto organization) {
return insertPrivateApplication(organization, defaults());
}

private static <T> Consumer<T> noExtraConfiguration() {
return (t) -> {
};
public final ComponentDto insertPrivateApplication(OrganizationDto organization, Consumer<ComponentDto> dtoPopulator) {
return insertComponentAndBranchAndProject(ComponentTesting.newApplication(organization).setPrivate(true), true, defaults(), dtoPopulator);
}

@SafeVarargs
private final ComponentDto insertComponentAndBranchAndProject(ComponentDto component, @Nullable Boolean isPrivate, @Nullable Consumer<BranchDto> branchPopulator,
Consumer<ComponentDto>... dtoPopulators) {
insertComponentImpl(component, isPrivate, dtoPopulators);
public final ComponentDto insertSubView(ComponentDto view) {
return insertSubView(view, defaults());
}

public final ComponentDto insertSubView(ComponentDto view, Consumer<ComponentDto> dtoPopulator) {
return insertComponentAndBranchAndProject(ComponentTesting.newSubView(view), view.isPrivate(), defaults(), dtoPopulator);
}

private ComponentDto insertComponentAndBranchAndProject(ComponentDto component, @Nullable Boolean isPrivate, Consumer<BranchDto> branchPopulator,
Consumer<ComponentDto> componentDtoPopulator, Consumer<ProjectDto> projectDtoPopulator) {
insertComponentImpl(component, isPrivate, componentDtoPopulator);

ProjectDto projectDto = toProjectDto(component, System2.INSTANCE.now());
projectDtoPopulator.accept(projectDto);
dbClient.projectDao().insert(dbSession, projectDto);

BranchDto branchDto = ComponentTesting.newBranchDto(component, BRANCH);
branchDto.setExcludeFromPurge(true);

if (branchPopulator != null) {
branchPopulator.accept(branchDto);
}
branchPopulator.accept(branchDto);
dbClient.branchDao().insert(dbSession, branchDto);

db.commit();
return component;
}

@SafeVarargs
private final ComponentDto insertComponentImpl(ComponentDto component, @Nullable Boolean isPrivate, Consumer<ComponentDto>... dtoPopulators) {
Arrays.stream(dtoPopulators).forEach(dtoPopulator -> dtoPopulator.accept(component));
private ComponentDto insertComponentAndBranchAndProject(ComponentDto component, @Nullable Boolean isPrivate, Consumer<BranchDto> branchPopulator,
Consumer<ComponentDto> componentDtoPopulator) {
return insertComponentAndBranchAndProject(component, isPrivate, branchPopulator, componentDtoPopulator, defaults());
}

private ComponentDto insertComponentAndBranchAndProject(ComponentDto component, @Nullable Boolean isPrivate, Consumer<BranchDto> branchPopulator) {
return insertComponentAndBranchAndProject(component, isPrivate, branchPopulator, defaults());
}

private ComponentDto insertComponentAndBranchAndProject(ComponentDto component, @Nullable Boolean isPrivate) {
return insertComponentAndBranchAndProject(component, isPrivate, defaults());
}

private ComponentDto insertComponentImpl(ComponentDto component, @Nullable Boolean isPrivate, Consumer<ComponentDto> dtoPopulator) {
dtoPopulator.accept(component);
checkState(isPrivate == null || component.isPrivate() == isPrivate, "Illegal modification of private flag");
dbClient.componentDao().insert(dbSession, component);
db.commit();
@@ -300,6 +325,10 @@ public class ComponentDbTester {
return component;
}

private ComponentDto insertComponentImpl(ComponentDto component, @Nullable Boolean isPrivate) {
return insertComponentImpl(component, isPrivate, defaults());
}

public void insertComponents(ComponentDto... components) {
dbClient.componentDao().insert(dbSession, asList(components));
db.commit();
@@ -312,7 +341,7 @@ public class ComponentDbTester {
}

public SnapshotDto insertSnapshot(ComponentDto componentDto) {
return insertSnapshot(componentDto, noExtraConfiguration());
return insertSnapshot(componentDto, defaults());
}

public SnapshotDto insertSnapshot(ComponentDto componentDto, Consumer<SnapshotDto> consumer) {
@@ -322,7 +351,7 @@ public class ComponentDbTester {
}

public SnapshotDto insertSnapshot(BranchDto branchDto) {
return insertSnapshot(branchDto, noExtraConfiguration());
return insertSnapshot(branchDto, defaults());
}

public SnapshotDto insertSnapshot(BranchDto branchDto, Consumer<SnapshotDto> consumer) {
@@ -392,8 +421,11 @@ public class ComponentDbTester {
.setPrivate(componentDto.isPrivate())
.setDescription(componentDto.description())
.setName(componentDto.name())
.setOrganizationUuid(componentDto.getOrganizationUuid())
.setTags(componentDto.getTags())
.setTagsString(componentDto.getTagsString());
.setOrganizationUuid(componentDto.getOrganizationUuid());
}

private static <T> Consumer<T> defaults() {
return t -> {
};
}
}

+ 4
- 0
server/sonar-db-dao/src/testFixtures/java/org/sonar/db/component/SnapshotTesting.java View File

@@ -26,6 +26,10 @@ import static org.apache.commons.lang.RandomStringUtils.randomAscii;

public class SnapshotTesting {

private SnapshotTesting() {
// nothing to do
}

public static SnapshotDto newAnalysis(ComponentDto rootComponent) {
checkNotNull(rootComponent.uuid(), "Project UUID must be set");
checkArgument(rootComponent.uuid().equals(rootComponent.projectUuid()), "Component is not a tree root");

+ 1
- 0
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v81/DbVersion81.java View File

@@ -46,6 +46,7 @@ public class DbVersion81 implements DbVersion {
.add(3114, "Rename table 'PROJECTS' to 'COMPONENTS'", RenameProjectsTableToComponents.class)
.add(3115, "Add PROJECTS table", CreateProjectsTable.class)
.add(3116, "Populate PROJECTS table", PopulateProjectsTable.class)
.add(3117, "Drop 'TAGS' column from COMPONENTS table", DropTagsColumnFromComponentsTable.class)
;
}
}

+ 40
- 0
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v81/DropTagsColumnFromComponentsTable.java View File

@@ -0,0 +1,40 @@
/*
* SonarQube
* Copyright (C) 2009-2020 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.server.platform.db.migration.version.v81;

import java.sql.SQLException;
import org.sonar.db.Database;
import org.sonar.server.platform.db.migration.sql.DropColumnsBuilder;
import org.sonar.server.platform.db.migration.step.DdlChange;

public class DropTagsColumnFromComponentsTable extends DdlChange {

static final String TABLE = "components";
static final String COLUMN_TO_DROP = "tags";

public DropTagsColumnFromComponentsTable(Database db) {
super(db);
}

@Override
public void execute(Context context) throws SQLException {
context.execute(new DropColumnsBuilder(getDialect(), TABLE, COLUMN_TO_DROP).build());
}
}

+ 1
- 1
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v81/DbVersion81Test.java View File

@@ -36,7 +36,7 @@ public class DbVersion81Test {

@Test
public void verify_migration_count() {
verifyMigrationCount(underTest, 17);
verifyMigrationCount(underTest, 18);
}

}

+ 56
- 0
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v81/DropTagsColumnFromComponentsTableTest.java View File

@@ -0,0 +1,56 @@
/*
* SonarQube
* Copyright (C) 2009-2020 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.server.platform.db.migration.version.v81;

import java.sql.SQLException;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.sonar.db.CoreDbTester;

import static org.sonar.server.platform.db.migration.version.v81.DropTagsColumnFromComponentsTable.COLUMN_TO_DROP;
import static org.sonar.server.platform.db.migration.version.v81.DropTagsColumnFromComponentsTable.TABLE;

public class DropTagsColumnFromComponentsTableTest {

@Rule
public CoreDbTester db = CoreDbTester.createForSchema(DropTagsColumnFromComponentsTableTest.class, "schema.sql");
@Rule
public ExpectedException expectedException = ExpectedException.none();

private DropTagsColumnFromComponentsTable underTest = new DropTagsColumnFromComponentsTable(db.database());

@Test
public void execute() throws SQLException {
underTest.execute();

db.assertColumnDoesNotExist(TABLE, COLUMN_TO_DROP);
}

@Test
public void migration_is_not_re_entrant() throws SQLException {
underTest.execute();

db.assertColumnDoesNotExist(TABLE, COLUMN_TO_DROP);
expectedException.expect(IllegalStateException.class);

underTest.execute();
}
}

+ 47
- 0
server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v81/DropTagsColumnFromComponentsTableTest/schema.sql View File

@@ -0,0 +1,47 @@
CREATE TABLE "COMPONENTS"(
"ID" INTEGER NOT NULL AUTO_INCREMENT (1,1),
"UUID" VARCHAR(50) NOT NULL,
"ORGANIZATION_UUID" VARCHAR(40) NOT NULL,
"KEE" VARCHAR(400),
"DEPRECATED_KEE" VARCHAR(400),
"NAME" VARCHAR(2000),
"LONG_NAME" VARCHAR(2000),
"DESCRIPTION" VARCHAR(2000),
"ENABLED" BOOLEAN DEFAULT TRUE NOT NULL,
"SCOPE" VARCHAR(3),
"QUALIFIER" VARCHAR(10),
"PRIVATE" BOOLEAN NOT NULL,
"ROOT_UUID" VARCHAR(50) NOT NULL,
"LANGUAGE" VARCHAR(20),
"COPY_COMPONENT_UUID" VARCHAR(50),
"DEVELOPER_UUID" VARCHAR(50),
"PATH" VARCHAR(2000),
"UUID_PATH" VARCHAR(1500) NOT NULL,
"PROJECT_UUID" VARCHAR(50) NOT NULL,
"MODULE_UUID" VARCHAR(50),
"MODULE_UUID_PATH" VARCHAR(1500),
"AUTHORIZATION_UPDATED_AT" BIGINT,
"TAGS" VARCHAR(500),
"MAIN_BRANCH_PROJECT_UUID" VARCHAR(50),
"B_CHANGED" BOOLEAN,
"B_NAME" VARCHAR(500),
"B_LONG_NAME" VARCHAR(500),
"B_DESCRIPTION" VARCHAR(2000),
"B_ENABLED" BOOLEAN,
"B_QUALIFIER" VARCHAR(10),
"B_LANGUAGE" VARCHAR(20),
"B_COPY_COMPONENT_UUID" VARCHAR(50),
"B_PATH" VARCHAR(2000),
"B_UUID_PATH" VARCHAR(1500),
"B_MODULE_UUID" VARCHAR(50),
"B_MODULE_UUID_PATH" VARCHAR(1500),
"CREATED_AT" TIMESTAMP
);
ALTER TABLE "COMPONENTS" ADD CONSTRAINT "PK_PROJECTS" PRIMARY KEY("ID");
CREATE INDEX "PROJECTS_ORGANIZATION" ON "COMPONENTS"("ORGANIZATION_UUID");
CREATE UNIQUE INDEX "PROJECTS_KEE" ON "COMPONENTS"("KEE");
CREATE INDEX "PROJECTS_MODULE_UUID" ON "COMPONENTS"("MODULE_UUID");
CREATE INDEX "PROJECTS_PROJECT_UUID" ON "COMPONENTS"("PROJECT_UUID");
CREATE INDEX "PROJECTS_QUALIFIER" ON "COMPONENTS"("QUALIFIER");
CREATE INDEX "PROJECTS_ROOT_UUID" ON "COMPONENTS"("ROOT_UUID");
CREATE INDEX "PROJECTS_UUID" ON "COMPONENTS"("UUID");

+ 1
- 0
server/sonar-server-common/src/main/java/org/sonar/server/es/ProjectIndexers.java View File

@@ -37,6 +37,7 @@ public interface ProjectIndexers {
*/
void commitAndIndexByProjectUuids(DbSession dbSession, Collection<String> projectUuids, ProjectIndexer.Cause cause);

//TODO remove?
default void commitAndIndex(DbSession dbSession, Collection<ComponentDto> projectOrModules, ProjectIndexer.Cause cause) {
Collection<String> projectUuids = projectOrModules.stream()
.map(ComponentDto::projectUuid)

+ 3
- 1
server/sonar-server-common/src/main/java/org/sonar/server/project/Project.java View File

@@ -26,6 +26,8 @@ import javax.annotation.concurrent.Immutable;
import org.sonar.db.component.ComponentDto;
import org.sonar.db.project.ProjectDto;

import static java.util.Collections.emptyList;

@Immutable
public class Project {

@@ -44,7 +46,7 @@ public class Project {
}

public static Project from(ComponentDto project) {
return new Project(project.uuid(), project.getKey(), project.name(), project.description(), project.getTags());
return new Project(project.uuid(), project.getKey(), project.name(), project.description(), emptyList());
}

public static Project from(ProjectDto project) {

+ 13
- 6
server/sonar-server-common/src/test/java/org/sonar/server/measure/index/ProjectMeasuresIndexerTest.java View File

@@ -21,6 +21,7 @@ package org.sonar.server.measure.index;

import java.util.Arrays;
import java.util.Collection;
import java.util.function.Consumer;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.search.SearchHit;
import org.junit.Rule;
@@ -32,6 +33,7 @@ import org.sonar.db.component.ComponentDto;
import org.sonar.db.component.SnapshotDto;
import org.sonar.db.es.EsQueueDto;
import org.sonar.db.organization.OrganizationDto;
import org.sonar.db.project.ProjectDto;
import org.sonar.server.es.EsTester;
import org.sonar.server.es.IndexingResult;
import org.sonar.server.es.ProjectIndexer;
@@ -137,15 +139,14 @@ public class ProjectMeasuresIndexerTest {

@Test
public void update_index_when_project_tags_are_updated() {
ComponentDto project = db.components().insertPrivateProject(p -> p.setTagsString("foo"));
ComponentDto project = db.components().insertPrivateProject(defaults(), p -> p.setTagsString("foo"));
indexProject(project, PROJECT_CREATION);
assertThatProjectHasTag(project, "foo");

project.setTagsString("bar");
// TODO this should be removed at some point
db.getDbClient().componentDao().updateTags(db.getSession(), project);
db.getDbClient().projectDao().updateTags(db.getSession(), db.components().getProjectDto(project).setTagsString("bar"));

ProjectDto projectDto = db.components().getProjectDto(project);
projectDto.setTagsString("bar");
db.getDbClient().projectDao().updateTags(db.getSession(), projectDto);
// TODO change indexing?
IndexingResult result = indexProject(project, PROJECT_TAGS_UPDATE);

assertThatProjectHasTag(project, "bar");
@@ -249,4 +250,10 @@ public class ProjectMeasuresIndexerTest {
return underTest.index(db.getSession(), items);
}

private static <T> Consumer<T> defaults() {
return t -> {
// do nothing
};
}

}

+ 2
- 2
server/sonar-server-common/src/test/java/org/sonar/server/view/index/ViewIndexerTest.java View File

@@ -138,7 +138,7 @@ public class ViewIndexerTest {

@Test
public void index_application() {
ComponentDto application = db.components().insertApplication(db.getDefaultOrganization());
ComponentDto application = db.components().insertPrivateApplication(db.getDefaultOrganization());
ComponentDto project = db.components().insertPrivateProject();
db.components().insertComponent(newProjectCopy("PC1", project, application));

@@ -153,7 +153,7 @@ public class ViewIndexerTest {

@Test
public void index_application_on_startup() {
ComponentDto application = db.components().insertApplication(db.getDefaultOrganization());
ComponentDto application = db.components().insertPrivateApplication(db.getDefaultOrganization());
ComponentDto project = db.components().insertPrivateProject();
db.components().insertComponent(newProjectCopy("PC1", project, application));


+ 3
- 3
server/sonar-webserver-es/src/test/java/org/sonar/server/issue/index/IssueQueryFactoryTest.java View File

@@ -263,7 +263,7 @@ public class IssueQueryFactoryTest {
public void application_search_project_issues() {
ComponentDto project1 = db.components().insertPublicProject();
ComponentDto project2 = db.components().insertPublicProject();
ComponentDto application = db.components().insertApplication(db.getDefaultOrganization());
ComponentDto application = db.components().insertPublicApplication(db.getDefaultOrganization());
db.components().insertComponents(newProjectCopy("PC1", project1, application));
db.components().insertComponents(newProjectCopy("PC2", project2, application));
userSession.registerComponents(application, project1, project2);
@@ -279,9 +279,9 @@ public class IssueQueryFactoryTest {
ComponentDto project1 = db.components().insertPublicProject();
SnapshotDto analysis1 = db.components().insertSnapshot(project1, s -> s.setPeriodDate(addDays(now, -14).getTime()));
ComponentDto project2 = db.components().insertPublicProject();
SnapshotDto analysis2 = db.components().insertSnapshot(project2, s -> s.setPeriodDate(null));
db.components().insertSnapshot(project2, s -> s.setPeriodDate(null));
ComponentDto project3 = db.components().insertPublicProject();
ComponentDto application = db.components().insertApplication(db.getDefaultOrganization());
ComponentDto application = db.components().insertPublicApplication(db.getDefaultOrganization());
db.components().insertComponents(newProjectCopy("PC1", project1, application));
db.components().insertComponents(newProjectCopy("PC2", project2, application));
db.components().insertComponents(newProjectCopy("PC3", project3, application));

+ 1
- 1
server/sonar-webserver-es/src/test/java/org/sonar/server/permission/index/PermissionIndexerDaoTest.java View File

@@ -83,7 +83,7 @@ public class PermissionIndexerDaoTest {
privateProject2 = componentDbTester.insertPrivateProject(organization);
view1 = componentDbTester.insertView(organization);
view2 = componentDbTester.insertView(organization);
application = componentDbTester.insertApplication(organization);
application = componentDbTester.insertPublicApplication(organization);
user1 = userDbTester.insertUser();
user2 = userDbTester.insertUser();
group = userDbTester.insertGroup(organization);

+ 42
- 11
server/sonar-webserver-webapi/src/main/java/org/sonar/server/component/ws/ComponentDtoToWsComponent.java View File

@@ -21,12 +21,13 @@ package org.sonar.server.component.ws;

import com.google.common.collect.ImmutableSet;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import javax.annotation.Nullable;
import org.sonar.api.resources.Qualifiers;
import org.sonar.db.component.ComponentDto;
import org.sonar.db.component.SnapshotDto;
import org.sonar.db.organization.OrganizationDto;
import org.sonar.db.project.ProjectDto;
import org.sonar.server.project.Visibility;
import org.sonarqube.ws.Components;

@@ -46,7 +47,44 @@ class ComponentDtoToWsComponent {
// prevent instantiation
}

static Components.Component.Builder componentDtoToWsComponent(ComponentDto dto, OrganizationDto organizationDto, Optional<SnapshotDto> lastAnalysis) {
static Components.Component.Builder projectOrAppToWsComponent(ProjectDto project, ComponentDto component, OrganizationDto organizationDto,
@Nullable SnapshotDto lastAnalysis) {

checkArgument(
Objects.equals(project.getOrganizationUuid(), organizationDto.getUuid()),
"OrganizationUuid (%s) of ComponentDto to convert to Ws Component is not the same as the one (%s) of the specified OrganizationDto",
project.getOrganizationUuid(), organizationDto.getUuid());

Components.Component.Builder wsComponent = Components.Component.newBuilder()
.setOrganization(organizationDto.getKey())
.setKey(project.getKey())
// TODO switch to using ProjectDto.getName() after fixing
// org.sonarqube.tests.project.ProjectInfoTest.project_name_and_description_should_be_truncated_if_too_long
.setName(component.name())
.setQualifier(project.getQualifier());

// TODO switch to using ProjectDto.getDescription() after fixing
// org.sonarqube.tests.project.ProjectInfoTest.project_name_and_description_should_be_truncated_if_too_long
ofNullable(emptyToNull(component.description())).ifPresent(wsComponent::setDescription);
ofNullable(emptyToNull(component.language())).ifPresent(wsComponent::setLanguage);
ofNullable(lastAnalysis).ifPresent(
analysis -> {
wsComponent.setAnalysisDate(formatDateTime(analysis.getCreatedAt()));
ofNullable(analysis.getPeriodDate()).ifPresent(leak -> wsComponent.setLeakPeriodDate(formatDateTime(leak)));
ofNullable(analysis.getProjectVersion()).ifPresent(wsComponent::setVersion);
});
if (QUALIFIERS_WITH_VISIBILITY.contains(project.getQualifier())) {
// TODO switch to using ProjectDto.isPrivate() instead after fixing UpdateVisibilityAction WS
wsComponent.setVisibility(Visibility.getLabel(component.isPrivate()));
if (Qualifiers.PROJECT.equals(project.getQualifier())) {
wsComponent.getTagsBuilder().addAllTags(project.getTags());
}
}

return wsComponent;
}

static Components.Component.Builder componentDtoToWsComponent(ComponentDto dto, OrganizationDto organizationDto, @Nullable SnapshotDto lastAnalysis) {
checkArgument(
Objects.equals(dto.getOrganizationUuid(), organizationDto.getUuid()),
"OrganizationUuid (%s) of ComponentDto to convert to Ws Component is not the same as the one (%s) of the specified OrganizationDto",
@@ -54,7 +92,7 @@ class ComponentDtoToWsComponent {
return componentDtoToWsComponent(dto, organizationDto.getKey(), lastAnalysis);
}

private static Components.Component.Builder componentDtoToWsComponent(ComponentDto dto, String organizationDtoKey, Optional<SnapshotDto> lastAnalysis) {
private static Components.Component.Builder componentDtoToWsComponent(ComponentDto dto, String organizationDtoKey, @Nullable SnapshotDto lastAnalysis) {
Components.Component.Builder wsComponent = Components.Component.newBuilder()
.setOrganization(organizationDtoKey)
.setKey(dto.getKey())
@@ -65,8 +103,7 @@ class ComponentDtoToWsComponent {
ofNullable(emptyToNull(dto.path())).ifPresent(wsComponent::setPath);
ofNullable(emptyToNull(dto.description())).ifPresent(wsComponent::setDescription);
ofNullable(emptyToNull(dto.language())).ifPresent(wsComponent::setLanguage);
setTags(dto, wsComponent);
lastAnalysis.ifPresent(
ofNullable(lastAnalysis).ifPresent(
analysis -> {
wsComponent.setAnalysisDate(formatDateTime(analysis.getCreatedAt()));
ofNullable(analysis.getPeriodDate()).ifPresent(leak -> wsComponent.setLeakPeriodDate(formatDateTime(leak)));
@@ -77,10 +114,4 @@ class ComponentDtoToWsComponent {
}
return wsComponent;
}

private static void setTags(ComponentDto dto, Components.Component.Builder wsComponent) {
if (Qualifiers.PROJECT.equals(dto.qualifier())) {
wsComponent.getTagsBuilder().addAllTags(dto.getTags());
}
}
}

+ 17
- 15
server/sonar-webserver-webapi/src/main/java/org/sonar/server/component/ws/SearchProjectsAction.java View File

@@ -24,6 +24,7 @@ import com.google.common.collect.Ordering;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
@@ -48,6 +49,7 @@ import org.sonar.db.DbSession;
import org.sonar.db.component.ComponentDto;
import org.sonar.db.component.SnapshotDto;
import org.sonar.db.organization.OrganizationDto;
import org.sonar.db.project.ProjectDto;
import org.sonar.db.property.PropertyDto;
import org.sonar.db.property.PropertyQuery;
import org.sonar.server.component.ws.FilterParser.Criterion;
@@ -222,7 +224,7 @@ public class SearchProjectsAction implements ComponentsWsAction {

private SearchProjectsWsResponse handleForAnyOrganization(DbSession dbSession, SearchProjectsRequest request) {
SearchResults searchResults = searchData(dbSession, request, null);
Set<String> organizationUuids = searchResults.projects.stream().map(ComponentDto::getOrganizationUuid).collect(toSet());
Set<String> organizationUuids = searchResults.projects.stream().map(ProjectDto::getOrganizationUuid).collect(toSet());
Map<String, OrganizationDto> organizationsByUuid = dbClient.organizationDao().selectByUuids(dbSession, organizationUuids)
.stream()
.collect(MoreCollectors.uniqueIndex(OrganizationDto::getUuid));
@@ -252,8 +254,8 @@ public class SearchProjectsAction implements ComponentsWsAction {
.setPage(request.getPage(), request.getPageSize()));

List<String> projectUuids = esResults.getIds();
Ordering<ComponentDto> ordering = Ordering.explicit(projectUuids).onResultOf(ComponentDto::uuid);
List<ComponentDto> projects = ordering.immutableSortedCopy(dbClient.componentDao().selectByUuids(dbSession, projectUuids));
Ordering<ProjectDto> ordering = Ordering.explicit(projectUuids).onResultOf(ProjectDto::getUuid);
List<ProjectDto> projects = ordering.immutableSortedCopy(dbClient.projectDao().selectByUuids(dbSession, new HashSet<>(projectUuids)));
Map<String, SnapshotDto> analysisByProjectUuid = getSnapshots(dbSession, request, projectUuids);
return new SearchResults(projects, favoriteProjectUuids, esResults, analysisByProjectUuid, query);
}
@@ -319,7 +321,7 @@ public class SearchProjectsAction implements ComponentsWsAction {
}

private SearchProjectsWsResponse buildResponse(SearchProjectsRequest request, SearchResults searchResults, Map<String, OrganizationDto> organizationsByUuid) {
Function<ComponentDto, Component> dbToWsComponent = new DbToWsComponent(request, organizationsByUuid, searchResults.favoriteProjectUuids, searchResults.analysisByProjectUuid,
Function<ProjectDto, Component> dbToWsComponent = new DbToWsComponent(request, organizationsByUuid, searchResults.favoriteProjectUuids, searchResults.analysisByProjectUuid,
userSession.isLoggedIn());

Map<String, OrganizationDto> organizationsByUuidForAdditionalInfo = new HashMap<>();
@@ -426,7 +428,7 @@ public class SearchProjectsAction implements ComponentsWsAction {
}
}

private static class DbToWsComponent implements Function<ComponentDto, Component> {
private static class DbToWsComponent implements Function<ProjectDto, Component> {
private final SearchProjectsRequest request;
private final Component.Builder wsComponent;
private final Map<String, OrganizationDto> organizationsByUuid;
@@ -445,19 +447,19 @@ public class SearchProjectsAction implements ComponentsWsAction {
}

@Override
public Component apply(ComponentDto dbComponent) {
String organizationUuid = dbComponent.getOrganizationUuid();
public Component apply(ProjectDto dbProject) {
String organizationUuid = dbProject.getOrganizationUuid();
OrganizationDto organizationDto = organizationsByUuid.get(organizationUuid);
checkFound(organizationDto, "Organization with uuid '%s' not found", organizationUuid);
wsComponent
.clear()
.setOrganization(organizationDto.getKey())
.setKey(dbComponent.getDbKey())
.setName(dbComponent.name())
.setVisibility(Visibility.getLabel(dbComponent.isPrivate()));
wsComponent.getTagsBuilder().addAllTags(dbComponent.getTags());
.setKey(dbProject.getKey())
.setName(dbProject.getName())
.setVisibility(Visibility.getLabel(dbProject.isPrivate()));
wsComponent.getTagsBuilder().addAllTags(dbProject.getTags());

SnapshotDto snapshotDto = analysisByProjectUuid.get(dbComponent.uuid());
SnapshotDto snapshotDto = analysisByProjectUuid.get(dbProject.getUuid());
if (snapshotDto != null) {
if (request.getAdditionalFields().contains(ANALYSIS_DATE)) {
wsComponent.setAnalysisDate(formatDateTime(snapshotDto.getCreatedAt()));
@@ -468,7 +470,7 @@ public class SearchProjectsAction implements ComponentsWsAction {
}

if (isUserLoggedIn) {
wsComponent.setIsFavorite(favoriteProjectUuids.contains(dbComponent.uuid()));
wsComponent.setIsFavorite(favoriteProjectUuids.contains(dbProject.getUuid()));
}

return wsComponent.build();
@@ -476,14 +478,14 @@ public class SearchProjectsAction implements ComponentsWsAction {
}

private static class SearchResults {
private final List<ComponentDto> projects;
private final List<ProjectDto> projects;
private final Set<String> favoriteProjectUuids;
private final Facets facets;
private final Map<String, SnapshotDto> analysisByProjectUuid;
private final ProjectMeasuresQuery query;
private final int total;

private SearchResults(List<ComponentDto> projects, Set<String> favoriteProjectUuids, SearchIdResult<String> searchResults, Map<String, SnapshotDto> analysisByProjectUuid,
private SearchResults(List<ProjectDto> projects, Set<String> favoriteProjectUuids, SearchIdResult<String> searchResults, Map<String, SnapshotDto> analysisByProjectUuid,
ProjectMeasuresQuery query) {
this.projects = projects;
this.favoriteProjectUuids = favoriteProjectUuids;

+ 34
- 5
server/sonar-webserver-webapi/src/main/java/org/sonar/server/component/ws/ShowAction.java View File

@@ -19,11 +19,14 @@
*/
package org.sonar.server.component.ws;

import com.google.common.collect.ImmutableSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.IntStream;
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
import org.sonar.api.resources.Qualifiers;
import org.sonar.api.server.ws.Change;
import org.sonar.api.server.ws.Response;
import org.sonar.api.server.ws.WebService;
@@ -33,11 +36,14 @@ import org.sonar.db.DbSession;
import org.sonar.db.component.ComponentDto;
import org.sonar.db.component.SnapshotDto;
import org.sonar.db.organization.OrganizationDto;
import org.sonar.db.project.ProjectDto;
import org.sonar.server.component.ComponentFinder;
import org.sonar.server.user.UserSession;
import org.sonarqube.ws.Components;
import org.sonarqube.ws.Components.ShowWsResponse;

import static org.sonar.server.component.ws.ComponentDtoToWsComponent.componentDtoToWsComponent;
import static org.sonar.server.component.ws.ComponentDtoToWsComponent.projectOrAppToWsComponent;
import static org.sonar.server.component.ws.MeasuresWsParameters.PARAM_BRANCH;
import static org.sonar.server.component.ws.MeasuresWsParameters.PARAM_PULL_REQUEST;
import static org.sonar.server.ws.KeyExamples.KEY_BRANCH_EXAMPLE_001;
@@ -48,6 +54,7 @@ import static org.sonarqube.ws.client.component.ComponentsWsParameters.ACTION_SH
import static org.sonarqube.ws.client.component.ComponentsWsParameters.PARAM_COMPONENT;

public class ShowAction implements ComponentsWsAction {
private static final Set<String> PROJECT_OR_APP_QUALIFIERS = ImmutableSet.of(Qualifiers.PROJECT, Qualifiers.APP);
private final UserSession userSession;
private final DbClient dbClient;
private final ComponentFinder componentFinder;
@@ -103,7 +110,7 @@ public class ShowAction implements ComponentsWsAction {
Optional<SnapshotDto> lastAnalysis = dbClient.snapshotDao().selectLastAnalysisByComponentUuid(dbSession, component.projectUuid());
List<ComponentDto> ancestors = dbClient.componentDao().selectAncestors(dbSession, component);
OrganizationDto organizationDto = componentFinder.getOrganization(dbSession, component);
return buildResponse(component, organizationDto, ancestors, lastAnalysis);
return buildResponse(dbClient, dbSession, component, organizationDto, ancestors, lastAnalysis.orElse(null));
}
}

@@ -115,15 +122,37 @@ public class ShowAction implements ComponentsWsAction {
return componentFinder.getByKeyAndOptionalBranchOrPullRequest(dbSession, componentKey, branch, pullRequest);
}

private static ShowWsResponse buildResponse(ComponentDto component, OrganizationDto organizationDto, List<ComponentDto> orderedAncestors, Optional<SnapshotDto> lastAnalysis) {
private static ShowWsResponse buildResponse(DbClient dbClient, DbSession dbSession, ComponentDto component,
OrganizationDto organizationDto, List<ComponentDto> orderedAncestors,
@Nullable SnapshotDto lastAnalysis) {
ShowWsResponse.Builder response = ShowWsResponse.newBuilder();
response.setComponent(componentDtoToWsComponent(component, organizationDto, lastAnalysis));
response.setComponent(toWsComponent(dbClient, dbSession, component, organizationDto, lastAnalysis));
addAncestorsToResponse(dbClient, dbSession, response, orderedAncestors, organizationDto, lastAnalysis);
return response.build();
}

private static void addAncestorsToResponse(DbClient dbClient, DbSession dbSession, ShowWsResponse.Builder response, List<ComponentDto> orderedAncestors,
OrganizationDto organizationDto,
@Nullable SnapshotDto lastAnalysis) {
// ancestors are ordered from root to leaf, whereas it's the opposite in WS response
int size = orderedAncestors.size() - 1;
IntStream.rangeClosed(0, size).forEach(
index -> response.addAncestors(componentDtoToWsComponent(orderedAncestors.get(size - index), organizationDto, lastAnalysis)));
return response.build();
index -> response.addAncestors(toWsComponent(dbClient, dbSession, orderedAncestors.get(size - index), organizationDto, lastAnalysis)));
}

private static Components.Component.Builder toWsComponent(DbClient dbClient, DbSession dbSession, ComponentDto component, OrganizationDto organizationDto,
@Nullable SnapshotDto lastAnalysis) {
if (isProjectOrApp(component)) {
ProjectDto project = dbClient.projectDao().selectProjectOrAppByKey(dbSession, component.getKey())
.orElseThrow(() -> new IllegalStateException("Project is in invalid state."));
return projectOrAppToWsComponent(project, component, organizationDto, lastAnalysis);
} else {
return componentDtoToWsComponent(component, organizationDto, lastAnalysis);
}
}

private static boolean isProjectOrApp(ComponentDto component) {
return component.getMainBranchProjectUuid() == null && PROJECT_OR_APP_QUALIFIERS.contains(component.qualifier());
}

private static Request toShowWsRequest(org.sonar.api.server.ws.Request request) {

+ 19
- 7
server/sonar-webserver-webapi/src/main/java/org/sonar/server/component/ws/TreeAction.java View File

@@ -29,10 +29,10 @@ import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
import org.sonar.api.resources.Qualifiers;
import org.sonar.api.resources.ResourceTypes;
import org.sonar.api.server.ws.Change;
import org.sonar.api.server.ws.Response;
@@ -48,6 +48,7 @@ import org.sonar.db.component.ComponentDto;
import org.sonar.db.component.ComponentTreeQuery;
import org.sonar.db.component.ComponentTreeQuery.Strategy;
import org.sonar.db.organization.OrganizationDto;
import org.sonar.db.project.ProjectDto;
import org.sonar.server.component.ComponentFinder;
import org.sonar.server.user.UserSession;
import org.sonarqube.ws.Components;
@@ -61,6 +62,7 @@ import static org.sonar.core.util.stream.MoreCollectors.toList;
import static org.sonar.db.component.ComponentTreeQuery.Strategy.CHILDREN;
import static org.sonar.db.component.ComponentTreeQuery.Strategy.LEAVES;
import static org.sonar.server.component.ws.ComponentDtoToWsComponent.componentDtoToWsComponent;
import static org.sonar.server.component.ws.ComponentDtoToWsComponent.projectOrAppToWsComponent;
import static org.sonar.server.ws.KeyExamples.KEY_BRANCH_EXAMPLE_001;
import static org.sonar.server.ws.KeyExamples.KEY_PROJECT_EXAMPLE_001;
import static org.sonar.server.ws.KeyExamples.KEY_PULL_REQUEST_EXAMPLE_001;
@@ -97,6 +99,8 @@ public class TreeAction implements ComponentsWsAction {
private final UserSession userSession;
private final I18n i18n;

private static final Set<String> PROJECT_OR_APP_QUALIFIERS = ImmutableSortedSet.of(Qualifiers.PROJECT, Qualifiers.APP);

public TreeAction(DbClient dbClient, ComponentFinder componentFinder, ResourceTypes resourceTypes, UserSession userSession, I18n i18n) {
this.dbClient = dbClient;
this.componentFinder = componentFinder;
@@ -182,7 +186,7 @@ public class TreeAction implements ComponentsWsAction {

Map<String, ComponentDto> referenceComponentsByUuid = searchReferenceComponentsByUuid(dbSession, components);

return buildResponse(baseComponent, organizationDto, components, referenceComponentsByUuid,
return buildResponse(dbSession, baseComponent, organizationDto, components, referenceComponentsByUuid,
Paging.forPageIndex(treeRequest.getPage()).withPageSize(treeRequest.getPageSize()).andTotal(total));
}
}
@@ -211,7 +215,7 @@ public class TreeAction implements ComponentsWsAction {
userSession.checkComponentPermission(UserRole.USER, baseComponent);
}

private static TreeWsResponse buildResponse(ComponentDto baseComponent, OrganizationDto organizationDto, List<ComponentDto> components,
private TreeWsResponse buildResponse(DbSession dbSession, ComponentDto baseComponent, OrganizationDto organizationDto, List<ComponentDto> components,
Map<String, ComponentDto> referenceComponentsByUuid, Paging paging) {
TreeWsResponse.Builder response = TreeWsResponse.newBuilder();
response.getPagingBuilder()
@@ -220,17 +224,25 @@ public class TreeAction implements ComponentsWsAction {
.setTotal(paging.total())
.build();

response.setBaseComponent(toWsComponent(baseComponent, organizationDto, referenceComponentsByUuid));
response.setBaseComponent(toWsComponent(dbSession, baseComponent, organizationDto, referenceComponentsByUuid));
for (ComponentDto dto : components) {
response.addComponents(toWsComponent(dto, organizationDto, referenceComponentsByUuid));
response.addComponents(toWsComponent(dbSession, dto, organizationDto, referenceComponentsByUuid));
}

return response.build();
}

private static Components.Component.Builder toWsComponent(ComponentDto component, OrganizationDto organizationDto,
private Components.Component.Builder toWsComponent(DbSession dbSession, ComponentDto component, OrganizationDto organizationDto,
Map<String, ComponentDto> referenceComponentsByUuid) {
Components.Component.Builder wsComponent = componentDtoToWsComponent(component, organizationDto, Optional.empty());

Components.Component.Builder wsComponent;
if (component.getMainBranchProjectUuid() == null && component.isRootProject() &&
PROJECT_OR_APP_QUALIFIERS.contains(component.qualifier())) {
ProjectDto projectDto = componentFinder.getProjectOrApplicationByKey(dbSession, component.getKey());
wsComponent = projectOrAppToWsComponent(projectDto, component, organizationDto, null);
} else {
wsComponent = componentDtoToWsComponent(component, organizationDto, null);
}

ComponentDto referenceComponent = referenceComponentsByUuid.get(component.getCopyResourceUuid());
if (referenceComponent != null) {

+ 0
- 4
server/sonar-webserver-webapi/src/main/java/org/sonar/server/projecttag/ws/SetAction.java View File

@@ -98,15 +98,11 @@ public class SetAction implements ProjectTagsWsAction {
ProjectDto project = componentFinder.getProjectByKey(dbSession, projectKey);
userSession.checkProjectPermission(UserRole.ADMIN, project);

ComponentDto component = componentFinder.getByKey(dbSession, projectKey);

project.setTags(tags);
project.setUpdatedAt(system2.now());
dbClient.projectDao().updateTags(dbSession, project);

// FIXME we use the old table to index and also when returning generic components, so we still need to add it to the old table.
component.setTags(tags);
dbClient.componentDao().updateTags(dbSession, component);
projectIndexers.commitAndIndexProjects(dbSession, singletonList(project), PROJECT_TAGS_UPDATE);
}


+ 1
- 1
server/sonar-webserver-webapi/src/test/java/org/sonar/server/ce/ws/ActivityActionTest.java View File

@@ -326,7 +326,7 @@ public class ActivityActionTest {

@Test
public void search_activity_returns_application() {
ComponentDto apacheApp = db.components().insertApplication(db.getDefaultOrganization(), a -> a.setName("Apache App"));
ComponentDto apacheApp = db.components().insertPublicApplication(db.getDefaultOrganization(), a -> a.setName("Apache App"));
db.components().insertSnapshot(apacheApp);
logInAsSystemAdministrator();
insertActivity("T2", apacheApp, SUCCESS);

+ 5
- 6
server/sonar-webserver-webapi/src/test/java/org/sonar/server/ce/ws/AnalysisStatusActionTest.java View File

@@ -30,7 +30,6 @@ import org.sonar.db.DbTester;
import org.sonar.db.ce.CeActivityDto;
import org.sonar.db.ce.CeQueueDto;
import org.sonar.db.ce.CeTaskMessageDto;
import org.sonar.db.component.BranchDao;
import org.sonar.db.component.BranchType;
import org.sonar.db.component.ComponentDto;
import org.sonar.db.component.SnapshotDto;
@@ -100,11 +99,11 @@ public class AnalysisStatusActionTest {
public void json_example() {
OrganizationDto organization = db.organizations().insert(o -> o.setKey("my-org-1"));
ComponentDto project = db.components().insertPrivateProject(organization,
p -> p.setUuid("AU_w74XMgAS1Hm6h4-Y-"),
p -> p.setProjectUuid("AU_w74XMgAS1Hm6h4-Y-"),
p -> p.setRootUuid("AU_w74XMgAS1Hm6h4-Y-"),
p -> p.setDbKey("com.github.kevinsawicki:http-request-parent"),
p -> p.setName("HttpRequest"));
p -> p.setUuid("AU_w74XMgAS1Hm6h4-Y-")
.setProjectUuid("AU_w74XMgAS1Hm6h4-Y-")
.setRootUuid("AU_w74XMgAS1Hm6h4-Y-")
.setDbKey("com.github.kevinsawicki:http-request-parent")
.setName("HttpRequest"));

userSession.addProjectPermission(UserRole.USER, project);


+ 2
- 4
server/sonar-webserver-webapi/src/test/java/org/sonar/server/component/ComponentServiceUpdateKeyTest.java View File

@@ -165,8 +165,7 @@ public class ComponentServiceUpdateKeyTest {
assertComponentKeyUpdated(inactiveModule.getDbKey(), "your_project:root:inactive_module");
assertComponentKeyUpdated(inactiveFile.getDbKey(), "your_project:root:module:src/InactiveFile.xoo");
verify(projectLifeCycleListeners).onProjectsRekeyed(ImmutableSet.of(
new RekeyedProject(new Project(project.uuid(), "your_project", project.name(), project.uuid(), emptyList()), "my_project")
));
new RekeyedProject(new Project(project.uuid(), "your_project", project.name(), project.uuid(), emptyList()), "my_project")));
}

@Test
@@ -182,8 +181,7 @@ public class ComponentServiceUpdateKeyTest {
assertComponentKeyUpdated(module.getDbKey(), "your_project:root:module");
assertComponentKeyUpdated(file.getDbKey(), "your_project:root:module:src/File.xoo");
verify(projectLifeCycleListeners).onProjectsRekeyed(ImmutableSet.of(
new RekeyedProject(new Project(project.uuid(), "your_project", project.name(), project.uuid(), emptyList()), "my_project")
));
new RekeyedProject(new Project(project.uuid(), "your_project", project.name(), project.uuid(), emptyList()), "my_project")));
}

private void assertComponentKeyUpdated(String oldKey, String newKey) {

+ 1
- 1
server/sonar-webserver-webapi/src/test/java/org/sonar/server/component/ws/ComponentDtoToWsComponentTest.java View File

@@ -44,7 +44,7 @@ public class ComponentDtoToWsComponentTest {
expectedException.expectMessage("OrganizationUuid (" + organizationDto1.getUuid() + ") of ComponentDto to convert " +
"to Ws Component is not the same as the one (" + organizationDto2.getUuid() + ") of the specified OrganizationDto");

componentDtoToWsComponent(componentDto, organizationDto2, Optional.empty());
componentDtoToWsComponent(componentDto, organizationDto2, null);
}

}

+ 33
- 26
server/sonar-webserver-webapi/src/test/java/org/sonar/server/component/ws/SearchProjectsActionTest.java View File

@@ -45,6 +45,7 @@ import org.sonar.db.component.ComponentDto;
import org.sonar.db.measure.LiveMeasureDto;
import org.sonar.db.metric.MetricDto;
import org.sonar.db.organization.OrganizationDto;
import org.sonar.db.project.ProjectDto;
import org.sonar.db.property.PropertyDto;
import org.sonar.server.component.ws.SearchProjectsAction.RequestBuilder;
import org.sonar.server.component.ws.SearchProjectsAction.SearchProjectsRequest;
@@ -201,19 +202,16 @@ public class SearchProjectsActionTest {
OrganizationDto organization2Dto = db.organizations().insert(dto -> dto.setKey("my-org-key-2").setName("Bar"));

MetricDto coverage = db.measures().insertMetric(c -> c.setKey(COVERAGE).setValueType("PERCENT"));
ComponentDto project1 = insertProject(organization1Dto, c -> c
.setDbKey(KEY_PROJECT_EXAMPLE_001)
.setName("My Project 1")
.setTagsString("finance, java"),
ComponentDto project1 = insertProject(organization1Dto,
c -> c.setDbKey(KEY_PROJECT_EXAMPLE_001).setName("My Project 1"),
p -> p.setTagsString("finance, java"),
new Measure(coverage, c -> c.setValue(80d)));
ComponentDto project2 = insertProject(organization1Dto, c -> c
.setDbKey(KEY_PROJECT_EXAMPLE_002)
.setName("My Project 2"),
ComponentDto project2 = insertProject(organization1Dto,
c -> c.setDbKey(KEY_PROJECT_EXAMPLE_002).setName("My Project 2"),
new Measure(coverage, c -> c.setValue(90d)));
ComponentDto project3 = insertProject(organization2Dto, c -> c
.setDbKey(KEY_PROJECT_EXAMPLE_003)
.setName("My Project 3")
.setTagsString("sales, offshore, java"),
ComponentDto project3 = insertProject(organization2Dto,
c -> c.setDbKey(KEY_PROJECT_EXAMPLE_003).setName("My Project 3"),
p -> p.setTagsString("sales, offshore, java"),
new Measure(coverage, c -> c.setValue(20d)));
addFavourite(project1);

@@ -372,9 +370,9 @@ public class SearchProjectsActionTest {
userSession.logIn();
OrganizationDto organizationDto = db.organizations().insert();
MetricDto ratingMetric = db.measures().insertMetric(c -> c.setKey(newMetricKey).setValueType(INT.name()));
ComponentDto project1 = insertProject(organizationDto, new Measure(ratingMetric, c -> c.setVariation(1d)));
insertProject(organizationDto, new Measure(ratingMetric, c -> c.setVariation(1d)));
ComponentDto project2 = insertProject(organizationDto, new Measure(ratingMetric, c -> c.setVariation(2d)));
ComponentDto project3 = insertProject(organizationDto, new Measure(ratingMetric, c -> c.setVariation(3d)));
insertProject(organizationDto, new Measure(ratingMetric, c -> c.setVariation(3d)));

SearchProjectsWsResponse result = call(request.setFilter(newMetricKey + " = 2"));

@@ -385,9 +383,9 @@ public class SearchProjectsActionTest {
public void filter_projects_by_tags() {
userSession.logIn();
OrganizationDto organizationDto = db.organizations().insert();
ComponentDto project1 = insertProject(organizationDto, c -> c.setTags(asList("finance", "platform")));
ComponentDto project2 = insertProject(organizationDto, c -> c.setTags(singletonList("marketing")));
ComponentDto project3 = insertProject(organizationDto, c -> c.setTags(singletonList("offshore")));
ComponentDto project1 = insertProject(organizationDto, defaults(), p -> p.setTags(asList("finance", "platform")));
insertProject(organizationDto, defaults(), p -> p.setTags(singletonList("marketing")));
ComponentDto project3 = insertProject(organizationDto, defaults(), p -> p.setTags(singletonList("offshore")));

SearchProjectsWsResponse result = call(request.setFilter("tags in (finance, offshore)"));

@@ -704,9 +702,9 @@ public class SearchProjectsActionTest {
public void return_tags_facet() {
userSession.logIn();
OrganizationDto organization = db.getDefaultOrganization();
insertProject(organization, c -> c.setTags(asList("finance", "platform")));
insertProject(organization, c -> c.setTags(singletonList("offshore")));
insertProject(organization, c -> c.setTags(singletonList("offshore")));
insertProject(organization, defaults(), p -> p.setTags(asList("finance", "platform")));
insertProject(organization, defaults(), p -> p.setTags(singletonList("offshore")));
insertProject(organization, defaults(), p -> p.setTags(singletonList("offshore")));

SearchProjectsWsResponse result = call(request.setFacets(singletonList(FILTER_TAGS)));

@@ -725,9 +723,9 @@ public class SearchProjectsActionTest {
public void return_tags_facet_with_tags_having_no_project_if_tags_is_in_filter() {
userSession.logIn();
OrganizationDto organization = db.getDefaultOrganization();
insertProject(organization, c -> c.setTags(asList("finance", "platform")));
insertProject(organization, c -> c.setTags(singletonList("offshore")));
insertProject(organization, c -> c.setTags(singletonList("offshore")));
insertProject(organization, defaults(), p -> p.setTags(asList("finance", "platform")));
insertProject(organization, defaults(), p -> p.setTags(singletonList("offshore")));
insertProject(organization, defaults(), p -> p.setTags(singletonList("offshore")));

SearchProjectsWsResponse result = call(request.setFilter("tags = marketing").setFacets(singletonList(FILTER_TAGS)));

@@ -1182,12 +1180,16 @@ public class SearchProjectsActionTest {
}

private ComponentDto insertProject(OrganizationDto organizationDto, Measure... measures) {
return insertProject(organizationDto, c -> {
}, measures);
return insertProject(organizationDto, defaults(), defaults(), measures);
}

private ComponentDto insertProject(OrganizationDto organizationDto, Consumer<ComponentDto> projectConsumer, Measure... measures) {
ComponentDto project = db.components().insertPublicProject(organizationDto, projectConsumer);
private ComponentDto insertProject(OrganizationDto organizationDto, Consumer<ComponentDto> componentConsumer, Measure... measures) {
return insertProject(organizationDto, componentConsumer, defaults(), measures);
}

private ComponentDto insertProject(OrganizationDto organizationDto, Consumer<ComponentDto> componentConsumer, Consumer<ProjectDto> projectConsumer,
Measure... measures) {
ComponentDto project = db.components().insertPublicProject(organizationDto, componentConsumer, projectConsumer);
Arrays.stream(measures).forEach(m -> db.measures().insertLiveMeasure(project, m.metric, m.consumer));
authorizationIndexerTester.allowOnlyAnyone(project);
projectMeasuresIndexer.indexOnAnalysis(project.uuid());
@@ -1203,4 +1205,9 @@ public class SearchProjectsActionTest {
this.consumer = consumer;
}
}

private static <T> Consumer<T> defaults() {
return t -> {
};
}
}

+ 8
- 6
server/sonar-webserver-webapi/src/test/java/org/sonar/server/component/ws/ShowActionTest.java View File

@@ -381,12 +381,14 @@ public class ShowActionTest {

private void insertJsonExampleComponentsAndSnapshots() {
OrganizationDto organizationDto = db.organizations().insertForKey("my-org-1");
ComponentDto project = db.components().insertComponent(newPrivateProjectDto(organizationDto, "AVIF98jgA3Ax6PH2efOW")
.setDbKey("com.sonarsource:java-markdown")
.setName("Java Markdown")
.setDescription("Java Markdown Project")
.setQualifier(Qualifiers.PROJECT)
.setTagsString("language, plugin"));
ComponentDto project = db.components().insertPrivateProject(organizationDto,
c -> c.setUuid("AVIF98jgA3Ax6PH2efOW")
.setProjectUuid("AVIF98jgA3Ax6PH2efOW")
.setDbKey("com.sonarsource:java-markdown")
.setName("Java Markdown")
.setDescription("Java Markdown Project")
.setQualifier(Qualifiers.PROJECT),
p -> p.setTagsString("language, plugin"));
db.components().insertSnapshot(project, snapshot -> snapshot
.setProjectVersion("1.1")
.setCreatedAt(parseDateTime("2017-03-01T11:39:03+0100").getTime())

+ 1
- 1
server/sonar-webserver-webapi/src/test/java/org/sonar/server/component/ws/SuggestionsActionTest.java View File

@@ -584,7 +584,7 @@ public class SuggestionsActionTest {
public void should_only_provide_project_for_certain_qualifiers() {
String query = randomAlphabetic(10);

ComponentDto app = db.components().insertApplication(organization, v -> v.setName(query));
ComponentDto app = db.components().insertPublicApplication(organization, v -> v.setName(query));
ComponentDto view = db.components().insertView(organization, v -> v.setName(query));
ComponentDto subView = db.components().insertComponent(ComponentTesting.newSubView(view).setName(query));
ComponentDto project = db.components().insertPrivateProject(organization, p -> p.setName(query));

+ 1
- 1
server/sonar-webserver-webapi/src/test/java/org/sonar/server/component/ws/TreeActionTest.java View File

@@ -300,7 +300,7 @@ public class TreeActionTest {

@Test
public void response_is_empty_on_provisioned_projects() {
ComponentDto project = db.components().insertComponent(newPrivateProjectDto(db.getDefaultOrganization(), "project-uuid"));
ComponentDto project = db.components().insertPrivateProject(db.getDefaultOrganization(), "project-uuid");
logInWithBrowsePermission(project);

TreeWsResponse response = ws.newRequest()

+ 6
- 6
server/sonar-webserver-webapi/src/test/java/org/sonar/server/issue/ws/SearchActionComponentsTest.java View File

@@ -505,7 +505,7 @@ public class SearchActionComponentsTest {
@Test
public void ignore_application_without_browse_permission() {
ComponentDto project = db.components().insertPublicProject();
ComponentDto application = db.components().insertApplication(db.getDefaultOrganization());
ComponentDto application = db.components().insertPublicApplication(db.getDefaultOrganization());
db.components().insertComponents(newProjectCopy("PC1", project, application));
RuleDefinitionDto rule = db.rules().insertIssueRule();
db.issues().insertIssue(rule, project, project);
@@ -522,7 +522,7 @@ public class SearchActionComponentsTest {
@Test
public void search_application_without_projects() {
ComponentDto project = db.components().insertPublicProject();
ComponentDto application = db.components().insertApplication(db.getDefaultOrganization());
ComponentDto application = db.components().insertPublicApplication(db.getDefaultOrganization());
RuleDefinitionDto rule = db.rules().insertIssueRule();
db.issues().insertIssue(rule, project, project);
allowAnyoneOnProjects(project, application);
@@ -539,7 +539,7 @@ public class SearchActionComponentsTest {
public void search_by_application_and_by_leak() {
Date now = new Date();
RuleDefinitionDto rule = db.rules().insertIssueRule();
ComponentDto application = db.components().insertApplication(db.getDefaultOrganization());
ComponentDto application = db.components().insertPublicApplication(db.getDefaultOrganization());
// Project 1
ComponentDto project1 = db.components().insertPublicProject();
db.components().insertSnapshot(project1, s -> s.setPeriodDate(addDays(now, -14).getTime()));
@@ -570,7 +570,7 @@ public class SearchActionComponentsTest {
public void search_by_application_and_project() {
ComponentDto project1 = db.components().insertPublicProject();
ComponentDto project2 = db.components().insertPublicProject();
ComponentDto application = db.components().insertApplication(db.getDefaultOrganization());
ComponentDto application = db.components().insertPublicApplication(db.getDefaultOrganization());
db.components().insertComponents(newProjectCopy("PC1", project1, application));
db.components().insertComponents(newProjectCopy("PC2", project2, application));
RuleDefinitionDto rule = db.rules().insertIssueRule();
@@ -593,7 +593,7 @@ public class SearchActionComponentsTest {
public void search_by_application_and_project_and_leak() {
Date now = new Date();
RuleDefinitionDto rule = db.rules().insertIssueRule();
ComponentDto application = db.components().insertApplication(db.getDefaultOrganization());
ComponentDto application = db.components().insertPublicApplication(db.getDefaultOrganization());
// Project 1
ComponentDto project1 = db.components().insertPublicProject();
db.components().insertSnapshot(project1, s -> s.setPeriodDate(addDays(now, -14).getTime()));
@@ -625,7 +625,7 @@ public class SearchActionComponentsTest {
public void search_by_application_and_by_leak_when_one_project_has_no_leak() {
Date now = new Date();
RuleDefinitionDto rule = db.rules().insertIssueRule();
ComponentDto application = db.components().insertApplication(db.getDefaultOrganization());
ComponentDto application = db.components().insertPublicApplication(db.getDefaultOrganization());
// Project 1
ComponentDto project1 = db.components().insertPublicProject();
db.components().insertSnapshot(project1, s -> s.setPeriodDate(addDays(now, -14).getTime()));

+ 3
- 3
server/sonar-webserver-webapi/src/test/java/org/sonar/server/permission/PermissionTemplateServiceTest.java View File

@@ -356,7 +356,7 @@ public class PermissionTemplateServiceTest {
@Test
public void apply_template_on_applications() {
OrganizationDto organization = dbTester.organizations().insert();
ComponentDto application = dbTester.components().insertApplication(organization);
ComponentDto application = dbTester.components().insertPublicApplication(organization);
PermissionTemplateDto permissionTemplate = dbTester.permissionTemplates().insertTemplate(organization);
GroupDto group = dbTester.users().insertGroup(organization);
dbTester.permissionTemplates().addGroupToTemplate(permissionTemplate, group, ADMINISTER.getKey());
@@ -372,7 +372,7 @@ public class PermissionTemplateServiceTest {
@Test
public void apply_default_view_template_on_application() {
OrganizationDto organization = dbTester.organizations().insert();
ComponentDto application = dbTester.components().insertApplication(organization);
ComponentDto application = dbTester.components().insertPublicApplication(organization);
PermissionTemplateDto projectPermissionTemplate = dbTester.permissionTemplates().insertTemplate(organization);
PermissionTemplateDto appPermissionTemplate = dbTester.permissionTemplates().insertTemplate(organization);
PermissionTemplateDto portPermissionTemplate = dbTester.permissionTemplates().insertTemplate(organization);
@@ -390,7 +390,7 @@ public class PermissionTemplateServiceTest {
@Test
public void apply_project_default_template_on_application_when_no_application_default_template() {
OrganizationDto organization = dbTester.organizations().insert();
ComponentDto application = dbTester.components().insertApplication(organization);
ComponentDto application = dbTester.components().insertPublicApplication(organization);
PermissionTemplateDto projectPermissionTemplate = dbTester.permissionTemplates().insertTemplate(organization);
GroupDto group = dbTester.users().insertGroup(organization);
dbTester.permissionTemplates().addGroupToTemplate(projectPermissionTemplate, group, PROVISION_PROJECTS.getKey());

+ 0
- 1
server/sonar-webserver-webapi/src/test/java/org/sonar/server/project/ws/UpdateKeyActionTest.java View File

@@ -31,7 +31,6 @@ import org.sonar.api.web.UserRole;
import org.sonar.db.DbClient;
import org.sonar.db.DbTester;
import org.sonar.db.component.ComponentDto;
import org.sonar.db.component.ComponentTesting;
import org.sonar.server.component.ComponentFinder;
import org.sonar.server.component.ComponentService;
import org.sonar.server.es.ProjectIndexers;

+ 1
- 1
server/sonar-webserver-webapi/src/test/java/org/sonar/server/project/ws/UpdateVisibilityActionTest.java View File

@@ -204,7 +204,7 @@ public class UpdateVisibilityActionTest {
ComponentDto dir = ComponentTesting.newDirectory(project, "path");
ComponentDto file = ComponentTesting.newFileDto(project);
dbTester.components().insertComponents(module, dir, file);
ComponentDto application = dbTester.components().insertApplication(organization);
ComponentDto application = dbTester.components().insertPublicApplication(organization);
ComponentDto portfolio = dbTester.components().insertView(organization);
ComponentDto subView = ComponentTesting.newSubView(portfolio);
ComponentDto projectCopy = newProjectCopy("foo", project, subView);

+ 7
- 7
server/sonar-webserver-webapi/src/test/java/org/sonar/server/projectanalysis/ws/SearchActionTest.java View File

@@ -234,7 +234,7 @@ public class SearchActionTest {
@Test
public void return_analyses_of_application() {
OrganizationDto organization = db.organizations().insert();
ComponentDto application = db.components().insertApplication(organization);
ComponentDto application = db.components().insertPublicApplication(organization);
userSession.registerComponents(application);
SnapshotDto firstAnalysis = db.components().insertSnapshot(newAnalysis(application).setCreatedAt(1_000_000L));
SnapshotDto secondAnalysis = db.components().insertSnapshot(newAnalysis(application).setCreatedAt(2_000_000L));
@@ -254,7 +254,7 @@ public class SearchActionTest {
@Test
public void return_definition_change_events_on_application_analyses() {
OrganizationDto organization = db.organizations().insert();
ComponentDto application = db.components().insertApplication(organization);
ComponentDto application = db.components().insertPublicApplication(organization);
userSession.registerComponents(application);
SnapshotDto firstAnalysis = db.components().insertSnapshot(newAnalysis(application).setCreatedAt(1_000_000L));
EventDto event = db.events().insertEvent(newEvent(firstAnalysis).setName("").setUuid("E11").setCategory(DEFINITION_CHANGE.getLabel()));
@@ -280,7 +280,7 @@ public class SearchActionTest {
@UseDataProvider("changedBranches")
public void application_definition_change_with_branch(@Nullable String oldBranch, @Nullable String newBranch) {
OrganizationDto organization = db.organizations().insert();
ComponentDto application = db.components().insertApplication(organization);
ComponentDto application = db.components().insertPublicApplication(organization);
userSession.registerComponents(application);
SnapshotDto firstAnalysis = db.components().insertSnapshot(newAnalysis(application).setCreatedAt(1_000_000L));
EventDto event = db.events().insertEvent(newEvent(firstAnalysis).setName("").setUuid("E11").setCategory(DEFINITION_CHANGE.getLabel()));
@@ -303,7 +303,7 @@ public class SearchActionTest {
@Test
public void incorrect_eventcomponentchange_two_identical_changes_added_on_same_project() {
OrganizationDto organization = db.organizations().insert();
ComponentDto application = db.components().insertApplication(organization);
ComponentDto application = db.components().insertPublicApplication(organization);
userSession.registerComponents(application);
SnapshotDto firstAnalysis = db.components().insertSnapshot(newAnalysis(application).setCreatedAt(1_000_000L));
EventDto event = db.events().insertEvent(newEvent(firstAnalysis).setName("").setUuid("E11").setCategory(DEFINITION_CHANGE.getLabel()));
@@ -333,7 +333,7 @@ public class SearchActionTest {
@Test
public void incorrect_eventcomponentchange_incorrect_category() {
OrganizationDto organization = db.organizations().insert();
ComponentDto application = db.components().insertApplication(organization);
ComponentDto application = db.components().insertPublicApplication(organization);
userSession.registerComponents(application);
SnapshotDto firstAnalysis = db.components().insertSnapshot(newAnalysis(application).setCreatedAt(1_000_000L));
EventDto event = db.events().insertEvent(newEvent(firstAnalysis).setName("").setUuid("E11").setCategory(DEFINITION_CHANGE.getLabel()));
@@ -360,7 +360,7 @@ public class SearchActionTest {
@Test
public void incorrect_eventcomponentchange_three_component_changes_on_same_project() {
OrganizationDto organization = db.organizations().insert();
ComponentDto application = db.components().insertApplication(organization);
ComponentDto application = db.components().insertPublicApplication(organization);
userSession.registerComponents(application);
SnapshotDto firstAnalysis = db.components().insertSnapshot(newAnalysis(application).setCreatedAt(1_000_000L));
EventDto event = db.events().insertEvent(newEvent(firstAnalysis).setName("").setUuid("E11").setCategory(DEFINITION_CHANGE.getLabel()));
@@ -392,7 +392,7 @@ public class SearchActionTest {
@Test
public void incorrect_quality_gate_information() {
OrganizationDto organization = db.organizations().insert();
ComponentDto application = db.components().insertApplication(organization);
ComponentDto application = db.components().insertPublicApplication(organization);
userSession.registerComponents(application);
SnapshotDto firstAnalysis = db.components().insertSnapshot(newAnalysis(application).setCreatedAt(1_000_000L));
EventDto event = db.events().insertEvent(

+ 3
- 2
server/sonar-webserver-webapi/src/test/java/org/sonar/server/projecttag/ws/SetActionTest.java View File

@@ -83,7 +83,8 @@ public class SetActionTest {

@Test
public void reset_tags() {
project = db.components().insertPrivateProjectDto(p -> p.setTagsString("platform,scanner"));
project = db.components().insertPrivateProjectDto(c -> {
}, p -> p.setTagsString("platform,scanner"));

call(project.getKey(), "");

@@ -92,7 +93,7 @@ public class SetActionTest {

@Test
public void override_existing_tags() {
project = db.components().insertPrivateProjectDto(p -> p.setTagsString("marketing,languages"));
project = db.components().insertPrivateProjectDto(c -> {}, p -> p.setTagsString("marketing,languages"));

call(project.getKey(), "finance,offshore,platform");


+ 1
- 1
server/sonar-webserver-webapi/src/test/java/org/sonar/server/ui/ws/ComponentActionTest.java View File

@@ -196,7 +196,7 @@ public class ComponentActionTest {
public void return_component_info_when_file_on_master() {
OrganizationDto organization = db.organizations().insert(o -> o.setKey("my-org2"));
db.qualityGates().createDefaultQualityGate(organization);
ComponentDto main = componentDbTester.insertPrivateProject(organization, p -> p.setName("Sample"), p -> p.setDbKey("sample"));
ComponentDto main = componentDbTester.insertPrivateProject(organization, p -> p.setName("Sample").setDbKey("sample"));
userSession.addProjectPermission(UserRole.USER, main);
init();


+ 1
- 1
server/sonar-webserver-webapi/src/test/java/org/sonar/server/user/ws/CurrentActionTest.java View File

@@ -213,7 +213,7 @@ public class CurrentActionTest {

@Test
public void json_example() {
ComponentDto componentDto = db.components().insertPrivateProject(u -> u.setUuid("UUID-of-the-death-star"), u -> u.setDbKey("death-star-key"));
ComponentDto componentDto = db.components().insertPrivateProject(u -> u.setUuid("UUID-of-the-death-star").setDbKey("death-star-key"));
UserDto obiwan = db.users().insertUser(user -> user
.setLogin("obiwan.kenobi")
.setName("Obiwan Kenobi")

Loading…
Cancel
Save