Skip to content

Commit

Permalink
Merge pull request #74 from jsanda/json-tests
Browse files Browse the repository at this point in the history
moving embedded json into their own separate files to reduce clutter
  • Loading branch information
edwardcapriolo committed Feb 5, 2013
2 parents 3560eaf + 3acf303 commit e065af7
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 85 deletions.
123 changes: 38 additions & 85 deletions src/test/java/org/usergrid/vx/experimental/RawJsonTest.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package org.usergrid.vx.experimental;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.util.concurrent.CountDownLatch;

import org.apache.cassandra.config.Schema;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.map.ObjectMapper;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -35,23 +39,8 @@ public void setup() {

@Test
public void createKeyspaceViaCQL() throws Exception {
String post =
"{\"e\":[" +
"{ " +
" \"type\": \"SETKEYSPACE\", " +
" \"op\": { " +
" \"keyspace\": \"system\" " +
" } " +
"}, " +
" { " +
" \"type\":\"CQLQUERY\", " +
" \"op\": { " +
" \"version\": \"3.0.0\", " +
" \"query\": \"CREATE KEYSPACE simple WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1}\" " +
" } " +
" } " +
" ]} ";
System.out.println("posting " + post);
String json = loadJSON("create_keyspace_cql.json");
System.out.println("posting " + json);
final CountDownLatch doneSignal = new CountDownLatch(1);
HttpClientRequest req = httpClient.request("POST", "/:appid/intrareq-json", new Handler<HttpClientResponse>() {
@Override
Expand All @@ -71,8 +60,8 @@ protected void handle() {
});
}
});
req.putHeader("content-length", post.length());
req.write(post);
req.putHeader("content-length", json.length());
req.write(json);
req.end();
doneSignal.await();

Expand All @@ -82,29 +71,8 @@ protected void handle() {

@Test
public void setAndGetColumn() throws Exception {
String setColumnJSON =
"{\"e\": [" +
" {" +
" \"type\": \"SETKEYSPACE\"," +
" \"op\": {" +
" \"keyspace\": \"myks\"" +
" }" +
" }," +
" {" +
" \"type\": \"SETCOLUMNFAMILY\"," +
" \"op\": {" +
" \"columnfamily\": \"mycf\"" +
" }" +
" }," +
" {" +
" \"type\": \"SET\"," +
" \"op\": {" +
" \"rowkey\": \"row_key1\"," +
" \"name\": \"column1\"," +
" \"value\": \"value1\"" +
" }" +
" }" +
"]}";
String setColumnJSON = loadJSON("set_column.json");

final CountDownLatch doneSignal = new CountDownLatch(1);
final HttpClientRequest setReq = httpClient.request("POST", "/:appid/intrareq-json", new Handler<HttpClientResponse>() {
@Override
Expand All @@ -121,46 +89,8 @@ protected void handle() {
setReq.write(setColumnJSON);
setReq.end();

final String getColumnJSON =
"{\"e\": [" +
" {" +
" \"type\": \"SETKEYSPACE\"," +
" \"op\": {" +
" \"keyspace\": \"myks\"" +
" }" +
" }," +
" {" +
" \"type\": \"SETCOLUMNFAMILY\"," +
" \"op\": {\n" +
" \"columnfamily\": \"mycf\"" +
" }" +
" }," +
"{" +
" \"type\": \"ASSUME\"," +
" \"op\": {\n" +
" \"keyspace\": \"myks\"," +
" \"columnfamily\": \"mycf\"," +
" \"type\": \"column\"," +
" \"clazz\": \"UTF-8\"" +
" }" +
" }," +
"{" +
" \"type\": \"ASSUME\"," +
" \"op\": {" +
" \"keyspace\": \"myks\"," +
" \"columnfamily\": \"mycf\"," +
" \"type\": \"value\"," +
" \"clazz\": \"UTF-8\"" +
" }" +
" }," +
" {" +
" \"type\": \"GET\"," +
" \"op\": {" +
" \"rowkey\": \"row_key1\"," +
" \"name\": \"column1\"" +
" }" +
" }" +
"]}";
final String getColumnJSON = loadJSON("get_column.json");

final Buffer data = new Buffer(0);
final HttpClientRequest getReq = httpClient.request("POST", "/:appid/intrareq-json",
new Handler<HttpClientResponse>() {
Expand All @@ -186,9 +116,32 @@ protected void handle() {
getReq.end();
doneSignal.await();

String expectedResponse =
"{\"exception\":null,\"exceptionId\":null,\"opsRes\":{\"0\":\"OK\",\"1\":\"OK\",\"2\":\"OK\",\"3\":\"OK\",\"4\":[{\"name\":\"column1\",\"value\":\"value1\"}]}}";
String expectedResponse = loadJSON("get_column_response.json");

assertJSONEquals("The response was incorrect", expectedResponse, data.toString());
}

private String loadJSON(String file) throws Exception {
try (
BufferedInputStream input = new BufferedInputStream(getClass().getResourceAsStream(file));
ByteArrayOutputStream output = new ByteArrayOutputStream()
) {
byte[] buffer = new byte[2048];

for (int bytesRead = input.read(buffer); bytesRead != -1; bytesRead = input.read(buffer)) {
output.write(buffer, 0, bytesRead);
}
output.flush();

return new String(output.toByteArray());
}
}

private void assertJSONEquals(String msg, String expected, String actual) throws Exception {
ObjectMapper mapper = new ObjectMapper();
JsonNode expectedJson = mapper.readTree(expected);
JsonNode actualJson = mapper.readTree(actual);

Assert.assertEquals("The response was incorrect", expectedResponse, data.toString());
Assert.assertEquals(msg, expectedJson, actualJson);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{"e":[
{
"type": "SETKEYSPACE",
"op": {
"keyspace": "system"
}
},
{
"type":"CQLQUERY",
"op": {
"version": "3.0.0",
"query": "CREATE KEYSPACE simple WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1}"
}
}
]}
39 changes: 39 additions & 0 deletions src/test/resources/org/usergrid/vx/experimental/get_column.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{"e": [
{
"type": "SETKEYSPACE",
"op": {
"keyspace": "myks"
}
},
{
"type": "SETCOLUMNFAMILY",
"op": {
"columnfamily": "mycf"
}
},
{
"type": "ASSUME",
"op": {
"keyspace": "myks",
"columnfamily": "mycf",
"type": "column",
"clazz": "UTF-8"
}
},
{
"type": "ASSUME",
"op": {
"keyspace": "myks",
"columnfamily": "mycf",
"type": "value",
"clazz": "UTF-8"
}
},
{
"type": "GET",
"op": {
"rowkey": "row_key1",
"name": "column1"
}
}
]}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"exception":null, "exceptionId":null, "opsRes":{
"0":"OK",
"1":"OK",
"2":"OK",
"3":"OK",
"4":[
{
"name":"column1",
"value":"value1"
}
]
}}
22 changes: 22 additions & 0 deletions src/test/resources/org/usergrid/vx/experimental/set_column.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{"e": [
{
"type": "SETKEYSPACE",
"op": {
"keyspace": "myks"
}
},
{
"type": "SETCOLUMNFAMILY",
"op": {
"columnfamily": "mycf"
}
},
{
"type": "SET",
"op": {
"rowkey": "row_key1",
"name": "column1",
"value": "value1"
}
}
]}

0 comments on commit e065af7

Please sign in to comment.