Search in sources :

Example 6 with JSONArray

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

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