Search in sources :

Example 1 with JSONObject

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());
        }
    }
}
Also used : JSONObject(org.datanucleus.api.rest.orgjson.JSONObject) HttpClient(org.mortbay.jetty.client.HttpClient) ContentExchange(org.mortbay.jetty.client.ContentExchange) IOException(java.io.IOException) ByteArrayBuffer(org.mortbay.io.ByteArrayBuffer)

Example 2 with JSONObject

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 {
    }
}
Also used : JSONObject(org.datanucleus.api.rest.orgjson.JSONObject) ClassWithStringCollection(org.datanucleus.samples.ClassWithStringCollection) HttpClient(org.mortbay.jetty.client.HttpClient) ContentExchange(org.mortbay.jetty.client.ContentExchange) IOException(java.io.IOException) ByteArrayBuffer(org.mortbay.io.ByteArrayBuffer)

Example 3 with JSONObject

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());
        }
    }
}
Also used : JSONObject(org.datanucleus.api.rest.orgjson.JSONObject) HttpClient(org.mortbay.jetty.client.HttpClient) Network(org.jpox.samples.embedded.Network) JSONArray(org.datanucleus.api.rest.orgjson.JSONArray) JSONObject(org.datanucleus.api.rest.orgjson.JSONObject) ContentExchange(org.mortbay.jetty.client.ContentExchange) IOException(java.io.IOException) HashSet(java.util.HashSet) ByteArrayBuffer(org.mortbay.io.ByteArrayBuffer)

Example 4 with JSONObject

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());
        }
    }
}
Also used : HashMap(java.util.HashMap) IOException(java.io.IOException) ByteArrayBuffer(org.mortbay.io.ByteArrayBuffer) ClassWithSimpleMap(org.datanucleus.samples.ClassWithSimpleMap) JSONObject(org.datanucleus.api.rest.orgjson.JSONObject) HttpClient(org.mortbay.jetty.client.HttpClient) Iterator(java.util.Iterator) JSONObject(org.datanucleus.api.rest.orgjson.JSONObject) ContentExchange(org.mortbay.jetty.client.ContentExchange)

Example 5 with JSONObject

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());
        }
    }
}
Also used : JSONObject(org.datanucleus.api.rest.orgjson.JSONObject) HttpClient(org.mortbay.jetty.client.HttpClient) ContentExchange(org.mortbay.jetty.client.ContentExchange) IOException(java.io.IOException) ByteArrayBuffer(org.mortbay.io.ByteArrayBuffer)

Aggregations

IOException (java.io.IOException)14 JSONObject (org.datanucleus.api.rest.orgjson.JSONObject)14 ByteArrayBuffer (org.mortbay.io.ByteArrayBuffer)14 ContentExchange (org.mortbay.jetty.client.ContentExchange)14 HttpClient (org.mortbay.jetty.client.HttpClient)14 JSONArray (org.datanucleus.api.rest.orgjson.JSONArray)7 Person (org.jpox.samples.models.company.Person)4 HashSet (java.util.HashSet)3 ClassWithStringCollection (org.datanucleus.samples.ClassWithStringCollection)3 HashMap (java.util.HashMap)2 ClassWithSimpleMap (org.datanucleus.samples.ClassWithSimpleMap)2 Collection (java.util.Collection)1 Iterator (java.util.Iterator)1 Map (java.util.Map)1 Network (org.jpox.samples.embedded.Network)1 MapFKHolder (org.jpox.samples.one_many.map_fk.MapFKHolder)1 MapFKValue (org.jpox.samples.one_many.map_fk.MapFKValue)1 DesktopComputer (org.jpox.samples.one_many.unidir.DesktopComputer)1 LaptopComputer (org.jpox.samples.one_many.unidir.LaptopComputer)1 Office (org.jpox.samples.one_many.unidir.Office)1