/*
 * Copyright 2016 Google Inc. All Rights Reserved.
 *
 * 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.example.appengine;

import static com.google.common.truth.Truth.assertThat;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.PropertyProjection;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig;
import com.google.appengine.tools.development.testing.LocalServiceTestHelper;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

import java.util.List;

/**
 * Unit tests to demonstrate App Engine Datastore projection queries.
 */
@RunWith(JUnit4.class)
public class ProjectionTest {

  private final LocalServiceTestHelper helper =
      new LocalServiceTestHelper(
          // Set no eventual consistency, that way queries return all results.
          // https://cloud.google.com/appengine/docs/java/tools/localunittesting#Java_Writing_High_Replication_Datastore_tests
          new LocalDatastoreServiceTestConfig()
              .setDefaultHighRepJobPolicyUnappliedJobPercentage(0));

  private DatastoreService datastore;

  @Before
  public void setUp() throws Exception {
    helper.setUp();
    datastore = DatastoreServiceFactory.getDatastoreService();
  }

  @After
  public void tearDown() {
    helper.tearDown();
  }


  @Test
  public void projectionQuery_grouping_filtersDuplicates() {
    putTestData("some duplicate", 0L);
    putTestData("some duplicate", 0L);
    putTestData("too big", 1L);

    Query q = new Query("TestKind");
    q.addProjection(new PropertyProjection("A", String.class));
    q.addProjection(new PropertyProjection("B", Long.class));
    q.setDistinct(true);
    q.setFilter(Query.FilterOperator.LESS_THAN.of("B", 1L));
    q.addSort("B", Query.SortDirection.DESCENDING);
    q.addSort("A");

    List<Entity> entities =
        datastore.prepare(q).asList(FetchOptions.Builder.withLimit(5));
    assertThat(entities).hasSize(1);
    // [START_EXCLUDE silent]
    Entity entity = entities.get(0);
    assertThat((String) entity.getProperty("A")).named("entity.A").isEqualTo("some duplicate");
    assertThat((long) entity.getProperty("B")).named("entity.B").isEqualTo(0L);
    // [END_EXCLUDE]
  }

  private void putTestData(String a, long b) {
    Entity entity = new Entity("TestKind");
    entity.setProperty("A", a);
    entity.setProperty("B", b);
    datastore.put(entity);
  }
}

@@ -2324,24 +2324,48 @@
 big%22, 1L);%0A%0A
+    // %5BSTART grouping%5D%0A
     Query q 
@@ -2385,24 +2385,24 @@
 TestKind%22);%0A
-
     q.addPro
@@ -2672,16 +2672,38 @@
 rt(%22A%22);
+%0A    // %5BEND grouping%5D
 %0A%0A    Li
@@ -2837,38 +2837,8 @@
 1);%0A
-    // %5BSTART_EXCLUDE silent%5D%0A
     
@@ -2962,24 +2962,24 @@
 uplicate%22);%0A
+
     assertTh
@@ -3050,29 +3050,8 @@
 L);%0A
-    // %5BEND_EXCLUDE%5D%0A
   %7D%0A

Add region tags for projection query sample.