Last active
October 1, 2018 15:24
-
-
Save ebuildy/2269df066a1430d3d36697654bf43555 to your computer and use it in GitHub Desktop.
elasticsearch mockup server
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
package com.qwant.datahub.djobi.test; | |
import org.json.JSONArray; | |
import org.json.JSONObject; | |
import org.mockserver.client.server.MockServerClient; | |
import org.mockserver.model.HttpRequest; | |
import org.mockserver.model.HttpResponse; | |
import org.mockserver.verify.VerificationTimes; | |
import java.util.ArrayList; | |
import java.util.HashMap; | |
import java.util.List; | |
import java.util.Map; | |
public class ElasticsearchMockClient { | |
private final MockServerClient client; | |
public String version = "2.4"; | |
public String clusterName = "mock"; | |
public ElasticsearchMockClient(final MockServerClient client) { | |
this.client = client; | |
setup(); | |
} | |
public void setup() { | |
this.client.when( | |
HttpRequest.request() | |
.withPath(".*/_count") | |
).respond(toHttpResponse(new JSONObject(map("count", 0)))); | |
this.client.when( | |
HttpRequest.request() | |
.withPath(".*/_search") | |
).respond(toHttpResponse(new JSONObject(map("took", 10, "hits" , map("total", 0))))); | |
this.client.when( | |
HttpRequest.request() | |
.withPath(".*/_refresh") | |
).respond(toHttpResponse(new JSONObject(map("_shards", map("total", 100, "successful", 100, "failed", 0))))); | |
this.client.when( | |
HttpRequest.request() | |
.withPath(".*/_bulk") | |
).respond(toHttpResponse(new JSONObject(map("took", 10, "errors", false, "items", new JSONArray())))); | |
// Create index | |
this.client.when( | |
HttpRequest.request().withMethod("PUT") | |
) | |
.respond(toHttpResponse(new JSONObject(map("acknowledged", true, "shards_acknowledged", true)))); | |
// Check if index exists | |
this.client.when( | |
HttpRequest.request().withMethod("HEAD") | |
) | |
.respond(HttpResponse.notFoundResponse()); | |
// Default | |
this.client.when( | |
HttpRequest.request() | |
).respond(toHttpResponse(new JSONObject(map("name", "mock", "cluster_name", clusterName, "version", map("number", version))))); | |
} | |
public void assertIndexCreated(final String index) throws AssertionError { | |
this.client.verify( | |
HttpRequest.request() | |
.withMethod("PUT") | |
.withPath("/" + index), | |
VerificationTimes.once()); | |
} | |
private HttpResponse toHttpResponse(final JSONObject data) { | |
return HttpResponse.response(data.toString()).withHeader("Content-Type", "application/json"); | |
} | |
private <K, V> Map<K, V> map(Object... args) { | |
Map<K, V> res = new HashMap<>(); | |
K key = null; | |
for (Object arg : args) { | |
if (key == null) { | |
key = (K) arg; | |
} else { | |
res.put(key, (V) arg); | |
key = null; | |
} | |
} | |
return res; | |
} | |
} |
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
public class SimplePipelineTest { | |
@Rule | |
public MockServerRule mockServerRule = new MockServerRule(9200, this); | |
private MockServerClient mockClient; | |
private ElasticsearchMockClient esMockClient; | |
@Before | |
public void setup() { | |
this.esMockClient = new ElasticsearchMockClient(mockClient).setup(); | |
} | |
@Test() | |
public void testCheckJob() throws Exception { | |
// Do someting | |
this.esMockClient.assertIndexCreated("out-context_a"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment