Search in sources :

Example 1 with DataStore

use of com.yahoo.elide.core.datastore.DataStore 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 2 with DataStore

use of com.yahoo.elide.core.datastore.DataStore 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 3 with DataStore

use of com.yahoo.elide.core.datastore.DataStore 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 4 with DataStore

use of com.yahoo.elide.core.datastore.DataStore 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)

Example 5 with DataStore

use of com.yahoo.elide.core.datastore.DataStore 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)

Aggregations

DataStore (com.yahoo.elide.core.datastore.DataStore)38 DataStoreTransaction (com.yahoo.elide.core.datastore.DataStoreTransaction)31 Test (org.junit.jupiter.api.Test)27 Elide (com.yahoo.elide.Elide)25 ElideResponse (com.yahoo.elide.ElideResponse)21 RequestScope (com.yahoo.elide.core.RequestScope)19 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)18 EntityProjection (com.yahoo.elide.core.request.EntityProjection)10 EntityDictionary (com.yahoo.elide.core.dictionary.EntityDictionary)5 MultivaluedHashMap (javax.ws.rs.core.MultivaluedHashMap)5 HashMapDataStore (com.yahoo.elide.core.datastore.inmemory.HashMapDataStore)3 FieldTestModel (com.yahoo.elide.core.lifecycle.FieldTestModel)3 AggregationDataStore (com.yahoo.elide.datastores.aggregation.AggregationDataStore)3 MetaDataStore (com.yahoo.elide.datastores.aggregation.metadata.MetaDataStore)3 JpaDataStore (com.yahoo.elide.datastores.jpa.JpaDataStore)3 NonJtaTransaction (com.yahoo.elide.datastores.jpa.transaction.NonJtaTransaction)3 MultiplexManager (com.yahoo.elide.datastores.multiplex.MultiplexManager)3 NoopBean (com.yahoo.elide.beans.NoopBean)2 TemplateConfigValidator (com.yahoo.elide.datastores.aggregation.validator.TemplateConfigValidator)2 ConfigDataStore (com.yahoo.elide.modelconfig.store.ConfigDataStore)2