-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update jzmq JAR file and add some unit tests
- Loading branch information
Showing
5 changed files
with
311 additions
and
12 deletions.
There are no files selected for viewing
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
Binary file not shown.
81 changes: 81 additions & 0 deletions
81
src/test/java/org/elasticsearch/zeromq/test/SimpleClient.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
|
||
} | ||
} | ||
} |
204 changes: 204 additions & 0 deletions
204
src/test/java/org/elasticsearch/zeromq/test/ZMQTransportPluginTest.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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")); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|