createService(VCAP_MONGO_SERVICE, "mongodb", "1.8");
int tunnelPort = LOCAL_PORT + 2;
createTunnelServer(VCAP_MONGO_SERVICE, tunnelPort);
Mongo mongo = new Mongo(LOCAL_HOST, tunnelPort);
MongoDbFactory mdbf = new SimpleMongoDbFactory(mongo, svc_dbname, new UserCredentials(svc_username, svc_passwd));
MongoTemplate mongoTemplate = new MongoTemplate(mdbf);
// Test data
ObjectMapper objectMapper = new ObjectMapper();
Map<String,Object> dataMap = null;
try {
dataMap = objectMapper.readValue(new File("data/load.json"), Map.class);
} catch (IOException e) {
e.printStackTrace();
}
List<Map<String, Object>> l = (List<Map<String, Object>>) dataMap.get("records");
for (Map<String, Object> m : l) {
Map<String, Object> rec = (Map<String, Object>) m.get("record");
final BasicDBObject dbo = new BasicDBObject();
dbo.putAll(rec);
mongoTemplate.execute("records", new CollectionCallback<Object>() {
public Object doInCollection(DBCollection collection) throws MongoException, DataAccessException {
collection.insert(dbo);
return null;
}
});
}
List records = mongoTemplate.findAll(BasicDBObject.class, "records");
Assert.assertEquals("Did not load the data correctly", 200, records.size());
mongo.close();
stopTunnelServer();
removeService(VCAP_MONGO_SERVICE);