import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.base.Strings;
+import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import org.apache.commons.lang.StringUtils;
import org.sonar.api.ServerComponent;
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
-import java.util.Arrays;
-import java.util.Calendar;
-import java.util.Date;
-import java.util.List;
-import java.util.Map;
+
+import java.util.*;
+
+import static com.google.common.collect.Lists.newArrayList;
public class MeasureFilterFactory implements ServerComponent {
}
}
} else {
- filter.setSortOn(MeasureFilterSort.Field.valueOf(s.toUpperCase()));
+ String sort = s.toUpperCase();
+ if (sortFieldLabels().contains(sort)) {
+ filter.setSortOn(MeasureFilterSort.Field.valueOf(sort));
+ }
}
}
}
}
+ private List<String> sortFieldLabels(){
+ return newArrayList(Iterables.transform(Arrays.asList(MeasureFilterSort.Field.values()), new Function<MeasureFilterSort.Field, String>() {
+ @Override
+ public String apply(@Nullable MeasureFilterSort.Field input) {
+ return input != null ? input.name() : null;
+ }
+ }));
+ }
+
@CheckForNull
private MeasureFilterCondition toCondition(Map<String, Object> props, int index) {
MeasureFilterCondition condition = null;
}
@CheckForNull
- private MeasureFilterCondition alertToCondition(List<String> alertLevels) {
+ private MeasureFilterCondition alertToCondition(@Nullable List<String> alertLevels) {
if (alertLevels == null || alertLevels.isEmpty()) {
return null;
}
String op = "in";
List<String> alertLevelsUppercase = Lists.transform(alertLevels, new Function<String, String>() {
@Override
- public String apply(String input) {
+ public String apply(@Nullable String input) {
return input != null ? input.toUpperCase() : "";
}
});
assertThat(filter.sort().isAsc()).isTrue();
}
+ @Test
+ public void fallback_on_name_sort_when_sort_is_unknown() {
+ MeasureFilterFactory factory = new MeasureFilterFactory(newMetricFinder(), system);
+ Map<String, Object> props = ImmutableMap.<String, Object>of("sort", "unknown");
+ MeasureFilter filter = factory.create(props);
+
+ assertThat(filter.sort().column()).isEqualTo("p.long_name");
+ assertThat(filter.sort().metric()).isNull();
+ assertThat(filter.sort().period()).isNull();
+ assertThat(filter.sort().isAsc()).isTrue();
+ }
+
@Test
public void descending_sort() {
MeasureFilterFactory factory = new MeasureFilterFactory(newMetricFinder(), system);