Search in sources :

Example 66 with DataDocument

use of io.lumeer.engine.api.data.DataDocument in project engine by Lumeer.

the class MongoDbStorageTest method testIncrementAttributeValueBy.

@Test
public void testIncrementAttributeValueBy() throws Exception {
    mongoDbStorage.createCollection(COLLECTION_INC_ATTR_VALUE_BY);
    String incAttribute = "incAttribute";
    DataDocument insertedDocument = createDummyDocument();
    String id = mongoDbStorage.createDocument(COLLECTION_INC_ATTR_VALUE_BY, insertedDocument);
    final DataFilter documentIdFilter = mongoDbStorageDialect.documentIdFilter(id);
    mongoDbStorage.incrementAttributeValueBy(COLLECTION_INC_ATTR_VALUE_BY, documentIdFilter, incAttribute, 1);
    DataDocument readDocument = mongoDbStorage.readDocument(COLLECTION_INC_ATTR_VALUE_BY, documentIdFilter);
    assertThat(readDocument.containsKey(incAttribute)).isTrue();
    assertThat(readDocument.getInteger(incAttribute)).isEqualTo(1);
    mongoDbStorage.incrementAttributeValueBy(COLLECTION_INC_ATTR_VALUE_BY, documentIdFilter, incAttribute, 10);
    readDocument = mongoDbStorage.readDocument(COLLECTION_INC_ATTR_VALUE_BY, documentIdFilter);
    assertThat(readDocument.containsKey(incAttribute)).isTrue();
    assertThat(readDocument.getInteger(incAttribute)).isEqualTo(11);
}
Also used : DataFilter(io.lumeer.engine.api.data.DataFilter) DataDocument(io.lumeer.engine.api.data.DataDocument) Test(org.junit.Test)

Example 67 with DataDocument

use of io.lumeer.engine.api.data.DataDocument in project engine by Lumeer.

the class MongoUtils method convertDocument.

public static DataDocument convertDocument(Document document) {
    MongoUtils.replaceId(document);
    DataDocument dataDocument = new DataDocument(document);
    MongoUtils.convertNestedAndListDocuments(dataDocument);
    return dataDocument;
}
Also used : DataDocument(io.lumeer.engine.api.data.DataDocument)

Example 68 with DataDocument

use of io.lumeer.engine.api.data.DataDocument in project engine by Lumeer.

the class MongoDataDao method updateData.

@Override
public DataDocument updateData(final String collectionId, final String documentId, final DataDocument data) {
    Document document = new Document(data);
    FindOneAndReplaceOptions options = new FindOneAndReplaceOptions().returnDocument(ReturnDocument.AFTER);
    Document updatedDocument = dataCollection(collectionId).findOneAndReplace(idFilter(documentId), document, options);
    if (updatedDocument == null) {
        throw new StorageException("Document '" + documentId + "' has not been updated (replaced).");
    }
    return MongoUtils.convertDocument(updatedDocument);
}
Also used : FindOneAndReplaceOptions(com.mongodb.client.model.FindOneAndReplaceOptions) Document(org.bson.Document) DataDocument(io.lumeer.engine.api.data.DataDocument) ReturnDocument(com.mongodb.client.model.ReturnDocument) StorageException(io.lumeer.storage.api.exception.StorageException)

Example 69 with DataDocument

use of io.lumeer.engine.api.data.DataDocument in project engine by Lumeer.

the class MongoDataDao method createData.

@Override
public List<DataDocument> createData(final String collectionId, final List<DataDocument> data) {
    List<Document> documents = data.stream().map(dataDocument -> new Document(dataDocument).append(ID, new ObjectId(dataDocument.getId()))).collect(Collectors.toList());
    dataCollection(collectionId).insertMany(documents);
    for (int i = 0; i < documents.size(); i++) {
        Object idObj = documents.get(i).get(ID);
        String id = idObj instanceof String ? (String) idObj : ((ObjectId) idObj).toHexString();
        data.get(i).setId(id);
    }
    return data;
}
Also used : Document(org.bson.Document) MongoCollection(com.mongodb.client.MongoCollection) FindOneAndUpdateOptions(com.mongodb.client.model.FindOneAndUpdateOptions) MongoUtils(io.lumeer.storage.mongodb.MongoUtils) ArrayList(java.util.ArrayList) Filters(com.mongodb.client.model.Filters) Bson(org.bson.conversions.Bson) DataDao(io.lumeer.storage.api.dao.DataDao) MongoCursor(com.mongodb.client.MongoCursor) ResourceType(io.lumeer.api.model.ResourceType) DataDocument(io.lumeer.engine.api.data.DataDocument) ResourceNotFoundException(io.lumeer.storage.api.exception.ResourceNotFoundException) ReturnDocument(com.mongodb.client.model.ReturnDocument) SearchQuery(io.lumeer.storage.api.query.SearchQuery) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) Indexes(com.mongodb.client.model.Indexes) StorageException(io.lumeer.storage.api.exception.StorageException) List(java.util.List) MongoFilters.idFilter(io.lumeer.storage.mongodb.util.MongoFilters.idFilter) FindIterable(com.mongodb.client.FindIterable) RequestScoped(javax.enterprise.context.RequestScoped) AttributeFilter(io.lumeer.storage.api.filter.AttributeFilter) ObjectId(org.bson.types.ObjectId) FindOneAndReplaceOptions(com.mongodb.client.model.FindOneAndReplaceOptions) MongoIterable(com.mongodb.client.MongoIterable) ObjectId(org.bson.types.ObjectId) Document(org.bson.Document) DataDocument(io.lumeer.engine.api.data.DataDocument) ReturnDocument(com.mongodb.client.model.ReturnDocument)

Example 70 with DataDocument

use of io.lumeer.engine.api.data.DataDocument in project engine by Lumeer.

the class DocumentServiceIT method testUpdateDocument.

@Test
public void testUpdateDocument() {
    String id = createDocument().getId();
    DataDocument data = new DataDocument(KEY1, VALUE2);
    Entity entity = Entity.json(data);
    LocalDateTime beforeUpdateTime = LocalDateTime.now();
    Response response = client.target(DOCUMENTS_URL_PREFIX).path(collection.getId()).path("documents").path(id).path("data").request(MediaType.APPLICATION_JSON).buildPut(entity).invoke();
    assertThat(response).isNotNull();
    assertThat(response.getStatusInfo()).isEqualTo(Response.Status.OK);
    Document storedDocument = documentDao.getDocumentById(id);
    SoftAssertions assertions = new SoftAssertions();
    assertions.assertThat(storedDocument.getId()).isEqualTo(id);
    assertions.assertThat(storedDocument.getCollectionId()).isEqualTo(collection.getId());
    assertions.assertThat(storedDocument.getCreatedBy()).isEqualTo(USER);
    assertions.assertThat(storedDocument.getCreationDate()).isBeforeOrEqualTo(beforeUpdateTime);
    assertions.assertThat(storedDocument.getUpdatedBy()).isEqualTo(USER);
    assertions.assertThat(storedDocument.getUpdateDate()).isAfterOrEqualTo(beforeUpdateTime).isBeforeOrEqualTo(LocalDateTime.now());
    assertions.assertThat(storedDocument.getDataVersion()).isEqualTo(2);
    assertions.assertThat(storedDocument.getData()).isNull();
    assertions.assertAll();
    DataDocument storedData = dataDao.getData(collection.getId(), id);
    assertThat(storedData).isNotNull();
    assertThat(storedData).containsEntry(KEY1, VALUE2);
    assertThat(storedData).doesNotContainKey(KEY2);
}
Also used : LocalDateTime(java.time.LocalDateTime) Response(javax.ws.rs.core.Response) DataDocument(io.lumeer.engine.api.data.DataDocument) Entity(javax.ws.rs.client.Entity) SoftAssertions(org.assertj.core.api.SoftAssertions) DataDocument(io.lumeer.engine.api.data.DataDocument) JsonDocument(io.lumeer.api.dto.JsonDocument) Document(io.lumeer.api.model.Document) Test(org.junit.Test)

Aggregations

DataDocument (io.lumeer.engine.api.data.DataDocument)75 Test (org.junit.Test)42 Collection (io.lumeer.api.model.Collection)16 Document (io.lumeer.api.model.Document)16 JsonDocument (io.lumeer.api.dto.JsonDocument)14 Document (org.bson.Document)13 SoftAssertions (org.assertj.core.api.SoftAssertions)10 ReturnDocument (com.mongodb.client.model.ReturnDocument)9 JsonCollection (io.lumeer.api.dto.JsonCollection)9 DataFilter (io.lumeer.engine.api.data.DataFilter)9 ImportedCollection (io.lumeer.api.model.ImportedCollection)6 SearchQuery (io.lumeer.storage.api.query.SearchQuery)6 LocalDateTime (java.time.LocalDateTime)6 BsonDocument (org.bson.BsonDocument)6 LinkedList (java.util.LinkedList)5 FindOneAndUpdateOptions (com.mongodb.client.model.FindOneAndUpdateOptions)4 ArrayList (java.util.ArrayList)4 List (java.util.List)4 Response (javax.ws.rs.core.Response)4 DataStorageStats (io.lumeer.engine.api.data.DataStorageStats)3