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;
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) {
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) {
checkKeyFormat(newComponent.qualifier(), newComponent.key());
ComponentDto project = createProject(session, newComponent);
removeDuplicatedProjects(session, project.getKey());
+ projectMeasuresIndexer.index(project.uuid());
return project;
}
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;
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;
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
assertThat(project.scope()).isEqualTo("PRJ");
assertThat(project.qualifier()).isEqualTo("TRK");
assertThat(project.getCreatedAt()).isNotNull();
+
+ assertProjectIsInIndex(project.uuid());
}
@Test
assertThat(project.scope()).isEqualTo("PRJ");
assertThat(project.qualifier()).isEqualTo("VW");
assertThat(project.getCreatedAt()).isNotNull();
+
+ assertIndexIsEmpty();
}
@Test
assertThat(dev.scope()).isEqualTo("PRJ");
assertThat(dev.qualifier()).isEqualTo("DEV");
assertThat(dev.getCreatedAt()).isNotNull();
+
+ assertIndexIsEmpty();
}
@Test
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);
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();
+ }
+
}
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;
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
assertThat(underTest.getNullableByKey("sample2:root:src/File.xoo")).isNotNull();
assertThat(dbClient.componentDao().selectByKey(dbSession, inactiveFile.getKey())).isPresent();
+
+ assertProjectKeyExistsInIndex("sample2:root");
}
@Test
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();
dbSession.commit();
assertComponentKeyHasBeenUpdated(provisionedProject.key(), "provisionedProject2");
+ assertProjectKeyExistsInIndex("provisionedProject2");
}
@Test
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) {
}
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) {
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);
+ }
+
}
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;
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;
@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);
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;
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;
@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() {
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;
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;
@Rule
public DbTester dbTester = DbTester.create(System2.INSTANCE);
+ @Rule
+ public EsTester es = new EsTester(new ProjectMeasuresIndexDefinition(new MapSettings()));
+
@Rule
public UserSessionRule userSession = UserSessionRule.standalone();
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