Search in sources :

Example 1 with CRUDEvent

use of com.yahoo.elide.core.lifecycle.CRUDEvent in project elide by yahoo.

the class NotifyTopicLifeCycleHookTest method testManagedModelNotification.

@Test
public void testManagedModelNotification() {
    NotifyTopicLifeCycleHook<Book> bookHook = new NotifyTopicLifeCycleHook<Book>(connectionFactory, JMSContext::createProducer, new GsonBuilder().create());
    Book book = new Book();
    PersistentResource<Book> resource = new PersistentResource<>(book, "123", scope);
    bookHook.execute(LifeCycleHookBinding.Operation.CREATE, LifeCycleHookBinding.TransactionPhase.PRECOMMIT, new CRUDEvent(LifeCycleHookBinding.Operation.CREATE, resource, "", Optional.empty()));
    ArgumentCaptor<String> topicCaptor = ArgumentCaptor.forClass(String.class);
    verify(context).createTopic(topicCaptor.capture());
    assertEquals("bookAdded", topicCaptor.getValue());
    verify(producer, times(1)).send(isA(Destination.class), isA(String.class));
}
Also used : PersistentResource(com.yahoo.elide.core.PersistentResource) Destination(javax.jms.Destination) CRUDEvent(com.yahoo.elide.core.lifecycle.CRUDEvent) GsonBuilder(com.google.gson.GsonBuilder) Book(example.Book) JMSContext(javax.jms.JMSContext) Test(org.junit.jupiter.api.Test)

Example 2 with CRUDEvent

use of com.yahoo.elide.core.lifecycle.CRUDEvent in project elide by yahoo.

the class PersistentResourceTest method testUpdateComplexAttributeCloneWithHook.

@Test
public void testUpdateComplexAttributeCloneWithHook() {
    reset(bookUpdatePrice);
    Book book = new Book();
    Price originalPrice = new Price();
    originalPrice.setUnits(new BigDecimal(1.0));
    originalPrice.setCurrency(Currency.getInstance("USD"));
    book.setPrice(originalPrice);
    Map<String, Object> newPrice = new HashMap<>();
    newPrice.put("units", new BigDecimal(2.0));
    newPrice.put("currency", Currency.getInstance("CNY"));
    RequestScope goodScope = buildRequestScope(tx, goodUser);
    PersistentResource<Book> bookResource = new PersistentResource<>(book, "1", goodScope);
    bookResource.updateAttribute("price", newPrice);
    // check that original value was unmodified.
    assertEquals(Currency.getInstance("USD"), originalPrice.getCurrency());
    assertEquals(new BigDecimal(1.0), originalPrice.getUnits());
    // check that new value matches expected.
    assertEquals(Currency.getInstance("CNY"), book.getPrice().getCurrency());
    assertEquals(new BigDecimal(2.0), book.getPrice().getUnits());
    goodScope.saveOrCreateObjects();
    verify(tx, times(1)).save(book, goodScope);
    ArgumentCaptor<CRUDEvent> eventCapture = ArgumentCaptor.forClass(CRUDEvent.class);
    verify(bookUpdatePrice, times(1)).execute(eq(UPDATE), eq(PRESECURITY), eventCapture.capture());
    assertEquals(originalPrice, eventCapture.getValue().getChanges().get().getOriginal());
    assertEquals(book.getPrice(), eventCapture.getValue().getChanges().get().getModified());
}
Also used : CRUDEvent(com.yahoo.elide.core.lifecycle.CRUDEvent) Price(example.Price) HashMap(java.util.HashMap) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) Book(example.Book) PatchRequestScope(com.yahoo.elide.jsonapi.extensions.PatchRequestScope) BigDecimal(java.math.BigDecimal) Test(org.junit.jupiter.api.Test)

Aggregations

CRUDEvent (com.yahoo.elide.core.lifecycle.CRUDEvent)2 Book (example.Book)2 Test (org.junit.jupiter.api.Test)2 GsonBuilder (com.google.gson.GsonBuilder)1 PersistentResource (com.yahoo.elide.core.PersistentResource)1 PatchRequestScope (com.yahoo.elide.jsonapi.extensions.PatchRequestScope)1 Price (example.Price)1 BigDecimal (java.math.BigDecimal)1 HashMap (java.util.HashMap)1 Destination (javax.jms.Destination)1 JMSContext (javax.jms.JMSContext)1 MultivaluedHashMap (javax.ws.rs.core.MultivaluedHashMap)1