@Test
public void testPathWrapperExample() throws Exception {

    EnvironmentKeySpace keySpace = new EnvironmentKeySpace("production");
    final FDBDatabase database = FDBDatabaseFactory.instance().getDatabase();
    // Create a tuple to represent a path to a user's main store. This will trigger the creation of the
    // necessary directory layer entries.
    final Tuple dataStoreTuple;
    final Tuple metadataStoreTuple;
    try (FDBRecordContext context = database.openContext()) {
        ApplicationPath application = keySpace.root().userid(123).application("myApplication");
        dataStoreTuple = application.dataStore().toTuple(context);
        metadataStoreTuple = application.metadataStore().toTuple(context);
        context.commit();
    }
    try (FDBRecordContext context = database.openContext()) {
        List<Long> entries = resolveBatch(context, keySpace.getRootName(), "myApplication");
        // Validate the entries created above look like what we expect
        assertEquals(Tuple.from(entries.get(0), 123L, entries.get(1), EnvironmentKeySpace.DATA_VALUE), dataStoreTuple);
        assertEquals(Tuple.from(entries.get(0), 123L, entries.get(1), EnvironmentKeySpace.METADATA_VALUE), metadataStoreTuple);
        ResolvedKeySpacePath path =  keySpace.fromKey(context, dataStoreTuple);
        assertThat(path.toPath(), instanceOf(DataPath.class));
        DataPath mainStorePath = (DataPath) path.toPath();
        assertEquals(EnvironmentKeySpace.DATA_VALUE, mainStorePath.getValue());
        assertEquals(EnvironmentKeySpace.DATA_VALUE, mainStorePath.resolveAsync(context).get().getResolvedValue());
        assertEquals(entries.get(1), mainStorePath.parent().resolveAsync(context).get().getResolvedValue());
        assertEquals("myApplication", mainStorePath.parent().getValue());
        assertEquals(123L, mainStorePath.parent().parent().getValue());
        assertEquals(entries.get(0), mainStorePath.parent().parent().parent().resolveAsync(context).get().getResolvedValue());
        assertEquals("production", mainStorePath.parent().parent().parent().getValue());
        assertEquals(null, mainStorePath.parent().parent().parent().parent());
        assertThat(keySpace.fromKey(context, TupleHelpers.subTuple(dataStoreTuple, 0, 1)).toPath(), instanceOf(EnvironmentRoot.class));
        assertThat(keySpace.fromKey(context, TupleHelpers.subTuple(dataStoreTuple, 0, 2)).toPath(), instanceOf(UserPath.class));
        assertThat(keySpace.fromKey(context, TupleHelpers.subTuple(dataStoreTuple, 0, 3)).toPath(), instanceOf(ApplicationPath.class));
        path = keySpace.fromKey(context, metadataStoreTuple);
        assertThat(path.toPath(), instanceOf(MetadataPath.class));
        MetadataPath metadataPath = (MetadataPath) path.toPath();
        assertEquals(EnvironmentKeySpace.METADATA_VALUE, metadataPath.getValue());
        assertEquals(EnvironmentKeySpace.METADATA_VALUE, metadataPath.resolveAsync(context).get().getResolvedValue());
        assertEquals(entries.get(1), metadataPath.parent().resolveAsync(context).get().getResolvedValue());
        assertEquals("myApplication", metadataPath.parent().getValue());
        assertEquals(123L, metadataPath.parent().parent().getValue());
        assertEquals(entries.get(0), metadataPath.parent().parent().parent().resolveAsync(context).get().getResolvedValue());
        assertEquals("production", metadataPath.parent().parent().parent().getValue());
        assertEquals(null, metadataPath.parent().parent().parent().parent());
        assertThat(keySpace.fromKey(context, TupleHelpers.subTuple(dataStoreTuple, 0, 1)).toPath(), instanceOf(EnvironmentRoot.class));
        assertThat(keySpace.fromKey(context, TupleHelpers.subTuple(dataStoreTuple, 0, 2)).toPath(), instanceOf(UserPath.class));
        assertThat(keySpace.fromKey(context, TupleHelpers.subTuple(dataStoreTuple, 0, 3)).toPath(), instanceOf(ApplicationPath.class));
        // Create a fake main store "record" key to demonstrate that we can get the key as the remainder
        Tuple recordTuple = dataStoreTuple.add(1L).add("someStr").add(0L); // 1=record space, record id, 0=unsplit record
        path =  keySpace.fromKey(context, recordTuple);
        assertThat(path.toPath(), instanceOf(DataPath.class));
        assertEquals(Tuple.from(1L, "someStr", 0L), path.getRemainder());
        assertEquals(dataStoreTuple, path.toTuple());
    }
}