@Test
public void testAsInitStage() throws Exception {

    Checklist.Builder builder = new Checklist.Builder("test");
    final URL cndA = cndAUrl;
    final URL cndB = cndBUrl;
    builder.withCndUrls(Arrays.asList(cndA, cndB));
    final String NS_PREFIX_FOO = "foo";
    final String NS_URI_FOO = "http://foo.com";
    final String NS_PREFIX_BAR = "bar";
    final String NS_URI_BAR = "http://bar.com";
    final List<JcrNs> jcrNamespaces = Arrays.asList(
            JcrNs.create(NS_PREFIX_FOO, NS_URI_FOO),
            JcrNs.create(NS_PREFIX_BAR, NS_URI_BAR));
    builder.withJcrNamespaces(jcrNamespaces);
    final NamespaceMapping mapping = JsonCnd.toNamespaceMapping(jcrNamespaces);
    final List<QNodeTypeDefinition> nodetypes = JsonCnd.getQTypesFromJson(obj()
            .key("foo:primaryType").val(key("extends", arr().val("nt:base")))
            .key("foo:mixinType").val(key("@", arr().val("mixin")))
            .key("bar:primaryType").val(key("extends", arr().val("nt:base")))
            .key("bar:mixinType").val(key("@", arr().val("mixin"))).get(), mapping);
    builder.withJcrNodetypes(nodetypes);
    final List<PrivilegeDefinition> privileges =
            JsonCnd.getPrivilegesFromJson(arr("foo:canDo", "bar:canBe").get(), mapping);
    builder.withJcrPrivileges(privileges);
    final List<ForcedRoot> forcedRoots = Arrays.asList(
            ForcedRoot.fromJson(obj()
                    .key(ForcedRoot.keys().path(), "/test/foo")
                    .key(ForcedRoot.keys().primaryType(), "foo:primaryType")
                    .key(ForcedRoot.keys().mixinTypes(), arr().val("foo:mixinType"))
                    .get()),
            ForcedRoot.fromJson(obj()
                    .key(ForcedRoot.keys().path(), "/test/bar")
                    .key(ForcedRoot.keys().primaryType(), "bar:primaryType")
                    .key(ForcedRoot.keys().mixinTypes(), arr().val("bar:mixinType"))
                    .get()));
    builder.withForcedRoots(forcedRoots);
    final List<String> repoInits = Arrays.asList(
            "create service user testAsInitStage1",
            "create service user testAsInitStage2"
    );
    builder.withRepoInits(repoInits);
    InitStage initStage = builder.build().asInitStage();
    final OakMachine machine = new OakMachine.Builder().withInitStage(initStage).build();
    machine.adminInitAndInspect(session -> {
        assertEquals("foo namespace should be registered",
                NS_URI_FOO, session.getNamespaceURI(NS_PREFIX_FOO));
        assertEquals("bar namespace should be registered",
                NS_URI_BAR, session.getNamespaceURI(NS_PREFIX_BAR));
        assertTrue("/test/foo path exists", session.nodeExists("/test/foo"));
        assertTrue("/test/foo is a foo:primaryType",
                session.getNode("/test/foo").isNodeType("foo:primaryType"));
        assertTrue("/test/foo is a foo:mixinType",
                session.getNode("/test/foo").isNodeType("foo:mixinType"));
        assertTrue("/test/bar path exists", session.nodeExists("/test/bar"));
        assertTrue("/test/bar is a bar:primaryType",
                session.getNode("/test/bar").isNodeType("bar:primaryType"));
        assertTrue("/test/bar is a bar:mixinType",
                session.getNode("/test/bar").isNodeType("bar:mixinType"));
        UserManager userManager = ((JackrabbitSession) session).getUserManager();
        assertNotNull("has principal testAsInitStage1",
                userManager.getAuthorizable("testAsInitStage1"));
        assertNotNull("has principal testAsInitStage2",
                userManager.getAuthorizable("testAsInitStage2"));
    });
}