Search in sources :

Example 16 with DataStoreTransaction

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

the class LifeCycleTest method testElideGetRelationship.

@Test
public void testElideGetRelationship() throws Exception {
    DataStore store = mock(DataStore.class);
    DataStoreTransaction tx = mock(DataStoreTransaction.class);
    FieldTestModel mockModel = mock(FieldTestModel.class);
    FieldTestModel child = mock(FieldTestModel.class);
    when(mockModel.getModels()).thenReturn(ImmutableSet.of(child));
    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/relationships/models", 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 17 with DataStoreTransaction

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

the class LifeCycleTest method testRead.

@Test
public void testRead() {
    FieldTestModel mockModel = mock(FieldTestModel.class);
    DataStoreTransaction tx = mock(DataStoreTransaction.class);
    RequestScope scope = buildRequestScope(dictionary, tx);
    when(tx.createNewObject(ClassType.of(FieldTestModel.class), scope)).thenReturn(mockModel);
    PersistentResource resource = new PersistentResource(mockModel, "1", scope);
    resource.getAttribute(Attribute.builder().type(String.class).name("field").build());
    verify(mockModel, never()).classCallback(any(), any());
    verify(mockModel, never()).attributeCallback(any(), any(), any());
    verify(mockModel, never()).classAllFieldsCallback(any(), any());
    verify(mockModel, never()).relationCallback(any(), any(), any());
    clearInvocations(mockModel);
    scope.runQueuedPreSecurityTriggers();
    verify(mockModel, never()).classCallback(any(), any());
    verify(mockModel, never()).classAllFieldsCallback(any(), any());
    verify(mockModel, never()).relationCallback(any(), any(), any());
    verify(mockModel, never()).attributeCallback(any(), any(), any());
    clearInvocations(mockModel);
    scope.runQueuedPreFlushTriggers();
    verify(mockModel, never()).classCallback(any(), any());
    verify(mockModel, never()).attributeCallback(any(), any(), any());
    verify(mockModel, never()).classAllFieldsCallback(any(), any());
    verify(mockModel, never()).relationCallback(any(), any(), any());
    clearInvocations(mockModel);
    scope.runQueuedPreCommitTriggers();
    verify(mockModel, never()).classCallback(any(), any());
    verify(mockModel, never()).attributeCallback(any(), any(), any());
    verify(mockModel, never()).classAllFieldsCallback(any(), any());
    verify(mockModel, never()).relationCallback(any(), any(), any());
    clearInvocations(mockModel);
    scope.runQueuedPostCommitTriggers();
    verify(mockModel, never()).classCallback(any(), any());
    verify(mockModel, never()).attributeCallback(any(), any(), any());
    verify(mockModel, never()).classAllFieldsCallback(any(), any());
    verify(mockModel, never()).relationCallback(any(), any(), any());
}
Also used : PersistentResource(com.yahoo.elide.core.PersistentResource) DataStoreTransaction(com.yahoo.elide.core.datastore.DataStoreTransaction) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) RequestScope(com.yahoo.elide.core.RequestScope) Test(org.junit.jupiter.api.Test)

Example 18 with DataStoreTransaction

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

the class LifeCycleTest method testElideDelete.

@Test
public void testElideDelete() 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);
    dictionary.setValue(mockModel, "id", "1");
    when(store.beginTransaction()).thenReturn(tx);
    when(tx.loadObject(isA(EntityProjection.class), any(), isA(RequestScope.class))).thenReturn(mockModel);
    ElideResponse response = elide.delete(baseUrl, "/testModel/1", "", null, NO_VERSION);
    assertEquals(HttpStatus.SC_NO_CONTENT, response.getResponseCode());
    verify(mockModel, never()).classAllFieldsCallback(any(), any());
    verify(mockModel, never()).classCallback(eq(UPDATE), any());
    verify(mockModel, never()).classCallback(eq(CREATE), any());
    verify(mockModel, times(1)).classCallback(eq(DELETE), eq(PRESECURITY));
    verify(mockModel, times(1)).classCallback(eq(DELETE), eq(PREFLUSH));
    verify(mockModel, times(1)).classCallback(eq(DELETE), eq(PRECOMMIT));
    verify(mockModel, times(1)).classCallback(eq(DELETE), eq(POSTCOMMIT));
    verify(mockModel, never()).attributeCallback(eq(UPDATE), any(), any());
    verify(mockModel, never()).attributeCallback(eq(CREATE), any(), any());
    verify(mockModel, never()).attributeCallback(eq(DELETE), any(), 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).delete(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) Elide(com.yahoo.elide.Elide) RequestScope(com.yahoo.elide.core.RequestScope) Test(org.junit.jupiter.api.Test)

Example 19 with DataStoreTransaction

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

the class LifeCycleTest method testPreSecurityLifecycleHookException.

@Test
public void testPreSecurityLifecycleHookException() {
    DataStoreTransaction tx = mock(DataStoreTransaction.class);
    FieldTestModel testModel = mock(FieldTestModel.class);
    doThrow(IllegalStateException.class).when(testModel).attributeCallback(eq(UPDATE), eq(PRESECURITY), any(ChangeSpec.class));
    RequestScope scope = buildRequestScope(dictionary, tx);
    PersistentResource resource = new PersistentResource(testModel, "1", scope);
    assertThrows(IllegalStateException.class, () -> resource.updateAttribute("field", "New value"));
}
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 20 with DataStoreTransaction

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

the class PersistentResourceNoopUpdateTest method testNOOPToManyAddRelation.

@Test
public void testNOOPToManyAddRelation() {
    FunWithPermissions fun = new FunWithPermissions();
    Child child = newChild(1);
    Set<Child> children = new HashSet<>();
    children.add(child);
    fun.setRelation1(children);
    DataStoreTransaction tx = mock(DataStoreTransaction.class);
    RequestScope goodScope = new RequestScope(null, null, NO_VERSION, null, tx, goodUser, null, null, UUID.randomUUID(), elideSettings);
    PersistentResource<FunWithPermissions> funResource = new PersistentResource<>(fun, "3", goodScope);
    PersistentResource<Child> childResource = new PersistentResource<>(child, null, goodScope);
    // We do not want the update to one method to be called when we add the existing entity to the relation
    funResource.addRelation("relation1", childResource);
    verify(tx, never()).updateToManyRelation(eq(tx), eq(child), eq("relation1"), any(), any(), eq(goodScope));
}
Also used : DataStoreTransaction(com.yahoo.elide.core.datastore.DataStoreTransaction) FunWithPermissions(example.FunWithPermissions) Child(example.Child) HashSet(java.util.HashSet) 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