/*
 * 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;

import com.mongodb.MongoException.DuplicateKey;
import com.mongodb.WriteConcern;
import junit.framework.Assert;
import org.jongo.model.Coordinate;
import org.jongo.model.Friend;
import org.jongo.util.JongoTestCase;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import static org.fest.assertions.Assertions.assertThat;
import static org.mockito.Mockito.mock;

public class MongoCollectionTest 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 canCreateACollection() throws Exception {

        MongoCollection mongoCollection = new MongoCollection(null, mock(Mapper.class));

        assertThat(mongoCollection).isNotNull();
    }

    @Test
    public void canUseConditionnalOperator() throws Exception {
        /* given */
        collection.save(new Coordinate(1, 1));
        collection.save(new Coordinate(2, 1));
        collection.save(new Coordinate(3, 1));

        /* then */
        assertThat(collection.find("{lat: {$gt: 2}}").as(Coordinate.class)).hasSize(1);
        assertThat(collection.find("{lat: {$lt: 2}}").as(Coordinate.class)).hasSize(1);
        assertThat(collection.find("{lat: {$gte: 2}}").as(Coordinate.class)).hasSize(2);
        assertThat(collection.find("{lat: {$lte: 2}}").as(Coordinate.class)).hasSize(2);
        assertThat(collection.find("{lat: {$gt: 1, $lt: 3}}").as(Coordinate.class)).hasSize(1);

        assertThat(collection.find("{lat: {$ne: 2}}").as(Coordinate.class)).hasSize(2);
        assertThat(collection.find("{lat: {$in: [1,2,3]}}").as(Coordinate.class)).hasSize(3);
    }

    @Test
    public void createIndexWithUniqueAsOption() {
        collection.ensureIndex("{name: 1}", "{unique: true}");
        collection.save(new Friend("John"), WriteConcern.SAFE);

        try {
            collection.save(new Friend("John"), WriteConcern.SAFE);
            Assert.fail();
        } catch (DuplicateKey e) {
        }
    }

    @Test
    public void canCreateGeospacialIndex() throws Exception {
        /* given */
        collection.save(new Friend("John", new Coordinate(1, 1)));
        collection.save(new Friend("Peter", new Coordinate(4, 4)));

        collection.ensureIndex("{ 'coordinate' : '2d'}");

        /* then */
        assertThat(collection.find("{'coordinate': {'$near': [0,0], $maxDistance: 5}}").as(Friend.class)).hasSize(1);
        assertThat(collection.find("{'coordinate': {'$near': [2,2], $maxDistance: 5}}").as(Friend.class)).hasSize(2);
        assertThat(collection.find("{'coordinate': {'$within': {'$box': [[0,0],[2,2]]}}}").as(Friend.class)).hasSize(1);
        assertThat(collection.find("{'coordinate': {'$within': {'$center': [[0,0],5]}}}").as(Friend.class)).hasSize(1);
    }

    @Test
    public void canDropIndex() {

        //given
        collection.ensureIndex("{name: 1}", "{unique: true}");
        collection.save(new Friend("John"), WriteConcern.SAFE);

        //when
        collection.dropIndex("{name: 1}");

        //then
        collection.save(new Friend("John"), WriteConcern.SAFE);
    }

    @Test
    public void canDropIndexes() {

        //given
        collection.ensureIndex("{name: 1}", "{unique: true}");
        collection.ensureIndex("{way: 1}", "{unique: true}");
        collection.save(new Friend("John", "way"), WriteConcern.SAFE);

        //when
        collection.dropIndexes();

        //then
        collection.save(new Friend("John", "way"), WriteConcern.SAFE);
    }

    @Test
    public void canGetCollectionName() throws Exception {
        assertThat(collection.getName()).isEqualTo("friends");
    }
}

@@ -1404,223 +1404,8 @@
  %7D%0A%0A
-    @Test%0A    public void canCreateACollection() throws Exception %7B%0A%0A        MongoCollection mongoCollection = new MongoCollection(null, mock(Mapper.class));%0A%0A        assertThat(mongoCollection).isNotNull();%0A    %7D%0A%0A
     

Remove useless test