/*
 * Copyright 2011-2018 B2i Healthcare Pte Ltd, http://b2i.sg
 * 
 * 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 com.b2international.index.revision;

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

import java.util.Collection;

import org.junit.Ignore;
import org.junit.Test;

import com.b2international.index.Fixtures.NestedData;
import com.b2international.index.Fixtures.ParentData;
import com.b2international.index.query.Expressions;
import com.b2international.index.query.Query;
import com.b2international.index.revision.RevisionFixtures.DeeplyNestedData;
import com.google.common.collect.ImmutableList;

/**
 * @since 4.7
 */
public class ComplexDocumentRevisionIndexTest extends BaseRevisionIndexTest {

	@Override
	protected final Collection<Class<?>> getTypes() {
		return ImmutableList.<Class<?>>of(DeeplyNestedData.class);
	}
	
	@Test
	public void searchDeeplyNestedDocument() throws Exception {
		final DeeplyNestedData data = new DeeplyNestedData(STORAGE_KEY1, new ParentData("field1", new NestedData("field2")));
		final DeeplyNestedData data2 = new DeeplyNestedData(STORAGE_KEY2, new ParentData("field12", new NestedData("field22")));
		
		indexRevision(MAIN, data, data2);
		
		final Query<DeeplyNestedData> deeplyNestedQuery = Query.select(DeeplyNestedData.class).where(Expressions.nestedMatch("parentData.nestedData", Expressions.exactMatch("field2", "field2"))).build();
		final Iterable<DeeplyNestedData> matches = search(MAIN, deeplyNestedQuery);
		assertThat(matches).hasSize(1);
		assertThat(matches).containsOnly(data);
	}
	
	@Ignore("We don't support this anymore")
	@Test
	public void searchAndReturnDeeplyNestedDocument() throws Exception {
		final NestedData nestedData = new NestedData("field2");
		final DeeplyNestedData data = new DeeplyNestedData(STORAGE_KEY1, new ParentData("field1", nestedData));
		final DeeplyNestedData data2 = new DeeplyNestedData(STORAGE_KEY2, new ParentData("field12", new NestedData("field22")));
		
		indexRevision(MAIN, data, data2);
		
		final Query<NestedData> query = Query.select(NestedData.class).from(DeeplyNestedData.class).where(Expressions.exactMatch("field2", "field2")).build();
		final Iterable<NestedData> matches = search(MAIN, query);
		assertThat(matches).hasSize(1);
		assertThat(matches).containsOnly(nestedData);
	}
	
}

@@ -20,10 +20,10 @@
 1-20
+2
 1
-8
  B2i
@@ -759,33 +759,8 @@
 n;%0A%0A
-import org.junit.Ignore;%0A
 impo
@@ -1995,32 +1995,32 @@
 es).hasSize(1);%0A
+
 %09%09assertThat(mat
@@ -2054,754 +2054,6 @@
 %7D%0A%09%0A
-%09@Ignore(%22We don't support this anymore%22)%0A%09@Test%0A%09public void searchAndReturnDeeplyNestedDocument() throws Exception %7B%0A%09%09final NestedData nestedData = new NestedData(%22field2%22);%0A%09%09final DeeplyNestedData data = new DeeplyNestedData(STORAGE_KEY1, new ParentData(%22field1%22, nestedData));%0A%09%09final DeeplyNestedData data2 = new DeeplyNestedData(STORAGE_KEY2, new ParentData(%22field12%22, new NestedData(%22field22%22)));%0A%09%09%0A%09%09indexRevision(MAIN, data, data2);%0A%09%09%0A%09%09final Query%3CNestedData%3E query = Query.select(NestedData.class).from(DeeplyNestedData.class).where(Expressions.exactMatch(%22field2%22, %22field2%22)).build();%0A%09%09final Iterable%3CNestedData%3E matches = search(MAIN, query);%0A%09%09assertThat(matches).hasSize(1);%0A%09%09assertThat(matches).containsOnly(nestedData);%0A%09%7D%0A%09%0A
 %7D%0A

remove obsolete test