Search in sources :

Example 1 with ElideResponse

use of com.yahoo.elide.ElideResponse 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 ElideResponse

use of com.yahoo.elide.ElideResponse 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 ElideResponse

use of com.yahoo.elide.ElideResponse 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 ElideResponse

use of com.yahoo.elide.ElideResponse 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 ElideResponse

use of com.yahoo.elide.ElideResponse 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

ElideResponse (com.yahoo.elide.ElideResponse)53 Test (org.junit.jupiter.api.Test)37 Elide (com.yahoo.elide.Elide)27 DataStoreTransaction (com.yahoo.elide.core.datastore.DataStoreTransaction)23 DataStore (com.yahoo.elide.core.datastore.DataStore)21 RequestScope (com.yahoo.elide.core.RequestScope)18 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)18 MultivaluedHashMap (javax.ws.rs.core.MultivaluedHashMap)13 User (com.yahoo.elide.core.security.User)12 EntityProjection (com.yahoo.elide.core.request.EntityProjection)10 JsonNode (com.fasterxml.jackson.databind.JsonNode)9 IntegrationTest (com.yahoo.elide.initialization.IntegrationTest)9 List (java.util.List)8 AuthenticationUser (com.yahoo.elide.spring.security.AuthenticationUser)6 Callable (java.util.concurrent.Callable)6 AsyncQueryResult (com.yahoo.elide.async.models.AsyncQueryResult)5 AsyncQuery (com.yahoo.elide.async.models.AsyncQuery)4 ElideSettingsBuilder (com.yahoo.elide.ElideSettingsBuilder)3 TestAuditLogger (com.yahoo.elide.core.audit.TestAuditLogger)3 InvalidOperationException (com.yahoo.elide.core.exceptions.InvalidOperationException)3