]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-19556 org.sonar.server.project.Project#from(org.sonar.db.component.ComponentDto...
authorLukasz Jarocki <lukasz.jarocki@sonarsource.com>
Thu, 15 Jun 2023 08:45:35 +0000 (10:45 +0200)
committersonartech <sonartech@sonarsource.com>
Tue, 20 Jun 2023 13:10:18 +0000 (13:10 +0000)
12 files changed:
server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/LoadReportAnalysisMetadataHolderStep.java
server/sonar-db-dao/src/it/java/org/sonar/db/entity/EntityDaoIT.java
server/sonar-db-dao/src/main/java/org/sonar/db/entity/EntityDto.java
server/sonar-db-dao/src/main/java/org/sonar/db/portfolio/PortfolioDto.java
server/sonar-db-dao/src/main/java/org/sonar/db/project/ProjectDto.java
server/sonar-db-dao/src/main/resources/org/sonar/db/entity/EntityMapper.xml
server/sonar-server-common/src/main/java/org/sonar/server/project/Project.java
server/sonar-server-common/src/test/java/org/sonar/server/project/ProjectTest.java
server/sonar-webserver-webapi/src/it/java/org/sonar/server/branch/ws/DeleteActionIT.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/branch/ws/DeleteAction.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/project/ws/BulkDeleteAction.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/project/ws/DeleteAction.java

index cf22eac9e871468443913367e60efaff26ef0be5..14ad8283eaa792a2494c29d94f7f049ee7bfc91c 100644 (file)
@@ -100,7 +100,7 @@ public class LoadReportAnalysisMetadataHolderStep implements ComputationStep {
     }
 
     ProjectDto dto = toProject(reportMetadata.getProjectKey());
-    analysisMetadata.setProject(Project.from(dto));
+    analysisMetadata.setProject(Project.fromProjectDtoWithTags(dto));
     return () -> {
       if (!mainComponentKey.equals(reportMetadata.getProjectKey())) {
         throw MessageException.of(format(
index 7c21aa67ce5d3a3f64b7ab2c3c93e38a4e3ab97e..7b54d98409c6079d8a205fa627486a2ec23a0d32 100644 (file)
@@ -37,7 +37,7 @@ public class EntityDaoIT {
   private final System2 system2 = new AlwaysIncreasingSystem2(1000L);
 
   @Rule
-  public DbTester db = DbTester.create(system2);
+  public DbTester db = DbTester.create(system2, true);
 
   private final EntityDao entityDao = new EntityDao();
 
@@ -84,6 +84,15 @@ public class EntityDaoIT {
     assertThat(entityDao.selectByUuid(db.getSession(), portfolio.getUuid()).get().getKey()).isEqualTo(portfolio.getKey());
   }
 
+  @Test
+  public void getDescription_shouldNotReturnNull() {
+    ProjectData project = db.components().insertPrivateProject();
+    PortfolioDto portfolio = db.components().insertPrivatePortfolioDto();
+
+    assertThat(entityDao.selectByUuid(db.getSession(), project.projectUuid()).get().getDescription()).isNotNull();
+    assertThat(entityDao.selectByUuid(db.getSession(), portfolio.getUuid()).get().getDescription()).isNotNull();
+  }
+
   @Test
   public void selectEntityByUuid_whenNoMatch_shouldReturnEmpty() {
     assertThat(entityDao.selectByUuid(db.getSession(), "unknown")).isEmpty();
index 15c4b85ef0f645fd248e3a88114b143231f0acc9..39932908c597151afae5f1c200416fc4fc41c745 100644 (file)
@@ -20,6 +20,7 @@
 package org.sonar.db.entity;
 
 import java.util.Objects;
+import javax.annotation.CheckForNull;
 import org.sonar.api.resources.Qualifiers;
 
 /**
@@ -27,10 +28,12 @@ import org.sonar.api.resources.Qualifiers;
  * Entities are stored either in the projects or portfolios tables.
  */
 public class EntityDto {
+
   protected String kee;
   protected String uuid;
   protected String name;
   protected String qualifier;
+  protected String description;
   protected boolean isPrivate;
 
   // This field should be null for anything that is not subportfolio
@@ -66,6 +69,11 @@ public class EntityDto {
     return name;
   }
 
+  @CheckForNull
+  public String getDescription() {
+    return description;
+  }
+
   public boolean isPrivate() {
     return isPrivate;
   }
index d5f63653f2651ef7f4fd43dad55295d623e35acf..50df9b0895ee4e79b15f7c2805525bf46169b1eb 100644 (file)
@@ -29,7 +29,6 @@ public class PortfolioDto extends EntityDto {
     NONE, MANUAL, REGEXP, REST, TAGS
   }
 
-  private String description;
   private String branchKey;
 
   private String rootUuid;
@@ -149,11 +148,6 @@ public class PortfolioDto extends EntityDto {
     return this;
   }
 
-  @CheckForNull
-  public String getDescription() {
-    return description;
-  }
-
   public PortfolioDto setDescription(@Nullable String description) {
     this.description = description;
     return this;
index 6e48cccde0cfe00e38e476bb3df4547fe640bf6f..81190a9f61a095db9edc86d15c735070475c8514 100644 (file)
@@ -30,7 +30,6 @@ import static org.sonar.db.component.DbTagsReader.readDbTags;
 
 public class ProjectDto extends EntityDto {
   private static final String TAGS_SEPARATOR = ",";
-  private String description;
   private String tags;
   private long createdAt;
   private long updatedAt;
@@ -104,11 +103,6 @@ public class ProjectDto extends EntityDto {
     return this;
   }
 
-  @CheckForNull
-  public String getDescription() {
-    return description;
-  }
-
   public ProjectDto setDescription(@Nullable String description) {
     this.description = description;
     return this;
index 3f3780008a47f074a75efd114cdde5c9ba4a531e..5e071421ae0c4945da16f22254895653e59eabde 100644 (file)
@@ -2,11 +2,12 @@
 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "mybatis-3-mapper.dtd">
 <mapper namespace="org.sonar.db.entity.EntityMapper">
   <sql id="entityProjectColumns">
-    p.uuid as uuid, p.kee as kee, p.name as name, p.private as isPrivate, p.qualifier as qualifier, null as authUuid
+    p.uuid as uuid, p.kee as kee, p.name as name, p.private as isPrivate, p.description as description, p.qualifier as qualifier,
+    null as authUuid
   </sql>
 
   <sql id="entityPortfolioColumns">
-    p.uuid as uuid, p.kee as kee, p.name as name, p.private as isPrivate,
+    p.uuid as uuid, p.kee as kee, p.name as name, p.private as isPrivate, p.description as description,
     case when p.parent_uuid is null then 'VW' else 'SVW' end as qualifier,
     case when p.root_uuid != p.uuid then p.root_uuid else null end as authUuid
   </sql>
index f09b33178acb59b169e408eb08a181cb63ad255a..65137bdd543d5eb5d35d377e66136929e1350386 100644 (file)
@@ -24,7 +24,7 @@ import java.util.Objects;
 import javax.annotation.Nullable;
 import javax.annotation.concurrent.Immutable;
 import org.sonar.db.component.ComponentDto;
-import org.sonar.db.portfolio.PortfolioDto;
+import org.sonar.db.entity.EntityDto;
 import org.sonar.db.project.ProjectDto;
 
 import static java.util.Collections.emptyList;
@@ -50,12 +50,12 @@ public class Project {
     return new Project(project.uuid(), project.getKey(), project.name(), project.description(), emptyList());
   }
 
-  public static Project from(ProjectDto project) {
+  public static Project fromProjectDtoWithTags(ProjectDto project) {
     return new Project(project.getUuid(), project.getKey(), project.getName(), project.getDescription(), project.getTags());
   }
 
-  public static Project from(PortfolioDto portfolio) {
-    return new Project(portfolio.getUuid(), portfolio.getKey(), portfolio.getName(), portfolio.getDescription(), emptyList());
+  public static Project from(EntityDto entityDto) {
+    return new Project(entityDto.getUuid(), entityDto.getKey(), entityDto.getName(), entityDto.getDescription(), emptyList());
   }
 
   /**
index 8084227d04bd3aaf0f647fe95c6c9b9a89cd8a86..9ff5ccf46d816153708240d8d414091f2e99f522 100644 (file)
 package org.sonar.server.project;
 
 import org.junit.Test;
+import org.sonar.db.entity.EntityDto;
 
 import static java.util.Collections.emptyList;
 import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
 
 public class ProjectTest {
   @Test
@@ -54,6 +57,27 @@ public class ProjectTest {
       .isEqualTo("Project{uuid='U1', key='K1', name='N1', description='D1'}");
   }
 
+  @Test
+  public void from_whenPortfolioPassed_shouldNotReturnNullFields() {
+    EntityDto entity = mock(EntityDto.class);
+
+    when(entity.getUuid()).thenReturn("U1");
+    when(entity.getKey()).thenReturn("K1");
+    when(entity.getName()).thenReturn("N1");
+    when(entity.getDescription()).thenReturn("D1");
+
+    Project underTest = Project.from(entity);
+
+    assertThat(underTest.getUuid()).isEqualTo("U1");
+    assertThat(underTest.getKey()).isEqualTo("K1");
+    assertThat(underTest.getName()).isEqualTo("N1");
+    assertThat(underTest.getDescription()).isEqualTo("D1");
+
+    assertThat(underTest.toString())
+      .isEqualTo(underTest.toString())
+      .isEqualTo("Project{uuid='U1', key='K1', name='N1', description='D1'}");
+  }
+
   @Test
   public void test_equals_and_hashCode() {
     Project project1 = new Project("U1", "K1", "N1", null, emptyList());
index d6878af3c6831395c9fa78221af8d0135a71e263..14a64e315ecde8eb2c4a53bfd074ab500a736076 100644 (file)
@@ -72,7 +72,7 @@ public class DeleteActionIT {
       .execute();
 
     verifyDeletedKey("branch1");
-    verify(projectLifeCycleListeners).onProjectBranchesDeleted(singleton(Project.from(project)));
+    verify(projectLifeCycleListeners).onProjectBranchesDeleted(singleton(Project.fromProjectDtoWithTags(project)));
   }
 
   @Test
index ce191851607c2ce219c06ff6a926dd3921c3d934..aac84f6ad7c45f9cd3ef0d1b84fbd0357addd0bb 100644 (file)
@@ -30,6 +30,7 @@ import org.sonar.db.component.BranchDto;
 import org.sonar.db.project.ProjectDto;
 import org.sonar.server.component.ComponentCleanerService;
 import org.sonar.server.component.ComponentFinder;
+import org.sonar.server.project.Project;
 import org.sonar.server.project.ProjectLifeCycleListeners;
 import org.sonar.server.user.UserSession;
 
@@ -89,7 +90,7 @@ public class DeleteAction implements BranchWsAction {
         throw new IllegalArgumentException("Only non-main branches can be deleted");
       }
       componentCleanerService.deleteBranch(dbSession, branch);
-      projectLifeCycleListeners.onProjectBranchesDeleted(singleton(from(project)));
+      projectLifeCycleListeners.onProjectBranchesDeleted(singleton(Project.fromProjectDtoWithTags(project)));
       response.noContent();
     }
   }
index a38add5920c6e07a399334e03885c636187df779..d19c97b8dd8012bb6f5d31f53e1be2c319ee865a 100644 (file)
@@ -39,6 +39,7 @@ import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 import org.sonar.db.component.ComponentDto;
 import org.sonar.db.component.ComponentQuery;
+import org.sonar.db.entity.EntityDto;
 import org.sonar.db.permission.GlobalPermission;
 import org.sonar.server.component.ComponentCleanerService;
 import org.sonar.server.project.Project;
@@ -49,6 +50,7 @@ import org.sonar.server.user.UserSession;
 import static com.google.common.base.Preconditions.checkArgument;
 import static java.lang.Math.min;
 import static java.lang.String.format;
+import static java.util.stream.Collectors.toSet;
 import static org.sonar.api.resources.Qualifiers.APP;
 import static org.sonar.api.resources.Qualifiers.PROJECT;
 import static org.sonar.api.resources.Qualifiers.VIEW;
@@ -146,11 +148,12 @@ public class BulkDeleteAction implements ProjectsWsAction {
 
       ComponentQuery query = buildDbQuery(searchRequest);
       Set<ComponentDto> componentDtos = new HashSet<>(dbClient.componentDao().selectByQuery(dbSession, query, 0, Integer.MAX_VALUE));
+      List<EntityDto> entities = dbClient.entityDao().selectByKeys(dbSession, componentDtos.stream().map(ComponentDto::getKey).collect(toSet()));
 
       try {
         componentDtos.forEach(p -> componentCleanerService.deleteComponent(dbSession, p));
       } finally {
-        projectLifeCycleListeners.onProjectsDeleted(componentDtos.stream().map(Project::from).collect(MoreCollectors.toSet(componentDtos.size())));
+        projectLifeCycleListeners.onProjectsDeleted(entities.stream().map(Project::from).collect(MoreCollectors.toSet(componentDtos.size())));
       }
     }
     response.noContent();
index 0f08cd62a47e2fd490047248754f294ff9fc8ec9..0417946b83225cfff9dfde0cf2314f6269d6adf5 100644 (file)
@@ -29,6 +29,7 @@ import org.sonar.db.permission.GlobalPermission;
 import org.sonar.db.project.ProjectDto;
 import org.sonar.server.component.ComponentCleanerService;
 import org.sonar.server.component.ComponentFinder;
+import org.sonar.server.project.Project;
 import org.sonar.server.project.ProjectLifeCycleListeners;
 import org.sonar.server.user.UserSession;
 
@@ -82,7 +83,7 @@ public class DeleteAction implements ProjectsWsAction {
       ProjectDto project = componentFinder.getProjectByKey(dbSession, key);
       checkPermission(project);
       componentCleanerService.delete(dbSession, project);
-      projectLifeCycleListeners.onProjectsDeleted(singleton(from(project)));
+      projectLifeCycleListeners.onProjectsDeleted(singleton(Project.fromProjectDtoWithTags(project)));
     }
 
     response.noContent();