Merge "Do not fail if user provides a too high/low label value in query"

This commit is contained in:
Edwin Kempin
2019-11-20 15:11:26 +00:00
committed by Gerrit Code Review
3 changed files with 51 additions and 0 deletions

View File

@@ -106,6 +106,10 @@ public final class RangeUtil {
break;
}
// Ensure that minValue <= min/max <= maxValue.
min = Ints.constrainToRange(min, minValue, maxValue);
max = Ints.constrainToRange(max, minValue, maxValue);
return new Range(prefix, min, max);
}

View File

@@ -18,6 +18,7 @@ import static com.google.common.truth.Truth.assertThat;
import static com.google.gerrit.acceptance.testsuite.project.TestProjectUpdate.block;
import static com.google.gerrit.server.group.SystemGroupBackend.REGISTERED_USERS;
import static java.util.stream.Collectors.toList;
import static javax.servlet.http.HttpServletResponse.SC_OK;
import com.google.common.collect.ImmutableList;
import com.google.gerrit.acceptance.AbstractDaemonTest;
@@ -182,6 +183,18 @@ public class QueryChangeIT extends AbstractDaemonTest {
.containsExactly(changeId3, changeId4);
}
@Test
public void usingOutOfRangeLabelValuesDoesNotCauseError() throws Exception {
for (String operator : ImmutableList.of("=", ">", ">=", "<", "<=")) {
QueryChanges queryChanges = queryChangesProvider.get();
queryChanges.addQuery("label:Code-Review" + operator + "10");
queryChanges.addQuery("label:Code-Review" + operator + "-10");
queryChanges.addQuery("Code-Review" + operator + "10");
queryChanges.addQuery("Code-Review" + operator + "-10");
assertThat(queryChanges.apply(TopLevelResource.INSTANCE).statusCode()).isEqualTo(SC_OK);
}
}
private static void assertNoChangeHasMoreChangesSet(List<ChangeInfo> results) {
for (ChangeInfo info : results) {
assertThat(info._moreChanges).isNull();

View File

@@ -0,0 +1,34 @@
// Copyright (C) 2019 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.index.query;
import static com.google.common.truth.Truth.assertThat;
import com.google.common.collect.ImmutableList;
import com.google.gerrit.index.query.RangeUtil.Range;
import org.junit.Test;
public class RangeUtilTest {
@Test
public void getRangeForValueOutsideOfMinMaxRange_minNotGreaterThanMax() {
for (String operator : ImmutableList.of("=", ">", ">=", "<", "<=")) {
Range range = RangeUtil.getRange("foo", operator, 10, -4, 4);
assertThat(range.min).isAtMost(range.max);
range = RangeUtil.getRange("foo", operator, -10, -4, 4);
assertThat(range.min).isAtMost(range.max);
}
}
}