Tests: Remove class only used by tests

The `Integers` class was only used in tests (but lurked around in the src) and is not needed.
Also replaced some lambda calls with their shorter equivalents.

Original commit: elastic/x-pack-elasticsearch@a81a5c33d3
This commit is contained in:
Alexander Reelsen 2017-01-24 18:10:05 +01:00
parent 984b1b0dd1
commit 9d002430b5
2 changed files with 11 additions and 48 deletions

View File

@ -1,40 +0,0 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
package org.elasticsearch.xpack.watcher.support;
import java.util.Arrays;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Objects;
public class Integers {
private Integers() {
}
public static Iterable<Integer> asIterable(int[] values) {
Objects.requireNonNull(values);
return () -> new Iterator<Integer>() {
private int position = 0;
@Override
public boolean hasNext() {
return position < values.length;
}
@Override
public Integer next() {
if (position < values.length) {
return values[position++];
} else {
throw new NoSuchElementException("position: " + position + ", length: " + values.length);
}
}
};
}
public static boolean contains(int[] values, final int value) {
return Arrays.stream(values).anyMatch(v -> v == value);
}
}

View File

@ -13,14 +13,14 @@ import org.elasticsearch.common.xcontent.json.JsonXContent;
import org.elasticsearch.xpack.watcher.support.Strings;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.elasticsearch.xpack.watcher.support.Integers.asIterable;
import static org.elasticsearch.xpack.watcher.support.Integers.contains;
import static org.hamcrest.Matchers.arrayContaining;
import static org.hamcrest.Matchers.arrayWithSize;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
@ -147,7 +147,7 @@ public class HourlyScheduleTests extends ScheduleTestCase {
int[] minutes = validMinutes();
XContentBuilder builder = jsonBuilder()
.startObject()
.field("minute", asIterable(minutes))
.field("minute", minutes)
.endObject();
BytesReference bytes = builder.bytes();
XContentParser parser = createParser(JsonXContent.jsonXContent, bytes);
@ -155,8 +155,9 @@ public class HourlyScheduleTests extends ScheduleTestCase {
HourlySchedule schedule = new HourlySchedule.Parser().parse(parser);
assertThat(schedule, notNullValue());
assertThat(schedule.minutes().length, is(minutes.length));
List<Integer> ints = Arrays.stream(schedule.minutes()).mapToObj(Integer::valueOf).collect(Collectors.toList());
for (int i = 0; i < minutes.length; i++) {
assertThat(contains(schedule.minutes(), minutes[i]), is(true));
assertThat(ints, hasItem(minutes[i]));
}
}
@ -164,7 +165,7 @@ public class HourlyScheduleTests extends ScheduleTestCase {
int[] minutes = invalidMinutes();
XContentBuilder builder = jsonBuilder()
.startObject()
.field("minute", asIterable(minutes))
.field("minute", minutes)
.endObject();
BytesReference bytes = builder.bytes();
XContentParser parser = createParser(JsonXContent.jsonXContent, bytes);
@ -181,7 +182,7 @@ public class HourlyScheduleTests extends ScheduleTestCase {
int[] minutes = validMinutes();
XContentBuilder builder = jsonBuilder()
.startObject()
.field("minute", Arrays.stream(minutes).mapToObj(p -> Integer.toString(p)).collect(Collectors.toList()))
.field("minute", Arrays.stream(minutes).mapToObj(Integer::toString).collect(Collectors.toList()))
.endObject();
BytesReference bytes = builder.bytes();
XContentParser parser = createParser(JsonXContent.jsonXContent, bytes);
@ -189,8 +190,10 @@ public class HourlyScheduleTests extends ScheduleTestCase {
HourlySchedule schedule = new HourlySchedule.Parser().parse(parser);
assertThat(schedule, notNullValue());
assertThat(schedule.minutes().length, is(minutes.length));
List<Integer> ints = Arrays.stream(schedule.minutes()).mapToObj(Integer::valueOf).collect(Collectors.toList());
for (int i = 0; i < minutes.length; i++) {
assertThat(contains(schedule.minutes(), minutes[i]), is(true));
assertThat(ints, hasItem(minutes[i]));
}
}
@ -198,7 +201,7 @@ public class HourlyScheduleTests extends ScheduleTestCase {
int[] minutes = invalidMinutes();
XContentBuilder builder = jsonBuilder()
.startObject()
.field("minute", Arrays.stream(minutes).mapToObj(p -> Integer.toString(p)).collect(Collectors.toList()))
.field("minute", Arrays.stream(minutes).mapToObj(Integer::toString).collect(Collectors.toList()))
.endObject();
BytesReference bytes = builder.bytes();
XContentParser parser = createParser(JsonXContent.jsonXContent, bytes);