Search in sources :

Example 6 with ContentExchange

use of org.mortbay.jetty.client.ContentExchange 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 7 with ContentExchange

use of org.mortbay.jetty.client.ContentExchange in project tests by datanucleus.

the class BasicTest method testOneToManyMapFK.

/**
 * Test of 1-N Map using FK.
 * @throws IOException
 */
public void testOneToManyMapFK() throws IOException {
    HttpClient client = new HttpClient();
    try {
        // Persist Office plus 2 computers
        ContentExchange post = new ContentExchange();
        post.setURL("http://localhost:" + PORT + "/dn/" + MapFKHolder.class.getName());
        post.setMethod("POST");
        JSONObject obj = new JSONObject();
        obj.put("id", 101);
        obj.put("name", "First Map Holder");
        Map map = new HashMap();
        JSONObject val1 = new JSONObject();
        val1.put("id", 1);
        val1.put("key", "First");
        val1.put("name", "First Value");
        val1.put("description", "The first description");
        map.put("First", val1);
        JSONObject val2 = new JSONObject();
        val2.put("id", 2);
        val2.put("key", "Second");
        val2.put("name", "Second Value");
        val2.put("description", "The second description");
        map.put("Second", val2);
        obj.put("map", map);
        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(101, obj.getLong("id"));
        try {
            // Retrieve objects to check persistence
            ContentExchange get = new ContentExchange();
            get.setURL("http://localhost:" + PORT + "/dn/" + MapFKHolder.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 mapObjs = obj.get("map");
            assertNotNull(mapObjs);
            assertTrue(mapObjs instanceof JSONObject);
            JSONObject mapObj = (JSONObject) mapObjs;
            assertEquals(2, mapObj.length());
            boolean firstPresent = false;
            boolean secondPresent = false;
            Iterator<String> mapKeys = mapObj.keys();
            while (mapKeys.hasNext()) {
                String mapKey = mapKeys.next();
                Object mapVal = mapObj.get(mapKey);
                assertTrue(mapVal instanceof JSONObject);
                JSONObject jsonVal = (JSONObject) mapVal;
                if (mapKey.equals("First")) {
                    assertEquals(1, jsonVal.getLong("id"));
                    assertEquals("First", jsonVal.getString("key"));
                    assertEquals("First Value", jsonVal.getString("name"));
                    assertEquals("The first description", jsonVal.getString("description"));
                    firstPresent = true;
                } else if (mapKey.equals("Second")) {
                    assertEquals(2, jsonVal.getLong("id"));
                    assertEquals("Second", jsonVal.getString("key"));
                    assertEquals("Second Value", jsonVal.getString("name"));
                    assertEquals("The second description", jsonVal.getString("description"));
                    secondPresent = true;
                }
            }
            assertTrue(firstPresent);
            assertTrue(secondPresent);
        } catch (Exception e) {
            LOG.error("Exception in validate", e);
            fail(e.getMessage());
        }
    } catch (Exception e) {
        LOG.error("Exception in persist", e);
        fail(e.getMessage());
    } finally {
        try {
            ContentExchange delete = new ContentExchange();
            delete.setURL("http://localhost:" + PORT + "/dn/" + MapFKHolder.class.getName() + "/101");
            delete.setMethod("DELETE");
            client.send(delete);
            delete.waitForDone();
            assertEquals(204, delete.getResponseStatus());
            assertNull(delete.getResponseContent());
            delete = new ContentExchange();
            delete.setURL("http://localhost:" + PORT + "/dn/" + MapFKValue.class.getName() + "/1");
            delete.setMethod("DELETE");
            client.send(delete);
            delete.waitForDone();
            assertEquals(204, delete.getResponseStatus());
            assertNull(delete.getResponseContent());
            delete = new ContentExchange();
            delete.setURL("http://localhost:" + PORT + "/dn/" + MapFKValue.class.getName() + "/2");
            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) MapFKHolder(org.jpox.samples.one_many.map_fk.MapFKHolder) MapFKValue(org.jpox.samples.one_many.map_fk.MapFKValue) JSONObject(org.datanucleus.api.rest.orgjson.JSONObject) HttpClient(org.mortbay.jetty.client.HttpClient) JSONObject(org.datanucleus.api.rest.orgjson.JSONObject) ClassWithSimpleMap(org.datanucleus.samples.ClassWithSimpleMap) HashMap(java.util.HashMap) Map(java.util.Map) ContentExchange(org.mortbay.jetty.client.ContentExchange)

Example 8 with ContentExchange

use of org.mortbay.jetty.client.ContentExchange in project tests by datanucleus.

the class BasicTest method testPersistSubclass.

public void testPersistSubclass() throws IOException {
    HttpClient client = new HttpClient();
    String globalNum = "global:1786244745";
    int personNum = 2;
    try {
        ContentExchange post = new ContentExchange();
        post.setURL("http://localhost:" + PORT + "/dn/" + Employee.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");
        obj.put("salary", 123.45);
        obj.put("serialNo", 12345);
        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/" + Employee.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());
        }
    } 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/" + Employee.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 9 with ContentExchange

use of org.mortbay.jetty.client.ContentExchange 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 10 with ContentExchange

use of org.mortbay.jetty.client.ContentExchange 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)

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