@Test
public void testPathToAndFromTuple() throws Exception {

    KeySpace root = new KeySpace(
            new DirectoryLayerDirectory("production", "production")
                    .addSubdirectory(new KeySpaceDirectory("userid", KeyType.LONG)
                            .addSubdirectory(new DirectoryLayerDirectory("application")
                                            .addSubdirectory(new KeySpaceDirectory("dataStore", KeyType.NULL))
                                            .addSubdirectory(new DirectoryLayerDirectory("metadataStore", "S")))),
            new DirectoryLayerDirectory("test", "test")
                    .addSubdirectory(new KeySpaceDirectory("userid", KeyType.LONG)
                            .addSubdirectory(new DirectoryLayerDirectory("application")
                                    .addSubdirectory(new KeySpaceDirectory("dataStore", KeyType.NULL))
                                    .addSubdirectory(new DirectoryLayerDirectory("metadataStore", "S")))));
    final KeySpacePath path1 = root.path("production")
            .add("userid", 123456789L)
            .add("application", "com.mybiz.application1")
            .add("dataStore");
    final KeySpacePath path2 = root.path("test")
            .add("userid", 987654321L)
            .add("application", "com.mybiz.application2")
            .add("metadataStore");
    final FDBDatabase database = FDBDatabaseFactory.instance().getDatabase();
    final Tuple path1Tuple;
    final Tuple path2Tuple;
    try (FDBRecordContext context = database.openContext()) {
        path1Tuple = path1.toTuple(context);
        path2Tuple = path2.toTuple(context);
        context.commit();
    }
    final Tuple path1ExpectedTuple;
    final Tuple path2ExpectedTuple;
    try (FDBRecordContext context = database.openContext()) {
        List<Long> entries = resolveBatch(context, "production", "test",
                "com.mybiz.application1", "com.mybiz.application2", "S");
        path1ExpectedTuple = Tuple.from(entries.get(0), 123456789L, entries.get(2), null);
        path2ExpectedTuple = Tuple.from(entries.get(1), 987654321L, entries.get(3), entries.get(4));
        assertEquals(path1ExpectedTuple, path1Tuple);
        assertEquals(path2ExpectedTuple, path2Tuple);
        // Now, make sure that we can take a tuple and turn it back into a keyspace path.
        List<ResolvedKeySpacePath> revPath1 = root.resolveFromKey(context, path1ExpectedTuple).flatten();
        assertEquals("production", revPath1.get(0).getDirectoryName());
        assertEquals(entries.get(0), revPath1.get(0).getResolvedValue());
        assertEquals("userid", revPath1.get(1).getDirectoryName());
        assertEquals(123456789L, revPath1.get(1).getResolvedValue());
        assertEquals("application", revPath1.get(2).getDirectoryName());
        assertEquals(entries.get(2), revPath1.get(2).getResolvedValue());
        assertEquals("dataStore", revPath1.get(3).getDirectoryName());
        assertEquals(null, revPath1.get(3).getResolvedValue());
        // Tack on extra value to make sure it is in the remainder.
        Tuple extendedPath2 = path2ExpectedTuple.add(10L);
        List<ResolvedKeySpacePath> revPath2 = root.resolveFromKey(context, extendedPath2).flatten();
        assertEquals("test", revPath2.get(0).getDirectoryName());
        assertEquals("userid", revPath2.get(1).getDirectoryName());
        assertEquals("application", revPath2.get(2).getDirectoryName());
        assertEquals("metadataStore", revPath2.get(3).getDirectoryName());
        assertEquals(Tuple.from(10L), revPath2.get(3).getRemainder());
    }
}