Skip to content

Commit e2fdad9

Browse files
committed
Backport RestActionTestCase.
1 parent c0c4291 commit e2fdad9

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.test.rest;
21+
22+
import org.elasticsearch.client.node.NodeClient;
23+
import org.elasticsearch.common.settings.Settings;
24+
import org.elasticsearch.common.util.concurrent.ThreadContext;
25+
import org.elasticsearch.indices.breaker.NoneCircuitBreakerService;
26+
import org.elasticsearch.rest.RestController;
27+
import org.elasticsearch.rest.RestRequest;
28+
import org.elasticsearch.test.ESTestCase;
29+
import org.elasticsearch.usage.UsageService;
30+
import org.junit.Before;
31+
32+
import java.util.Collections;
33+
34+
import static org.mockito.Mockito.mock;
35+
36+
/**
37+
* A common base class for Rest*ActionTests. Provides access to a {@link RestController}
38+
* that can be used to register individual REST actions, and test request handling.
39+
*/
40+
public abstract class RestActionTestCase extends ESTestCase {
41+
private RestController controller;
42+
43+
@Before
44+
public void setUpController() {
45+
controller = new RestController(Collections.emptySet(), null,
46+
mock(NodeClient.class),
47+
new NoneCircuitBreakerService(),
48+
new UsageService());
49+
}
50+
51+
/**
52+
* A test {@link RestController}. This controller can be used to register and delegate
53+
* to handlers, but uses a mock client and cannot carry out the full request.
54+
*/
55+
protected RestController controller() {
56+
return controller;
57+
}
58+
59+
/**
60+
* Sends the given request to the test controller in {@link #controller()}.
61+
*/
62+
protected void dispatchRequest(RestRequest request) {
63+
FakeRestChannel channel = new FakeRestChannel(request, false, 1);
64+
ThreadContext threadContext = new ThreadContext(Settings.EMPTY);
65+
controller.dispatchRequest(request, channel, threadContext);
66+
}
67+
}

0 commit comments

Comments
 (0)