use of org.datanucleus.api.rest.orgjson.JSONObject in project tests by datanucleus.
the class BasicTest method testUpdate.
public void testUpdate() throws IOException {
HttpClient client = new HttpClient();
String globalNum = "global:1786244744";
int personNum = 1;
try {
// Persist the object
ContentExchange post = new ContentExchange();
post.setURL("http://localhost:" + PORT + "/dn/" + Person.class.getName());
post.setMethod("POST");
JSONObject obj = new JSONObject();
obj.put("globalNum", globalNum);
obj.put("personNum", personNum);
obj.put("lastName", "lastName");
obj.put("age", 0);
obj.put("emailAddress", "email");
obj.put("firstName", "firstName");
post.setRequestContent(new ByteArrayBuffer(obj.toString().getBytes()));
client.start();
client.send(post);
post.waitForDone();
// validate
assertEquals(201, post.getResponseStatus());
assertNotNull(post.getResponseContent());
obj = new JSONObject(post.getResponseContent());
assertEquals(globalNum, obj.getString("globalNum"));
assertEquals(personNum, obj.getLong("personNum"));
try {
ContentExchange get = new ContentExchange();
get.setURL("http://localhost:" + PORT + "/dn/" + Person.class.getName());
get.setMethod("GET");
obj = new JSONObject();
obj.put("globalNum", globalNum);
obj.put("personNum", personNum);
get.setRequestContent(new ByteArrayBuffer(obj.toString().getBytes()));
client.send(get);
get.waitForDone();
assertEquals(200, get.getResponseStatus());
assertNotNull(get.getResponseContent());
obj = new JSONObject(get.getResponseContent());
} catch (Exception e) {
fail(e.getMessage());
}
// Update the object
post = new ContentExchange();
post.setURL("http://localhost:" + PORT + "/dn/" + Person.class.getName());
post.setMethod("PUT");
obj = new JSONObject();
obj.put("globalNum", globalNum);
obj.put("personNum", personNum);
obj.put("age", 15);
post.setRequestContent(new ByteArrayBuffer(obj.toString().getBytes()));
client.start();
client.send(post);
post.waitForDone();
// validate
assertEquals(201, post.getResponseStatus());
assertNotNull(post.getResponseContent());
obj = new JSONObject(post.getResponseContent());
assertEquals(globalNum, obj.getString("globalNum"));
assertEquals(personNum, obj.getLong("personNum"));
try {
ContentExchange get = new ContentExchange();
get.setURL("http://localhost:" + PORT + "/dn/" + Person.class.getName());
get.setMethod("GET");
obj = new JSONObject();
obj.put("globalNum", globalNum);
obj.put("personNum", personNum);
get.setRequestContent(new ByteArrayBuffer(obj.toString().getBytes()));
client.send(get);
get.waitForDone();
assertEquals(200, get.getResponseStatus());
assertNotNull(get.getResponseContent());
obj = new JSONObject(get.getResponseContent());
assertEquals(15, obj.getInt("age"));
} catch (Exception e) {
fail(e.getMessage());
}
} catch (Exception e) {
LOG.error("Exception in test", e);
fail("Exception in test : " + e.getMessage());
} finally {
try {
ContentExchange delete = new ContentExchange();
delete.setURL("http://localhost:" + PORT + "/dn/" + Person.class.getName());
delete.setMethod("DELETE");
JSONObject obj = new JSONObject();
obj.put("globalNum", globalNum);
obj.put("personNum", personNum);
delete.setRequestContent(new ByteArrayBuffer(obj.toString().getBytes()));
client.send(delete);
delete.waitForDone();
assertEquals(204, delete.getResponseStatus());
assertNull(delete.getResponseContent());
} catch (Exception e) {
fail(e.getMessage());
}
}
}
use of org.datanucleus.api.rest.orgjson.JSONObject in project tests by datanucleus.
the class BasicTest method testGetOfNonExistentObject.
/**
* Check the correct response status when a non-existent object is passed to GET.
* @throws IOException
*/
public void testGetOfNonExistentObject() throws IOException {
HttpClient client = new HttpClient();
try {
ContentExchange get = new ContentExchange();
get.setURL("http://localhost:" + PORT + "/dn/" + ClassWithStringCollection.class.getName() + "/101?fetchGroup=all");
get.setMethod("GET");
client.start();
client.send(get);
get.waitForDone();
assertEquals(404, get.getResponseStatus());
assertNull(get.getResponseContent());
get = new ContentExchange();
get.setURL("http://localhost:" + PORT + "/dn/" + Person.class.getName());
get.setMethod("GET");
JSONObject obj = new JSONObject();
obj.put("globalNum", "global:1786244744");
obj.put("personNum", 1);
get.setRequestContent(new ByteArrayBuffer(obj.toString().getBytes()));
client.send(get);
get.waitForDone();
assertEquals(404, get.getResponseStatus());
assertNull(get.getResponseContent());
} catch (Exception e) {
LOG.error("Exception on get of non-existent data", e);
fail("Exception on get of data : " + e.getMessage());
} finally {
}
}
use of org.datanucleus.api.rest.orgjson.JSONObject in project tests by datanucleus.
the class BasicTest method testPersistEmbeddedCollection.
/**
* Example using an embedded collection field, with persist then get then delete
* @throws IOException
*/
public void testPersistEmbeddedCollection() throws IOException {
HttpClient client = new HttpClient();
try {
ContentExchange post = new ContentExchange();
post.setURL("http://localhost:" + PORT + "/dn/" + Network.class.getName());
post.setMethod("POST");
JSONObject obj = new JSONObject();
obj.put("id", 1);
obj.put("name", "Home Network");
Collection<JSONObject> devs = new HashSet<JSONObject>();
JSONObject dev1 = new JSONObject();
dev1.put("name", "Toaster");
dev1.put("description", "Kitchen Toaster");
devs.add(dev1);
JSONArray jsonarr = new JSONArray(devs);
obj.put("devices", jsonarr);
post.setRequestContent(new ByteArrayBuffer(obj.toString().getBytes()));
// persist
client.start();
client.send(post);
post.waitForDone();
// validate
assertEquals(201, post.getResponseStatus());
assertNotNull(post.getResponseContent());
obj = new JSONObject(post.getResponseContent());
assertEquals(1, obj.getLong("id"));
try {
ContentExchange get = new ContentExchange();
get.setURL("http://localhost:" + PORT + "/dn/" + Network.class.getName() + "/1?fetch=all");
get.setMethod("GET");
client.send(get);
get.waitForDone();
assertEquals(200, get.getResponseStatus());
assertNotNull(get.getResponseContent());
obj = new JSONObject(get.getResponseContent());
assertEquals("Home Network", obj.getString("name"));
Object devObjs = obj.get("devices");
assertNotNull(devObjs);
assertTrue(devObjs instanceof JSONArray);
JSONArray devArr = (JSONArray) devObjs;
assertEquals(1, devArr.length());
Object devObj = devArr.get(0);
assertTrue(devObj instanceof JSONObject);
JSONObject dev = (JSONObject) devObj;
assertEquals("Toaster", dev.getString("name"));
assertEquals("Kitchen Toaster", dev.getString("description"));
} catch (Exception e) {
LOG.error("Exception in validate", e);
fail(e.getMessage());
}
} catch (Exception e) {
LOG.error("Exception in test", e);
fail("Exception in test : " + e.getMessage());
} finally {
try {
ContentExchange delete = new ContentExchange();
delete.setURL("http://localhost:" + PORT + "/dn/" + Network.class.getName() + "/1");
delete.setMethod("DELETE");
client.send(delete);
delete.waitForDone();
assertEquals(204, delete.getResponseStatus());
assertNull(delete.getResponseContent());
} catch (Exception e) {
fail(e.getMessage());
}
}
}
use of org.datanucleus.api.rest.orgjson.JSONObject in project tests by datanucleus.
the class BasicTest method testClassWithNonPersistableMap.
public void testClassWithNonPersistableMap() throws IOException {
HttpClient client = new HttpClient();
try {
ContentExchange post = new ContentExchange();
post.setURL("http://localhost:" + PORT + "/dn/" + ClassWithSimpleMap.class.getName());
post.setMethod("POST");
JSONObject obj = new JSONObject();
obj.put("id", 101);
obj.put("name", "Name of Object1");
Map<Integer, String> map = new HashMap<>();
map.put(new Integer(1), "First");
map.put(new Integer(2), "Second");
map.put(new Integer(3), "Third");
obj.put("map", new JSONObject(map));
post.setRequestContent(new ByteArrayBuffer(obj.toString().getBytes()));
// persist
client.start();
client.send(post);
post.waitForDone();
// validate
assertEquals(201, post.getResponseStatus());
assertNotNull(post.getResponseContent());
obj = new JSONObject(post.getResponseContent());
assertEquals(101, obj.getLong("id"));
try {
ContentExchange get = new ContentExchange();
get.setURL("http://localhost:" + PORT + "/dn/" + ClassWithSimpleMap.class.getName() + "/101?fetchGroup=all");
get.setMethod("GET");
client.send(get);
get.waitForDone();
assertEquals(200, get.getResponseStatus());
assertNotNull(get.getResponseContent());
obj = new JSONObject(get.getResponseContent());
Object mapObj = obj.get("map");
assertTrue(mapObj instanceof JSONObject);
JSONObject theMap = (JSONObject) mapObj;
assertEquals(3, theMap.length());
boolean[] present = new boolean[3];
present[0] = false;
present[1] = false;
present[2] = false;
Iterator keyIter = theMap.keys();
while (keyIter.hasNext()) {
Object key = keyIter.next();
Object val = theMap.get("" + key);
if (val.equals("First")) {
present[0] = true;
} else if (val.equals("Second")) {
present[1] = true;
} else if (val.equals("Third")) {
present[2] = true;
}
}
for (int i = 0; i < 3; i++) {
assertTrue("Map Key " + i + " is not present on retrieval", present[i]);
}
} catch (Exception e) {
LOG.error("Exception validating data", e);
fail("Exception validating data : " + e.getMessage());
}
} catch (Exception e) {
LOG.error("Exception persisting/checking data", e);
fail("Exception in persist/check of data : " + e.getMessage());
} finally {
try {
ContentExchange delete = new ContentExchange();
delete.setURL("http://localhost:" + PORT + "/dn/" + ClassWithSimpleMap.class.getName() + "/101");
delete.setMethod("DELETE");
client.send(delete);
delete.waitForDone();
assertEquals(204, delete.getResponseStatus());
assertNull(delete.getResponseContent());
} catch (Exception e) {
fail(e.getMessage());
}
}
}
use of org.datanucleus.api.rest.orgjson.JSONObject in project tests by datanucleus.
the class BasicTest method testPersistWithValueStrategy.
public void testPersistWithValueStrategy() throws IOException {
HttpClient client = new HttpClient();
String idValue = null;
try {
ContentExchange post = new ContentExchange();
post.setURL("http://localhost:" + PORT + "/dn/" + ClassWithValueStrategy.class.getName());
post.setMethod("POST");
// No need to set "id" since is generated
JSONObject obj = new JSONObject();
post.setRequestContent(new ByteArrayBuffer(obj.toString().getBytes()));
// persist
client.start();
client.send(post);
post.waitForDone();
// validate
assertEquals(201, post.getResponseStatus());
assertNotNull(post.getResponseContent());
obj = new JSONObject(post.getResponseContent());
idValue = obj.getString("id");
try {
ContentExchange get = new ContentExchange();
get.setURL("http://localhost:" + PORT + "/dn/" + ClassWithValueStrategy.class.getName() + "/" + idValue);
get.setMethod("GET");
client.send(get);
get.waitForDone();
assertEquals(200, get.getResponseStatus());
assertNotNull(get.getResponseContent());
obj = new JSONObject(get.getResponseContent());
} catch (Exception e) {
fail(e.getMessage());
}
} catch (Exception e) {
LOG.error("Exception in test", e);
fail("Exception in test : " + e.getMessage());
} finally {
try {
ContentExchange delete = new ContentExchange();
delete.setURL("http://localhost:" + PORT + "/dn/" + ClassWithValueStrategy.class.getName() + "/" + idValue);
delete.setMethod("DELETE");
client.send(delete);
delete.waitForDone();
assertEquals(204, delete.getResponseStatus());
assertNull(delete.getResponseContent());
} catch (Exception e) {
fail(e.getMessage());
}
}
}
Aggregations