|
32 | 32 | import java.net.InetAddress;
|
33 | 33 | import java.util.Collections;
|
34 | 34 | import java.util.List;
|
| 35 | +import java.util.Locale; |
35 | 36 | import java.util.Map;
|
36 | 37 |
|
37 | 38 | import static org.hamcrest.Matchers.equalTo;
|
|
41 | 42 |
|
42 | 43 | public class UsageServiceTests extends ESTestCase {
|
43 | 44 |
|
| 45 | + /** |
| 46 | + * Test that we can not add a null reference to a {@link org.elasticsearch.rest.RestHandler} to the {@link UsageService}. |
| 47 | + */ |
| 48 | + public void testHandlerCanNotBeNull() { |
| 49 | + final UsageService service = new UsageService(); |
| 50 | + expectThrows(NullPointerException.class, () -> service.addRestHandler(null)); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Test that we can not add an instance of a {@link org.elasticsearch.rest.RestHandler} with no name to the {@link UsageService}. |
| 55 | + */ |
| 56 | + public void testAHandlerWithNoName() { |
| 57 | + final UsageService service = new UsageService(); |
| 58 | + final BaseRestHandler horse = new MockRestHandler(null); |
| 59 | + final IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> service.addRestHandler(horse)); |
| 60 | + assertThat( |
| 61 | + e.getMessage(), |
| 62 | + equalTo("handler of type [org.elasticsearch.usage.UsageServiceTests$MockRestHandler] does not have a name")); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Test that we can add the same instance of a {@link org.elasticsearch.rest.RestHandler} to the {@link UsageService} multiple times. |
| 67 | + */ |
| 68 | + public void testHandlerWithConflictingNamesButSameInstance() { |
| 69 | + final UsageService service = new UsageService(); |
| 70 | + final String name = randomAlphaOfLength(8); |
| 71 | + final BaseRestHandler first = new MockRestHandler(name); |
| 72 | + service.addRestHandler(first); |
| 73 | + // nothing bad ever happens to me |
| 74 | + service.addRestHandler(first); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Test that we can not add different instances of {@link org.elasticsearch.rest.RestHandler} with the same name to the |
| 79 | + * {@link UsageService}. |
| 80 | + */ |
| 81 | + public void testHandlersWithConflictingNamesButDifferentInstances() { |
| 82 | + final UsageService service = new UsageService(); |
| 83 | + final String name = randomAlphaOfLength(8); |
| 84 | + final BaseRestHandler first = new MockRestHandler(name); |
| 85 | + final BaseRestHandler second = new MockRestHandler(name); |
| 86 | + service.addRestHandler(first); |
| 87 | + final IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> service.addRestHandler(second)); |
| 88 | + final String expected = String.format( |
| 89 | + Locale.ROOT, |
| 90 | + "handler of type [%s] conflicts with handler of type [%1$s] as they both have the same name [%s]", |
| 91 | + "org.elasticsearch.usage.UsageServiceTests$MockRestHandler", |
| 92 | + name |
| 93 | + ); |
| 94 | + assertThat(e.getMessage(), equalTo(expected)); |
| 95 | + } |
| 96 | + |
44 | 97 | public void testRestUsage() throws Exception {
|
45 | 98 | DiscoveryNode discoveryNode = new DiscoveryNode("foo", new TransportAddress(InetAddress.getByName("localhost"), 12345),
|
46 | 99 | Version.CURRENT);
|
|
0 commit comments