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();
}
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());
}
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();
}
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();
}
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();
}
Aggregations