You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AllFiltersTest.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2023 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  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.
  10. *
  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.
  15. *
  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.
  19. */
  20. package org.sonar.server.es.searchrequest;
  21. import java.util.List;
  22. import java.util.Random;
  23. import java.util.stream.IntStream;
  24. import java.util.stream.Stream;
  25. import org.assertj.core.api.ThrowableAssert.ThrowingCallable;
  26. import org.elasticsearch.index.query.BoolQueryBuilder;
  27. import org.elasticsearch.index.query.QueryBuilder;
  28. import org.junit.Test;
  29. import org.sonar.server.es.searchrequest.TopAggregationDefinition.FilterScope;
  30. import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic;
  31. import static org.assertj.core.api.Assertions.assertThat;
  32. import static org.assertj.core.api.Assertions.assertThatThrownBy;
  33. import static org.elasticsearch.index.query.QueryBuilders.boolQuery;
  34. import static org.mockito.Mockito.mock;
  35. public class AllFiltersTest {
  36. @Test
  37. public void newalways_returns_a_new_instance() {
  38. int expected = 1 + new Random().nextInt(200);
  39. RequestFiltersComputer.AllFilters[] instances = IntStream.range(0, expected)
  40. .mapToObj(t -> RequestFiltersComputer.newAllFilters())
  41. .toArray(RequestFiltersComputer.AllFilters[]::new);
  42. assertThat(instances).hasSize(expected);
  43. }
  44. @Test
  45. public void addFilter_fails_if_name_is_null() {
  46. FilterScope filterScope = mock(FilterScope.class);
  47. RequestFiltersComputer.AllFilters allFilters = RequestFiltersComputer.newAllFilters();
  48. BoolQueryBuilder boolQuery = boolQuery();
  49. assertThatThrownBy(() -> allFilters.addFilter(null, filterScope, boolQuery))
  50. .isInstanceOf(NullPointerException.class)
  51. .hasMessage("name can't be null");
  52. }
  53. @Test
  54. public void addFilter_fails_if_fieldname_is_null() {
  55. String name = randomAlphabetic(12);
  56. RequestFiltersComputer.AllFilters allFilters = RequestFiltersComputer.newAllFilters();
  57. BoolQueryBuilder boolQuery = boolQuery();
  58. assertThatThrownBy(() -> allFilters.addFilter(name, null, boolQuery))
  59. .isInstanceOf(NullPointerException.class)
  60. .hasMessage("filterScope can't be null");
  61. }
  62. @Test
  63. public void addFilter_fails_if_field_with_name_already_exists() {
  64. String name1 = randomAlphabetic(12);
  65. String name2 = randomAlphabetic(15);
  66. FilterScope filterScope1 = mock(FilterScope.class);
  67. FilterScope filterScope2 = mock(FilterScope.class);
  68. RequestFiltersComputer.AllFilters allFilters = RequestFiltersComputer.newAllFilters();
  69. allFilters.addFilter(name2, filterScope1, boolQuery());
  70. Stream.<ThrowingCallable>of(
  71. // exact same call
  72. () -> allFilters.addFilter(name2, filterScope1, boolQuery()),
  73. // call with a different fieldName
  74. () -> allFilters.addFilter(name2, filterScope2, boolQuery()))
  75. .forEach(t -> assertThatThrownBy(t)
  76. .isInstanceOf(IllegalArgumentException.class)
  77. .hasMessage("A filter with name " + name2 + " has already been added"));
  78. }
  79. @Test
  80. public void addFilter_does_not_add_filter_if_QueryBuilder_is_null() {
  81. String name = randomAlphabetic(12);
  82. String name2 = randomAlphabetic(14);
  83. RequestFiltersComputer.AllFilters allFilters = RequestFiltersComputer.newAllFilters();
  84. BoolQueryBuilder query = boolQuery();
  85. allFilters.addFilter(name, mock(FilterScope.class), query)
  86. .addFilter(name2, mock(FilterScope.class), null);
  87. List<QueryBuilder> all = allFilters.stream().toList();
  88. assertThat(all).hasSize(1);
  89. assertThat(all.iterator().next()).isSameAs(query);
  90. }
  91. @Test
  92. public void stream_is_empty_when_addFilter_never_called() {
  93. RequestFiltersComputer.AllFilters allFilters = RequestFiltersComputer.newAllFilters();
  94. assertThat(allFilters.stream()).isEmpty();
  95. }
  96. }