Search in sources :

Example 1 with DataStoreTransaction

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

the class LifeCycleTest method testPreFlushLifecycleHookException.

@Test
public void testPreFlushLifecycleHookException() {
    DataStoreTransaction tx = mock(DataStoreTransaction.class);
    FieldTestModel testModel = mock(FieldTestModel.class);
    doThrow(IllegalStateException.class).when(testModel).attributeCallback(eq(UPDATE), eq(PREFLUSH), any(ChangeSpec.class));
    RequestScope scope = buildRequestScope(dictionary, tx);
    PersistentResource resource = new PersistentResource(testModel, "1", scope);
    resource.updateAttribute("field", "New value");
    scope.runQueuedPreSecurityTriggers();
    assertThrows(IllegalStateException.class, () -> scope.runQueuedPreFlushTriggers());
}
Also used : PersistentResource(com.yahoo.elide.core.PersistentResource) ChangeSpec(com.yahoo.elide.core.security.ChangeSpec) DataStoreTransaction(com.yahoo.elide.core.datastore.DataStoreTransaction) RequestScope(com.yahoo.elide.core.RequestScope) Test(org.junit.jupiter.api.Test)

Example 2 with DataStoreTransaction

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

the class LifeCycleTest method testLegacyElidePatch.

@Test
public void testLegacyElidePatch() 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 = "{\"data\": {\"type\":\"legacyTestModel\",\"id\":\"1\",\"attributes\": {\"field\":\"Foo\"}}}";
    dictionary.setValue(mockModel, "id", "1");
    when(store.beginTransaction()).thenReturn(tx);
    when(tx.loadObject(isA(EntityProjection.class), any(), isA(RequestScope.class))).thenReturn(mockModel);
    String contentType = JSONAPI_CONTENT_TYPE;
    ElideResponse response = elide.patch(baseUrl, contentType, contentType, "/legacyTestModel/1", body, null, NO_VERSION);
    assertEquals(HttpStatus.SC_NO_CONTENT, response.getResponseCode());
    verify(mockModel, never()).classCreatePreCommitAllUpdates();
    verify(mockModel, never()).classCreatePreSecurity();
    verify(mockModel, never()).classCreatePreCommit();
    verify(mockModel, never()).classCreatePostCommit();
    verify(mockModel, never()).classDeletePreSecurity();
    verify(mockModel, never()).classDeletePreCommit();
    verify(mockModel, never()).classDeletePostCommit();
    verify(mockModel, times(1)).classUpdatePreSecurity();
    verify(mockModel, times(1)).classUpdatePreCommit();
    verify(mockModel, times(1)).classUpdatePostCommit();
    verify(mockModel, times(3)).classMultiple();
    verify(mockModel, never()).fieldCreatePreSecurity();
    verify(mockModel, never()).fieldCreatePreCommit();
    verify(mockModel, never()).fieldCreatePostCommit();
    verify(mockModel, times(1)).fieldUpdatePreSecurity();
    verify(mockModel, times(1)).fieldUpdatePreCommit();
    verify(mockModel, times(1)).fieldUpdatePostCommit();
    verify(mockModel, times(3)).fieldMultiple();
    verify(tx).preCommit(any());
    verify(tx).save(eq(mockModel), isA(RequestScope.class));
    verify(tx).flush(isA(RequestScope.class));
    verify(tx).commit(isA(RequestScope.class));
    verify(tx).close();
}
Also used : 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 3 with DataStoreTransaction

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

the class LifeCycleTest method testLifecycleError.

@Test
public void testLifecycleError() throws Exception {
    DataStore store = mock(DataStore.class);
    DataStoreTransaction tx = mock(DataStoreTransaction.class);
    ErrorTestModel mockModel = mock(ErrorTestModel.class);
    Elide elide = getElide(store, dictionary, MOCK_AUDIT_LOGGER);
    String body = "{\"data\": {\"type\":\"errorTestModel\",\"id\":\"1\",\"attributes\": {\"field\":\"Foo\"}}}";
    when(store.beginTransaction()).thenReturn(tx);
    when(tx.createNewObject(eq(ClassType.of(ErrorTestModel.class)), any())).thenReturn(mockModel);
    ElideResponse response = elide.post(baseUrl, "/errorTestModel", body, null, NO_VERSION);
    assertEquals(HttpStatus.SC_BAD_REQUEST, response.getResponseCode());
    assertEquals("{\"errors\":[{\"detail\":\"Invalid\"}]}", response.getBody());
}
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) Test(org.junit.jupiter.api.Test)

Example 4 with DataStoreTransaction

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

the class LifeCycleTest method testElidePatchFailure.

public void testElidePatchFailure() 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\"}}}";
    dictionary.setValue(mockModel, "id", "1");
    when(store.beginTransaction()).thenReturn(tx);
    when(tx.loadObject(isA(EntityProjection.class), any(), isA(RequestScope.class))).thenReturn(mockModel);
    doThrow(ConstraintViolationException.class).when(tx).flush(any());
    String contentType = JSONAPI_CONTENT_TYPE;
    ElideResponse response = elide.patch(baseUrl, contentType, contentType, "/testModel/1", body, null, NO_VERSION);
    assertEquals(HttpStatus.SC_BAD_REQUEST, response.getResponseCode());
    assertEquals("{\"errors\":[{\"detail\":\"Constraint violation\"}]}", response.getBody());
    verify(mockModel, never()).classAllFieldsCallback(any(), any());
    verify(mockModel, never()).classCallback(eq(CREATE), any());
    verify(mockModel, never()).classCallback(eq(DELETE), any());
    verify(mockModel, times(1)).classCallback(eq(UPDATE), eq(PRESECURITY));
    verify(mockModel, times(1)).classCallback(eq(UPDATE), eq(PREFLUSH));
    verify(mockModel, never()).classCallback(eq(UPDATE), eq(PRECOMMIT));
    verify(mockModel, never()).classCallback(eq(UPDATE), eq(POSTCOMMIT));
    verify(mockModel, never()).attributeCallback(eq(CREATE), any(), any());
    verify(mockModel, never()).attributeCallback(eq(DELETE), any(), any());
    verify(mockModel, times(1)).attributeCallback(eq(UPDATE), eq(PRESECURITY), any());
    verify(mockModel, times(1)).attributeCallback(eq(UPDATE), eq(PREFLUSH), any());
    verify(mockModel, never()).attributeCallback(eq(UPDATE), eq(PRECOMMIT), any());
    verify(mockModel, never()).attributeCallback(eq(UPDATE), eq(POSTCOMMIT), any());
    verify(mockModel, never()).relationCallback(eq(UPDATE), any(), any());
    verify(mockModel, never()).relationCallback(eq(CREATE), any(), any());
    verify(mockModel, never()).relationCallback(eq(DELETE), any(), any());
    verify(tx).preCommit(any());
    verify(tx).save(eq(mockModel), isA(RequestScope.class));
    verify(tx).flush(isA(RequestScope.class));
    verify(tx, never()).commit(isA(RequestScope.class));
    verify(tx).close();
}
Also used : 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)

Example 5 with DataStoreTransaction

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

the class LifeCycleTest method testElideCreateFailure.

@Test
public void testElideCreateFailure() throws Exception {
    DataStore store = mock(DataStore.class);
    DataStoreTransaction tx = mock(DataStoreTransaction.class);
    FieldTestModel mockModel = mock(FieldTestModel.class);
    doThrow(RuntimeException.class).when(mockModel).setField(anyString());
    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_INTERNAL_SERVER_ERROR, response.getResponseCode());
    assertEquals("{\"errors\":[{\"detail\":\"Unexpected exception caught\"}]}", response.getBody());
    verify(mockModel, never()).classCallback(any(), any());
    verify(mockModel, never()).attributeCallback(any(), any(), any());
    verify(mockModel, never()).relationCallback(any(), any(), any());
    verify(mockModel, never()).classAllFieldsCallback(any(), any());
    verify(tx, never()).preCommit(any());
    verify(tx, never()).createObject(eq(mockModel), isA(RequestScope.class));
    verify(tx, never()).flush(isA(RequestScope.class));
    verify(tx, never()).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)

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