use of com.yahoo.elide.core.datastore.DataStore in project elide by yahoo.
the class LifeCycleTest method failElidePatchExtensionCreate.
@Test
public void failElidePatchExtensionCreate() 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 = "[{\"op\": \"add\",\"path\": \"/testModel\",\"value\":{" + "\"type\":\"testModel\",\"attributes\": {\"field\":\"Foo\"}}}]";
RequestScope scope = buildRequestScope(dictionary, tx);
when(store.beginTransaction()).thenReturn(tx);
when(tx.createNewObject(ClassType.of(FieldTestModel.class), scope)).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_BAD_REQUEST, response.getResponseCode());
assertEquals("[{\"errors\":[{\"detail\":\"Bad Request Body'Patch extension requires all objects to have an assigned ID (temporary or permanent) when assigning relationships.'\",\"status\":\"400\"}]}]", response.getBody());
verify(mockModel, never()).classCallback(eq(CREATE), eq(PRESECURITY));
verify(mockModel, never()).classCallback(eq(CREATE), eq(PREFLUSH));
verify(mockModel, never()).classCallback(eq(CREATE), eq(PRECOMMIT));
verify(mockModel, never()).classCallback(eq(CREATE), eq(POSTCOMMIT));
verify(mockModel, never()).classCallback(eq(UPDATE), any());
verify(mockModel, never()).classCallback(eq(DELETE), any());
verify(mockModel, never()).classAllFieldsCallback(any(), any());
verify(mockModel, never()).classAllFieldsCallback(eq(CREATE), eq(PRECOMMIT));
verify(mockModel, never()).attributeCallback(eq(CREATE), eq(PRESECURITY), any());
verify(mockModel, never()).attributeCallback(eq(CREATE), eq(PREFLUSH), any());
verify(mockModel, never()).attributeCallback(eq(CREATE), eq(PRECOMMIT), any());
verify(mockModel, never()).attributeCallback(eq(CREATE), eq(POSTCOMMIT), any());
verify(mockModel, never()).attributeCallback(eq(UPDATE), any(), any());
verify(mockModel, never()).attributeCallback(eq(DELETE), any(), any());
verify(mockModel, never()).relationCallback(eq(CREATE), eq(PRESECURITY), any());
verify(mockModel, never()).relationCallback(eq(CREATE), eq(PREFLUSH), any());
verify(mockModel, never()).relationCallback(eq(CREATE), eq(PRECOMMIT), any());
verify(mockModel, never()).relationCallback(eq(CREATE), eq(POSTCOMMIT), any());
verify(mockModel, never()).relationCallback(eq(UPDATE), any(), any());
verify(mockModel, never()).relationCallback(eq(DELETE), 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 testElidePatchRelationshipAddMultiple.
@Test
public void testElidePatchRelationshipAddMultiple() {
DataStore store = mock(DataStore.class);
DataStoreTransaction tx = mock(DataStoreTransaction.class);
FieldTestModel parent = mock(FieldTestModel.class);
FieldTestModel child1 = mock(FieldTestModel.class);
FieldTestModel child2 = mock(FieldTestModel.class);
FieldTestModel child3 = mock(FieldTestModel.class);
Elide elide = getElide(store, dictionary, MOCK_AUDIT_LOGGER);
String body = "{\"data\": {\"type\":\"testModel\",\"id\":\"1\",\"relationships\": { \"models\": { \"data\": [ { \"type\": \"testModel\", \"id\": \"2\" }, {\"type\": \"testModel\", \"id\": \"3\" } ] } } } }";
dictionary.setValue(parent, "id", "1");
dictionary.setValue(child1, "id", "2");
dictionary.setValue(child2, "id", "3");
dictionary.setValue(child3, "id", "4");
when(store.beginTransaction()).thenReturn(tx);
when(tx.loadObject(isA(EntityProjection.class), eq("1"), isA(RequestScope.class))).thenReturn(parent);
when(tx.loadObject(isA(EntityProjection.class), eq("2"), isA(RequestScope.class))).thenReturn(child1);
when(tx.loadObject(isA(EntityProjection.class), eq("3"), isA(RequestScope.class))).thenReturn(child2);
when(tx.loadObject(isA(EntityProjection.class), eq("4"), isA(RequestScope.class))).thenReturn(child3);
DataStoreIterable iterable = new DataStoreIterableBuilder(List.of(child3)).build();
when(tx.getToManyRelation(any(), any(), isA(Relationship.class), isA(RequestScope.class))).thenReturn(iterable);
String contentType = JSONAPI_CONTENT_TYPE;
ElideResponse response = elide.patch(baseUrl, contentType, contentType, "/testModel/1", body, null, NO_VERSION);
assertEquals(HttpStatus.SC_NO_CONTENT, response.getResponseCode());
verify(parent, times(1)).relationCallback(eq(UPDATE), eq(POSTCOMMIT), notNull());
verify(parent, times(4)).classCallback(eq(UPDATE), any());
verify(parent, never()).classAllFieldsCallback(any(), any());
verify(parent, never()).attributeCallback(any(), any(), any());
}
use of com.yahoo.elide.core.datastore.DataStore in project elide by yahoo.
the class LifeCycleTest method testElidePatch.
@Test
public void testElidePatch() 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);
String contentType = JSONAPI_CONTENT_TYPE;
ElideResponse response = elide.patch(baseUrl, contentType, contentType, "/testModel/1", body, null, NO_VERSION);
assertEquals(HttpStatus.SC_NO_CONTENT, response.getResponseCode());
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, times(1)).classCallback(eq(UPDATE), eq(PRECOMMIT));
verify(mockModel, times(1)).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, times(1)).attributeCallback(eq(UPDATE), eq(PRECOMMIT), any());
verify(mockModel, times(1)).attributeCallback(eq(UPDATE), eq(POSTCOMMIT), any());
verify(mockModel, never()).relationCallback(eq(CREATE), any(), any());
verify(mockModel, never()).relationCallback(eq(DELETE), any(), any());
verify(mockModel, never()).relationCallback(eq(UPDATE), any(), any());
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 testElidePatchExtensionCreate.
@Test
public void testElidePatchExtensionCreate() 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 = "[{\"op\": \"add\",\"path\": \"/testModel\",\"value\":{" + "\"type\":\"testModel\",\"id\": \"1\",\"attributes\": {\"field\":\"Foo\"}}}]";
when(store.beginTransaction()).thenReturn(tx);
when(tx.createNewObject(eq(ClassType.of(FieldTestModel.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)).classCallback(eq(CREATE), eq(PRESECURITY));
verify(mockModel, times(1)).classCallback(eq(CREATE), eq(PREFLUSH));
verify(mockModel, times(1)).classCallback(eq(CREATE), eq(PRECOMMIT));
verify(mockModel, times(1)).classCallback(eq(CREATE), eq(POSTCOMMIT));
verify(mockModel, never()).classCallback(eq(UPDATE), any());
verify(mockModel, never()).classCallback(eq(DELETE), any());
verify(mockModel, times(2)).classAllFieldsCallback(any(), any());
verify(mockModel, times(2)).classAllFieldsCallback(eq(CREATE), eq(PRECOMMIT));
verify(mockModel, times(1)).attributeCallback(eq(CREATE), eq(PRESECURITY), any());
verify(mockModel, times(1)).attributeCallback(eq(CREATE), eq(PREFLUSH), any());
verify(mockModel, times(1)).attributeCallback(eq(CREATE), eq(PRECOMMIT), any());
verify(mockModel, times(1)).attributeCallback(eq(CREATE), eq(POSTCOMMIT), any());
verify(mockModel, never()).attributeCallback(eq(UPDATE), any(), any());
verify(mockModel, never()).attributeCallback(eq(DELETE), any(), any());
verify(mockModel, times(1)).relationCallback(eq(CREATE), eq(PRESECURITY), any());
verify(mockModel, times(1)).relationCallback(eq(CREATE), eq(PREFLUSH), any());
verify(mockModel, times(1)).relationCallback(eq(CREATE), eq(PRECOMMIT), any());
verify(mockModel, times(1)).relationCallback(eq(CREATE), eq(POSTCOMMIT), any());
verify(mockModel, never()).relationCallback(eq(UPDATE), any(), any());
verify(mockModel, never()).relationCallback(eq(DELETE), any(), any());
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();
}
use of com.yahoo.elide.core.datastore.DataStore in project elide by yahoo.
the class LifeCycleTest method testLegacyElideCreate.
@Test
public void testLegacyElideCreate() 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\"}}}";
when(store.beginTransaction()).thenReturn(tx);
when(tx.createNewObject(eq(ClassType.of(LegacyTestModel.class)), any())).thenReturn(mockModel);
ElideResponse response = elide.post(baseUrl, "/legacyTestModel", body, null, NO_VERSION);
assertEquals(HttpStatus.SC_CREATED, response.getResponseCode());
verify(mockModel, times(1)).classCreatePreCommitAllUpdates();
verify(mockModel, times(1)).classCreatePreSecurity();
verify(mockModel, times(1)).classCreatePreCommit();
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)).fieldCreatePreSecurity();
verify(mockModel, times(1)).fieldCreatePreCommit();
verify(mockModel, times(1)).fieldCreatePostCommit();
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