]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8223 Update project measures index when creating/updating project 1303/head
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 13 Oct 2016 13:22:58 +0000 (15:22 +0200)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Mon, 17 Oct 2016 13:35:54 +0000 (15:35 +0200)
server/sonar-server/src/main/java/org/sonar/server/component/ComponentService.java
server/sonar-server/src/test/java/org/sonar/server/component/ComponentServiceTest.java
server/sonar-server/src/test/java/org/sonar/server/component/ComponentServiceUpdateKeyTest.java
server/sonar-server/src/test/java/org/sonar/server/component/DefaultRubyComponentServiceTest.java
server/sonar-server/src/test/java/org/sonar/server/component/ws/BulkUpdateKeyActionTest.java
server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/AddProjectActionTest.java

index e9d7ec06212d6b76876f0c33811426eaf4fec13e..184a7b8ed37c645e1d9864b9c8305f5d05687b28 100644 (file)
@@ -44,6 +44,7 @@ import org.sonar.db.DbSession;
 import org.sonar.db.component.ComponentDto;
 import org.sonar.server.exceptions.BadRequestException;
 import org.sonar.server.exceptions.NotFoundException;
+import org.sonar.server.project.es.ProjectMeasuresIndexer;
 import org.sonar.server.user.UserSession;
 
 import static com.google.common.collect.Lists.newArrayList;
@@ -60,13 +61,15 @@ public class ComponentService {
   private final UserSession userSession;
   private final System2 system2;
   private final ComponentFinder componentFinder;
+  private final ProjectMeasuresIndexer projectMeasuresIndexer;
 
-  public ComponentService(DbClient dbClient, I18n i18n, UserSession userSession, System2 system2, ComponentFinder componentFinder) {
+  public ComponentService(DbClient dbClient, I18n i18n, UserSession userSession, System2 system2, ComponentFinder componentFinder, ProjectMeasuresIndexer projectMeasuresIndexer) {
     this.dbClient = dbClient;
     this.i18n = i18n;
     this.userSession = userSession;
     this.system2 = system2;
     this.componentFinder = componentFinder;
+    this.projectMeasuresIndexer = projectMeasuresIndexer;
   }
 
   public ComponentDto getByKey(String key) {
@@ -112,12 +115,15 @@ public class ComponentService {
     userSession.checkComponentUuidPermission(UserRole.ADMIN, component.projectUuid());
     checkIsProjectOrModule(component);
     checkProjectOrModuleKeyFormat(newKey);
-
     dbClient.componentKeyUpdaterDao().updateKey(component.uuid(), newKey);
+    dbSession.commit();
+    projectMeasuresIndexer.index(component.uuid());
   }
 
   public void bulkUpdateKey(DbSession dbSession, String projectUuid, String stringToReplace, String replacementString) {
     dbClient.componentKeyUpdaterDao().bulkUpdateKey(dbSession, projectUuid, stringToReplace, replacementString);
+    dbSession.commit();
+    projectMeasuresIndexer.index(projectUuid);
   }
 
   public ComponentDto create(DbSession session, NewComponent newComponent) {
@@ -125,6 +131,7 @@ public class ComponentService {
     checkKeyFormat(newComponent.qualifier(), newComponent.key());
     ComponentDto project = createProject(session, newComponent);
     removeDuplicatedProjects(session, project.getKey());
+    projectMeasuresIndexer.index(project.uuid());
     return project;
   }
 
index 9e074d6dda24fd200a5b82a0d0f3f3a428a383f2..05c52930a891cd4461ba81ed8911aa73a502ac4c 100644 (file)
@@ -26,6 +26,7 @@ import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.ExpectedException;
+import org.sonar.api.config.MapSettings;
 import org.sonar.api.resources.Qualifiers;
 import org.sonar.api.utils.System2;
 import org.sonar.db.DbClient;
@@ -36,9 +37,12 @@ import org.sonar.db.component.ComponentDbTester;
 import org.sonar.db.component.ComponentDto;
 import org.sonar.db.component.ComponentTesting;
 import org.sonar.db.component.ResourceIndexDao;
+import org.sonar.server.es.EsTester;
 import org.sonar.server.exceptions.BadRequestException;
 import org.sonar.server.exceptions.NotFoundException;
 import org.sonar.server.i18n.I18nRule;
+import org.sonar.server.project.es.ProjectMeasuresIndexDefinition;
+import org.sonar.server.project.es.ProjectMeasuresIndexer;
 import org.sonar.server.tester.UserSessionRule;
 
 import static com.google.common.collect.Lists.newArrayList;
@@ -54,29 +58,38 @@ import static org.sonar.core.permission.GlobalPermissions.PROVISIONING;
 import static org.sonar.db.component.ComponentTesting.newFileDto;
 import static org.sonar.db.component.ComponentTesting.newModuleDto;
 import static org.sonar.db.component.ComponentTesting.newProjectDto;
+import static org.sonar.server.project.es.ProjectMeasuresIndexDefinition.INDEX_PROJECT_MEASURES;
+import static org.sonar.server.project.es.ProjectMeasuresIndexDefinition.TYPE_PROJECT_MEASURES;
 
 public class ComponentServiceTest {
 
   @Rule
   public UserSessionRule userSession = UserSessionRule.standalone();
+
   @Rule
   public ExpectedException expectedException = ExpectedException.none();
 
+  @Rule
+  public EsTester es = new EsTester(new ProjectMeasuresIndexDefinition(new MapSettings()));
+
   @Rule
   public DbTester dbTester = DbTester.create(System2.INSTANCE);
+
   ComponentDbTester componentDb = new ComponentDbTester(dbTester);
   DbClient dbClient = dbTester.getDbClient();
   DbSession dbSession = dbTester.getSession();
 
   I18nRule i18n = new I18nRule();
 
+  ProjectMeasuresIndexer projectMeasuresIndexer = new ProjectMeasuresIndexer(dbClient, es.client());
+
   ComponentService underTest;
 
   @Before
   public void setUp() {
     i18n.put("qualifier.TRK", "Project");
 
-    underTest = new ComponentService(dbClient, i18n, userSession, System2.INSTANCE, new ComponentFinder(dbClient));
+    underTest = new ComponentService(dbClient, i18n, userSession, System2.INSTANCE, new ComponentFinder(dbClient), projectMeasuresIndexer);
   }
 
   @Test
@@ -123,6 +136,8 @@ public class ComponentServiceTest {
     assertThat(project.scope()).isEqualTo("PRJ");
     assertThat(project.qualifier()).isEqualTo("TRK");
     assertThat(project.getCreatedAt()).isNotNull();
+
+    assertProjectIsInIndex(project.uuid());
   }
 
   @Test
@@ -154,6 +169,8 @@ public class ComponentServiceTest {
     assertThat(project.scope()).isEqualTo("PRJ");
     assertThat(project.qualifier()).isEqualTo("VW");
     assertThat(project.getCreatedAt()).isNotNull();
+
+    assertIndexIsEmpty();
   }
 
   @Test
@@ -176,6 +193,8 @@ public class ComponentServiceTest {
     assertThat(dev.scope()).isEqualTo("PRJ");
     assertThat(dev.qualifier()).isEqualTo("DEV");
     assertThat(dev.getCreatedAt()).isNotNull();
+
+    assertIndexIsEmpty();
   }
 
   @Test
@@ -236,7 +255,7 @@ public class ComponentServiceTest {
       ComponentTesting.newProjectDto().setId(2L).setKey(projectKey),
       ComponentTesting.newProjectDto().setId(3L).setKey(projectKey)));
 
-    underTest = new ComponentService(dbClient, i18n, userSession, System2.INSTANCE, new ComponentFinder(dbClient));
+    underTest = new ComponentService(dbClient, i18n, userSession, System2.INSTANCE, new ComponentFinder(dbClient), projectMeasuresIndexer);
     underTest.create(session, NewComponent.create(projectKey, projectKey));
 
     verify(componentDao).delete(session, 2L);
@@ -310,4 +329,12 @@ public class ComponentServiceTest {
     return componentDb.insertComponent(newProjectDto().setKey("sample:root"));
   }
 
+  private void assertProjectIsInIndex(String uuid) {
+    assertThat(es.getIds(INDEX_PROJECT_MEASURES, TYPE_PROJECT_MEASURES)).containsOnly(uuid);
+  }
+
+  private void assertIndexIsEmpty() {
+    assertThat(es.getIds(INDEX_PROJECT_MEASURES, TYPE_PROJECT_MEASURES)).isEmpty();
+  }
+
 }
index 327e07be42c86a2de4cd2c958079766df22b42b5..ff63771f9f775a9c5cefe064ab18c072e990cfb6 100644 (file)
 
 package org.sonar.server.component;
 
+import org.elasticsearch.action.search.SearchRequestBuilder;
 import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.ExpectedException;
+import org.sonar.api.config.MapSettings;
 import org.sonar.api.utils.System2;
 import org.sonar.api.web.UserRole;
 import org.sonar.core.permission.GlobalPermissions;
@@ -33,39 +35,54 @@ import org.sonar.db.DbTester;
 import org.sonar.db.component.ComponentDbTester;
 import org.sonar.db.component.ComponentDto;
 import org.sonar.db.component.ComponentTesting;
+import org.sonar.server.es.EsTester;
 import org.sonar.server.exceptions.BadRequestException;
 import org.sonar.server.exceptions.ForbiddenException;
 import org.sonar.server.i18n.I18nRule;
+import org.sonar.server.project.es.ProjectMeasuresIndexDefinition;
+import org.sonar.server.project.es.ProjectMeasuresIndexer;
 import org.sonar.server.tester.UserSessionRule;
 
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.assertj.guava.api.Assertions.assertThat;
+import static org.elasticsearch.index.query.QueryBuilders.boolQuery;
+import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery;
+import static org.elasticsearch.index.query.QueryBuilders.termQuery;
 import static org.sonar.db.component.ComponentTesting.newFileDto;
 import static org.sonar.db.component.ComponentTesting.newModuleDto;
 import static org.sonar.db.component.ComponentTesting.newProjectDto;
+import static org.sonar.server.project.es.ProjectMeasuresIndexDefinition.INDEX_PROJECT_MEASURES;
+import static org.sonar.server.project.es.ProjectMeasuresIndexDefinition.TYPE_PROJECT_MEASURES;
 
 public class ComponentServiceUpdateKeyTest {
 
   @Rule
   public UserSessionRule userSession = UserSessionRule.standalone();
+
   @Rule
   public ExpectedException expectedException = ExpectedException.none();
 
+  @Rule
+  public EsTester es = new EsTester(new ProjectMeasuresIndexDefinition(new MapSettings()));
+
   @Rule
   public DbTester db = DbTester.create(System2.INSTANCE);
+
   ComponentDbTester componentDb = new ComponentDbTester(db);
   DbClient dbClient = db.getDbClient();
   DbSession dbSession = db.getSession();
 
   I18nRule i18n = new I18nRule();
 
+  ProjectMeasuresIndexer projectMeasuresIndexer = new ProjectMeasuresIndexer(dbClient, es.client());
+
   ComponentService underTest;
 
   @Before
   public void setUp() {
     i18n.put("qualifier.TRK", "Project");
 
-    underTest = new ComponentService(dbClient, i18n, userSession, System2.INSTANCE, new ComponentFinder(dbClient));
+    underTest = new ComponentService(dbClient, i18n, userSession, System2.INSTANCE, new ComponentFinder(dbClient), projectMeasuresIndexer);
   }
 
   @Test
@@ -89,6 +106,8 @@ public class ComponentServiceUpdateKeyTest {
     assertThat(underTest.getNullableByKey("sample2:root:src/File.xoo")).isNotNull();
 
     assertThat(dbClient.componentDao().selectByKey(dbSession, inactiveFile.getKey())).isPresent();
+
+    assertProjectKeyExistsInIndex("sample2:root");
   }
 
   @Test
@@ -96,26 +115,24 @@ public class ComponentServiceUpdateKeyTest {
     ComponentDto project = insertSampleRootProject();
     ComponentDto module = ComponentTesting.newModuleDto(project).setKey("sample:root:module");
     dbClient.componentDao().insert(dbSession, module);
-
     ComponentDto file = ComponentTesting.newFileDto(module, null).setKey("sample:root:module:src/File.xoo");
     dbClient.componentDao().insert(dbSession, file);
-
     dbSession.commit();
-
     userSession.login("john").addProjectUuidPermissions(UserRole.ADMIN, project.uuid());
+
     underTest.updateKey(dbSession, module.key(), "sample:root2:module");
     dbSession.commit();
 
     assertThat(dbClient.componentDao().selectByKey(dbSession, project.key())).isPresent();
-
     assertComponentKeyHasBeenUpdated(module.key(), "sample:root2:module");
     assertComponentKeyHasBeenUpdated(file.key(), "sample:root2:module:src/File.xoo");
+
+    assertProjectKeyExistsInIndex(project.key());
   }
 
   @Test
   public void update_provisioned_project_key() {
-    ComponentDto provisionedProject = newProjectDto().setKey("provisionedProject");
-    dbClient.componentDao().insert(dbSession, provisionedProject);
+    ComponentDto provisionedProject = insertProject("provisionedProject");
 
     dbSession.commit();
 
@@ -124,6 +141,7 @@ public class ComponentServiceUpdateKeyTest {
     dbSession.commit();
 
     assertComponentKeyHasBeenUpdated(provisionedProject.key(), "provisionedProject2");
+    assertProjectKeyExistsInIndex("provisionedProject2");
   }
 
   @Test
@@ -197,6 +215,8 @@ public class ComponentServiceUpdateKeyTest {
     assertComponentKeyUpdated(file.key(), "your_project:root:module:src/File.xoo");
     assertComponentKeyNotUpdated(inactiveModule.key());
     assertComponentKeyNotUpdated(inactiveFile.key());
+
+    assertProjectKeyExistsInIndex("your_project");
   }
 
   private void assertComponentKeyUpdated(String oldKey, String newKey) {
@@ -213,7 +233,13 @@ public class ComponentServiceUpdateKeyTest {
   }
 
   private ComponentDto insertSampleRootProject() {
-    return componentDb.insertComponent(newProjectDto().setKey("sample:root"));
+    return insertProject("sample:root");
+  }
+
+  private ComponentDto insertProject(String key) {
+    ComponentDto project = componentDb.insertComponent(newProjectDto().setKey(key));
+    projectMeasuresIndexer.index(project.uuid());
+    return project;
   }
 
   private void assertComponentKeyHasBeenUpdated(String oldKey, String newKey) {
@@ -221,4 +247,13 @@ public class ComponentServiceUpdateKeyTest {
     assertThat(dbClient.componentDao().selectByKey(dbSession, newKey)).isPresent();
   }
 
+  private void assertProjectKeyExistsInIndex(String key) {
+    SearchRequestBuilder request = es.client()
+      .prepareSearch(INDEX_PROJECT_MEASURES)
+      .setTypes(TYPE_PROJECT_MEASURES)
+      .setQuery(boolQuery().must(matchAllQuery()).filter(
+        boolQuery().must(termQuery(ProjectMeasuresIndexDefinition.FIELD_KEY, key))));
+    assertThat(request.get().getHits()).hasSize(1);
+  }
+
 }
index 1179868fc10abac4788fa13e4c170c1ef7693781..4529ce165235f11db0e36141c99ec7ab9d67c9f7 100644 (file)
@@ -23,6 +23,7 @@ import java.util.List;
 import java.util.Map;
 import org.junit.Rule;
 import org.junit.Test;
+import org.sonar.api.config.MapSettings;
 import org.sonar.api.resources.Qualifiers;
 import org.sonar.api.utils.System2;
 import org.sonar.db.DbClient;
@@ -32,9 +33,12 @@ import org.sonar.db.component.ComponentDbTester;
 import org.sonar.db.component.ComponentDto;
 import org.sonar.db.component.ResourceDao;
 import org.sonar.db.component.ResourceDto;
+import org.sonar.server.es.EsTester;
 import org.sonar.server.exceptions.BadRequestException;
 import org.sonar.server.i18n.I18nRule;
 import org.sonar.server.permission.PermissionService;
+import org.sonar.server.project.es.ProjectMeasuresIndexDefinition;
+import org.sonar.server.project.es.ProjectMeasuresIndexer;
 import org.sonar.server.tester.UserSessionRule;
 
 import static com.google.common.collect.Lists.newArrayList;
@@ -54,13 +58,17 @@ public class DefaultRubyComponentServiceTest {
   @Rule
   public UserSessionRule userSession = UserSessionRule.standalone();
 
+  @Rule
+  public EsTester es = new EsTester(new ProjectMeasuresIndexDefinition(new MapSettings()));
+
   I18nRule i18n = new I18nRule();
 
   DbClient dbClient = db.getDbClient();
   DbSession dbSession = db.getSession();
 
   ResourceDao resourceDao = dbClient.resourceDao();
-  ComponentService componentService = new ComponentService(dbClient, i18n, userSession, System2.INSTANCE, new ComponentFinder(dbClient));
+  ComponentService componentService = new ComponentService(dbClient, i18n, userSession, System2.INSTANCE, new ComponentFinder(dbClient),
+    new ProjectMeasuresIndexer(dbClient, es.client()));
   PermissionService permissionService = mock(PermissionService.class);
 
   ComponentDbTester componentDb = new ComponentDbTester(db);
index ed8cc60a78eb99fe1fdede36774082d9fb1e87a9..2625cf7967b0bb242166de5049dd247b182e784a 100644 (file)
@@ -27,6 +27,7 @@ import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.ExpectedException;
+import org.sonar.api.config.MapSettings;
 import org.sonar.api.server.ws.WebService;
 import org.sonar.api.utils.System2;
 import org.sonar.core.permission.GlobalPermissions;
@@ -37,9 +38,12 @@ import org.sonar.db.component.ComponentDbTester;
 import org.sonar.db.component.ComponentDto;
 import org.sonar.server.component.ComponentFinder;
 import org.sonar.server.component.ComponentService;
+import org.sonar.server.es.EsTester;
 import org.sonar.server.exceptions.BadRequestException;
 import org.sonar.server.exceptions.ForbiddenException;
 import org.sonar.server.exceptions.NotFoundException;
+import org.sonar.server.project.es.ProjectMeasuresIndexDefinition;
+import org.sonar.server.project.es.ProjectMeasuresIndexer;
 import org.sonar.server.tester.UserSessionRule;
 import org.sonar.server.ws.TestRequest;
 import org.sonar.server.ws.WsActionTester;
@@ -68,17 +72,24 @@ public class BulkUpdateKeyActionTest {
 
   @Rule
   public ExpectedException expectedException = ExpectedException.none();
+
   @Rule
   public UserSessionRule userSession = UserSessionRule.standalone();
+
+  @Rule
+  public EsTester es = new EsTester(new ProjectMeasuresIndexDefinition(new MapSettings()));
+
   @Rule
   public DbTester db = DbTester.create(System2.INSTANCE);
+
   ComponentDbTester componentDb = new ComponentDbTester(db);
   DbClient dbClient = db.getDbClient();
   DbSession dbSession = db.getSession();
 
   ComponentFinder componentFinder = new ComponentFinder(dbClient);
 
-  WsActionTester ws = new WsActionTester(new BulkUpdateKeyAction(dbClient, componentFinder, new ComponentService(dbClient, null, null, null, null), userSession));
+  WsActionTester ws = new WsActionTester(
+    new BulkUpdateKeyAction(dbClient, componentFinder, new ComponentService(dbClient, null, null, null, null, new ProjectMeasuresIndexer(dbClient, es.client())), userSession));
 
   @Before
   public void setUp() {
index 9308721cff964df6829e7136e760fff41faceb39..fddb4f8bcce73cc831361bba04ccd9e59585996d 100644 (file)
@@ -22,6 +22,7 @@ package org.sonar.server.qualityprofile.ws;
 import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
+import org.sonar.api.config.MapSettings;
 import org.sonar.api.resources.Languages;
 import org.sonar.api.utils.System2;
 import org.sonar.db.DbClient;
@@ -34,7 +35,10 @@ import org.sonar.db.qualityprofile.QualityProfileDbTester;
 import org.sonar.db.qualityprofile.QualityProfileDto;
 import org.sonar.server.component.ComponentFinder;
 import org.sonar.server.component.ComponentService;
+import org.sonar.server.es.EsTester;
 import org.sonar.server.language.LanguageTesting;
+import org.sonar.server.project.es.ProjectMeasuresIndexDefinition;
+import org.sonar.server.project.es.ProjectMeasuresIndexer;
 import org.sonar.server.qualityprofile.QProfileLookup;
 import org.sonar.server.qualityprofile.QProfileName;
 import org.sonar.server.qualityprofile.QProfileProjectOperations;
@@ -54,6 +58,9 @@ public class AddProjectActionTest {
   @Rule
   public DbTester dbTester = DbTester.create(System2.INSTANCE);
 
+  @Rule
+  public EsTester es = new EsTester(new ProjectMeasuresIndexDefinition(new MapSettings()));
+
   @Rule
   public UserSessionRule userSession = UserSessionRule.standalone();
 
@@ -70,7 +77,7 @@ public class AddProjectActionTest {
 
   WsActionTester ws = new WsActionTester(new AddProjectAction(projectAssociationParameters,
     qProfileProjectOperations, new ProjectAssociationFinder(new QProfileLookup(dbClient),
-      new ComponentService(dbClient, null, userSession, null, new ComponentFinder(dbClient))),
+      new ComponentService(dbClient, null, userSession, null, new ComponentFinder(dbClient), new ProjectMeasuresIndexer(dbClient, es.client()))),
     userSession));
 
   @Before