public ElasticsearchClient build() { return new ElasticsearchClient( log, buildObjectMapper(), buildHttpClient(socketTimeout), hostname, port); }
@Override public Boolean call() throws Exception { return isProcessRunning(baseDir) && isInstanceRunning(clusterName, client); } }
@Override public Boolean call() throws Exception { return isClusterRunning(clusterName, nodesCount, client); } }
/** * Check whether the cluster with the given name exists in the ES running on the given port. * @param clusterName the ES cluster name * @param httpPort the HTTP port to connect to ES * @return true if the instance is running, false otherwise */ public static boolean isInstanceRunning(String clusterName, int httpPort) { Log log = Mockito.mock(Log.class); ElasticsearchClient client = new ElasticsearchClient.Builder() .withLog(log) .withHostname("localhost") .withPort(httpPort) .withSocketTimeout(5000) .build(); return isInstanceRunning(clusterName, client); }
@Test public void parseStringCommand() { String command = "PUT:index/type/id:{}"; ElasticsearchCommand esCommand = step.parseStringCommand(command); assertFalse(esCommand.isSkip()); assertEquals(RequestMethod.PUT, esCommand.getRequestMethod()); assertEquals("index/type/id", esCommand.getRelativeUrl()); assertEquals("{}", esCommand.getJson()); }
@Override public void execute(ClusterConfiguration config) { // the instances have already started; // waiting just 10 seconds for them to form the cluster int timeout = 10; ElasticsearchClient client = new ElasticsearchClient.Builder() .withInstanceConfiguration(config.getInstanceConfigurationList().get(0)) .withHostname("localhost") .build(); Monitor monitor = new Monitor(client, config.getLog()); monitor.waitToStartCluster( config.getClusterName(), config.getInstanceConfigurationList().size(), timeout); } }
@Override public void execute(InstanceConfiguration config) { int timeout = config.getClusterConfiguration().getTimeout(); ElasticsearchClient client = new ElasticsearchClient.Builder() .withInstanceConfiguration(config) .withHostname("localhost") .build(); Monitor monitor = new Monitor(client, config.getClusterConfiguration().getLog()); monitor.waitToStartInstance( config.getBaseDir(), config.getClusterConfiguration().getClusterName(), timeout); } }
@Test public void testExecuteInitCommandPost() throws ElasticsearchClientException { ElasticsearchCommand command = mock(ElasticsearchCommand.class); when(command.getRequestMethod()).thenReturn(RequestMethod.POST); when(command.getJson()).thenReturn("json"); when(command.getRelativeUrl()).thenReturn("index/type/id"); step.executeInitCommand(client, log, command); verify(client).post("/index/type/id", "json", String.class); }
@Test public void testExecuteInitCommandPut() throws ElasticsearchClientException { ElasticsearchCommand command = mock(ElasticsearchCommand.class); when(command.getRequestMethod()).thenReturn(RequestMethod.PUT); when(command.getJson()).thenReturn("json"); when(command.getRelativeUrl()).thenReturn("index/type/id"); step.executeInitCommand(client, log, command); verify(client).put("/index/type/id", "json"); }
@Test public void testExecuteInitCommandDelete() throws ElasticsearchClientException { ElasticsearchCommand command = mock(ElasticsearchCommand.class); when(command.getRequestMethod()).thenReturn(RequestMethod.DELETE); when(command.getRelativeUrl()).thenReturn("index/type/id"); step.executeInitCommand(client, log, command); verify(client).delete("/index/type/id"); }
public <T> T get(String path, String entity, Class<T> clazz) throws ElasticsearchClientException { String uri = String.format("http://%s:%d%s", hostname, port, path); log.debug(String.format("Sending GET request to %s with entity '%s'", uri, entity)); HttpGetWithEntity request = new HttpGetWithEntity(uri); request.setHeader(HttpHeaders.ACCEPT, ContentType.APPLICATION_JSON.getMimeType()); request.setEntity(new StringEntity(entity, ContentType.APPLICATION_JSON)); String content = executeRequest(request); T result = deserialize(content, clazz); return result; }
@Test public void parseStringCommandSkip() { String command = " # comment"; ElasticsearchCommand esCommand = step.parseStringCommand(command); assertTrue(esCommand.isSkip()); }
public HttpGetWithEntity(URI uri) { super(); setURI(uri); }
public void delete(String path) throws ElasticsearchClientException { String uri = String.format("http://%s:%d%s", hostname, port, path); log.debug(String.format("Sending DELETE request to %s", uri)); HttpDelete request = new HttpDelete(uri); executeRequest(request); }
/** * Verify that the cluster name and the number of nodes in the cluster, * as reported by the ES node, is as expected. * @param clusterName the ES cluster name * @param instanceCount the number of ES nodes in the cluster * @param httpPort the HTTP port to connect to ES * @return true if the cluster is running, false otherwise */ public static boolean isClusterRunning(String clusterName, int instanceCount, int httpPort) { Log log = Mockito.mock(Log.class); ElasticsearchClient client = new ElasticsearchClient.Builder() .withLog(log) .withHostname("localhost") .withPort(httpPort) .withSocketTimeout(5000) .build(); return isClusterRunning(clusterName, instanceCount, client); } }
@Test public void parseStringCommandEmptyJson() { String command = "PUT:index/type/id:"; ElasticsearchCommand esCommand = step.parseStringCommand(command); assertFalse(esCommand.isSkip()); assertEquals(RequestMethod.PUT, esCommand.getRequestMethod()); assertEquals("index/type/id", esCommand.getRelativeUrl()); assertEquals("", esCommand.getJson()); }
@Test(expected = ElasticsearchSetupException.class) public void testExecuteInitCommandWithException() throws ElasticsearchClientException { ElasticsearchCommand command = mock(ElasticsearchCommand.class); when(command.getRequestMethod()).thenReturn(RequestMethod.DELETE); when(command.getRelativeUrl()).thenReturn("index/type/id"); doThrow(ElasticsearchClientException.class).when(client).delete("/index/type/id"); step.executeInitCommand(client, log, command); }
public HttpGetWithEntity(String uri) { super(); setURI(URI.create(uri)); }
@Before public void before() { client = new ElasticsearchClient.Builder() .withLog(log) .withHostname("localhost") .withPort(httpPort) .withSocketTimeout(5000) .build(); }