/**
 * Copyright 2013 Cloudera Inc.
 *
 * 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.cloudera.data.crunch;

import com.cloudera.data.Dataset;
import com.cloudera.data.DatasetDescriptor;
import com.cloudera.data.DatasetRepository;
import com.cloudera.data.filesystem.CrunchDatasets;
import com.cloudera.data.filesystem.FileSystemDatasetRepository;
import com.google.common.io.Files;
import java.io.IOException;
import org.apache.avro.generic.GenericData;
import org.apache.crunch.PCollection;
import org.apache.crunch.Pipeline;
import org.apache.crunch.Target;
import org.apache.crunch.impl.mr.MRPipeline;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.junit.Before;
import org.junit.Test;

import static com.cloudera.data.filesystem.DatasetTestUtilities.*;

public class TestCrunchDatasets {

  private FileSystem fileSystem;
  private Path testDirectory;
  private DatasetRepository repo;

  @Before
  public void setUp() throws IOException {
    fileSystem = FileSystem.get(new Configuration());
    testDirectory = new Path(Files.createTempDir().getAbsolutePath());
    repo = new FileSystemDatasetRepository(fileSystem, testDirectory);
  }

  @Test
  public void testGeneric() throws IOException {
    Dataset inputDataset = repo.create("in", new DatasetDescriptor.Builder()
        .schema(USER_SCHEMA).get());
    Dataset outputDataset = repo.create("out", new DatasetDescriptor.Builder()
        .schema(USER_SCHEMA).get());

    writeTestUsers(inputDataset, 10);

    Pipeline pipeline = new MRPipeline(TestCrunchDatasets.class);
    PCollection<GenericData.Record> data = pipeline.read(
        CrunchDatasets.asSource(inputDataset, GenericData.Record.class));
    pipeline.write(data, CrunchDatasets.asTarget(outputDataset), Target.WriteMode.APPEND);
    pipeline.run();

    checkTestUsers(outputDataset, 10);
  }

}

@@ -736,32 +736,76 @@
 asetRepository;%0A
+import com.cloudera.data.PartitionStrategy;%0A
 import com.cloud
@@ -964,24 +964,55 @@
 OException;%0A
+import junit.framework.Assert;%0A
 import org.a
@@ -2498,10 +2498,945 @@
 );%0A  %7D%0A%0A
+  @Test%0A  public void testPartitionedSource() throws IOException %7B%0A    PartitionStrategy partitionStrategy = new PartitionStrategy.Builder().hash(%0A        %22username%22, 2).get();%0A%0A    Dataset inputDataset = repo.create(%22in%22, new DatasetDescriptor.Builder()%0A        .schema(USER_SCHEMA).partitionStrategy(partitionStrategy).get());%0A    Dataset outputDataset = repo.create(%22out%22, new DatasetDescriptor.Builder()%0A        .schema(USER_SCHEMA).get());%0A%0A    writeTestUsers(inputDataset, 10);%0A%0A    Dataset part = inputDataset.getPartition(partitionStrategy.partitionKey(0), false);%0A    Pipeline pipeline = new MRPipeline(TestCrunchDatasets.class);%0A    PCollection%3CGenericData.Record%3E data = pipeline.read(%0A        CrunchDatasets.asSource(part, GenericData.Record.class));%0A    pipeline.write(data, CrunchDatasets.asTarget(outputDataset), Target.WriteMode.APPEND);%0A    pipeline.run();%0A%0A    Assert.assertEquals(5, datasetSize(outputDataset));%0A  %7D%0A
 %7D%0A

Add test for Crunch partitioned source.