/*
 * 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.DBObject;
import org.jongo.model.Friend;
import org.jongo.util.JongoTestCase;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;

public class FindPartialFieldTest extends JongoTestCase {

    private MongoCollection collection;
    private Friend friend;

    @Before
    public void setUp() throws Exception {
        collection = createEmptyCollection("friends");
        friend = new Friend("John", "22 Wall Street Avenue");
    }

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

    @Test
    public void canFind() throws Exception {
        /* given */
        collection.save(friend);

        /* when */
        collection.find("{name:'John'}").projection("{name:1}").map(new AssertionResultHandler());
    }

    @Test
    public void canFindWithProjectionParams() throws Exception {
        /* given */
        collection.save(friend);

        /* when */
        collection.find("{name:'John'}").projection("{name:#}", 1).map(new AssertionResultHandler());
    }

    @Test
    public void canFindWithComplexProjection() throws Exception {
        /* given */
        collection.insert("{subElements: [{ name: \"foo\"},{ name: \"bar\"}]}");

        /* when */
        Iterator<Map> maps = collection.find().projection("{ subElements: {$elemMatch: {name: #} } }", "bar").as(Map.class);

        Map map = maps.next();
        assertThat(map.get("subElements")).isNotNull();
        List subElements = ((List) map.get("subElements"));
        assertThat(subElements).hasSize(1);
    }

    @Test
    public void canFindOne() throws Exception {
        /* given */
        collection.save(friend);

        /* when */
        Boolean result = collection.findOne("{name:'John'}").projection("{name:1}").map(new AssertionResultHandler());

        assertThat(result).isTrue();
    }

    @Test
    public void canFindOneWithProjectionParams() throws Exception {
        /* given */
        collection.save(friend);

        /* when */
        Boolean result = collection.findOne("{name:'John'}").projection("{name:#}", 1).map(new AssertionResultHandler());

        assertThat(result).isTrue();
    }

    @Test
    public void canFindOneWithComplexProjection() throws Exception {
        /* given */
        collection.insert("{subElements: [{ name: \"foo\"},{ name: \"bar\"}]}");

        /* when */
        Map map = collection.findOne().projection("{ subElements: {$elemMatch: {name: #} } }", "bar").as(Map.class);

        assertThat(map).isNotNull();
        assertThat(map.get("subElements")).isNotNull();
        List subElements = ((List) map.get("subElements"));
        assertThat(subElements).hasSize(1);
    }

    @Test
    public void shouldIgnoreNullProjection() throws Exception {
        /* given */
        collection.save(friend);

        /* when */
        Friend result = collection.findOne("{name:'John'}").projection(null).as(Friend.class);

        assertThat(friend.getName()).isEqualTo("John");
        assertThat(friend.getAddress()).isEqualTo("22 Wall Street Avenue");
    }

    private static class AssertionResultHandler implements ResultHandler<Boolean> {
        public Boolean map(DBObject result) {
            assertThat(result.containsField("address")).isFalse();
            assertThat(result.containsField("name")).isTrue();
            return true;
        }
    }
}

@@ -728,16 +728,51 @@
 Object;%0A
+import org.jongo.model.Coordinate;%0A
 import o
@@ -3966,24 +3966,458 @@
 e%22);%0A    %7D%0A%0A
+    @Test%0A    public void canFindOneWithProjectionParamsWithDot() throws Exception %7B%0A        collection.save(new Friend(%22John%22, %2223 Wall Street Av.%22, new Coordinate(1, 1)));%0A%0A        Map map = collection.findOne(%22%7Bname:'John'%7D%22).projection(%22%7Bcoordinate.lat:#%7D%22, 1).as(Map.class);%0A%0A        assertThat(((Map) map.get(%22coordinate%22)).get(%22lng%22)).isNull();%0A        assertThat(((Map) map.get(%22coordinate%22)).get(%22lat%22)).isNotNull();%0A    %7D%0A%0A%0A
     private 

Add a test with projection with dots