@@ -0,0 +1,2414 @@
+package com.groupon.jenkins.mongo;%0A%0Aimport com.groupon.jenkins.SetupConfig;%0Aimport com.mongodb.BasicDBList;%0Aimport com.mongodb.BasicDBObject;%0Aimport com.mongodb.DB;%0Aimport com.mongodb.DBCollection;%0Aimport com.mongodb.DBObject;%0Aimport com.mongodb.util.JSON;%0Aimport jenkins.model.Jenkins;%0Aimport org.apache.commons.io.FileUtils;%0Aimport org.junit.rules.TestRule;%0Aimport org.junit.runner.Description;%0Aimport org.junit.runners.model.Statement;%0Aimport org.mongodb.morphia.Datastore;%0A%0Aimport java.io.File;%0A%0Apublic class MongoDataLoadRule implements TestRule %7B%0A    public MongoDataLoadRule() %7B%0A    %7D%0A%0A    @Override%0A    public Statement apply(final Statement base, final Description description)%7B%0A        return new Statement() %7B%0A            @Override%0A            public void evaluate() throws Throwable %7B%0A                if(Jenkins.getInstance() == null%0A                        %7C%7C SetupConfig.get().getInjector() == null%0A                        %7C%7C SetupConfig.get().getInjector().getInstance(Datastore.class) == null ) %7B%0A                    throw new IllegalStateException(%22Requires configured Jenkins and Mongo configurations%22);%0A                %7D%0A%0A                DB db = SetupConfig.get().getInjector().getInstance(Datastore.class).getDB();%0A%0A                //Load mongo data%0A                File homedir = Jenkins.getInstance().getRootDir();%0A%0A                for(File fileOfData : homedir.listFiles()) %7B%0A                    if(! fileOfData.getName().endsWith(%22.json%22) ) continue;%0A%0A                    String collectionName = fileOfData.getName().replaceAll(%22%5C%5C.json$%22, %22%22);%0A                    DBCollection collection = db.createCollection(collectionName, new BasicDBObject());%0A%0A                    String data = FileUtils.readFileToString(fileOfData);%0A                    Object bsonObject = JSON.parse(data);%0A                    if(bsonObject instanceof BasicDBList) %7B%0A                        BasicDBList basicDBList = (BasicDBList) bsonObject;%0A                        collection.insert(basicDBList.toArray(new DBObject%5B0%5D));%0A                    %7D else %7B%0A                        collection.insert((DBObject) bsonObject);%0A                    %7D%0A%0A                %7D%0A%0A                base.evaluate();%0A%0A                // Clean up mongo data%0A                for(String collectioName : db.getCollectionNames()) %7B%0A                    db.getCollection(collectioName).drop();%0A                %7D%0A            %7D%0A        %7D;%0A    %7D%0A%7D%0A

Add JUnit rule for loading fixtures into MongoDB