3 * Copyright (C) 2009-2017 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 package org.sonar.server.component.index;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.sonar.db.component.ComponentDto;
26 import org.sonar.server.es.textsearch.ComponentTextSearchFeatureRepertoire;
28 import static com.google.common.collect.ImmutableSet.of;
29 import static java.util.Collections.singletonList;
30 import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery;
31 import static org.elasticsearch.index.query.QueryBuilders.termQuery;
32 import static org.sonar.api.resources.Qualifiers.PROJECT;
33 import static org.sonar.server.component.index.ComponentIndexDefinition.FIELD_KEY;
35 public class ComponentIndexFeatureFavoriteTest extends ComponentIndexTest {
38 public void before() {
39 features.set(q -> matchAllQuery(), ComponentTextSearchFeatureRepertoire.FAVORITE);
43 public void scoring_cares_about_favorites() {
44 ComponentDto project1 = indexProject("sonarqube", "SonarQube");
45 ComponentDto project2 = indexProject("recent", "SonarQube Recently");
47 ComponentIndexQuery query1 = ComponentIndexQuery.builder()
48 .setQuery("SonarQube")
49 .setQualifiers(singletonList(PROJECT))
50 .setFavoriteKeys(of(project1.getDbKey()))
52 assertSearch(query1).containsExactly(uuids(project1, project2));
54 ComponentIndexQuery query2 = ComponentIndexQuery.builder()
55 .setQuery("SonarQube")
56 .setQualifiers(singletonList(PROJECT))
57 .setFavoriteKeys(of(project2.getDbKey()))
59 assertSearch(query2).containsExactly(uuids(project2, project1));
63 public void irrelevant_favorites_are_not_returned() {
64 features.set(q -> termQuery(FIELD_KEY, "non-existing-value"), ComponentTextSearchFeatureRepertoire.FAVORITE);
65 ComponentDto project1 = indexProject("foo", "foo");
67 ComponentIndexQuery query1 = ComponentIndexQuery.builder()
69 .setQualifiers(singletonList(PROJECT))
70 .setFavoriteKeys(of(project1.getDbKey()))
72 assertSearch(query1).isEmpty();