@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)); }
public class DocStoreDeleteEventTest { static EbeanServer server = Ebean.getDefaultServer(); <T> BeanType<T> beanType(Class<T> cls) { return server.getPluginApi().getBeanType(cls); } BeanType<Order> orderType() { return beanType(Order.class); } @Test @SuppressWarnings("unchecked") public void docStoreUpdate() throws Exception { BeanType<Order> mock = (BeanType<Order>) Mockito.mock(BeanType.class); BeanDocType<Order> mockDocType = (BeanDocType<Order>) Mockito.mock(BeanDocType.class); when(mock.docStore()).thenReturn(mockDocType); DocStoreDeleteEvent event = new DocStoreDeleteEvent(mock, 42); event.docStoreUpdate(null); verify(mock, times(1)).docStore(); verify(mockDocType, times(1)).deleteById(42, null); } @Test public void addToQueue() throws Exception { DocStoreDeleteEvent event = new DocStoreDeleteEvent(orderType(), 42); DocStoreUpdates updates = new DocStoreUpdates(); event.addToQueue(updates); List<DocStoreQueueEntry> queueEntries = updates.getQueueEntries(); assertThat(queueEntries).hasSize(1); DocStoreQueueEntry entry = queueEntries.get(0); StrictAssertions.assertThat(entry.getBeanId()).isEqualTo(42); StrictAssertions.assertThat(entry.getQueueId()).isEqualTo("order"); StrictAssertions.assertThat(entry.getPath()).isNull(); assertThat(entry.getType()).isEqualTo(DocStoreQueueEntry.Action.DELETE); } }
@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()); }