Search in sources :

Example 6 with DataStoreTransaction

use of com.yahoo.elide.core.datastore.DataStoreTransaction in project elide by yahoo.

the class LifeCycleTest method testAddToCollectionTrigger.

@Test
public void testAddToCollectionTrigger() {
    PropertyTestModel mockModel = mock(PropertyTestModel.class);
    DataStoreTransaction tx = mock(DataStoreTransaction.class);
    RequestScope scope = buildRequestScope(dictionary, tx);
    when(tx.createNewObject(ClassType.of(PropertyTestModel.class), scope)).thenReturn(mockModel);
    PropertyTestModel modelToAdd1 = mock(PropertyTestModel.class);
    PropertyTestModel modelToAdd2 = mock(PropertyTestModel.class);
    // First we test adding to a newly created object.
    PersistentResource resource = PersistentResource.createObject(ClassType.of(PropertyTestModel.class), scope, Optional.of("1"));
    PersistentResource resourceToAdd1 = new PersistentResource(modelToAdd1, scope.getUUIDFor(mockModel), scope);
    PersistentResource resourceToAdd2 = new PersistentResource(modelToAdd2, scope.getUUIDFor(mockModel), scope);
    resource.updateRelation("models", new HashSet<>(Arrays.asList(resourceToAdd1, resourceToAdd2)));
    scope.runQueuedPreSecurityTriggers();
    scope.runQueuedPreCommitTriggers();
    scope.runQueuedPostCommitTriggers();
    verify(mockModel, never()).relationCallback(eq(UPDATE), any(), any());
    verify(mockModel, times(1)).relationCallback(eq(CREATE), eq(POSTCOMMIT), notNull());
    // Build another resource, scope & reset the mock to do a pure update (no create):
    scope = buildRequestScope(dictionary, tx);
    resource = new PersistentResource(mockModel, scope.getUUIDFor(mockModel), scope);
    reset(mockModel);
    resource.updateRelation("models", new HashSet<>(Arrays.asList(resourceToAdd1, resourceToAdd2)));
    scope.runQueuedPreSecurityTriggers();
    scope.runQueuedPreCommitTriggers();
    scope.runQueuedPostCommitTriggers();
    verify(mockModel, never()).relationCallback(eq(CREATE), any(), any());
    verify(mockModel, times(1)).relationCallback(eq(UPDATE), eq(POSTCOMMIT), notNull());
}
Also used : PersistentResource(com.yahoo.elide.core.PersistentResource) DataStoreTransaction(com.yahoo.elide.core.datastore.DataStoreTransaction) RequestScope(com.yahoo.elide.core.RequestScope) Test(org.junit.jupiter.api.Test)

Example 7 with DataStoreTransaction

use of com.yahoo.elide.core.datastore.DataStoreTransaction in project elide by yahoo.

the class LifeCycleTest method testLegacyElidePatchExtensionCreate.

@Test
public void testLegacyElidePatchExtensionCreate() throws Exception {
    DataStore store = mock(DataStore.class);
    DataStoreTransaction tx = mock(DataStoreTransaction.class);
    LegacyTestModel mockModel = mock(LegacyTestModel.class);
    Elide elide = getElide(store, dictionary, MOCK_AUDIT_LOGGER);
    String body = "[{\"op\": \"add\",\"path\": \"/legacyTestModel\",\"value\":{" + "\"type\":\"legacyTestModel\",\"id\": \"1\",\"attributes\": {\"field\":\"Foo\"}}}]";
    when(store.beginTransaction()).thenReturn(tx);
    when(tx.createNewObject(eq(ClassType.of(LegacyTestModel.class)), any())).thenReturn(mockModel);
    String contentType = JSONAPI_CONTENT_TYPE_WITH_JSON_PATCH_EXTENSION;
    ElideResponse response = elide.patch(baseUrl, contentType, contentType, "/", body, null, NO_VERSION);
    assertEquals(HttpStatus.SC_OK, response.getResponseCode());
    verify(mockModel, times(1)).classCreatePreSecurity();
    verify(mockModel, times(1)).classCreatePreCommit();
    verify(mockModel, times(1)).classCreatePreCommitAllUpdates();
    verify(mockModel, times(1)).classCreatePostCommit();
    verify(mockModel, times(3)).classMultiple();
    verify(mockModel, never()).classUpdatePreCommit();
    verify(mockModel, never()).classUpdatePostCommit();
    verify(mockModel, never()).classUpdatePreSecurity();
    verify(mockModel, never()).classDeletePreCommit();
    verify(mockModel, never()).classDeletePostCommit();
    verify(mockModel, never()).classDeletePreSecurity();
    verify(mockModel, times(1)).fieldCreatePostCommit();
    verify(mockModel, times(1)).fieldCreatePreCommit();
    verify(mockModel, times(1)).fieldCreatePreSecurity();
    verify(mockModel, times(3)).fieldMultiple();
    verify(mockModel, never()).fieldUpdatePreCommit();
    verify(mockModel, never()).fieldUpdatePostCommit();
    verify(mockModel, never()).fieldUpdatePreSecurity();
    verify(tx).preCommit(any());
    verify(tx, times(1)).createObject(eq(mockModel), isA(RequestScope.class));
    verify(tx).flush(isA(RequestScope.class));
    verify(tx).commit(isA(RequestScope.class));
    verify(tx).close();
}
Also used : ElideResponse(com.yahoo.elide.ElideResponse) DataStore(com.yahoo.elide.core.datastore.DataStore) DataStoreTransaction(com.yahoo.elide.core.datastore.DataStoreTransaction) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Elide(com.yahoo.elide.Elide) RequestScope(com.yahoo.elide.core.RequestScope) Test(org.junit.jupiter.api.Test)

Example 8 with DataStoreTransaction

use of com.yahoo.elide.core.datastore.DataStoreTransaction in project elide by yahoo.

the class LifeCycleTest method testElideCreate.

@Test
public void testElideCreate() throws Exception {
    DataStore store = mock(DataStore.class);
    DataStoreTransaction tx = mock(DataStoreTransaction.class);
    FieldTestModel mockModel = mock(FieldTestModel.class);
    Elide elide = getElide(store, dictionary, MOCK_AUDIT_LOGGER);
    String body = "{\"data\": {\"type\":\"testModel\",\"id\":\"1\",\"attributes\": {\"field\":\"Foo\"}}}";
    when(store.beginTransaction()).thenReturn(tx);
    when(tx.createNewObject(eq(ClassType.of(FieldTestModel.class)), any())).thenReturn(mockModel);
    ElideResponse response = elide.post(baseUrl, "/testModel", body, null, NO_VERSION);
    assertEquals(HttpStatus.SC_CREATED, response.getResponseCode());
    verify(mockModel, times(1)).classCallback(eq(CREATE), eq(PRESECURITY));
    verify(mockModel, times(1)).classCallback(eq(CREATE), eq(PREFLUSH));
    verify(mockModel, times(1)).classCallback(eq(CREATE), eq(PRECOMMIT));
    verify(mockModel, times(1)).classCallback(eq(CREATE), eq(POSTCOMMIT));
    verify(mockModel, never()).classCallback(eq(UPDATE), any());
    verify(mockModel, never()).classCallback(eq(DELETE), any());
    verify(mockModel, times(2)).classAllFieldsCallback(any(), any());
    verify(mockModel, times(2)).classAllFieldsCallback(eq(CREATE), eq(PRECOMMIT));
    verify(mockModel, times(1)).attributeCallback(eq(CREATE), eq(PRESECURITY), any());
    verify(mockModel, times(1)).attributeCallback(eq(CREATE), eq(PREFLUSH), any());
    verify(mockModel, times(1)).attributeCallback(eq(CREATE), eq(PRECOMMIT), any());
    verify(mockModel, times(1)).attributeCallback(eq(CREATE), eq(POSTCOMMIT), any());
    verify(mockModel, never()).attributeCallback(eq(UPDATE), any(), any());
    verify(mockModel, never()).attributeCallback(eq(DELETE), any(), any());
    verify(mockModel, times(1)).relationCallback(eq(CREATE), eq(PRESECURITY), any());
    verify(mockModel, times(1)).relationCallback(eq(CREATE), eq(PREFLUSH), any());
    verify(mockModel, times(1)).relationCallback(eq(CREATE), eq(PRECOMMIT), any());
    verify(mockModel, times(1)).relationCallback(eq(CREATE), eq(POSTCOMMIT), any());
    verify(mockModel, never()).relationCallback(eq(UPDATE), any(), any());
    verify(mockModel, never()).relationCallback(eq(DELETE), any(), any());
    verify(tx).preCommit(any());
    verify(tx, times(1)).createObject(eq(mockModel), isA(RequestScope.class));
    verify(tx).flush(isA(RequestScope.class));
    verify(tx).commit(isA(RequestScope.class));
    verify(tx).close();
}
Also used : ElideResponse(com.yahoo.elide.ElideResponse) DataStore(com.yahoo.elide.core.datastore.DataStore) DataStoreTransaction(com.yahoo.elide.core.datastore.DataStoreTransaction) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Elide(com.yahoo.elide.Elide) RequestScope(com.yahoo.elide.core.RequestScope) Test(org.junit.jupiter.api.Test)

Example 9 with DataStoreTransaction

use of com.yahoo.elide.core.datastore.DataStoreTransaction in project elide by yahoo.

the class LifeCycleTest method testElideGet.

@Test
public void testElideGet() throws Exception {
    DataStore store = mock(DataStore.class);
    DataStoreTransaction tx = mock(DataStoreTransaction.class);
    FieldTestModel mockModel = mock(FieldTestModel.class);
    Elide elide = getElide(store, dictionary, MOCK_AUDIT_LOGGER);
    when(store.beginReadTransaction()).thenCallRealMethod();
    when(store.beginTransaction()).thenReturn(tx);
    when(tx.loadObject(isA(EntityProjection.class), any(), isA(RequestScope.class))).thenReturn(mockModel);
    MultivaluedMap<String, String> queryParams = new MultivaluedHashMap<>();
    ElideResponse response = elide.get(baseUrl, "/testModel/1", queryParams, null, NO_VERSION);
    assertEquals(HttpStatus.SC_OK, response.getResponseCode());
    verify(mockModel, never()).classAllFieldsCallback(any(), any());
    verify(mockModel, never()).classCallback(eq(CREATE), any());
    verify(mockModel, never()).classCallback(eq(UPDATE), any());
    verify(mockModel, never()).classCallback(eq(DELETE), any());
    verify(mockModel, never()).attributeCallback(eq(CREATE), any(), any());
    verify(mockModel, never()).attributeCallback(eq(UPDATE), any(), any());
    verify(mockModel, never()).attributeCallback(eq(DELETE), any(), any());
    verify(mockModel, never()).relationCallback(eq(CREATE), any(), any());
    verify(mockModel, never()).relationCallback(eq(UPDATE), any(), any());
    verify(mockModel, never()).relationCallback(eq(DELETE), any(), any());
    verify(tx).preCommit(any());
    verify(tx).flush(any());
    verify(tx).commit(any());
    verify(tx).close();
}
Also used : MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) EntityProjection(com.yahoo.elide.core.request.EntityProjection) ElideResponse(com.yahoo.elide.ElideResponse) DataStore(com.yahoo.elide.core.datastore.DataStore) DataStoreTransaction(com.yahoo.elide.core.datastore.DataStoreTransaction) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Elide(com.yahoo.elide.Elide) RequestScope(com.yahoo.elide.core.RequestScope) Test(org.junit.jupiter.api.Test)

Example 10 with DataStoreTransaction

use of com.yahoo.elide.core.datastore.DataStoreTransaction in project elide by yahoo.

the class LifeCycleTest method testElideGetSparse.

@Test
public void testElideGetSparse() throws Exception {
    DataStore store = mock(DataStore.class);
    DataStoreTransaction tx = mock(DataStoreTransaction.class);
    FieldTestModel mockModel = mock(FieldTestModel.class);
    Elide elide = getElide(store, dictionary, MOCK_AUDIT_LOGGER);
    when(store.beginReadTransaction()).thenCallRealMethod();
    when(store.beginTransaction()).thenReturn(tx);
    when(tx.loadObject(isA(EntityProjection.class), any(), isA(RequestScope.class))).thenReturn(mockModel);
    MultivaluedMap<String, String> queryParams = new MultivaluedHashMap<>();
    queryParams.putSingle("fields[testModel]", "field");
    ElideResponse response = elide.get(baseUrl, "/testModel/1", queryParams, null, NO_VERSION);
    assertEquals(HttpStatus.SC_OK, response.getResponseCode());
    verify(mockModel, never()).classAllFieldsCallback(any(), any());
    verify(mockModel, never()).classCallback(eq(CREATE), any());
    verify(mockModel, never()).classCallback(eq(UPDATE), any());
    verify(mockModel, never()).classCallback(eq(DELETE), any());
    verify(mockModel, never()).attributeCallback(eq(CREATE), any(), any());
    verify(mockModel, never()).attributeCallback(eq(UPDATE), any(), any());
    verify(mockModel, never()).attributeCallback(eq(DELETE), any(), any());
    verify(mockModel, never()).relationCallback(any(), any(), any());
    verify(tx).preCommit(any());
    verify(tx).flush(any());
    verify(tx).commit(any());
    verify(tx).close();
}
Also used : MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) EntityProjection(com.yahoo.elide.core.request.EntityProjection) ElideResponse(com.yahoo.elide.ElideResponse) DataStore(com.yahoo.elide.core.datastore.DataStore) DataStoreTransaction(com.yahoo.elide.core.datastore.DataStoreTransaction) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Elide(com.yahoo.elide.Elide) RequestScope(com.yahoo.elide.core.RequestScope) Test(org.junit.jupiter.api.Test)

Aggregations

DataStoreTransaction (com.yahoo.elide.core.datastore.DataStoreTransaction)139 Test (org.junit.jupiter.api.Test)101 RequestScope (com.yahoo.elide.core.RequestScope)40 DataStore (com.yahoo.elide.core.datastore.DataStore)30 FilterExpression (com.yahoo.elide.core.filter.expression.FilterExpression)28 Elide (com.yahoo.elide.Elide)27 ElideResponse (com.yahoo.elide.ElideResponse)22 EntityProjection (com.yahoo.elide.core.request.EntityProjection)22 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)18 PersistentResource (com.yahoo.elide.core.PersistentResource)17 Item (com.yahoo.elide.datastores.search.models.Item)17 Book (example.Book)13 MultivaluedHashMap (javax.ws.rs.core.MultivaluedHashMap)9 BeforeEach (org.junit.jupiter.api.BeforeEach)9 Author (example.Author)8 FilterPredicate (com.yahoo.elide.core.filter.predicates.FilterPredicate)7 FirstBean (com.yahoo.elide.example.beans.FirstBean)7 ArrayList (java.util.ArrayList)7 HashMap (java.util.HashMap)7 AndFilterExpression (com.yahoo.elide.core.filter.expression.AndFilterExpression)6