/**
 * Copyright 2014 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 org.kitesdk.data.hbase;

import java.io.InputStream;

import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTablePool;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.kitesdk.data.ColumnMapping;
import org.kitesdk.data.DatasetDescriptor;
import org.kitesdk.data.FieldMapping;
import org.kitesdk.data.PartitionStrategy;
import org.kitesdk.data.hbase.avro.AvroUtils;
import org.kitesdk.data.hbase.impl.SchemaManager;
import org.kitesdk.data.hbase.manager.DefaultSchemaManager;
import org.kitesdk.data.hbase.testing.HBaseTestUtils;

import static org.junit.Assert.assertEquals;

public class HBaseMetadataProviderTest {

  private static final String testEntity;
  private static final String tableName = "testtable";
  private static final String managedTableName = "managed_schemas";
  private static HBaseMetadataProvider provider;

  static {
    try {
      testEntity = AvroUtils
          .inputStreamToString(HBaseMetadataProviderTest.class
              .getResourceAsStream("/TestEntity.avsc"));
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }

  @BeforeClass
  public static void beforeClass() throws Exception {
    HTablePool tablePool = HBaseTestUtils.startHBaseAndGetPool();

    // managed table should be created by HBaseDatasetRepository
    HBaseTestUtils.util.deleteTable(Bytes.toBytes(managedTableName));

    SchemaManager schemaManager = new DefaultSchemaManager(tablePool);
    HBaseAdmin admin = new HBaseAdmin(HBaseTestUtils.getConf());
    provider = new HBaseMetadataProvider(admin, schemaManager);
  }

  @AfterClass
  public static void afterClass() throws Exception {
    HBaseTestUtils.util.deleteTable(Bytes.toBytes(tableName));
  }

  @After
  public void after() throws Exception {
    HBaseTestUtils.util.truncateTable(Bytes.toBytes(tableName));
    HBaseTestUtils.util.truncateTable(Bytes.toBytes(managedTableName));
  }

  @Test
  public void testBasic() {
    DatasetDescriptor desc = provider.create(tableName + ".TestEntity",
        new DatasetDescriptor.Builder().schemaLiteral(testEntity).build());
    ColumnMapping columnMapping = desc.getColumnMapping();
    PartitionStrategy partStrat = desc.getPartitionStrategy();
    assertEquals(9, columnMapping.getFieldMappings().size());
    assertEquals(2, partStrat.getFieldPartitioners().size());
  }

  @Test
  public void testOverride() {
    InputStream partStratIn = HBaseMetadataProviderTest.class
        .getResourceAsStream("/partition_strategy/AlternateTestEntity.json");
    InputStream columnMappingIn = HBaseMetadataProviderTest.class
        .getResourceAsStream("/column_mapping/AlternateTestEntity.json");

    DatasetDescriptor desc = provider.create(
        tableName + ".AlternateTestEntity", new DatasetDescriptor.Builder()
            .schemaLiteral(testEntity).partitionStrategy(partStratIn)
            .columnMapping(columnMappingIn).build());

    ColumnMapping columnMapping = desc.getColumnMapping();
    PartitionStrategy partStrat = desc.getPartitionStrategy();

    assertEquals(1, partStrat.getFieldPartitioners().size());
    assertEquals(8, columnMapping.getFieldMappings().size());

    assertEquals("alt", columnMapping.getFieldMapping("field1")
        .getFamilyAsString());
    assertEquals("field1", columnMapping.getFieldMapping("field1")
        .getQualifierAsString());
    assertEquals("altstring", columnMapping.getFieldMapping("field3")
        .getFamilyAsString());
    assertEquals(FieldMapping.MappingType.COLUMN, columnMapping.getFieldMapping("field4")
        .getMappingType());
  }
}

@@ -624,37 +624,8 @@
 e;%0A%0A
-import java.io.InputStream;%0A%0A
 impo
@@ -955,46 +955,8 @@
 or;%0A
-import org.kitesdk.data.FieldMapping;%0A
 impo
@@ -2997,1240 +2997,6 @@
  %7D%0A%0A
-  @Test%0A  public void testOverride() %7B%0A    InputStream partStratIn = HBaseMetadataProviderTest.class%0A        .getResourceAsStream(%22/partition_strategy/AlternateTestEntity.json%22);%0A    InputStream columnMappingIn = HBaseMetadataProviderTest.class%0A        .getResourceAsStream(%22/column_mapping/AlternateTestEntity.json%22);%0A%0A    DatasetDescriptor desc = provider.create(%0A        tableName + %22.AlternateTestEntity%22, new DatasetDescriptor.Builder()%0A            .schemaLiteral(testEntity).partitionStrategy(partStratIn)%0A            .columnMapping(columnMappingIn).build());%0A%0A    ColumnMapping columnMapping = desc.getColumnMapping();%0A    PartitionStrategy partStrat = desc.getPartitionStrategy();%0A%0A    assertEquals(1, partStrat.getFieldPartitioners().size());%0A    assertEquals(8, columnMapping.getFieldMappings().size());%0A%0A    assertEquals(%22alt%22, columnMapping.getFieldMapping(%22field1%22)%0A        .getFamilyAsString());%0A    assertEquals(%22field1%22, columnMapping.getFieldMapping(%22field1%22)%0A        .getQualifierAsString());%0A    assertEquals(%22altstring%22, columnMapping.getFieldMapping(%22field3%22)%0A        .getFamilyAsString());%0A    assertEquals(FieldMapping.MappingType.COLUMN, columnMapping.getFieldMapping(%22field4%22)%0A        .getMappingType());%0A  %7D%0A
 %7D%0A

Remove duplicate test that breaks others.