@Test
  public void test_toObject() throws Exception {

    EbeanServer server = Ebean.getDefaultServer();

    JsonContext json = server.json();

    Customer customer = new Customer();
    customer.setId(1);
    customer.setName("Jim");

    String asJson = json.toJson(customer);

    Object bean = json.toObject(Customer.class, asJson);

    assertTrue(bean instanceof Customer);
    assertEquals(Integer.valueOf(1), ((Customer) bean).getId());
    assertEquals("Jim", ((Customer) bean).getName());

    StringReader reader = new StringReader(asJson);
    bean = json.toObject(Customer.class, reader);
    assertTrue(bean instanceof Customer);
    assertEquals(Integer.valueOf(1), ((Customer) bean).getId());
    assertEquals("Jim", ((Customer) bean).getName());
  }
  @Test
  public void testCreateGenerator() throws Exception {

    EbeanServer server = Ebean.getDefaultServer();

    StringWriter writer = new StringWriter();
    JsonContext json = server.json();
    JsonGenerator generator = json.createGenerator(writer);

    Customer customer = new Customer();
    customer.setId(1);
    customer.setName("Jim");

    // we can use the generator before and after our json.toJson() call
    // ... confirming we are not closing the generator
    generator.writeStartArray();
    json.toJson(customer, generator, PathProperties.parse("id,name"));
    generator.writeEndArray();
    generator.close();

    String jsonString = writer.toString();
    assertTrue(jsonString, jsonString.startsWith("["));
    assertTrue(jsonString, jsonString.endsWith("]"));
    assertTrue(jsonString, jsonString.contains("{\"id\":1,\"name\":\"Jim\"}"));
  }
  @Test
  public void testIsSupportedType() throws Exception {

    EbeanServer server = Ebean.getDefaultServer();

    JsonContext json = server.json();
    assertTrue(json.isSupportedType(Customer.class));
    assertFalse(json.isSupportedType(System.class));
  }
Esempio n. 4
0
  @Ignore
  @Test
  public void testBulkUpdate() throws IOException {

    ResetBasicData.reset();

    EbeanServer server = Ebean.getServer(null);
    JsonContext jsonContext = server.json();

    StringWriter writer = new StringWriter();
    JsonGenerator generator = jsonContext.createGenerator(writer);

    List<Customer> customers = Ebean.find(Customer.class).findList();

    for (Customer customer : customers) {

      customer.setName(customer.getName() + "esMod");
      PathProperties updateProps = PathProperties.parse("name");

      generator.writeStartObject();
      generator.writeFieldName("update");
      generator.writeStartObject();
      generator.writeStringField("_id", customer.getId().toString());
      generator.writeStringField("_type", "customer");
      generator.writeStringField("_index", "customer");
      generator.writeEndObject();
      generator.writeEndObject();
      generator.writeRaw("\n");

      generator.writeStartObject();
      generator.writeFieldName("doc");
      jsonContext.toJson(customer, generator, updateProps);
      generator.writeEndObject();
      generator.writeRaw("\n");
    }

    generator.close();
    String json = writer.toString();

    post("http://localhost:9200/_bulk", json);

    // curl -s -XPOST localhost:9200/_bulk

    //    { "update" : {"_id" : "1", "_type" : "type1", "_index" : "index1"} }
    //    { "doc" : {"field2" : "value2"} }

  }
Esempio n. 5
0
  @Ignore
  @Test
  public void test() throws IOException {

    ResetBasicData.reset();

    EbeanServer server = Ebean.getServer(null);
    JsonContext jsonContext = server.json();

    List<Customer> customers = Ebean.find(Customer.class).findList();

    PathProperties paths = PathProperties.parse("name, status, anniversary");
    for (Customer customer : customers) {

      String json = jsonContext.toJson(customer, paths);
      put("http://localhost:9200/customer/customer/" + customer.getId(), json);
    }
  }
  @Test
  public void testCreateGenerator_writeRaw() throws Exception {

    EbeanServer server = Ebean.getDefaultServer();

    StringWriter writer = new StringWriter();
    JsonContext json = server.json();
    JsonGenerator generator = json.createGenerator(writer);

    // test that we can write anything via writeRaw()
    generator.writeRaw("START");
    generator.writeStartArray();
    generator.writeStartObject();
    generator.writeNumberField("count", 12);
    generator.writeEndObject();
    generator.writeEndArray();
    generator.writeRaw("END");
    generator.close();

    assertEquals("START[{\"count\":12}]END", writer.toString());
  }