MongoClientProvider implements AutoCloseable { @Nonnull public MongoCollection <Document> getCollection (@Nonnull @Nonempty final String sName) { ValueEnforcer.notEmpty (sName, "Name"); return m_aDatabase.getCollection (sName); } MongoClientProvider(@Nonnull @Nonempty final String sConnectionString, @Nonnull @Nonempty final String sDBName); void close(); @Nonnull MongoCollection <Document> getCollection(@Nonnull @Nonempty final String sName); static final Integer INDEX_ASCENDING; static final Integer INDEX_DESCENDING; }
@Test public void testBasic () { try (final MongoClientProvider aCP = new MongoClientProvider ("mongodb: { final Document doc = new Document ().append ("name", "MongoDB") .append ("type", "database") .append ("count", Integer.valueOf (1)) .append ("versions", new CommonsArrayList <> ("v3.2", "v3.0", "v2.6")) .append ("info", new Document ().append ("x", Integer.valueOf (203)).append ("y", Integer.valueOf (102))); final MongoCollection <Document> aCollection = aCP.getCollection ("coll1"); final String sIndexName = aCollection.createIndex (new Document ("name", MongoClientProvider.INDEX_ASCENDING)); LOGGER.info ("Index name: " + sIndexName); assertNotNull (aCollection); assertEquals (0, aCollection.countDocuments ()); aCollection.insertOne (doc); assertEquals (1, aCollection.countDocuments ()); LOGGER.info (aCollection.countDocuments () + " docs"); Document aDoc = aCollection.find ().first (); assertNotNull (aDoc); LOGGER.info ("Created: " + aDoc.toJson ()); DeleteResult aDR = aCollection.deleteMany (new Document ("name", "MongoDB")); LOGGER.info ("DeleteResult = " + aDR); assertNotNull (aDR); assertTrue (aDR.wasAcknowledged ()); assertEquals (1, aDR.getDeletedCount ()); LOGGER.info (aCollection.countDocuments () + " docs"); assertEquals (0, aCollection.countDocuments ()); UpdateResult aUR = aCollection.updateOne (new Document ("name", "MongoDB"), Updates.set ("type", "bla")); LOGGER.info ("UpdateResult = " + aUR); assertNotNull (aUR); assertTrue (aUR.wasAcknowledged ()); assertEquals (0, aUR.getMatchedCount ()); assertEquals (0, aUR.getModifiedCount ()); aUR = aCollection.updateMany (new Document ("name", "MongoDB"), Updates.set ("type", "bla")); LOGGER.info ("UpdateResult = " + aUR); assertNotNull (aUR); assertTrue (aUR.wasAcknowledged ()); assertEquals (0, aUR.getMatchedCount ()); assertEquals (0, aUR.getModifiedCount ()); aCollection.insertOne (doc); assertEquals (1, aCollection.countDocuments ()); LOGGER.info (aCollection.countDocuments () + " docs"); aDoc = aCollection.find ().first (); assertNotNull (aDoc); LOGGER.info ("Created: " + aDoc.toJson ()); aUR = aCollection.updateOne (new Document ("name", "MongoDB"), Updates.set ("type", "Update1")); LOGGER.info ("UpdateResult = " + aUR); assertNotNull (aUR); assertTrue (aUR.wasAcknowledged ()); assertEquals (1, aUR.getMatchedCount ()); assertEquals (1, aUR.getModifiedCount ()); aUR = aCollection.updateMany (new Document ("name", "MongoDB"), Updates.set ("type", "Update2")); LOGGER.info ("UpdateResult = " + aUR); assertNotNull (aUR); assertTrue (aUR.wasAcknowledged ()); assertEquals (1, aUR.getMatchedCount ()); assertEquals (1, aUR.getModifiedCount ()); aDR = aCollection.deleteMany (new Document ("name", "MongoDB")); LOGGER.info ("DeleteResult = " + aDR); assertNotNull (aDR); assertTrue (aDR.wasAcknowledged ()); assertEquals (1, aDR.getDeletedCount ()); aDR = aCollection.deleteMany (new Document ("name", "MongoDB")); LOGGER.info ("DeleteResult = " + aDR); assertNotNull (aDR); assertTrue (aDR.wasAcknowledged ()); assertEquals (0, aDR.getDeletedCount ()); } }