Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Update jzmq JAR file and add some unit tests

  • Loading branch information...
commit 4efab66f9025892ccfab7b16e080d4416b49e14d 1 parent c8cd944
@tlrx authored
View
21 README.textile
@@ -2,15 +2,15 @@ h1. ØMQ transport layer plugin for Elasticsearch
This plugin exposes the REST interfaces of "Elasticsearch":http://www.elasticsearch.org over "ZeroMQ":http://www.zeromq.org/ messaging library.
-h2. Installation
-
h2. Versions
-|_. ØMQ Transport Plugin|_. ElasticSearch |
-| master (0.0.3) | master (0.19.2) |
-| 0.0.2 | 0.18.2 |
+|_. ØMQ Transport Plugin|_. ElasticSearch |_. ØMQ |
+| master (0.0.3) | master (0.19.2) | 2.2 |
+| 0.0.2 | 0.18.2 | 2.1 |
+h2. Installation
+
h3. Requirements
Before installing and using this plugin with Elasticsearch (0.19.2), you need to install ZeroMQ Java Binding library (2.2) on your system. This binding uses native library to work.
@@ -91,8 +91,7 @@ Message:
<pre>java org.elasticsearch.zeromq.test.SimpleClient tcp://localhost:9700 PUT /twitter/tweet/2 "{\"user\": \"kimchy\",\"post_date\": \"2009-11-15T14:12:12\",\"message\": \"You know, for Search\"}"</pre>
Reply:
-<pre>Response:
-201|CREATED|{"ok" : true, "_index" : "twitter", "_type" : "tweet", "_id" : "2", "_version" : 1}</pre>
+<pre>201|CREATED|{"ok" : true, "_index" : "twitter", "_type" : "tweet", "_id" : "2", "_version" : 1}</pre>
h4. Search for document
@@ -101,8 +100,7 @@ Message:
<pre>java org.elasticsearch.zeromq.test.SimpleClient tcp://localhost:9700 GET /twitter/tweet/_search?q=user:kimchy</pre>
Reply:
-<pre>Response:
-200|OK|{"took" : 29, "timed_out" : false, "_shards" : {"total" : 5, "successful" : 5, "failed" : 0}, "hits" : {"total" : 1, "max_score" : 0.30685282, "hits" : [{"_index" : "twitter", "_type" : "tweet", "_id" : "2", "_score" : 0.30685282, "_source" : {
+<pre>200|OK|{"took" : 29, "timed_out" : false, "_shards" : {"total" : 5, "successful" : 5, "failed" : 0}, "hits" : {"total" : 1, "max_score" : 0.30685282, "hits" : [{"_index" : "twitter", "_type" : "tweet", "_id" : "2", "_score" : 0.30685282, "_source" : {
"user" : "kimchy",
"post_date" : "2009-11-15T14:12:12",
"message" : "You know, for Search"
@@ -116,12 +114,11 @@ Message:
<pre>java org.elasticsearch.zeromq.test.SimpleClient tcp://localhost:9700 DELETE /twitter/tweet/2</pre>
Reply:
-<pre>Response:
-200|OK|{"ok" : true, "found" : true, "_index" : "twitter", "_type" : "tweet", "_id" : "2", "_version" : 2}</pre>
+<pre>200|OK|{"ok" : true, "found" : true, "_index" : "twitter", "_type" : "tweet", "_id" : "2", "_version" : 2}</pre>
h3. Other examples
-The @ZMQTransportPluginTest@ Java class in test package has other test examples.
+The @ZMQTransportPluginTest@ Java class in test package has other examples.
Thanks to "David Pilato":https://github.com/dadoonet for the Maven pom and README files.
View
BIN  jzmq-1.0.0.jar
Binary file not shown
View
81 src/test/java/org/elasticsearch/zeromq/test/SimpleClient.java
@@ -0,0 +1,81 @@
+/**
+ *
+ */
+package org.elasticsearch.zeromq.test;
+
+import java.io.UnsupportedEncodingException;
+
+import org.elasticsearch.zeromq.ZMQSocket;
+import org.zeromq.ZMQ;
+
+/**
+ * A simple ØMQ client (XREQ)
+ *
+ * @author tlrx
+ *
+ */
+public class SimpleClient {
+
+ /**
+ * @param args
+ */
+ public static void main(String[] args) {
+
+ if (args == null || args.length < 3) {
+ System.err
+ .println("Usage: SimpleClient <address> <method> <url> <json>");
+ return;
+ }
+
+ // tcp://localhost:9700 by default
+ String address = args[0];
+ String method = args[1];
+ String url = args[2];
+ String json = null;
+ if(args.length > 3){
+ json = args[3];
+ }
+
+
+ final ZMQ.Context context = ZMQ.context(1);
+ ZMQ.Socket socket = context.socket(ZMQ.DEALER);
+ socket.connect(address);
+
+ // Handshake
+ try {
+ Thread.sleep(1000);
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+
+ StringBuilder sb = new StringBuilder(method);
+ sb.append(ZMQSocket.SEPARATOR);
+ sb.append(url).append(ZMQSocket.SEPARATOR);
+
+ if(json != null){
+ sb.append(json);
+ }
+
+ try {
+ socket.send(sb.toString().getBytes("UTF-8"), 0);
+
+ byte[] response = socket.recv(0);
+ System.out.println("Response: \r\n" + new String(response, "UTF-8"));
+
+ } catch (UnsupportedEncodingException e) {
+ e.printStackTrace();
+ } finally {
+ try {
+ socket.close();
+ } catch (Exception e2) {
+ // ignore
+ }
+ try {
+ context.term();
+ } catch (Exception e2) {
+ // ignore
+ }
+
+ }
+ }
+}
View
204 src/test/java/org/elasticsearch/zeromq/test/ZMQTransportPluginTest.java
@@ -0,0 +1,204 @@
+package org.elasticsearch.zeromq.test;
+
+import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
+
+import java.io.IOException;
+import java.io.UnsupportedEncodingException;
+import java.nio.charset.Charset;
+import java.util.Date;
+
+import org.elasticsearch.common.settings.ImmutableSettings;
+import org.elasticsearch.common.xcontent.XContentBuilder;
+import org.elasticsearch.node.Node;
+import org.elasticsearch.node.NodeBuilder;
+import org.elasticsearch.zeromq.ZMQSocket;
+import org.junit.AfterClass;
+import org.junit.Assert;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.zeromq.ZMQ;
+
+public class ZMQTransportPluginTest {
+
+ private static Node node = null;
+
+ private static ZMQ.Context context = null;
+
+ /*
+ * ØMQ Socket binding adress, must be coherent with elasticsearch.yml config file
+ */
+ private static final String address = "tcp://localhost:9800";
+
+ @BeforeClass
+ public static void setUpBeforeClass() throws Exception {
+ // Instantiate an ES server
+ node = NodeBuilder.nodeBuilder()
+ .settings(
+ ImmutableSettings.settingsBuilder()
+ .put("es.config", "elasticsearch.yml")
+ ).node();
+
+ // Instantiate a ZMQ context
+ context = ZMQ.context(1);
+ }
+
+ @AfterClass
+ public static void tearDownAfterClass() throws Exception {
+ if(node != null){
+ node.close();
+ }
+
+ try {
+ context.term();
+ } catch (Exception e2) {
+ // ignore
+ }
+ }
+
+ /**
+ * Simple method to send & receive zeromq message
+ *
+ * @param method
+ * @param uri
+ * @param json
+ * @return
+ */
+ private String sendAndReceive(String method, String uri, String json){
+
+ ZMQ.Socket socket = context.socket(ZMQ.DEALER);
+ socket.connect(address);
+
+ // Handshake
+ try {
+ Thread.sleep(100);
+ } catch (Exception e) {
+ Assert.fail("Handshake failed");
+ }
+
+ StringBuilder sb = new StringBuilder(method);
+ sb.append(ZMQSocket.SEPARATOR).append(uri).append(ZMQSocket.SEPARATOR);
+
+ if(json != null){
+ sb.append(json);
+ }
+
+ String result = null;
+ try {
+ socket.send(sb.toString().getBytes("UTF-8"), 0);
+
+ byte[] response = socket.recv(0);
+ result = new String(response, Charset.forName("UTF-8"));
+
+ } catch (UnsupportedEncodingException e) {
+ Assert.fail("Exception when sending/receiving message");
+ } finally {
+ try {
+ socket.close();
+ } catch (Exception e2) {
+ // ignore
+ }
+ }
+ return result;
+ }
+
+
+ @Test
+ public void testDeleteMissingIndex(){
+ String response = sendAndReceive("DELETE", "/test-index-missing/", null);
+ Assert.assertEquals("404|NOT_FOUND|{\"error\":\"IndexMissingException[[test-index-missing] missing]\",\"status\":404}", response);
+ }
+
+ @Test
+ public void testCreateIndex(){
+ String response = sendAndReceive("DELETE", "/books/", null);
+ Assert.assertNotNull(response);
+
+ response = sendAndReceive("PUT", "/books/", null);
+ Assert.assertEquals("200|OK|{\"ok\":true,\"acknowledged\":true}", response);
+ }
+
+ @Test
+ public void testMapping() throws IOException{
+ XContentBuilder mapping = jsonBuilder()
+ .startObject()
+ .startObject("book")
+ .startObject("properties")
+ .startObject("title")
+ .field("type", "string")
+ .field("analyzer", "french")
+ .endObject()
+ .startObject("author")
+ .field("type", "string")
+ .endObject()
+ .startObject("year")
+ .field("type", "integer")
+ .endObject()
+ .startObject("publishedDate")
+ .field("type", "date")
+ .endObject()
+ .endObject()
+ .endObject()
+ .endObject();
+
+ String response = sendAndReceive("PUT", "/books/book/_mapping", mapping.string());
+ Assert.assertEquals("200|OK|{\"ok\":true,\"acknowledged\":true}", response);
+ }
+
+ @Test
+ public void testIndex() throws IOException{
+ XContentBuilder book1 = jsonBuilder()
+ .startObject()
+ .field("title", "Les Misérables")
+ .field("author", "Victor Hugo")
+ .field("year", "1862")
+ .field("publishedDate", new Date())
+ .endObject();
+
+ String response = sendAndReceive("PUT", "/books/book/1", book1.string());
+ Assert.assertEquals("201|CREATED|{\"ok\":true,\"_index\":\"books\",\"_type\":\"book\",\"_id\":\"1\",\"_version\":1}", response);
+
+ XContentBuilder book2 = jsonBuilder()
+ .startObject()
+ .field("title", "Notre-Dame de Paris")
+ .field("author", "Victor Hugo")
+ .field("year", "1831")
+ .field("publishedDate", new Date())
+ .endObject();
+
+ response = sendAndReceive("PUT", "/books/book/2", book2.string());
+ Assert.assertEquals("201|CREATED|{\"ok\":true,\"_index\":\"books\",\"_type\":\"book\",\"_id\":\"2\",\"_version\":1}", response);
+
+ XContentBuilder book3 = jsonBuilder()
+ .startObject()
+ .field("title", "Le Dernier Jour d'un condamné")
+ .field("author", "Victor Hugo")
+ .field("year", "1829")
+ .field("publishedDate", new Date())
+ .endObject();
+
+ response = sendAndReceive("POST", "/books/book", book3.string());
+ Assert.assertNotNull("Response should not be null", response);
+ Assert.assertTrue(response.startsWith("201|CREATED|{\"ok\":true,\"_index\":\"books\",\"_type\":\"book\",\"_id\""));
+ }
+
+ @Test
+ public void testRefresh() throws IOException{
+ String response = sendAndReceive("GET", "/_all/_refresh", null);
+ Assert.assertTrue(response.startsWith("200|OK"));
+ }
+
+ @Test
+ public void testSearch() throws IOException{
+ String response = sendAndReceive("GET", "/_all/_search", "{\"query\":{\"match_all\":{}}}");
+ Assert.assertTrue(response.contains("\"hits\":{\"total\":3"));
+
+ response = sendAndReceive("GET", "_search", "{\"query\":{\"bool\":{\"must\":[{\"range\":{\"year\":{\"gte\":1820,\"lte\":1832}}}],\"must_not\":[],\"should\":[]}},\"from\":0,\"size\":50,\"sort\":[],\"facets\":{},\"version\":true}:");
+ Assert.assertTrue(response.contains("\"hits\":{\"total\":2"));
+ }
+
+ @Test
+ public void testGet() throws IOException{
+ String response = sendAndReceive("GET", "/books/book/2", null);
+ Assert.assertTrue(response.contains("Notre-Dame de Paris"));
+ }
+}
View
17 src/test/resources/elasticsearch.yml
@@ -0,0 +1,17 @@
+# Elasticsearch configuration for unit tests
+node:
+ data: true
+ local: true
+
+path:
+ data: ./target/es/data
+
+index:
+ store:
+ type: memory
+
+# ZeroMQ Transport config
+zeromq.router.bind: tcp://*:9800
+zeromq.workers.threads: 2
+zeromq.workers.bind: inproc://es_zeromq_workers
+
Please sign in to comment.
Something went wrong with that request. Please try again.