/*
 * 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.fasterxml.jackson.annotation.JsonProperty;
import com.mongodb.DBObject;
import com.mongodb.WriteConcern;
import org.fest.assertions.Condition;
import org.jongo.util.JongoTestCase;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.List;

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

public class CommandTest extends JongoTestCase {

    private Jongo jongo;
    private MongoCollection collection;

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

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

    @Test
    public void canRunACommand() throws Exception {
        DBObject result = jongo.runCommand("{ ping: 1 }").map(new RawResultHandler<DBObject>());

        assertThat(result).isNotNull();
        assertThat(result.get("ok")).isEqualTo(1.0);
    }

    @Test
    public void canRunACommandWithParameter() throws Exception {

        collection.withWriteConcern(WriteConcern.SAFE).insert("{test:1}");

        DBObject result = jongo.runCommand("{ count: #}", "friends").map(new RawResultHandler<DBObject>());

        assertThat(result.get("n")).isEqualTo(1.0);
    }

    @Test
    public void canRunAGeoNearCommand() throws Exception {

        MongoCollection safeCollection = collection.withWriteConcern(WriteConcern.SAFE);
        safeCollection.insert("{loc:{lat:48.690833,lng:9.140556}, name:'Paris'}");
        safeCollection.ensureIndex("{loc:'2d'}");

        GeoNearResult geoNearResult = jongo.runCommand("{ geoNear : 'friends', near : [48.690,9.140], spherical: true}")
                .throwOnError()
                .as(GeoNearResult.class);

        List<Location> locations = geoNearResult.locations;
        assertThat(locations.size()).isEqualTo(1);
        assertThat(locations.get(0).dis).satisfies(new Condition<Double>() {
            @Override
            public boolean matches(Double value) {
                return value instanceof Double && value > 1.7E-5 && value < 1.8E-5;
            }
        });
        assertThat(locations.get(0).getName()).isEqualTo("Paris");
    }

    @Test
    public void canRunACommandAs() throws Exception {
        Validate status = jongo.runCommand("{ validate: 1 }").as(Validate.class);

        assertThat(status.errmsg).isNotNull();
        assertThat(status.ok).isEqualTo("0.0");
    }

    @Test
    public void canRunInvalidCommand() throws Exception {
        Validate status = jongo.runCommand("{forceerror:1}").as(Validate.class);

        assertThat(status.ok).isEqualTo("0.0");
    }

    @Test
    public void mustForceExceptionToBeThrownOnInvalidCommand() throws Exception {
        try {
            jongo.runCommand("{forceerror:1}").throwOnError().as(Validate.class);
        } catch (RuntimeException e) {
            assertThat(e.getMessage()).contains("forced error");
        }
    }

    private static class Validate {
        String ok, errmsg;
    }

    private static class GeoNearResult {
        @JsonProperty("results")
        List<Location> locations;
    }

    private static class Location {

        double dis;

        /**
         * Real Location document is contained into 'obj' property
         * Jackson doesn't support nested mapping. see http://jira.codehaus.org/browse/JACKSON-781
         */
        @JsonProperty("obj")
        NestedLocation nestedLocation;

        public String getName() {
            return nestedLocation.locationName;
        }
    }

    private static class NestedLocation {
        @JsonProperty("name")
        String locationName;
    }
}
@@ -830,33 +830,101 @@
 org.
-fest.assertions.Condition
+bson.types.BasicBSONList;%0Aimport org.fest.assertions.Condition;%0Aimport org.jongo.model.Friend
 ;%0Aim
@@ -1725,32 +1725,426 @@
 To(1.0);%0A    %7D%0A%0A
+    @Test%0A    public void canRunAnEvalCommand() throws Exception %7B%0A%0A        String js = %22return db.getCollectionNames();%22;%0A        DBObject result = jongo.runCommand(%22%7B eval: # %7D%22, js).map(new RawResultHandler%3CDBObject%3E());%0A%0A        assertThat(result).isNotNull();%0A        List%3C?%3E jsResult = (BasicBSONList) result.get(%22retval%22);%0A        assertThat(jsResult).contains(%22system.indexes%22);%0A    %7D%0A%0A
     @Test%0A    pu

Add eval command test