コード例 #1
0
  @Test
  public void testMultiLevelJavaBeans() {
    String jsonString =
        "{"
            + " \"_id\" : \"1001\","
            + " \"first_name\" : \"John\","
            + " \"last_name\" : \"Doe\","
            + " \"active\" : false,"
            + " \"age\" : 35,"
            + " \"interests\" : [\"sports\",\"computers\"],"
            + " \"account\" : 1234.4321,"
            + " \"address\" : {"
            + "     \"street\" : \"1015 15th av\","
            + "     \"city\" : \"SFO\","
            + "     \"zip\" : 94065"
            + "    }"
            + "}";

    Document doc = Json.newDocument(jsonString);
    User user = doc.toJavaBean(User.class);
    Document userDoc = Json.newDocument(user);

    assertNotEquals(doc, userDoc);

    assertEquals(doc.getString("first_name"), userDoc.getString("first_name"));
    assertEquals(user.getFirstName(), userDoc.getString("first_name"));

    assertEquals(doc.getString("last_name"), userDoc.getString("last_name"));
    assertEquals(user.getLastName(), userDoc.getString("last_name"));

    assertEquals(doc.getDouble("age"), userDoc.getInt("age"), 0.0);
    assertEquals(user.getAge(), userDoc.getInt("age"), 0.0);

    assertEquals(doc.getString("address.city"), userDoc.getString("address.city"));
    assertEquals(user.getAddress().getTown(), userDoc.getString("address.city"));

    Document addressDoc = Json.newDocument(user.getAddress());
    assertEquals(addressDoc, userDoc.getMap("address"));
  }
コード例 #2
0
  @Test
  public void updateDocuments() throws IOException {

    // System.out.println("\t\tAdd address and status to jdoe");
    // System.out.println("before :\t" + mainColl.findById(new HValue("jdoe")));

    Document doc = mainColl.findById(new HValue("jdoe"));
    assertNull(doc.getMap("address"));

    // create a mutation
    DocumentMutation mutation =
        new HDocumentMutation()
            .set("active", true)
            .set("address.line", "1015 15th Avenue")
            .set("address.city", "Redwood City")
            .set("address.zip", 94065);

    mainColl.update(new HValue("jdoe"), mutation);
    mainColl.flush();

    // System.out.println("after :\t\t" + mainColl.findById(new HValue("jdoe")));

    doc = mainColl.findById(new HValue("jdoe"));
    assertEquals(3, doc.getMap("address").size());

    // System.out.println("\n\n\t\tAppend new interests to users");

    doc = mainColl.findById(new HValue("jdoe"));
    assertNull(doc.getList("interests"));
    doc = mainColl.findById(new HValue("mdupont"));
    assertEquals(3, doc.getList("interests").size());

    // create a mutation
    mutation =
        new HDocumentMutation().append("interests", Arrays.asList(new String[] {"development"}));

    mainColl.update(new HValue("jdoe"), mutation);
    mainColl.update(new HValue("mdupont"), mutation);
    mainColl.flush();

    // System.out.println("after :\t\t" + mainColl.findById(new HValue("jdoe"), "first_name",
    // "last_name", "interests"));
    // System.out.println("after :\t\t" + mainColl.findById(new HValue("mdupont"), "first_name",
    // "last_name", "interests"));

    doc = mainColl.findById(new HValue("jdoe"));
    assertEquals(1, doc.getList("interests").size());
    doc = mainColl.findById(new HValue("mdupont"));
    assertEquals(4, doc.getList("interests").size());

    // System.out.println("\n\n\t\tRemove attributes (dob)");
    // System.out.println("before :\t" + mainColl.findById(new HValue("jdoe")));

    doc = mainColl.findById(new HValue("jdoe"));
    assertTrue(doc.getDate("dob").compareTo(ODate.parse("1970-06-23")) == 0);

    // create a mutation
    mutation = new HDocumentMutation().delete("dob");
    mainColl.update(new HValue("jdoe"), mutation);
    mainColl.flush();

    // System.out.println("after :\t\t" + mainColl.findById(new HValue("jdoe")));

    doc = mainColl.findById(new HValue("jdoe"));
    assertNull(doc.getDate("dob"));
  }