@Test public void testGetMulitipleLayouts() throws Exception { final KijiTableLayout layout1 = KijiTableLayout.newLayout(KijiTableLayouts.getLayout(KijiTableLayouts.SIMPLE)); final KijiTableLayout layout2 = KijiTableLayout.newLayout(KijiTableLayouts.getLayout(KijiTableLayouts.SIMPLE)); final KijiTableLayout layout3 = KijiTableLayout.newLayout(KijiTableLayouts.getLayout(KijiTableLayouts.SIMPLE)); layout1.getDesc().setVersion("layout-1.0"); layout2.getDesc().setVersion("layout-1.0.1"); layout3.getDesc().setVersion("layout-1.1"); final Get expectedGet = new Get(Bytes.toBytes(layout1.getDesc().getName())) .addColumn( Bytes.toBytes(mFamily), Bytes.toBytes(HBaseTableLayoutDatabase.QUALIFIER_LAYOUT)) .setMaxVersions(2); final List<KeyValue> kvs = new ArrayList<KeyValue>(); kvs.add( new KeyValue( Bytes.toBytes(layout3.getDesc().getName()), Bytes.toBytes(mFamily), Bytes.toBytes(HBaseTableLayoutDatabase.QUALIFIER_LAYOUT), 3L, encode(layout3.getDesc()))); kvs.add( new KeyValue( Bytes.toBytes(layout2.getDesc().getName()), Bytes.toBytes(mFamily), Bytes.toBytes(HBaseTableLayoutDatabase.QUALIFIER_LAYOUT), 2L, encode(layout2.getDesc()))); Result cannedResult = new Result(kvs); expect(mHTable.get(eqGet(expectedGet))).andReturn(cannedResult); replay(mHTable); NavigableMap<Long, KijiTableLayout> timedLayouts = mDb.getTimedTableLayoutVersions(layout1.getDesc().getName().toString(), 2); Set<Long> timestamps = timedLayouts.keySet(); Iterator<Long> iterator = timestamps.iterator(); assertEquals(2, timedLayouts.size()); long time2 = iterator.next(); long time3 = iterator.next(); assertEquals(2L, time2); assertEquals(layout2, timedLayouts.get(time2)); assertEquals(3L, time3); assertEquals(layout3, timedLayouts.get(time3)); verify(mHTable); }
@Test public void testMultipleArguments() throws Exception { final Kiji kiji = getKiji(); final KijiTableLayout layout = KijiTableLayouts.getTableLayout(KijiTableLayouts.FORMATTED_RKF); new InstanceBuilder(kiji) .withTable(layout.getName(), layout) .withRow("dummy", "str1", "str2", 1, 2L) .withFamily("family") .withQualifier("column") .withValue(1L, "string-value") .withValue(2L, "string-value2") .withRow("dummy", "str1", "str2", 1) .withFamily("family") .withQualifier("column") .withValue(1L, "string-value") .withRow("dummy", "str1", "str2") .withFamily("family") .withQualifier("column") .withValue(1L, "string-value") .withRow("dummy", "str1") .withFamily("family") .withQualifier("column") .withValue(1L, "string-value") .withRow("dummy") .withFamily("family") .withQualifier("column") .withValue(1L, "string-value") .build(); final KijiTableLayout layoutTwo = KijiTableLayouts.getTableLayout(KijiTableLayouts.FOO_TEST); kiji.createTable(layoutTwo.getDesc()); final KijiTable table = kiji.openTable(layout.getName()); try { final KijiTable tableTwo = kiji.openTable(layoutTwo.getName()); try { assertEquals( BaseTool.SUCCESS, runTool(new LsTool(), table.getURI().toString(), tableTwo.getURI().toString())); assertEquals(9, mToolOutputLines.length); assertEquals(BaseTool.SUCCESS, runTool(new LsTool(), kiji.getURI().toString())); assertEquals(2, mToolOutputLines.length); assertEquals( BaseTool.SUCCESS, runTool(new LsTool(), kiji.getURI().toString(), table.getURI().toString())); assertEquals(3, mToolOutputLines.length); } finally { tableTwo.release(); } } finally { table.release(); } }
@Test public void testScanTable() throws Exception { final Kiji kiji = getKiji(); final KijiTableLayout layout = KijiTableLayouts.getTableLayout(KijiTableLayouts.SIMPLE); kiji.createTable(layout.getName(), layout); final KijiTable table = kiji.openTable(layout.getName()); try { // Table is empty: assertEquals(BaseTool.SUCCESS, runTool(new LsTool(), "--kiji=" + table.getURI())); assertEquals(1, mToolOutputLines.length); assertTrue(mToolOutputLines[0].startsWith("Scanning kiji table: ")); new InstanceBuilder(kiji) .withTable(table) .withRow("hashed") .withFamily("family") .withQualifier("column") .withValue(314L, "value") .build(); // Table has now one row: assertEquals(BaseTool.SUCCESS, runTool(new LsTool(), "--kiji=" + table.getURI())); assertEquals(3, mToolOutputLines.length); assertTrue(mToolOutputLines[0].startsWith("Scanning kiji table: ")); assertTrue(mToolOutputLines[1].startsWith("entity-id=hbase='")); } finally { table.close(); } }
@Test public void testGetLayout() throws Exception { final KijiTableLayout version1 = KijiTableLayout.newLayout(KijiTableLayouts.getLayout(KijiTableLayouts.SIMPLE)); final Get expectedGet = new Get(Bytes.toBytes(version1.getDesc().getName())) .addColumn( Bytes.toBytes(mFamily), Bytes.toBytes(HBaseTableLayoutDatabase.QUALIFIER_LAYOUT)) .setMaxVersions(1); final List<KeyValue> kvs = new ArrayList<KeyValue>(); kvs.add( new KeyValue( Bytes.toBytes(version1.getDesc().getName()), Bytes.toBytes(mFamily), Bytes.toBytes(HBaseTableLayoutDatabase.QUALIFIER_LAYOUT), 1L, encode(version1.getDesc()))); final Result cannedResult = new Result(kvs); expect(mHTable.get(eqGet(expectedGet))).andReturn(cannedResult); replay(mHTable); assertEquals(version1, mDb.getTableLayout(version1.getDesc().getName().toString())); verify(mHTable); }
@Test public void testSetLayout() throws Exception { final TableLayoutDesc layoutDesc = KijiTableLayouts.getLayout(KijiTableLayouts.SIMPLE); final KijiTableLayout layout = KijiTableLayout.newLayout(layoutDesc); final Get expectedGet = new Get(Bytes.toBytes(layout.getDesc().getName())) .addColumn( Bytes.toBytes(mFamily), Bytes.toBytes(HBaseTableLayoutDatabase.QUALIFIER_LAYOUT)) .setMaxVersions(1); final Result expectedGetResult = new Result(Collections.<KeyValue>emptyList()); expect(mHTable.get(eqGet(expectedGet))).andReturn(expectedGetResult); final Put expectedPut = new Put(Bytes.toBytes(layout.getDesc().getName())) .add( Bytes.toBytes(mFamily), Bytes.toBytes(HBaseTableLayoutDatabase.QUALIFIER_UPDATE), encode(layoutDesc)) .add( Bytes.toBytes(mFamily), Bytes.toBytes(HBaseTableLayoutDatabase.QUALIFIER_LAYOUT), encode(layout.getDesc())) .add( Bytes.toBytes(mFamily), Bytes.toBytes(HBaseTableLayoutDatabase.QUALIFIER_LAYOUT_ID), Bytes.toBytes("1")); mHTable.put(eqPut(expectedPut)); replay(mHTable); mDb.updateTableLayout(layout.getDesc().getName(), layoutDesc); verify(mHTable); }
@Test public void testRetrieveAll() throws Exception { getKiji().createTable(KijiTableLayouts.getLayout(KijiTableLayouts.COUNTER_TEST)); final KijiFreshnessManager manager = KijiFreshnessManager.create(getKiji()); manager.storePolicy("user", "info:name", TestProducer.class, new NeverFreshen()); manager.storePolicy("user", "info:visits", TestProducer.class, new NeverFreshen()); LOG.info(KijiURI.newBuilder(getKiji().getURI()).withTableName("user").build().toString()); assertEquals( BaseTool.SUCCESS, runTool( new FreshTool(), KijiURI.newBuilder(getKiji().getURI()).withTableName("user").build().toString(), "--do=retrieve-all")); assertEquals( "Freshness policy attached to column: info:visits\n" + " Freshness policy class: org.kiji.scoring.lib.NeverFreshen\n" + " Freshness policy state: \n" + " Producer class: org.kiji.scoring.tools.TestFreshTool$TestProducer\n" + "Freshness policy attached to column: info:name\n" + " Freshness policy class: org.kiji.scoring.lib.NeverFreshen\n" + " Freshness policy state: \n" + " Producer class: org.kiji.scoring.tools.TestFreshTool$TestProducer\n", mToolOutputStr); }
@Test public void testBuilder() throws Exception { final KijiTableLayout layout = KijiTableLayout.newLayout(KijiTableLayouts.getLayout(KijiTableLayouts.SIMPLE)); final Kiji kiji = new InstanceBuilder() .withTable("table", layout) .withRow("row1") .withFamily("family") .withQualifier("column") .withValue(1, "foo1") .withValue(2, "foo2") .withRow("row2") .withFamily("family") .withQualifier("column") .withValue(100, "foo3") .build(); final KijiTable table = kiji.openTable("table"); final KijiTableReader reader = table.openTableReader(); // Verify the first row. final KijiDataRequest req = KijiDataRequest.create("family", "column"); final KijiRowData row1 = reader.get(table.getEntityId("row1"), req); assertEquals("foo2", row1.getValue("family", "column", 2).toString()); // Verify the second row. final KijiRowData row2 = reader.get(table.getEntityId("row2"), req); assertEquals("foo3", row2.getValue("family", "column", 100).toString()); ResourceUtils.closeOrLog(reader); ResourceUtils.releaseOrLog(table); ResourceUtils.releaseOrLog(kiji); }
@Test public void testTableExists() throws IOException { final KijiTableLayout simple = KijiTableLayout.newLayout(KijiTableLayouts.getLayout(KijiTableLayouts.SIMPLE)); final Kiji kiji = new InstanceBuilder(getKiji()).withTable("table", simple).build(); assertTrue(kiji.getMetaTable().tableExists(simple.getDesc().getName())); assertFalse(kiji.getMetaTable().tableExists("faketablename")); }
@Before public void readLayout() throws Exception { final KijiTableLayout tableLayout = KijiTableLayout.newLayout( KijiTableLayouts.getLayout(KijiTableLayouts.FULL_FEATURED_IDENTITY)); mTranslator = HBaseColumnNameTranslator.from(tableLayout); }
@Test public void testRetrieveAllEmpty() throws Exception { getKiji().createTable(KijiTableLayouts.getLayout(KijiTableLayouts.COUNTER_TEST)); assertEquals( BaseTool.SUCCESS, runTool( new FreshTool(), KijiURI.newBuilder(getKiji().getURI()).withTableName("user").build().toString(), "--do=retrieve-all")); assertEquals( "There are no freshness policies attached to columns in table: user", mToolOutputStr); }
/** Sets up one table to freshen and creates a file for a key value store. */ @Before public void setup() throws IOException { TableLayoutDesc userLayout = KijiTableLayouts.getLayout(KijiTableLayouts.COUNTER_TEST); TableLayoutDesc tableLayout = KijiTableLayouts.getLayout(KijiTableLayouts.SIMPLE); new InstanceBuilder(getKiji()) .withTable(userLayout) .withRow("felix") .withFamily("info") .withQualifier("name") .withValue("Felis") .build(); // Set up a file for a key value store as well. final File file = new File(getLocalTempDir(), KV_FILENAME); final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file))); try { writer.append("Jennyanydots\tOld Gumbie Cat\n"); writer.append("Skimbleshanks\tRailway Cat\n"); } finally { writer.close(); } }
@Test public void testTableNoFamilies() throws Exception { final Kiji kiji = new InstanceBuilder(getKiji()) .withTable(KijiTableLayouts.getLayout(KijiTableLayouts.NOFAMILY)) .build(); final KijiTable table = kiji.openTable("nofamily"); try { assertEquals(BaseTool.SUCCESS, runTool(new LsTool(), table.getURI().toString())); } finally { table.release(); } }
@Test public void testKijiLsStartAndLimitRow() throws Exception { final Kiji kiji = getKiji(); final KijiTableLayout layout = KijiTableLayouts.getTableLayout(KijiTableLayouts.FOO_TEST); kiji.createTable(layout.getDesc()); final KijiTable table = kiji.openTable(layout.getName()); try { assertEquals(BaseTool.SUCCESS, runTool(new LsTool(), table.getURI().toString())); // TODO: Validate output } finally { ResourceUtils.releaseOrLog(table); } }
@Test public void testListTables() throws Exception { final Kiji kiji = getKiji(); assertEquals(BaseTool.SUCCESS, runTool(new LsTool(), kiji.getURI().toString())); assertEquals(1, mToolOutputLines.length); final KijiTableLayout layout = KijiTableLayouts.getTableLayout(KijiTableLayouts.SIMPLE); kiji.createTable(layout.getDesc()); assertEquals(BaseTool.SUCCESS, runTool(new LsTool(), kiji.getURI().toString())); assertEquals(1, mToolOutputLines.length); assertEquals(kiji.getURI() + layout.getName(), mToolOutputLines[0]); }
@Test public void testCreateTableWithInvalidSchemaClassInLayout() throws Exception { final TableLayoutDesc layout = KijiTableLayouts.getLayout(KijiTableLayouts.INVALID_SCHEMA); final File layoutFile = getTempLayoutFile(layout); final KijiURI tableURI = KijiURI.newBuilder(getKiji().getURI()).withTableName(layout.getName()).build(); assertEquals( BaseTool.FAILURE, runTool(new CreateTableTool(), "--table=" + tableURI, "--layout=" + layoutFile)); assertEquals(2, mToolOutputLines.length); assertTrue( mToolOutputLines[1].startsWith( "Error: Schema with type 'class' must be a valid Java identifier.")); }
@Test public void testTableColumns() throws Exception { final Kiji kiji = getKiji(); final KijiTableLayout layout = KijiTableLayouts.getTableLayout(KijiTableLayouts.SIMPLE); kiji.createTable(layout.getDesc()); final KijiTable table = kiji.openTable(layout.getName()); try { // Table is empty: assertEquals(BaseTool.SUCCESS, runTool(new LsTool(), table.getURI().toString())); assertEquals(1, mToolOutputLines.length); assertTrue(mToolOutputLines[0].contains("family:column")); } finally { ResourceUtils.releaseOrLog(table); } }
@Test public void testUnregisterAll() throws Exception { getKiji().createTable(KijiTableLayouts.getLayout(KijiTableLayouts.COUNTER_TEST)); final KijiFreshnessManager manager = KijiFreshnessManager.create(getKiji()); manager.storePolicy("user", "info:name", TestProducer.class, new NeverFreshen()); manager.storePolicy("user", "info:visits", TestProducer.class, new NeverFreshen()); assertEquals( BaseTool.SUCCESS, runTool( new FreshTool(), KijiURI.newBuilder(getKiji().getURI()).withTableName("user").build().toString(), "--do=unregister-all")); assertEquals(0, manager.retrievePolicies("user").size()); assertEquals("All freshness policies removed from table: user", mToolOutputStr); }
@Test public void testListTables() throws Exception { final Kiji kiji = getKiji(); assertEquals(BaseTool.SUCCESS, runTool(new LsTool(), "--kiji=" + kiji.getURI())); assertEquals(1, mToolOutputLines.length); assertTrue(mToolOutputLines[0].startsWith("Listing tables in kiji instance:")); final KijiTableLayout layout = KijiTableLayouts.getTableLayout(KijiTableLayouts.SIMPLE); kiji.createTable(layout.getName(), layout); assertEquals(BaseTool.SUCCESS, runTool(new LsTool(), "--kiji=" + kiji.getURI())); assertEquals(2, mToolOutputLines.length); assertTrue(mToolOutputLines[0].startsWith("Listing tables in kiji instance:")); assertEquals(layout.getName(), mToolOutputLines[1]); }
@Test public void testRetrieveEmpty() throws Exception { getKiji().createTable(KijiTableLayouts.getLayout(KijiTableLayouts.COUNTER_TEST)); assertEquals( BaseTool.SUCCESS, runTool( new FreshTool(), KijiURI.newBuilder(getKiji().getURI()) .withTableName("user") .withColumnNames(Lists.newArrayList("info:name")) .build() .toString(), "--do=retrieve")); assertEquals( "There is no freshness policy attached to column: info:name in table: user", mToolOutputStr); }
@Test public void testCreateHashedTableWithNumRegions() throws Exception { final KijiTableLayout layout = KijiTableLayouts.getTableLayout(KijiTableLayouts.FOO_TEST); final File layoutFile = getTempLayoutFile(layout); final KijiURI tableURI = KijiURI.newBuilder(getKiji().getURI()).withTableName(layout.getName()).build(); assertEquals( BaseTool.SUCCESS, runTool( new CreateTableTool(), "--table=" + tableURI, "--layout=" + layoutFile, "--num-regions=" + 2, "--debug")); assertEquals(2, mToolOutputLines.length); assertTrue(mToolOutputLines[0].startsWith("Parsing table layout: ")); assertTrue(mToolOutputLines[1].startsWith("Creating Kiji table")); }
@Test public void testValidate() throws Exception { getKiji().createTable(KijiTableLayouts.getLayout(KijiTableLayouts.COUNTER_TEST)); final KijiFreshnessManager manager = KijiFreshnessManager.create(getKiji()); manager.storePolicy("user", "info:name", TestProducer.class, new AlwaysFreshen()); assertEquals( BaseTool.SUCCESS, runTool( new FreshTool(), KijiURI.newBuilder(getKiji().getURI()).withTableName("user").build().toString(), "--do=validate-all")); KijiFreshnessPolicyRecord record = KijiFreshnessPolicyRecord.newBuilder() .setRecordVersion(ProtocolVersion.parse("policyrecord-0.1").toCanonicalString()) .setProducerClass(TestProducer.class.getName()) .setFreshnessPolicyClass(AlwaysFreshen.class.getName()) .setFreshnessPolicyState("") .build(); final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); final EncoderFactory encoderFactory = EncoderFactory.get(); Encoder encoder = encoderFactory.directBinaryEncoder(outputStream, null); final DatumWriter<KijiFreshnessPolicyRecord> recordWriter = new SpecificDatumWriter<KijiFreshnessPolicyRecord>(KijiFreshnessPolicyRecord.SCHEMA$); recordWriter.write(record, encoder); getKiji() .getMetaTable() .putValue("user", "kiji.scoring.fresh.columnName", outputStream.toByteArray()); assertEquals( BaseTool.FAILURE, runTool( new FreshTool(), KijiURI.newBuilder(getKiji().getURI()).withTableName("user").build().toString(), "--do=validate-all")); assertEquals( "Freshness policy attached to column: columnName is not valid.", mToolOutputLines[1]); assertEquals( "NO_FAMILY_IN_TABLE: java.lang.IllegalArgumentException: Table: user does not " + "contain family: columnName", mToolOutputLines[2]); }
@Test public void testUnregister() throws Exception { getKiji().createTable(KijiTableLayouts.getLayout(KijiTableLayouts.COUNTER_TEST)); final KijiFreshnessManager manager = KijiFreshnessManager.create(getKiji()); manager.storePolicy("user", "info:name", TestProducer.class, new NeverFreshen()); assertEquals( BaseTool.SUCCESS, runTool( new FreshTool(), KijiURI.newBuilder(getKiji().getURI()) .withTableName("user") .withColumnNames(Lists.newArrayList("info:name")) .build() .toString(), "--do=unregister")); assertEquals(null, manager.retrievePolicy("user", "info:name")); assertEquals("Freshness policy removed from column: info:name in table user", mToolOutputStr); }
@Test public void testCreateUnhashedTableWithNumRegions() throws Exception { final KijiTableLayout layout = KijiTableLayout.newLayout(KijiTableLayouts.getFooUnhashedTestLayout()); final File layoutFile = getTempLayoutFile(layout); final KijiURI tableURI = KijiURI.newBuilder(getKiji().getURI()).withTableName(layout.getName()).build(); assertEquals( BaseTool.FAILURE, runTool( new CreateTableTool(), "--table=" + tableURI, "--layout=" + layoutFile, "--num-regions=4")); assertEquals(3, mToolOutputLines.length); assertTrue( mToolOutputLines[2].startsWith( "Error: May not use numRegions > 1 if row key hashing is disabled in the layout")); }
@Test public void testIllegalRegister() throws Exception { getKiji().createTable(KijiTableLayouts.getLayout(KijiTableLayouts.COUNTER_TEST)); runTool( new FreshTool(), KijiURI.newBuilder(getKiji().getURI()) .withTableName("user") .withColumnNames(Lists.newArrayList("info:name")) .build() .toString(), "--do=register", "--policy-class=org.kiji..badname", "--policy-state={\"shelfLife\":10}", "--producer-class=org.kiji.scoring.tools.TestFreshTool$TestProducer", "--interactive=false"); assertEquals( "BAD_POLICY_NAME: java.lang.IllegalArgumentException: Policy class name: " + "org.kiji..badname is not a valid Java class identifier.", mToolOutputLines[0]); runTool( new FreshTool(), KijiURI.newBuilder(getKiji().getURI()) .withTableName("user") .withColumnNames(Lists.newArrayList("info:name")) .build() .toString(), "--do=register", "--policy-class=org.kiji.class", "--policy-state={\"shelfLife\":10}", "--producer-class=org.kiji.scoring.tools.TestFreshTool$TestProducer.", "--interactive=false"); assertEquals( "BAD_PRODUCER_NAME: java.lang.IllegalArgumentException: Producer class name: org." + "kiji.scoring.tools.TestFreshTool$TestProducer. is not a valid Java class identifier.", mToolOutputLines[0]); }
@Test public void testCreateHashedTableWithSplitKeys() throws Exception { final KijiTableLayout layout = KijiTableLayouts.getTableLayout(KijiTableLayouts.FOO_TEST); final File layoutFile = getTempLayoutFile(layout); final KijiURI tableURI = KijiURI.newBuilder(getKiji().getURI()).withTableName(layout.getName()).build(); final String splitKeyFile = getClass().getClassLoader().getResource(REGION_SPLIT_KEY_FILE).getPath(); assertEquals( BaseTool.FAILURE, runTool( new CreateTableTool(), "--table=" + tableURI, "--layout=" + layoutFile, "--split-key-file=file://" + splitKeyFile)); assertEquals(3, mToolOutputLines.length); assertTrue( mToolOutputLines[2].startsWith( "Error: Row key hashing is enabled for the table. Use --num-regions=N instead.")); }
@Test public void testCreateUnhashedTableWithSplitKeys() throws Exception { final KijiTableLayout layout = KijiTableLayout.newLayout(KijiTableLayouts.getFooUnhashedTestLayout()); final File layoutFile = getTempLayoutFile(layout); final KijiURI tableURI = KijiURI.newBuilder(getKiji().getURI()).withTableName(layout.getName()).build(); final String splitKeyFile = getClass().getClassLoader().getResource(REGION_SPLIT_KEY_FILE).getPath(); assertEquals( BaseTool.SUCCESS, runTool( new CreateTableTool(), "--table=" + tableURI, "--layout=" + layoutFile, "--split-key-file=file://" + splitKeyFile, "--debug")); assertEquals(2, mToolOutputLines.length); assertTrue(mToolOutputLines[0].startsWith("Parsing table layout: ")); assertTrue(mToolOutputLines[1].startsWith("Creating Kiji table")); }
@Test public void testFormattedRowKey() throws Exception { final Kiji kiji = getKiji(); final KijiTableLayout layout = KijiTableLayouts.getTableLayout(KijiTableLayouts.FORMATTED_RKF); new InstanceBuilder(kiji) .withTable(layout.getName(), layout) .withRow("dummy", "str1", "str2", 1, 2L) .withFamily("family") .withQualifier("column") .withValue(1L, "string-value") .withValue(2L, "string-value2") .withRow("dummy", "str1", "str2", 1) .withFamily("family") .withQualifier("column") .withValue(1L, "string-value") .withRow("dummy", "str1", "str2") .withFamily("family") .withQualifier("column") .withValue(1L, "string-value") .withRow("dummy", "str1") .withFamily("family") .withQualifier("column") .withValue(1L, "string-value") .withRow("dummy") .withFamily("family") .withQualifier("column") .withValue(1L, "string-value") .build(); final KijiTable table = kiji.openTable(layout.getName()); try { assertEquals(BaseTool.SUCCESS, runTool(new LsTool(), "--kiji=" + table.getURI())); // TODO: Validate LsTool output } finally { table.close(); } }
public class TestToolUtils { private static final Logger LOG = LoggerFactory.getLogger(TestToolUtils.class); public TestToolUtils() throws IOException {} private final KijiTableLayout mFormattedLayout = KijiTableLayouts.getTableLayout(KijiTableLayouts.FORMATTED_RKF); private final KijiTableLayout mRawLayout = KijiTableLayouts.getTableLayout(KijiTableLayouts.SQOOP_EXPORT_SAMPLING_UNHASHED_TEST); private final KijiTableLayout mHashedLayout = KijiTableLayouts.getTableLayout(KijiTableLayouts.SQOOP_EXPORT_SAMPLING_HASHED_TEST); private final KijiTableLayout mHashPrefixedLayout = KijiTableLayouts.getTableLayout(KijiTableLayouts.HASH_PREFIXED_RKF); // ----------------------------------------------------------------------------------------------- /** FormattedEntityId. */ @Test public void testTooLargeInt() throws Exception { try { ToolUtils.createEntityIdFromUserInputs( "['dummy', 'str1', 'str2', 2147483648, 10]", mFormattedLayout); fail("Should fail with EntityIdException."); } catch (EntityIdException eie) { assertEquals( "Invalid type for component 2147483648 at index 3 in kijiRowKey", eie.getMessage()); } } @Test public void testTooLargeLong() throws Exception { try { ToolUtils.createEntityIdFromUserInputs( "['dummy', 'str1', 'str2', 5, 9223372036854775808]", mFormattedLayout); fail("Should fail with EntityIdException."); } catch (IOException ioe) { assertEquals( "Invalid JSON value: '9223372036854775808', expecting string, " + "int, long, or null.", ioe.getMessage()); } } @Test public void testIntForLong() throws Exception { final EntityIdFactory factory = EntityIdFactory.getFactory((RowKeyFormat2) mFormattedLayout.getDesc().getKeysFormat()); final EntityId eid = factory.getEntityId("dummy", "str1", "str2", 5, 10); assertEquals( eid, ToolUtils.createEntityIdFromUserInputs( "['dummy', 'str1', 'str2', 5, 10]", mFormattedLayout)); } @Test public void testTooFewComponents() throws IOException { final EntityIdFactory factory = EntityIdFactory.getFactory((RowKeyFormat2) mFormattedLayout.getDesc().getKeysFormat()); final EntityId eid = factory.getEntityId("dummy", "str1", "str2", 5, null); assertEquals( eid, ToolUtils.createEntityIdFromUserInputs("['dummy', 'str1', 'str2', 5]", mFormattedLayout)); } @Test public void testNonNullFollowsNull() throws IOException { try { ToolUtils.createEntityIdFromUserInputs( "['dummy', 'str1', 'str2', null, 5]", mFormattedLayout); fail("Should fail with EntityIdException."); } catch (EntityIdException eie) { assertEquals("Non null component follows null component", eie.getMessage()); } } @Test public void testEmptyString() throws IOException { final EntityIdFactory factory = EntityIdFactory.getFactory((RowKeyFormat2) mFormattedLayout.getDesc().getKeysFormat()); final EntityId eid = factory.getEntityId("", "", "", null, null); assertEquals( eid, ToolUtils.createEntityIdFromUserInputs("['', '', '', null, null]", mFormattedLayout)); } @Test public void testASCIIChars() throws Exception { final EntityIdFactory factory = EntityIdFactory.getFactory((RowKeyFormat2) mFormattedLayout.getDesc().getKeysFormat()); for (byte b = 32; b < 127; b++) { for (byte b2 = 32; b2 < 127; b2++) { final EntityId eid = factory.getEntityId( String.format("dumm%sy", new String(new byte[] {b, b2}, "Utf-8")), "str1", "str2", 5, 10L); assertEquals( eid, ToolUtils.createEntityIdFromUserInputs(eid.toShellString(), mFormattedLayout)); } } } /** RawEntityId. */ @Test public void testRawParserLoop() throws Exception { final EntityIdFactory factory = EntityIdFactory.getFactory((RowKeyFormat) mRawLayout.getDesc().getKeysFormat()); final EntityId eid = factory.getEntityIdFromHBaseRowKey(Bytes.toBytes("rawRowKey")); assertEquals(eid, ToolUtils.createEntityIdFromUserInputs(eid.toShellString(), mRawLayout)); } /** HashedEntityId. */ @Test public void testHashedParserLoop() throws Exception { final EntityIdFactory factory = EntityIdFactory.getFactory((RowKeyFormat) mHashedLayout.getDesc().getKeysFormat()); final EntityId eid = factory.getEntityId("hashedRowKey"); assertEquals(eid, ToolUtils.createEntityIdFromUserInputs(eid.toShellString(), mHashedLayout)); } /** HashPrefixedEntityId. */ @Test public void testHashPrefixedParserLoop() throws Exception { final EntityIdFactory factory = EntityIdFactory.getFactory((RowKeyFormat) mHashPrefixedLayout.getDesc().getKeysFormat()); final EntityId eid = factory.getEntityId("hashPrefixedRowKey"); assertEquals( eid, ToolUtils.createEntityIdFromUserInputs(eid.toShellString(), mHashPrefixedLayout)); } /** HBaseEntityId. */ @Test public void testHBaseEIDtoRawEID() throws Exception { final EntityIdFactory factory = EntityIdFactory.getFactory((RowKeyFormat) mRawLayout.getDesc().getKeysFormat()); final EntityId reid = factory.getEntityId("rawEID"); final EntityId hbeid = HBaseEntityId.fromHBaseRowKey(reid.getHBaseRowKey()); assertEquals(reid, ToolUtils.createEntityIdFromUserInputs(hbeid.toShellString(), mRawLayout)); } @Test public void testHBaseEIDtoHashedEID() throws Exception { final EntityIdFactory factory = EntityIdFactory.getFactory((RowKeyFormat) mHashedLayout.getDesc().getKeysFormat()); final EntityId heid = factory.getEntityId("hashedEID"); final EntityId hbeid = HBaseEntityId.fromHBaseRowKey(heid.getHBaseRowKey()); assertEquals( heid, ToolUtils.createEntityIdFromUserInputs(hbeid.toShellString(), mHashedLayout)); } @Test public void testHBaseEIDtoHashPrefixedEID() throws Exception { final EntityIdFactory factory = EntityIdFactory.getFactory((RowKeyFormat) mHashPrefixedLayout.getDesc().getKeysFormat()); final EntityId hpeid = factory.getEntityId("hashPrefixedEID"); final EntityId hbeid = HBaseEntityId.fromHBaseRowKey(hpeid.getHBaseRowKey()); assertEquals( hpeid, ToolUtils.createEntityIdFromUserInputs(hbeid.toShellString(), mHashPrefixedLayout)); } @Test public void testHBaseEIDtoFormattedEID() throws Exception { final EntityIdFactory factory = EntityIdFactory.getFactory((RowKeyFormat2) mFormattedLayout.getDesc().getKeysFormat()); final EntityId feid = factory.getEntityId("dummy", "str1", "str2", 5, 10); final EntityId hbeid = HBaseEntityId.fromHBaseRowKey(feid.getHBaseRowKey()); assertEquals( feid, ToolUtils.createEntityIdFromUserInputs(hbeid.toShellString(), mFormattedLayout)); } }
@Before public final void setup() throws Exception { mKiji = Kiji.Factory.open(getKijiURI()); mKiji.createTable("user", KijiTableLayouts.getTableLayout(KijiTableLayouts.PAGING_TEST)); mUserTable = mKiji.openTable("user"); mTableReader = mUserTable.openTableReader(); mGarrettId = mUserTable.getEntityId("garrett"); mJulietId = mUserTable.getEntityId("juliet"); final KijiTableWriter writer = mUserTable.openTableWriter(); try { writer.put(mGarrettId, "info", "name", "garrett"); writer.put( mGarrettId, "info", "location", new GregorianCalendar(1984, 9, 9).getTime().getTime(), "Bothell, WA"); writer.put( mGarrettId, "info", "location", new GregorianCalendar(2002, 9, 20).getTime().getTime(), "Seattle, WA"); writer.put( mGarrettId, "info", "location", new GregorianCalendar(2006, 10, 1).getTime().getTime(), "San Jose, CA"); writer.put( mGarrettId, "info", "location", new GregorianCalendar(2008, 9, 1).getTime().getTime(), "New York, NY"); writer.put( mGarrettId, "info", "location", new GregorianCalendar(2010, 10, 3).getTime().getTime(), "San Francisco, CA"); writer.put( mGarrettId, "jobs", "Papa Murphy's Pizza", new GregorianCalendar(1999, 10, 10).getTime().getTime(), "Pizza Maker"); writer.put( mGarrettId, "jobs", "The Catalyst Group", new GregorianCalendar(2004, 9, 10).getTime().getTime(), "Software Developer"); writer.put( mGarrettId, "jobs", "Google", new GregorianCalendar(2006, 6, 26).getTime().getTime(), "Software Engineer"); writer.put( mGarrettId, "jobs", "WibiData", new GregorianCalendar(2010, 10, 4).getTime().getTime(), "MTS"); } finally { writer.close(); } }
@Test public void testKijiLsStartAndLimitRow() throws Exception { final Kiji kiji = getKiji(); final KijiTableLayout layout = KijiTableLayouts.getTableLayout(KijiTableLayouts.FOO_TEST); final long timestamp = 10L; new InstanceBuilder(kiji) .withTable(layout.getName(), layout) .withRow("*****@*****.**") .withFamily("info") .withQualifier("email") .withValue(timestamp, "*****@*****.**") .withQualifier("name") .withValue(timestamp, "Garrett Wu") .withRow("*****@*****.**") .withFamily("info") .withQualifier("email") .withValue(timestamp, "*****@*****.**") .withQualifier("name") .withValue(timestamp, "Aaron Kimball") .withRow("*****@*****.**") .withFamily("info") .withQualifier("email") .withValue(timestamp, "*****@*****.**") .withQualifier("name") .withValue(timestamp, "Christophe Bisciglia") .withRow("*****@*****.**") .withFamily("info") .withQualifier("email") .withValue(timestamp, "*****@*****.**") .withQualifier("name") .withValue(timestamp, "Kiyan Ahmadizadeh") .withRow("*****@*****.**") .withFamily("info") .withQualifier("email") .withValue(timestamp, "*****@*****.**") .withQualifier("name") .withValue(timestamp, "John Doe") .withRow("*****@*****.**") .withFamily("info") .withQualifier("email") .withValue(timestamp, "*****@*****.**") .withQualifier("name") .withValue(timestamp, "Jane Doe") .build(); final KijiTable table = kiji.openTable(layout.getName()); try { assertEquals( BaseTool.SUCCESS, runTool(new LsTool(), "--kiji=" + table.getURI(), "--columns=info:name")); // TODO: Validate output assertEquals( BaseTool.SUCCESS, runTool( new LsTool(), "--kiji=" + table.getURI(), "--columns=info:name", "--start-row=hex:50000000000000000000000000000000", // after the second row. "--limit-row=hex:e0000000000000000000000000000000" // before the last row. )); // TODO: Validate output } finally { table.close(); } }