InsertOperation implements MongoDbOperation { @Override public void execute(final MongoDatabase connection, final Document data) { for (final Entry<String, Object> entry : data.entrySet()) { final String collectionName = entry.getKey(); final Object content = entry.getValue(); final List<Document> indexes = getIndexData(content); if (!indexes.isEmpty()) { insertIndexes(connection.getCollection(collectionName), indexes); } final List<Document> entries = getCollectionData(content); if (!entries.isEmpty()) { insertData(connection.getCollection(collectionName), entries); } } }  @Override void execute(final MongoDatabase connection, final Document data);  }
@Test public void testInsertNotStructuredData() { final List<Document> values1 = Arrays.asList(DOCUMENT_1); final List<Document> values2 = Arrays.asList(DOCUMENT_2); final Document data = new Document().append(COLLECTION_NAME_1, values1).append(COLLECTION_NAME_2, values2); operation.execute(connection, data); verify(connection, times(2)).getCollection(collectionNameCaptor.capture()); final List<String> capturedCollectionNames = collectionNameCaptor.getAllValues(); assertThat(capturedCollectionNames.size(), equalTo(2)); assertThat(capturedCollectionNames, hasItems(COLLECTION_NAME_1, COLLECTION_NAME_2)); verify(collection, times(2)).insertMany(documentCaptor.capture()); final List<List<Document>> capturedDocuments = documentCaptor.getAllValues(); assertThat(capturedDocuments.size(), equalTo(2)); assertThat(capturedDocuments, hasItems(values1, values2)); }
@Test public void testInsertStructuredDataWithoutIndexDefinitions() { final List<Document> values1 = Arrays.asList(DOCUMENT_1); final Document doc1 = new Document().append("data", values1); final List<Document> values2 = Arrays.asList(DOCUMENT_2); final Document doc2 = new Document().append("data", values2); final Document data = new Document().append(COLLECTION_NAME_1, doc1).append(COLLECTION_NAME_2, doc2); operation.execute(connection, data); verify(connection, times(2)).getCollection(collectionNameCaptor.capture()); final List<String> capturedCollectionNames = collectionNameCaptor.getAllValues(); assertThat(capturedCollectionNames.size(), equalTo(2)); assertThat(capturedCollectionNames, hasItems(COLLECTION_NAME_1, COLLECTION_NAME_2)); verify(collection, times(2)).insertMany(documentCaptor.capture()); final List<List<Document>> capturedDocuments = documentCaptor.getAllValues(); assertThat(capturedDocuments.size(), equalTo(2)); assertThat(capturedDocuments, hasItems(values1, values2)); }
@Test public void testInsertStructuredDataWithIndexDefinitionsWithoutOptions() { final List<Document> values1 = Arrays.asList(DOCUMENT_1); final Document index1 = new Document().append("a", "1"); final List<Document> indexes1 = Arrays.asList(new Document().append("index", index1)); final Document doc1 = new Document().append("data", values1).append("indexes", indexes1); final List<Document> values2 = Arrays.asList(DOCUMENT_2); final Document index2 = new Document().append("c", "1"); final List<Document> indexes2 = Arrays.asList(new Document().append("index", index2)); final Document doc2 = new Document().append("data", values2).append("indexes", indexes2); final Document data = new Document().append(COLLECTION_NAME_1, doc1).append(COLLECTION_NAME_2, doc2); operation.execute(connection, data); verify(connection, times(4)).getCollection(collectionNameCaptor.capture()); final Collection<String> capturedCollectionNames = new HashSet<>(collectionNameCaptor.getAllValues()); assertThat(capturedCollectionNames.size(), equalTo(2)); assertThat(capturedCollectionNames, hasItems(COLLECTION_NAME_1, COLLECTION_NAME_2)); final ArgumentCaptor<Document> indexCaptor = ArgumentCaptor.forClass(Document.class); verify(collection, times(2)).createIndex(indexCaptor.capture()); final List<Document> capturedIndexes = indexCaptor.getAllValues(); assertThat(capturedIndexes.size(), equalTo(2)); assertThat(capturedIndexes, hasItems(index1, index2)); verify(collection, times(2)).insertMany(documentCaptor.capture()); final List<List<Document>> capturedDocuments = documentCaptor.getAllValues(); assertThat(capturedDocuments.size(), equalTo(2)); assertThat(capturedDocuments, hasItems(values1, values2)); }
@Test public void testInsertStructuredDataWithIndexDefinitionsAndOptions() { final List<Document> values1 = Arrays.asList(DOCUMENT_1); final Document index1 = new Document().append("a", "1"); final Document options1 = new Document().append("default_language", "english").append("collation", new Document().append("locale", "simple").append("strength", new Integer(1)).append("caseLevel", true).append("alternate", "non-ignorable")); final List<Document> indexes1 = Arrays.asList(new Document().append("index", index1).append("options", options1)); final Document doc1 = new Document().append("data", values1).append("indexes", indexes1); final List<Document> values2 = Arrays.asList(DOCUMENT_2); final Document index2 = new Document().append("c", 1).append("d", -1); final Document options2 = new Document().append("backgound", true).append("unique", true).append("name", "foo") .append("expireAfterSeconds", new Long(30)); final List<Document> indexes2 = Arrays.asList(new Document().append("index", index2).append("options", options2)); final Document doc2 = new Document().append("data", values2).append("indexes", indexes2); final Document data = new Document().append(COLLECTION_NAME_1, doc1).append(COLLECTION_NAME_2, doc2); operation.execute(connection, data); verify(connection, times(4)).getCollection(collectionNameCaptor.capture()); final Collection<String> capturedCollectionNames = new HashSet<>(collectionNameCaptor.getAllValues()); assertThat(capturedCollectionNames.size(), equalTo(2)); assertThat(capturedCollectionNames, hasItems(COLLECTION_NAME_1, COLLECTION_NAME_2)); final ArgumentCaptor<Document> indexCaptor = ArgumentCaptor.forClass(Document.class); final ArgumentCaptor<IndexOptions> optionsCaptor = ArgumentCaptor.forClass(IndexOptions.class); verify(collection, times(2)).createIndex(indexCaptor.capture(), optionsCaptor.capture()); final List<Document> capturedIndexes = indexCaptor.getAllValues(); assertThat(capturedIndexes.size(), equalTo(2)); assertThat(capturedIndexes, hasItems(index1, index2)); final List<IndexOptions> indexOptions = optionsCaptor.getAllValues(); assertThat(indexOptions.size(), equalTo(2)); verify(collection, times(2)).insertMany(documentCaptor.capture()); final List<List<Document>> capturedDocuments = documentCaptor.getAllValues(); assertThat(capturedDocuments.size(), equalTo(2)); assertThat(capturedDocuments, hasItems(values1, values2)); }