/*
 * Copyright (C) 2011 Benoit GUEROUT <bguerout at gmail dot com> and Yves AMSELLEM <amsellem dot yves at gmail dot com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.jongo.spike;

import com.fasterxml.jackson.databind.MappingIterator;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;
import com.mongodb.WriteConcern;
import de.undercouch.bson4jackson.BsonFactory;
import org.bson.BSONObject;
import org.bson.BasicBSONObject;
import org.jongo.MongoCollection;
import org.jongo.util.JongoTestCase;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;

import static org.fest.assertions.Assertions.assertThat;

public class MongoDumpTest extends JongoTestCase {

    private MongoCollection collection;

    @Before
    public void setUp() throws Exception {
        collection = createEmptyCollection("friends");
    }

    @After
    public void tearDown() throws Exception {
        dropCollection("friends");
    }

    @Test
    public void importBsonDumpFileIntoCollection() throws Exception {

        InputStream bsonDump = getClass().getClassLoader().getResourceAsStream("1000friends.bson");
        BsonFactory bsonFactory = new BsonFactory();
        //bsonFactory.enable(BsonParser.Feature.HONOR_DOCUMENT_LENGTH); // fails when enabled
        ObjectReader reader = new ObjectMapper(bsonFactory).reader(BasicBSONObject.class);

        MappingIterator<BSONObject> iterator = reader.readValues(bsonDump);
        try {
            while (safeHasNext(iterator)) {
                BSONObject bsonObject = iterator.next();
                collection.withWriteConcern(WriteConcern.SAFE).save(bsonObject);
            }
        } finally {
            iterator.close();
        }

        assertThat(collection.count()).isEqualTo(1000);
    }

    private boolean safeHasNext(MappingIterator<BSONObject> iterator) {
        try {
            return iterator.hasNextValue();
        } catch (IOException e) {
            // Erroneous bson4jackson exception?
            return false;
        }
    }
}

/*
 * Copyright (C) 2011 Benoit GUEROUT <bguerout at gmail dot com> and Yves AMSELLEM <amsellem dot yves at gmail dot com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.jongo.spike;

import com.fasterxml.jackson.databind.MappingIterator;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;
import com.mongodb.WriteConcern;
import de.undercouch.bson4jackson.BsonFactory;
import org.bson.BSONObject;
import org.bson.BasicBSONObject;
import org.jongo.MongoCollection;
import org.jongo.util.JongoTestCase;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;

import static org.fest.assertions.Assertions.assertThat;

public class MongoDumpTest extends JongoTestCase {

    private MongoCollection collection;

    @Before
    public void setUp() throws Exception {
        collection = createEmptyCollection("friends");
    }

    @After
    public void tearDown() throws Exception {
        dropCollection("friends");
    }

    @Test
    public void importBsonDumpFileIntoCollection() throws Exception {

        InputStream bsonDump = getClass().getClassLoader().getResourceAsStream("1000friends.bson");
        BsonFactory bsonFactory = new BsonFactory();
        //bsonFactory.enable(BsonParser.Feature.HONOR_DOCUMENT_LENGTH); // fails when enabled
        ObjectReader reader = new ObjectMapper(bsonFactory).reader(BasicBSONObject.class);

        MappingIterator<BSONObject> iterator = reader.readValues(bsonDump);
        try {
            while (iterator.hasNext()) {
                BSONObject bsonObject = iterator.next();
                collection.withWriteConcern(WriteConcern.SAFE).save(bsonObject);
            }
        } finally {
            iterator.close();
        }

        assertThat(collection.count()).isEqualTo(1000);
    }
}

Remove safeHasNext method thanks to bson4jackson 2.1.1