Search in sources :

Example 86 with DataStoreTransaction

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

the class ErrorMapperTest method testElideCreateWithErrorMapperUnmapped.

@Test
public void testElideCreateWithErrorMapperUnmapped() throws Exception {
    DataStore store = mock(DataStore.class);
    DataStoreTransaction tx = mock(DataStoreTransaction.class);
    FieldTestModel mockModel = mock(FieldTestModel.class);
    Elide elide = getElide(store, dictionary, MOCK_ERROR_MAPPER);
    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);
    doThrow(EXPECTED_EXCEPTION).when(tx).preCommit(any());
    RuntimeException result = assertThrows(RuntimeException.class, () -> elide.post(baseUrl, "/testModel", body, null, NO_VERSION));
    assertEquals(EXPECTED_EXCEPTION, result.getCause());
    verify(tx).close();
}
Also used : FieldTestModel(com.yahoo.elide.core.lifecycle.FieldTestModel) DataStore(com.yahoo.elide.core.datastore.DataStore) DataStoreTransaction(com.yahoo.elide.core.datastore.DataStoreTransaction) Elide(com.yahoo.elide.Elide) Test(org.junit.jupiter.api.Test)

Example 87 with DataStoreTransaction

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

the class TableExportOperation method call.

@Override
public AsyncAPIResult call() {
    log.debug("TableExport Object from request: {}", exportObj);
    Elide elide = service.getElide();
    TableExportResult exportResult = new TableExportResult();
    UUID requestId = UUID.fromString(exportObj.getRequestId());
    try (DataStoreTransaction tx = elide.getDataStore().beginTransaction()) {
        // Do Not Cache Export Results
        Map<String, List<String>> requestHeaders = new HashMap<String, List<String>>();
        requestHeaders.put("bypasscache", new ArrayList<String>(Arrays.asList("true")));
        RequestScope requestScope = getRequestScope(exportObj, scope, tx, requestHeaders);
        Collection<EntityProjection> projections = getProjections(exportObj, requestScope);
        validateProjections(projections);
        EntityProjection projection = projections.iterator().next();
        Observable<PersistentResource> observableResults = Observable.empty();
        elide.getTransactionRegistry().addRunningTransaction(requestId, tx);
        // TODO - we need to add the baseUrlEndpoint to the queryObject.
        // TODO - Can we have projectionInfo as null?
        requestScope.setEntityProjection(projection);
        if (projection != null) {
            projection.setPagination(null);
            observableResults = PersistentResource.loadRecords(projection, Collections.emptyList(), requestScope);
        }
        Observable<String> results = Observable.empty();
        String preResult = formatter.preFormat(projection, exportObj);
        results = observableResults.map(resource -> {
            this.recordNumber++;
            return formatter.format(resource, recordNumber);
        });
        String postResult = formatter.postFormat(projection, exportObj);
        // Stitch together Pre-Formatted, Formatted, Post-Formatted results of Formatter in single observable.
        Observable<String> interimResults = concatStringWithObservable(preResult, results, true);
        Observable<String> finalResults = concatStringWithObservable(postResult, interimResults, false);
        TableExportResult result = storeResults(exportObj, engine, finalResults);
        if (result != null && result.getMessage() != null) {
            throw new IllegalStateException(result.getMessage());
        }
        exportResult.setUrl(new URL(generateDownloadURL(exportObj, scope)));
        exportResult.setRecordCount(recordNumber);
        tx.flush(requestScope);
        elide.getAuditLogger().commit();
        tx.commit(requestScope);
    } catch (BadRequestException e) {
        exportResult.setMessage(e.getMessage());
    } catch (MalformedURLException e) {
        exportResult.setMessage("Download url generation failure.");
    } catch (IOException e) {
        log.error("IOException during TableExport", e);
        exportResult.setMessage(e.getMessage());
    } catch (Exception e) {
        exportResult.setMessage(e.getMessage());
    } finally {
        // Follows same flow as GraphQL. The query may result in failure but request was successfully processed.
        exportResult.setHttpStatus(200);
        exportResult.setCompletedOn(new Date());
        elide.getTransactionRegistry().removeRunningTransaction(requestId);
        elide.getAuditLogger().clear();
    }
    return exportResult;
}
Also used : Arrays(java.util.Arrays) Getter(lombok.Getter) ResultStorageEngine(com.yahoo.elide.async.service.storageengine.ResultStorageEngine) AsyncAPIResult(com.yahoo.elide.async.models.AsyncAPIResult) URL(java.net.URL) Date(java.util.Date) SingleRootProjectionValidator(com.yahoo.elide.async.export.validator.SingleRootProjectionValidator) HashMap(java.util.HashMap) Callable(java.util.concurrent.Callable) ArrayList(java.util.ArrayList) Validator(com.yahoo.elide.async.export.validator.Validator) Map(java.util.Map) PersistentResource(com.yahoo.elide.core.PersistentResource) Observable(io.reactivex.Observable) AsyncAPI(com.yahoo.elide.async.models.AsyncAPI) TableExport(com.yahoo.elide.async.models.TableExport) RequestScope(com.yahoo.elide.core.RequestScope) Elide(com.yahoo.elide.Elide) DataStoreTransaction(com.yahoo.elide.core.datastore.DataStoreTransaction) FileExtensionType(com.yahoo.elide.async.models.FileExtensionType) MalformedURLException(java.net.MalformedURLException) Collection(java.util.Collection) EntityProjection(com.yahoo.elide.core.request.EntityProjection) IOException(java.io.IOException) TableExportFormatter(com.yahoo.elide.async.export.formatter.TableExportFormatter) UUID(java.util.UUID) TableExportResult(com.yahoo.elide.async.models.TableExportResult) AsyncExecutorService(com.yahoo.elide.async.service.AsyncExecutorService) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) BadRequestException(com.yahoo.elide.core.exceptions.BadRequestException) Collections(java.util.Collections) EntityProjection(com.yahoo.elide.core.request.EntityProjection) PersistentResource(com.yahoo.elide.core.PersistentResource) MalformedURLException(java.net.MalformedURLException) HashMap(java.util.HashMap) IOException(java.io.IOException) RequestScope(com.yahoo.elide.core.RequestScope) TableExportResult(com.yahoo.elide.async.models.TableExportResult) URL(java.net.URL) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) BadRequestException(com.yahoo.elide.core.exceptions.BadRequestException) Date(java.util.Date) DataStoreTransaction(com.yahoo.elide.core.datastore.DataStoreTransaction) BadRequestException(com.yahoo.elide.core.exceptions.BadRequestException) ArrayList(java.util.ArrayList) List(java.util.List) Elide(com.yahoo.elide.Elide) UUID(java.util.UUID)

Example 88 with DataStoreTransaction

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

the class ResourceIT method setup.

@BeforeEach
public void setup() throws IOException {
    dataStore.populateEntityDictionary(EntityDictionary.builder().build());
    DataStoreTransaction tx = dataStore.beginTransaction();
    final Company company = new Company();
    company.setId("abc");
    company.setDescription("ABC");
    Address address = new Address();
    address.setStreet1("foo");
    address.setProperties(Map.of("boo", "baz"));
    company.setAddress(address);
    tx.createObject(company, null);
    // id 1
    Parent parent = new Parent();
    // id 1
    Child child = new Child();
    parent.setChildren(Sets.newHashSet(child));
    parent.setSpouses(Sets.newHashSet());
    child.setParents(Sets.newHashSet(parent));
    tx.createObject(parent, null);
    tx.createObject(child, null);
    // Single tests
    // id 2
    Parent p1 = new Parent();
    p1.setFirstName("John");
    p1.setSpouses(Sets.newHashSet());
    // id 2
    Child c1 = new Child();
    c1.setName("Child-ID2");
    c1.setParents(Sets.newHashSet(p1));
    // id 3
    Child c2 = new Child();
    c2.setName("Child-ID3");
    c2.setParents(Sets.newHashSet(p1));
    Set<Child> friendSet = new HashSet<>();
    friendSet.add(c2);
    c1.setFriends(friendSet);
    Set<Child> childrenSet1 = new HashSet<>();
    childrenSet1.add(c1);
    childrenSet1.add(c2);
    p1.setChildren(childrenSet1);
    // List tests
    // id 3
    Parent p2 = new Parent();
    // id 4
    Parent p3 = new Parent();
    // id 4
    Child c3 = new Child();
    c3.setParents(Sets.newHashSet(p2));
    // id 5
    Child c4 = new Child();
    c4.setParents(Sets.newHashSet(p2));
    Set<Child> childrenSet2 = new HashSet<>();
    childrenSet2.add(c3);
    childrenSet2.add(c4);
    p2.setChildren(childrenSet2);
    p2.setFirstName("Link");
    p3.setFirstName("Unknown");
    p2.setSpouses(Sets.newHashSet());
    p3.setSpouses(Sets.newHashSet(p2));
    p3.setChildren(Sets.newHashSet());
    tx.createObject(c1, null);
    tx.createObject(c2, null);
    tx.createObject(c3, null);
    tx.createObject(c4, null);
    tx.createObject(p1, null);
    tx.createObject(p2, null);
    tx.createObject(p3, null);
    Book bookWithPercentage = new Book();
    bookWithPercentage.setTitle("titlewith%percentage");
    Book bookWithoutPercentage = new Book();
    bookWithoutPercentage.setTitle("titlewithoutpercentage");
    tx.createObject(bookWithPercentage, null);
    tx.createObject(bookWithoutPercentage, null);
    FunWithPermissions fun = new FunWithPermissions();
    tx.createObject(fun, null);
    // ID 1
    User user = new User();
    user.setPassword("god");
    tx.createObject(user, null);
    Invoice invoice = new Invoice();
    invoice.setId(1);
    LineItem item = new LineItem();
    invoice.setItems(Sets.newHashSet(item));
    item.setInvoice(invoice);
    tx.createObject(invoice, null);
    tx.createObject(item, null);
    ExceptionThrowingBean etb = new ExceptionThrowingBean();
    etb.setId(1L);
    tx.createObject(etb, null);
    tx.commit(null);
    tx.close();
}
Also used : Company(example.Company) User(example.User) Invoice(example.Invoice) Address(example.Address) Parent(example.Parent) LineItem(example.LineItem) ExceptionThrowingBean(example.ExceptionThrowingBean) Book(example.Book) DataStoreTransaction(com.yahoo.elide.core.datastore.DataStoreTransaction) FunWithPermissions(example.FunWithPermissions) Child(example.Child) HashSet(java.util.HashSet) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 89 with DataStoreTransaction

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

the class TransferableIT method setUp.

@BeforeEach
public void setUp() throws Exception {
    dataStore.populateEntityDictionary(EntityDictionary.builder().build());
    DataStoreTransaction tx = dataStore.beginTransaction();
    Left left = new Left();
    tx.createObject(left, null);
    tx.commit(null);
    tx.close();
}
Also used : Left(example.Left) DataStoreTransaction(com.yahoo.elide.core.datastore.DataStoreTransaction) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 90 with DataStoreTransaction

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

the class AccessIT method accessParentBean.

@Test
public void accessParentBean() throws IOException {
    DataStoreTransaction tx = dataStore.beginTransaction();
    Parent parent = new Parent();
    parent.setChildren(new HashSet<>());
    parent.setSpouses(new HashSet<>());
    tx.createObject(parent, null);
    tx.commit(null);
    tx.close();
}
Also used : Parent(example.Parent) DataStoreTransaction(com.yahoo.elide.core.datastore.DataStoreTransaction) Test(org.junit.jupiter.api.Test) IntegrationTest(com.yahoo.elide.initialization.IntegrationTest)

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