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

@@ -869,16 +869,62 @@
 .Test;%0A%0A
+import java.util.List;%0Aimport java.util.Map;%0A%0A
 import s
@@ -973,16 +973,16 @@
 rtThat;%0A
-
 %0Apublic 
@@ -1871,16 +1871,587 @@
     %7D%0A%0A%0A
+    @Test%0A    public void canFindWithComplexProjection() throws Exception %7B%0A        /* given */%0A        collection.insert(%22%7BsubElements: %5B%7B name: %5C%22foo%5C%22%7D,%7B name: %5C%22bar%5C%22%7D%5D%7D%22);%0A%0A        /* when */%0A        Iterable%3CMap%3E maps = collection.find().projection(%22%7B subElements: %7B$elemMatch: %7Bname: #%7D %7D %7D%22, %22bar%22).as(Map.class);%0A%0A        assertThat(maps).isNotEmpty();%0A        Map map = maps.iterator().next();%0A        assertThat(map.get(%22subElements%22)).isNotNull();%0A        List subElements = ((List) map.get(%22subElements%22));%0A        assertThat(subElements).hasSize(1);%0A    %7D%0A%0A
     @Tes
@@ -3003,32 +3003,32 @@
 ultHandler());%0A%0A
-
         assertTh
@@ -3047,32 +3047,554 @@
 sTrue();%0A    %7D%0A%0A
+    @Test%0A    public void canFindOneWithComplexProjection() throws Exception %7B%0A        /* given */%0A        collection.insert(%22%7BsubElements: %5B%7B name: %5C%22foo%5C%22%7D,%7B name: %5C%22bar%5C%22%7D%5D%7D%22);%0A%0A        /* when */%0A        Map map = collection.findOne().projection(%22%7B subElements: %7B$elemMatch: %7Bname: #%7D %7D %7D%22, %22bar%22).as(Map.class);%0A%0A        assertThat(map).isNotNull();%0A        assertThat(map.get(%22subElements%22)).isNotNull();%0A        List subElements = ((List) map.get(%22subElements%22));%0A        assertThat(subElements).hasSize(1);%0A    %7D%0A%0A
     @Test%0A    pu

Add projection test with $elemMatch