Search in sources :

Example 1 with JSONArray

use of org.datanucleus.api.rest.orgjson.JSONArray 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 2 with JSONArray

use of org.datanucleus.api.rest.orgjson.JSONArray in project tests by datanucleus.

the class BasicTest method testPersist.

public void testPersist() throws IOException {
    HttpClient client = new HttpClient();
    String globalNum = "global:1786244744";
    int personNum = 1;
    try {
        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()));
        // persist
        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) {
            LOG.error("Exception in test", e);
            fail(e.getMessage());
        }
        try {
            ContentExchange get = new ContentExchange();
            get.setURL("http://localhost:" + PORT + "/dn/" + Person.class.getName() + "?fetchGroup=all");
            get.setMethod("GET");
            client.send(get);
            get.waitForDone();
            assertEquals(200, get.getResponseStatus());
            assertNotNull(get.getResponseContent());
            JSONArray arr = new JSONArray(get.getResponseContent());
            assertEquals(1, arr.length());
        } catch (Exception e) {
            LOG.error("Exception in test", 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) JSONArray(org.datanucleus.api.rest.orgjson.JSONArray) Person(org.jpox.samples.models.company.Person) ContentExchange(org.mortbay.jetty.client.ContentExchange) IOException(java.io.IOException) ByteArrayBuffer(org.mortbay.io.ByteArrayBuffer)

Example 3 with JSONArray

use of org.datanucleus.api.rest.orgjson.JSONArray in project tests by datanucleus.

the class BasicTest method testPersistAndQuery.

public void testPersistAndQuery() throws IOException {
    HttpClient client = new HttpClient();
    String globalNum = "global:1786244744";
    int personNum = 1;
    try {
        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()));
        // persist
        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();
            String encodedQuery = URLEncoder.encode("SELECT FROM " + Person.class.getName() + " WHERE firstName == 'firstName' && lastName == 'lastName'", "UTF-8");
            get.setURL("http://localhost:" + PORT + "/dn/jdoql?query=" + encodedQuery + "&fetchGroup=all");
            get.setMethod("GET");
            client.send(get);
            get.waitForDone();
            assertEquals(200, get.getResponseStatus());
            assertNotNull(get.getResponseContent());
            JSONArray arr = new JSONArray(get.getResponseContent());
            assertEquals(1, arr.length());
        } 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) JSONArray(org.datanucleus.api.rest.orgjson.JSONArray) Person(org.jpox.samples.models.company.Person) ContentExchange(org.mortbay.jetty.client.ContentExchange) IOException(java.io.IOException) ByteArrayBuffer(org.mortbay.io.ByteArrayBuffer)

Example 4 with JSONArray

use of org.datanucleus.api.rest.orgjson.JSONArray in project tests by datanucleus.

the class BasicTest method testPersistAndJPQLQuery.

public void testPersistAndJPQLQuery() throws IOException {
    HttpClient client = new HttpClient();
    String globalNum = "global:1786244744";
    int personNum = 1;
    try {
        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()));
        // persist
        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();
            String encodedQuery = URLEncoder.encode("SELECT p FROM " + Person.class.getName() + " p", "UTF-8");
            get.setURL("http://localhost:" + PORT + "/dn/jpql?query=" + encodedQuery);
            get.setMethod("GET");
            client.send(get);
            get.waitForDone();
            assertEquals(200, get.getResponseStatus());
            assertNotNull(get.getResponseContent());
            JSONArray arr = new JSONArray(get.getResponseContent());
            assertEquals(1, arr.length());
        } 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) JSONArray(org.datanucleus.api.rest.orgjson.JSONArray) Person(org.jpox.samples.models.company.Person) ContentExchange(org.mortbay.jetty.client.ContentExchange) IOException(java.io.IOException) ByteArrayBuffer(org.mortbay.io.ByteArrayBuffer)

Example 5 with JSONArray

use of org.datanucleus.api.rest.orgjson.JSONArray in project tests by datanucleus.

the class BasicTest method testBulkDelete.

public void testBulkDelete() throws IOException {
    HttpClient client = new HttpClient();
    String globalNum = "global:1786244744";
    try {
        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", 1);
        obj.put("lastName", "lastName");
        obj.put("age", 0);
        obj.put("emailAddress", "email");
        obj.put("firstName", "firstName");
        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(globalNum, obj.getString("globalNum"));
        assertEquals(1, obj.getLong("personNum"));
        post = new ContentExchange();
        post.setURL("http://localhost:" + PORT + "/dn/" + Person.class.getName());
        post.setMethod("POST");
        obj = new JSONObject();
        obj.put("globalNum", globalNum);
        obj.put("personNum", 2);
        obj.put("lastName", "lastName");
        obj.put("age", 15);
        obj.put("emailAddress", "email");
        obj.put("firstName", "firstName");
        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(globalNum, obj.getString("globalNum"));
        assertEquals(2, obj.getLong("personNum"));
        // Do bulk delete of both objects
        try {
            ContentExchange delete = new ContentExchange();
            delete.setURL("http://localhost:" + PORT + "/dn/" + Person.class.getName());
            delete.setMethod("DELETE");
            client.send(delete);
            delete.waitForDone();
            assertEquals(204, delete.getResponseStatus());
            assertNull(delete.getResponseContent());
        } catch (Exception e) {
            fail(e.getMessage());
        }
        // Check results
        try {
            ContentExchange get = new ContentExchange();
            String encodedQuery = URLEncoder.encode("SELECT FROM " + Person.class.getName(), "UTF-8");
            get.setURL("http://localhost:" + PORT + "/dn/jdoql?query=" + encodedQuery);
            get.setMethod("GET");
            client.send(get);
            get.waitForDone();
            assertEquals(200, get.getResponseStatus());
            assertNotNull(get.getResponseContent());
            JSONArray arr = new JSONArray(get.getResponseContent());
            assertEquals(0, arr.length());
        } catch (Exception e) {
            try {
                ContentExchange delete = new ContentExchange();
                delete.setURL("http://localhost:" + PORT + "/dn/" + Person.class.getName());
                delete.setMethod("DELETE");
                obj = new JSONObject();
                obj.put("globalNum", globalNum);
                obj.put("personNum", 1);
                delete.setRequestContent(new ByteArrayBuffer(obj.toString().getBytes()));
                client.send(delete);
                delete.waitForDone();
                delete = new ContentExchange();
                delete.setURL("http://localhost:" + PORT + "/dn/" + Person.class.getName());
                delete.setMethod("DELETE");
                obj = new JSONObject();
                obj.put("globalNum", globalNum);
                obj.put("personNum", 2);
                delete.setRequestContent(new ByteArrayBuffer(obj.toString().getBytes()));
                client.send(delete);
                delete.waitForDone();
            } catch (Exception e2) {
                fail(e2.getMessage());
            }
            fail(e.getMessage());
        }
    } catch (Exception e) {
        LOG.error("Exception in test", e);
        fail("Exception in test : " + e.getMessage());
    } finally {
    }
}
Also used : JSONObject(org.datanucleus.api.rest.orgjson.JSONObject) HttpClient(org.mortbay.jetty.client.HttpClient) JSONArray(org.datanucleus.api.rest.orgjson.JSONArray) Person(org.jpox.samples.models.company.Person) ContentExchange(org.mortbay.jetty.client.ContentExchange) IOException(java.io.IOException) ByteArrayBuffer(org.mortbay.io.ByteArrayBuffer)

Aggregations

IOException (java.io.IOException)7 JSONArray (org.datanucleus.api.rest.orgjson.JSONArray)7 JSONObject (org.datanucleus.api.rest.orgjson.JSONObject)7 ByteArrayBuffer (org.mortbay.io.ByteArrayBuffer)7 ContentExchange (org.mortbay.jetty.client.ContentExchange)7 HttpClient (org.mortbay.jetty.client.HttpClient)7 Person (org.jpox.samples.models.company.Person)4 HashSet (java.util.HashSet)3 ClassWithStringCollection (org.datanucleus.samples.ClassWithStringCollection)2 Collection (java.util.Collection)1 Network (org.jpox.samples.embedded.Network)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