/*
 * Copyright (C) 2014 Andrés Testi.
 *
 * 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 blobstoretest;

import static org.junit.Assert.assertEquals;
import static blobstoretest.FileDataConverters.fileDataToEntity;

import blobstoretest.shared.FileData;

import com.google.appengine.api.blobstore.BlobKey;
import com.google.appengine.api.blobstore.BlobstoreService;
import com.google.appengine.api.blobstore.BlobstoreServiceFactory;
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.tools.development.testing.LocalDatastoreServiceTestConfig;
import com.google.appengine.tools.development.testing.LocalServiceTestHelper;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

/**
 * Tests for the Blobstoretest API.
 * 
 * @author Andrés Testi
 */
public class ApiTest {

  private final LocalServiceTestHelper helper = new LocalServiceTestHelper(
      new LocalDatastoreServiceTestConfig());

  @Before
  public void setUp() {
    helper.setUp();
  }

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

  @Test
  public void testlistFiles() {

    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    BlobstoreService blobstore = BlobstoreServiceFactory.getBlobstoreService();

    Blobstoretest blobstoretest = new Blobstoretest(datastore, blobstore);

    List<FileData> savedFiles = generateFiles(10);
    Iterable<Entity> entities = Iterables.transform(savedFiles, fileDataToEntity());    
    datastore.put(entities);
    
    List<FileData> expected = savedFiles.subList(0, 5);
    Iterable<FileData> actual = Lists.newArrayList(blobstoretest.listFiles(null, 0, 5));

    assertEquals(expected, actual);
  }
    
  private List<FileData> generateFiles(int count) {
    ArrayList<FileData> files = new ArrayList<>();
    for (int i = 0; i < count; i++) {
      files.add(new FileData("file" + i + ".txt", new BlobKey("file" + i)));
    }
    return files;
  }
}

@@ -618,53 +618,8 @@
 t;%0A%0A
-import static org.junit.Assert.assertEquals;%0A
 impo
@@ -678,16 +678,61 @@
 oEntity;
+%0Aimport static org.junit.Assert.assertEquals;
 %0A%0Aimport
@@ -1331,48 +1331,8 @@
 les;
-%0Aimport com.google.common.collect.Lists;
 %0A%0Aim
@@ -2308,24 +2308,20 @@
 5);%0A    
-Iterable
+List
 %3CFileDat
@@ -2336,27 +2336,8 @@
 l = 
-Lists.newArrayList(
 blob
@@ -2367,17 +2367,16 @@
 l, 0, 5)
-)
 ;%0A%0A    a

update imports