From aa08b062fe0ad882f7da840e8a6eb01bcc87b4e1 Mon Sep 17 00:00:00 2001 From: Lukasz Jarocki Date: Thu, 25 May 2023 15:21:09 +0200 Subject: [PATCH] SONAR-18856 fixed elasticsearch scoring tests --- .../component/index/ComponentIndexerIT.java | 31 +++++++++++----- .../index/ComponentIndexDefinition.java | 13 ++++--- .../index/ComponentIndexScoreTest.java | 7 ---- .../component/index/ComponentIndexTest.java | 1 - .../sonar/server/setting/ws/ResetAction.java | 26 +++++++------- .../sonar/server/setting/ws/SetAction.java | 35 +++++++++---------- 6 files changed, 61 insertions(+), 52 deletions(-) diff --git a/server/sonar-server-common/src/it/java/org/sonar/server/component/index/ComponentIndexerIT.java b/server/sonar-server-common/src/it/java/org/sonar/server/component/index/ComponentIndexerIT.java index 28bc6d98112..8566850cd64 100644 --- a/server/sonar-server-common/src/it/java/org/sonar/server/component/index/ComponentIndexerIT.java +++ b/server/sonar-server-common/src/it/java/org/sonar/server/component/index/ComponentIndexerIT.java @@ -31,6 +31,7 @@ import org.sonar.db.DbSession; import org.sonar.db.DbTester; import org.sonar.db.component.ComponentDto; import org.sonar.db.component.ComponentUpdateDto; +import org.sonar.db.component.ProjectData; import org.sonar.db.es.EsQueueDto; import org.sonar.db.project.ProjectDto; import org.sonar.server.es.EsClient; @@ -197,6 +198,28 @@ public class ComponentIndexerIT { assertThatIndexHasSize(0); } + @Test + public void indexOnAnalysis_updates_index_on_changes() { + ProjectData project = db.components().insertPrivateProject(); + underTest.indexOnAnalysis(project.projectUuid()); + assertThatComponentHasName(project.projectUuid(), project.getProjectDto().getName()); + + // modify + ProjectDto projectDto = project.getProjectDto(); + projectDto.setName("NewName"); + + db.getDbClient().projectDao().update(dbSession, projectDto); + db.commit(); + + // verify that index is updated + underTest.indexOnAnalysis(projectDto.getUuid()); + + assertThatIndexContainsOnly(projectDto.getUuid()); + assertThatComponentHasName(projectDto.getUuid(), "NewName"); +} + + + @Test public void errors_during_indexing_are_recovered() { ComponentDto project1 = db.components().insertPrivateProject().getMainBranchComponent(); @@ -227,14 +250,6 @@ public class ComponentIndexerIT { return underTest.index(dbSession, items); } - private void updateDb(ComponentDto component) { - ComponentUpdateDto updateComponent = ComponentUpdateDto.copyFrom(component); - updateComponent.setBChanged(true); - dbClient.componentDao().update(dbSession, updateComponent, component.qualifier()); - dbClient.componentDao().applyBChangesForBranchUuid(dbSession, component.branchUuid()); - dbSession.commit(); - } - private IndexingResult recover() { Collection items = db.getDbClient().esQueueDao().selectForRecovery(db.getSession(), System.currentTimeMillis() + 1_000L, 10); return underTest.index(db.getSession(), items); diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/component/index/ComponentIndexDefinition.java b/server/sonar-server-common/src/main/java/org/sonar/server/component/index/ComponentIndexDefinition.java index 3c0802f194f..5291ce3ba5d 100644 --- a/server/sonar-server-common/src/main/java/org/sonar/server/component/index/ComponentIndexDefinition.java +++ b/server/sonar-server-common/src/main/java/org/sonar/server/component/index/ComponentIndexDefinition.java @@ -48,20 +48,22 @@ public class ComponentIndexDefinition implements IndexDefinition { public static final String FIELD_QUALIFIER = "qualifier"; private static final int DEFAULT_NUMBER_OF_SHARDS = 5; + private final int numberOfShards; static final DefaultIndexSettingsElement[] NAME_ANALYZERS = {SORTABLE_ANALYZER, SEARCH_PREFIX_ANALYZER, SEARCH_PREFIX_CASE_INSENSITIVE_ANALYZER, SEARCH_GRAMS_ANALYZER}; private final Configuration config; private final boolean enableSource; - private ComponentIndexDefinition(Configuration config, boolean enableSource) { + private ComponentIndexDefinition(Configuration config, boolean enableSource, int numberOfShards) { this.config = config; - this.enableSource = true; + this.enableSource = enableSource; + this.numberOfShards = numberOfShards; } @Inject public ComponentIndexDefinition(Configuration config) { - this(config, false); + this(config, false, DEFAULT_NUMBER_OF_SHARDS); } /** @@ -69,7 +71,8 @@ public class ComponentIndexDefinition implements IndexDefinition { * of indexed documents. */ public static ComponentIndexDefinition createForTest() { - return new ComponentIndexDefinition(new MapSettings().asConfig(), true); + // different shards can have different scoring, affecting tests, so we use a single shard + return new ComponentIndexDefinition(new MapSettings().asConfig(), true, 1); } @Override @@ -78,7 +81,7 @@ public class ComponentIndexDefinition implements IndexDefinition { DESCRIPTOR, newBuilder(config) .setRefreshInterval(MANUAL_REFRESH_INTERVAL) - .setDefaultNbOfShards(DEFAULT_NUMBER_OF_SHARDS) + .setDefaultNbOfShards(numberOfShards) .build()) .setEnableSource(enableSource); diff --git a/server/sonar-webserver-es/src/test/java/org/sonar/server/component/index/ComponentIndexScoreTest.java b/server/sonar-webserver-es/src/test/java/org/sonar/server/component/index/ComponentIndexScoreTest.java index e911826d01f..cf49942126f 100644 --- a/server/sonar-webserver-es/src/test/java/org/sonar/server/component/index/ComponentIndexScoreTest.java +++ b/server/sonar-webserver-es/src/test/java/org/sonar/server/component/index/ComponentIndexScoreTest.java @@ -19,22 +19,15 @@ */ package org.sonar.server.component.index; -import com.google.common.collect.ImmutableSet; import java.util.List; import java.util.Set; import org.junit.Test; -import org.sonar.db.component.ComponentDto; -import org.sonar.db.component.ComponentTesting; import org.sonar.db.project.ProjectDto; import org.sonar.server.es.textsearch.ComponentTextSearchFeatureRepertoire; -import static java.util.Arrays.asList; -import static org.sonar.api.resources.Qualifiers.DIRECTORY; -import static org.sonar.api.resources.Qualifiers.FILE; import static org.sonar.api.resources.Qualifiers.PROJECT; public class ComponentIndexScoreTest extends ComponentIndexTest { - @Test public void should_prefer_components_without_prefix() { assertResultOrder("File.java", diff --git a/server/sonar-webserver-es/src/test/java/org/sonar/server/component/index/ComponentIndexTest.java b/server/sonar-webserver-es/src/test/java/org/sonar/server/component/index/ComponentIndexTest.java index 41c8abcaa1e..2f392e5792b 100644 --- a/server/sonar-webserver-es/src/test/java/org/sonar/server/component/index/ComponentIndexTest.java +++ b/server/sonar-webserver-es/src/test/java/org/sonar/server/component/index/ComponentIndexTest.java @@ -59,7 +59,6 @@ public abstract class ComponentIndexTest { private final ComponentDbTester componentDbTester = new ComponentDbTester(db); protected void assertResultOrder(String query, String... resultsInOrder) { - indexProject("key-1", "Quality Product"); List projects = Arrays.stream(resultsInOrder) .map(r -> componentDbTester.insertPublicProject(c -> c.setName(r)).getProjectDto()) .toList(); diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/setting/ws/ResetAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/setting/ws/ResetAction.java index 8fae7a82c42..b66b6dbaad4 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/setting/ws/ResetAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/setting/ws/ResetAction.java @@ -94,17 +94,17 @@ public class ResetAction implements SettingsWsAction { public void handle(Request request, Response response) throws Exception { try (DbSession dbSession = dbClient.openSession(false)) { ResetRequest resetRequest = toWsRequest(request); - Optional component = getComponent(dbSession, resetRequest); - checkPermissions(component); + Optional entity = getEntity(dbSession, resetRequest); + checkPermissions(entity); resetRequest.getKeys().forEach(key -> { SettingsWsSupport.validateKey(key); - SettingData data = new SettingData(key, emptyList(), component.orElse(null)); + SettingData data = new SettingData(key, emptyList(), entity.orElse(null)); List.of(validations.scope(), validations.qualifier()).forEach(validation -> validation.accept(data)); }); List keys = getKeys(resetRequest); - if (component.isPresent()) { - settingsUpdater.deleteComponentSettings(dbSession, component.get(), keys); + if (entity.isPresent()) { + settingsUpdater.deleteComponentSettings(dbSession, entity.get(), keys); } else { settingsUpdater.deleteGlobalSettings(dbSession, keys); } @@ -125,11 +125,11 @@ public class ResetAction implements SettingsWsAction { private static ResetRequest toWsRequest(Request request) { return new ResetRequest() .setKeys(request.mandatoryParamAsStrings(PARAM_KEYS)) - .setComponent(request.param(PARAM_COMPONENT)); + .setEntity(request.param(PARAM_COMPONENT)); } - private Optional getComponent(DbSession dbSession, ResetRequest request) { - String componentKey = request.getComponent(); + private Optional getEntity(DbSession dbSession, ResetRequest request) { + String componentKey = request.getEntity(); if (componentKey == null) { return Optional.empty(); } @@ -147,17 +147,17 @@ public class ResetAction implements SettingsWsAction { } private static class ResetRequest { - private String component; + private String entity; private List keys; - public ResetRequest setComponent(@Nullable String component) { - this.component = component; + public ResetRequest setEntity(@Nullable String entity) { + this.entity = entity; return this; } @CheckForNull - public String getComponent() { - return component; + public String getEntity() { + return entity; } public ResetRequest setKeys(List keys) { diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/setting/ws/SetAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/setting/ws/SetAction.java index c6b285a1a47..30dbdd19f7c 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/setting/ws/SetAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/setting/ws/SetAction.java @@ -143,7 +143,7 @@ public class SetAction implements SettingsWsAction { } private void doHandle(DbSession dbSession, SetRequest request) { - Optional component = searchComponent(dbSession, request); + Optional component = searchEntity(dbSession, request); String projectKey = component.map(EntityDto::getKey).orElse(null); String projectName = component.map(EntityDto::getName).orElse(null); String qualifier = component.map(EntityDto::getQualifier).orElse(null); @@ -204,10 +204,10 @@ public class SetAction implements SettingsWsAction { } } - private void commonChecks(SetRequest request, Optional component) { + private void commonChecks(SetRequest request, Optional entity) { checkValueIsSet(request); String settingKey = request.getKey(); - SettingData settingData = new SettingData(settingKey, valuesFromRequest(request), component.orElse(null)); + SettingData settingData = new SettingData(settingKey, valuesFromRequest(request), entity.orElse(null)); List.of(validations.scope(), validations.qualifier(), validations.valueType()) .forEach(validation -> validation.accept(settingData)); } @@ -283,9 +283,9 @@ public class SetAction implements SettingsWsAction { : request.getValue(); } - private void checkPermissions(Optional component) { - if (component.isPresent()) { - userSession.checkEntityPermission(UserRole.ADMIN, component.get()); + private void checkPermissions(Optional entity) { + if (entity.isPresent()) { + userSession.checkEntityPermission(UserRole.ADMIN, entity.get()); } else { userSession.checkIsSystemAdministrator(); } @@ -297,7 +297,7 @@ public class SetAction implements SettingsWsAction { .setValue(request.param(PARAM_VALUE)) .setValues(request.multiParam(PARAM_VALUES)) .setFieldValues(request.multiParam(PARAM_FIELD_VALUES)) - .setComponent(request.param(PARAM_COMPONENT)); + .setEntity(request.param(PARAM_COMPONENT)); checkArgument(set.getValues() != null, "Setting values must not be null"); checkArgument(set.getFieldValues() != null, "Setting fields values must not be null"); return set; @@ -314,13 +314,13 @@ public class SetAction implements SettingsWsAction { } } - private Optional searchComponent(DbSession dbSession, SetRequest request) { - String componentKey = request.getComponent(); - if (componentKey == null) { + private Optional searchEntity(DbSession dbSession, SetRequest request) { + String entityKey = request.getEntity(); + if (entityKey == null) { return Optional.empty(); } - return Optional.of(dbClient.entityDao().selectByKey(dbSession, componentKey) - .orElseThrow(() -> new NotFoundException(format("Component key '%s' not found", componentKey)))); + return Optional.of(dbClient.entityDao().selectByKey(dbSession, entityKey) + .orElseThrow(() -> new NotFoundException(format("Component key '%s' not found", entityKey)))); } private PropertyDto toProperty(SetRequest request, Optional entity) { @@ -354,21 +354,20 @@ public class SetAction implements SettingsWsAction { private static class SetRequest { - private String pullRequest; - private String component; + private String entity; private List fieldValues; private String key; private String value; private List values; - public SetRequest setComponent(@Nullable String component) { - this.component = component; + public SetRequest setEntity(@Nullable String entity) { + this.entity = entity; return this; } @CheckForNull - public String getComponent() { - return component; + public String getEntity() { + return entity; } public SetRequest setFieldValues(List fieldValues) { -- 2.39.5