All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.bwaldvogel.mongo.backend.TestUtils Maven / Gradle / Ivy

There is a newer version: 1.45.0
Show newest version
package de.bwaldvogel.mongo.backend;

import java.time.Instant;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.bson.Document;

import com.mongodb.client.MongoDatabase;

public class TestUtils {

    private TestUtils() {
    }

    public static  List toArray(Iterable iterable) {
        List array = new ArrayList<>();
        for (T obj : iterable) {
            array.add(obj);
        }
        return array;
    }

    public static Document json(String string) {
        string = string.trim();
        if (!string.startsWith("{")) {
            string = "{" + string + "}";
        }
        return Document.parse(string);
    }

    static List jsonList(String... json) {
        return Stream.of(json)
            .map(TestUtils::json)
            .collect(Collectors.toList());
    }

    public static Document getCollectionStatistics(MongoDatabase database, String collectionName) {
        Document collStats = new Document("collStats", collectionName);
        return database.runCommand(collStats);
    }

    static Instant instant(String value) {
        return Instant.parse(value);
    }

    static Date date(String value) {
        return Date.from(instant(value));
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy