Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes for RestService.find{Shard,Slice}Partitions
Add securemock as a test dependency for mr Add tests
- Loading branch information
Showing
4 changed files
with
1,056 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
128 changes: 128 additions & 0 deletions
128
mr/src/test/java/org/elasticsearch/hadoop/rest/FindPartitionsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you 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 org.elasticsearch.hadoop.rest; | ||
|
||
import org.codehaus.jackson.JsonParser; | ||
import org.codehaus.jackson.map.ObjectMapper; | ||
import org.elasticsearch.hadoop.rest.query.MatchAllQueryBuilder; | ||
import org.junit.Test; | ||
import org.mockito.Mockito; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import static org.junit.Assert.assertArrayEquals; | ||
import static org.junit.Assert.assertEquals; | ||
|
||
public class FindPartitionsTest { | ||
private static final ObjectMapper MAPPER = | ||
new ObjectMapper() | ||
.configure(JsonParser.Feature.ALLOW_COMMENTS, true); | ||
private static final RestService.PartitionDefinition[] EXPECTED_SHARDS_PARTITIONS; | ||
static { | ||
List<RestService.PartitionDefinition> expected = | ||
new ArrayList<RestService.PartitionDefinition>(); | ||
for (int i = 0; i < 15; i++) { | ||
expected.add(new RestService.ShardPartitionDefinition("index1", Integer.toString(i))); | ||
} | ||
for (int i = 0; i < 18; i++) { | ||
expected.add(new RestService.ShardPartitionDefinition("index2", Integer.toString(i))); | ||
} | ||
for (int i = 0; i < 1; i++) { | ||
expected.add(new RestService.ShardPartitionDefinition("index3", Integer.toString(i))); | ||
} | ||
Collections.sort(expected); | ||
EXPECTED_SHARDS_PARTITIONS = expected.toArray(new RestService.PartitionDefinition[0]); | ||
} | ||
|
||
@Test | ||
public void testEmpty() { | ||
assertEquals(RestService.findShardPartitions(Collections.<List<Map<String,Object>>>emptyList()).size(), 0); | ||
assertEquals(RestService.findSlicePartitions(null, null, null, 0, | ||
Collections.<List<Map<String,Object>>>emptyList()).size(), 0); | ||
} | ||
|
||
@Test | ||
public void testShardPartitions() throws IOException { | ||
List<List<Map<String, Object>>> shards = | ||
MAPPER.readValue(getClass().getResourceAsStream("search-shards-response.json"), ArrayList.class); | ||
List<RestService.PartitionDefinition> partitions = RestService.findShardPartitions(shards); | ||
Collections.sort(partitions); | ||
assertEquals(partitions.size(), 34); | ||
assertEquals(new HashSet(partitions).size(), 34); | ||
assertArrayEquals(partitions.toArray(), EXPECTED_SHARDS_PARTITIONS); | ||
} | ||
|
||
@Test | ||
public void testSlicePartitions() throws IOException { | ||
List<List<Map<String, Object>>> shards = | ||
MAPPER.readValue(getClass().getResourceAsStream("search-shards-response.json"), ArrayList.class); | ||
RestClient client = Mockito.mock(RestClient.class); | ||
MatchAllQueryBuilder query = new MatchAllQueryBuilder(); | ||
Mockito.when(client.count("index1/type1", query)).thenReturn(1000L); | ||
Mockito.when(client.count("index2/type1", query)).thenReturn(10000L); | ||
Mockito.when(client.count("index3/type1", query)).thenReturn(100000L); | ||
{ | ||
List<RestService.PartitionDefinition> partitions = RestService.findSlicePartitions(client, | ||
"type1", | ||
new MatchAllQueryBuilder(), | ||
1000, | ||
shards); | ||
assertEquals(partitions.size(), 111); | ||
assertEquals(new HashSet(partitions).size(), 111); | ||
} | ||
{ | ||
|
||
List<RestService.PartitionDefinition> partitions = RestService.findSlicePartitions(client, | ||
"type1", | ||
new MatchAllQueryBuilder(), | ||
100, | ||
shards); | ||
assertEquals(partitions.size(), 1110); | ||
assertEquals(new HashSet(partitions).size(), 1110); | ||
} | ||
{ | ||
|
||
List<RestService.PartitionDefinition> partitions = RestService.findSlicePartitions(client, | ||
"type1", | ||
new MatchAllQueryBuilder(), | ||
Integer.MAX_VALUE, | ||
shards); | ||
assertEquals(partitions.size(), 3); | ||
assertEquals(new HashSet(partitions).size(), 3); | ||
} | ||
Mockito.when(client.count("index1/type1", query)).thenReturn(0L); | ||
Mockito.when(client.count("index2/type1", query)).thenReturn(0L); | ||
Mockito.when(client.count("index3/type1", query)).thenReturn(0L); | ||
{ | ||
|
||
List<RestService.PartitionDefinition> partitions = RestService.findSlicePartitions(client, | ||
"type1", | ||
new MatchAllQueryBuilder(), | ||
100, | ||
shards); | ||
assertEquals(partitions.size(), 3); | ||
assertEquals(new HashSet(partitions).size(), 3); | ||
} | ||
} | ||
} |
Oops, something went wrong.