Example usage for org.springframework.data.mongodb.core MongoOperations findAll

List of usage examples for org.springframework.data.mongodb.core MongoOperations findAll

Introduction

In this page you can find the example usage for org.springframework.data.mongodb.core MongoOperations findAll.

Prototype

<T> List<T> findAll(Class<T> entityClass);

Source Link

Document

Query for a list of objects of type T from the collection used by the entity class.

Usage

From source file:com.avanza.ymer.MongoProbes.java

/**
 * This probe checks all objects of the specified class in the mongo instance. If any of them matches the matcher,
 * the probe is satisfied.//from  w ww . j  a v a 2 s.com
 */
public static <T> Probe containsObject(final MongoOperations mongo, final Matcher<T> matcher,
        final Class<T> objectClass) {
    return new Probe() {

        private List<T> sampledObjects = new ArrayList<T>();

        @Override
        public void sample() {
            sampledObjects = mongo.findAll(objectClass);
        }

        @Override
        public boolean isSatisfied() {
            for (T object : sampledObjects) {
                if (matcher.matches(object)) {
                    return true;
                }
            }
            return false;
        }

        @Override
        public void describeFailureTo(Description description) {
            StringBuilder sb = new StringBuilder();
            for (T details : sampledObjects) {
                sb.append(details);
                sb.append(" ");
            }
            description.appendText("failed to find object in mongo that ").appendDescriptionOf(matcher)
                    .appendText(" - most recent sampled objects: ").appendText(sb.toString().trim());
        }
    };

}