Search in sources :

Example 11 with HttpClient

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

Example 12 with HttpClient

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

the class BasicTest method testDatastoreIdPersistUpdate.

public void testDatastoreIdPersistUpdate() throws IOException {
    HttpClient client = new HttpClient();
    long idValue = 0;
    try {
        // Persist the object
        ContentExchange post = new ContentExchange();
        post.setURL("http://localhost:" + PORT + "/dn/" + ClassUsingDatastoreId.class.getName());
        post.setMethod("POST");
        JSONObject obj = new JSONObject();
        String name = "First Name";
        obj.put("name", name);
        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(name, obj.getString("name"));
        idValue = obj.getLong("_id");
        try {
            ContentExchange get = new ContentExchange();
            get.setURL("http://localhost:" + PORT + "/dn/" + ClassUsingDatastoreId.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());
        }
        // Update the object
        post = new ContentExchange();
        post.setURL("http://localhost:" + PORT + "/dn/" + ClassUsingDatastoreId.class.getName() + "/" + idValue);
        post.setMethod("PUT");
        obj = new JSONObject();
        obj.put("name", "Second Name");
        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("Second Name", obj.getString("name"));
        try {
            ContentExchange get = new ContentExchange();
            get.setURL("http://localhost:" + PORT + "/dn/" + ClassUsingDatastoreId.class.getName() + "/" + idValue);
            get.setMethod("GET");
            client.send(get);
            get.waitForDone();
            assertEquals(200, get.getResponseStatus());
            assertNotNull(get.getResponseContent());
            obj = new JSONObject(get.getResponseContent());
            assertEquals("Second Name", obj.getString("name"));
        } 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/" + ClassUsingDatastoreId.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)

Example 13 with HttpClient

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

the class BasicTest method testClassWithNonPersistableCollection.

public void testClassWithNonPersistableCollection() throws IOException {
    HttpClient client = new HttpClient();
    try {
        ContentExchange post = new ContentExchange();
        post.setURL("http://localhost:" + PORT + "/dn/" + ClassWithStringCollection.class.getName());
        post.setMethod("POST");
        JSONObject obj = new JSONObject();
        obj.put("id", 101);
        obj.put("name", "Name of Object1");
        Set<String> strings = new HashSet<String>();
        strings.add("FirstString");
        strings.add("SecondString");
        strings.add("ThirdString");
        obj.put("strings", strings);
        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/" + ClassWithStringCollection.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 stringsObj = obj.get("strings");
            assertTrue(stringsObj instanceof JSONArray);
            JSONArray stringsArr = (JSONArray) stringsObj;
            assertEquals(3, stringsArr.length());
            boolean[] present = new boolean[3];
            present[0] = false;
            present[1] = false;
            present[2] = false;
            for (int i = 0; i < stringsArr.length(); i++) {
                if (stringsArr.get(i).equals("FirstString")) {
                    present[0] = true;
                } else if (stringsArr.get(i).equals("SecondString")) {
                    present[1] = true;
                } else if (stringsArr.get(i).equals("ThirdString")) {
                    present[2] = true;
                }
            }
            for (int i = 0; i < 3; i++) {
                assertTrue("String " + 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/" + ClassWithStringCollection.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 : JSONArray(org.datanucleus.api.rest.orgjson.JSONArray) IOException(java.io.IOException) ByteArrayBuffer(org.mortbay.io.ByteArrayBuffer) JSONObject(org.datanucleus.api.rest.orgjson.JSONObject) ClassWithStringCollection(org.datanucleus.samples.ClassWithStringCollection) HttpClient(org.mortbay.jetty.client.HttpClient) JSONObject(org.datanucleus.api.rest.orgjson.JSONObject) ContentExchange(org.mortbay.jetty.client.ContentExchange) HashSet(java.util.HashSet)

Example 14 with HttpClient

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

the class BasicTest method testOneToManyJoin.

/**
 * Test of 1-N unidir join table relation persistence/retrieval.
 * @throws IOException
 */
public void testOneToManyJoin() throws IOException {
    HttpClient client = new HttpClient();
    try {
        // Persist Office plus 2 computers
        ContentExchange post = new ContentExchange();
        post.setURL("http://localhost:" + PORT + "/dn/" + Office.class.getName());
        post.setMethod("POST");
        JSONObject obj = new JSONObject();
        obj.put("name", "Headquarters");
        Collection<JSONObject> computers = new HashSet<JSONObject>();
        JSONObject dev1 = new JSONObject();
        dev1.put("class", DesktopComputer.class.getName());
        dev1.put("id", 1);
        dev1.put("ipAddress", "192.168.1.2");
        dev1.put("operatingSystem", "Linux");
        dev1.put("numberOfProcessors", 4);
        computers.add(dev1);
        JSONObject dev2 = new JSONObject();
        dev2.put("class", LaptopComputer.class.getName());
        dev2.put("id", 2);
        dev2.put("ipAddress", "192.168.1.3");
        dev2.put("operatingSystem", "Windows");
        dev2.put("batteryLife", 5);
        dev2.put("numberOfPcmcia", 0);
        computers.add(dev2);
        JSONArray jsonarr = new JSONArray(computers);
        obj.put("computers", jsonarr);
        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("Headquarters", obj.getString("name"));
        try {
            // Retrieve objects to check persistence
            ContentExchange get = new ContentExchange();
            get.setURL("http://localhost:" + PORT + "/dn/" + Office.class.getName() + "/Headquarters?fetchGroup=all&maxFetchDepth=2");
            get.setMethod("GET");
            client.send(get);
            get.waitForDone();
            assertEquals(200, get.getResponseStatus());
            assertNotNull(get.getResponseContent());
            obj = new JSONObject(get.getResponseContent());
            Object devObjs = obj.get("computers");
            assertNotNull(devObjs);
            assertTrue(devObjs instanceof JSONArray);
            JSONArray devArr = (JSONArray) devObjs;
            assertEquals(2, devArr.length());
            boolean laptopFound = false;
            boolean pcFound = false;
            for (int i = 0; i < devArr.length(); i++) {
                Object devObj = devArr.get(i);
                assertTrue(devObj instanceof JSONObject);
                JSONObject dev = (JSONObject) devObj;
                if (dev.getLong("id") == 1) {
                    assertEquals(DesktopComputer.class.getName(), dev.getString("class"));
                    assertEquals("192.168.1.2", dev.getString("ipAddress"));
                    assertEquals("Linux", dev.getString("operatingSystem"));
                    assertEquals(4, dev.getInt("numberOfProcessors"));
                    pcFound = true;
                } else if (dev.getLong("id") == 2) {
                    assertEquals(LaptopComputer.class.getName(), dev.getString("class"));
                    assertEquals("192.168.1.3", dev.getString("ipAddress"));
                    assertEquals("Windows", dev.getString("operatingSystem"));
                    assertEquals(5, dev.getLong("batteryLife"));
                    assertEquals(0, dev.getInt("numberOfPcmcia"));
                    laptopFound = true;
                }
            }
            assertTrue(pcFound);
            assertTrue(laptopFound);
        } catch (Exception e) {
            LOG.error("Exception in validate", e);
            fail(e.getMessage());
        }
        try {
            // Retrieve Office and add new Computer
            ContentExchange get = new ContentExchange();
            get.setURL("http://localhost:" + PORT + "/dn/" + Office.class.getName() + "/Headquarters?fetchGroup=all&maxFetchDepth=2");
            get.setMethod("GET");
            client.send(get);
            get.waitForDone();
            assertEquals(200, get.getResponseStatus());
            assertNotNull(get.getResponseContent());
            obj = new JSONObject(get.getResponseContent());
            Object devObjs = obj.get("computers");
            assertNotNull(devObjs);
            assertTrue(devObjs instanceof JSONArray);
            JSONArray devArr = (JSONArray) devObjs;
            Collection coll = new HashSet();
            for (int i = 0; i < devArr.length(); i++) {
                coll.add(devArr.get(i));
            }
            JSONObject dev3 = new JSONObject();
            dev3.put("class", LaptopComputer.class.getName());
            dev3.put("id", 3);
            dev3.put("ipAddress", "192.168.1.4");
            dev3.put("operatingSystem", "Linux");
            dev3.put("batteryLife", 8);
            dev3.put("numberOfPcmcia", 0);
            coll.add(dev3);
            obj.put("computers", coll);
            post = new ContentExchange();
            post.setURL("http://localhost:" + PORT + "/dn/" + Office.class.getName() + "/Headquarters");
            post.setMethod("POST");
            post.setRequestContent(new ByteArrayBuffer(obj.toString().getBytes()));
            client.start();
            client.send(post);
            post.waitForDone();
            // validate
            assertEquals(201, post.getResponseStatus());
            assertNotNull(post.getResponseContent());
        } catch (Exception e) {
            LOG.error("Exception in update", e);
            fail(e.getMessage());
        }
        try {
            // Retrieve objects to check persistence
            ContentExchange get = new ContentExchange();
            get.setURL("http://localhost:" + PORT + "/dn/" + Office.class.getName() + "/Headquarters?fetchGroup=all&maxFetchDepth=2");
            get.setMethod("GET");
            client.send(get);
            get.waitForDone();
            assertEquals(200, get.getResponseStatus());
            assertNotNull(get.getResponseContent());
            obj = new JSONObject(get.getResponseContent());
            Object devObjs = obj.get("computers");
            assertNotNull(devObjs);
            assertTrue(devObjs instanceof JSONArray);
            JSONArray devArr = (JSONArray) devObjs;
            assertEquals(3, devArr.length());
            boolean laptopFound = false;
            boolean laptop2Found = false;
            boolean pcFound = false;
            for (int i = 0; i < devArr.length(); i++) {
                Object devObj = devArr.get(i);
                assertTrue(devObj instanceof JSONObject);
                JSONObject dev = (JSONObject) devObj;
                if (dev.getLong("id") == 1) {
                    assertEquals(DesktopComputer.class.getName(), dev.getString("class"));
                    assertEquals("192.168.1.2", dev.getString("ipAddress"));
                    assertEquals("Linux", dev.getString("operatingSystem"));
                    assertEquals(4, dev.getInt("numberOfProcessors"));
                    pcFound = true;
                } else if (dev.getLong("id") == 2) {
                    assertEquals(LaptopComputer.class.getName(), dev.getString("class"));
                    assertEquals("192.168.1.3", dev.getString("ipAddress"));
                    assertEquals("Windows", dev.getString("operatingSystem"));
                    assertEquals(5, dev.getLong("batteryLife"));
                    assertEquals(0, dev.getInt("numberOfPcmcia"));
                    laptopFound = true;
                } else if (dev.getLong("id") == 3) {
                    assertEquals(LaptopComputer.class.getName(), dev.getString("class"));
                    assertEquals("192.168.1.4", dev.getString("ipAddress"));
                    assertEquals("Linux", dev.getString("operatingSystem"));
                    assertEquals(8, dev.getLong("batteryLife"));
                    assertEquals(0, dev.getInt("numberOfPcmcia"));
                    laptop2Found = true;
                }
            }
            assertTrue(pcFound);
            assertTrue(laptopFound);
            assertTrue(laptop2Found);
        } 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/" + Office.class.getName() + "/Headquarters");
            delete.setMethod("DELETE");
            client.send(delete);
            delete.waitForDone();
            assertEquals(204, delete.getResponseStatus());
            assertNull(delete.getResponseContent());
            delete = new ContentExchange();
            delete.setURL("http://localhost:" + PORT + "/dn/" + LaptopComputer.class.getName() + "/2");
            delete.setMethod("DELETE");
            client.send(delete);
            delete.waitForDone();
            assertEquals(204, delete.getResponseStatus());
            assertNull(delete.getResponseContent());
            delete = new ContentExchange();
            delete.setURL("http://localhost:" + PORT + "/dn/" + DesktopComputer.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/" + LaptopComputer.class.getName() + "/3");
            delete.setMethod("DELETE");
            client.send(delete);
            delete.waitForDone();
            assertEquals(204, delete.getResponseStatus());
            assertNull(delete.getResponseContent());
        } catch (Exception e) {
            fail(e.getMessage());
        }
    }
}
Also used : Office(org.jpox.samples.one_many.unidir.Office) DesktopComputer(org.jpox.samples.one_many.unidir.DesktopComputer) JSONArray(org.datanucleus.api.rest.orgjson.JSONArray) IOException(java.io.IOException) ByteArrayBuffer(org.mortbay.io.ByteArrayBuffer) JSONObject(org.datanucleus.api.rest.orgjson.JSONObject) LaptopComputer(org.jpox.samples.one_many.unidir.LaptopComputer) HttpClient(org.mortbay.jetty.client.HttpClient) Collection(java.util.Collection) ClassWithStringCollection(org.datanucleus.samples.ClassWithStringCollection) JSONObject(org.datanucleus.api.rest.orgjson.JSONObject) ContentExchange(org.mortbay.jetty.client.ContentExchange) HashSet(java.util.HashSet)

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