@Test
    public void testApply() throws Exception {
        // Test using a direct bind to the user account
        //////////////////////////////////////////////////
        String json = "{\r\n" +
                "    \"realm\" : \"TestRealm\",\r\n" +
                "    \"forwardIdentityHttpHeader\" : \"X-Authenticated-Identity\",\r\n" +
                "    \"ldapIdentity\" : {\r\n" +
                "        \"url\" : \"ldap://" + LDAP_SERVER + ":" + ldapServer.getPort() + "\",\r\n" +
                "        \"dnPattern\" : \"uid=${username},ou=system\"\r\n" +
                "    }\r\n" +
                "}";

        doTest(json, null, null, PolicyFailureCodes.BASIC_AUTH_REQUIRED, ldapComponent);
        doTest(json, "admin", "invalid_password", PolicyFailureCodes.BASIC_AUTH_FAILED,
            ldapComponent);
        doTest(json, "admin", "secret", null, ldapComponent);

        // Test using a service account with user search
        //////////////////////////////////////////////////
        json = "{\r\n" +
                "  \"realm\" : \"TestRealm\",\r\n" +
                "  \"ldapIdentity\" : {\r\n" +
                "    \"url\" : \"ldap://" + LDAP_SERVER + ":" + ldapServer.getPort() + "\",\r\n" +
                "    \"dnPattern\" : \"uid=${username},ou=system\",\r\n" +
                "    \"bindAs\" : \"ServiceAccount\",\r\n" +
                "    \"credentials\" : {\r\n" +
                "      \"username\" : \"admin\",\r\n" +
                "      \"password\" : \"secret\"\r\n" +
                "    },\r\n" +
                "    \"userSearch\" : {\r\n" +
                "      \"baseDn\" : \"ou=people,o=apiman\",\r\n" +
                "      \"expression\" : \"(uid=${username})\"\r\n" +
                "    }\r\n" +
                "  }\r\n" +
                "}";

        doTest(json, null, null, PolicyFailureCodes.BASIC_AUTH_REQUIRED, ldapComponent);
        doTest(json, "ewittman", "invalid_password", PolicyFailureCodes.BASIC_AUTH_FAILED,
            ldapComponent);
        doTest(json, "unknown_user", "password", PolicyFailureCodes.BASIC_AUTH_FAILED, ldapComponent);
        doTest(json, "ewittman", "ewittman", null, ldapComponent);

        // Test using a service account with user search
        //////////////////////////////////////////////////
        json = "{\r\n" +
                "  \"realm\" : \"TestRealm\",\r\n" +
                "  \"ldapIdentity\" : {\r\n" +
                "    \"url\" : \"ldap://" + LDAP_SERVER + ":" + ldapServer.getPort() + "\",\r\n" +
                "    \"dnPattern\" : \"uid=${username},ou=system\",\r\n" +
                "    \"bindAs\" : \"ServiceAccount\",\r\n" +
                "    \"credentials\" : {\r\n" +
                "      \"username\" : \"admin\",\r\n" +
                "      \"password\" : \"secret\"\r\n" +
                "    },\r\n" +
                "    \"userSearch\" : {\r\n" +
                "      \"baseDn\" : \"ou=people,o=apiman\",\r\n" +
                "      \"expression\" : \"(uid=${username})\"\r\n" +
                "    }\r\n" +
                "  }\r\n" +
                "}";
        doTest(json, null, null, PolicyFailureCodes.BASIC_AUTH_REQUIRED, ldapComponent);
        doTest(json, "ewittman", "invalid_password", PolicyFailureCodes.BASIC_AUTH_FAILED,
            ldapComponent);
        doTest(json, "unknown_user", "password", PolicyFailureCodes.BASIC_AUTH_FAILED, ldapComponent);
        doTest(json, "ewittman", "ewittman", null, ldapComponent);

        // Test with the extraction of user roles
        //////////////////////////////////////////////////
         json = "{\r\n" +
                "  \"realm\" : \"TestRealm\",\r\n" +
                "  \"ldapIdentity\" : {\r\n" +
                "    \"url\" : \"ldap://localhost:7654\",\r\n" +
                "    \"dnPattern\" : \"uid=${username},ou=system\",\r\n" +
                "    \"bindAs\" : \"ServiceAccount\",\r\n" +
                "    \"credentials\" : {\r\n" +
                "      \"username\" : \"admin\",\r\n" +
                "      \"password\" : \"secret\"\r\n" +
                "    },\r\n" +
                "    \"userSearch\" : {\r\n" +
                "      \"baseDn\" : \"ou=people,o=apiman\",\r\n" +
                "      \"expression\" : \"(uid=${username})\"\r\n" +
                "    },\r\n" +
                "    \"extractRoles\" : true,\r\n" +
                "    \"membershipAttribute\" : \"title\",\r\n" +
                "    \"rolenameAttribute\" : \"cn\"\r\n" +
                "  }\r\n" +
                "}";
        Set<String> expectedRoles = new HashSet<>();
        expectedRoles.add("user");
        expectedRoles.add("admin");
        doTest(json, "ewittman", "ewittman", null, expectedRoles, ldapComponent);
        doTest(json, "ewittman", "ewittmanx", PolicyFailureCodes.BASIC_AUTH_FAILED, expectedRoles,
            ldapComponent);
    }
Test method for {@link io.apiman.gateway.engine.policies.BasicAuthenticationPolicy#apply(ApiRequest, IPolicyContext, Object, IPolicyChain)}
gateway/engine/policies/src/test/java/io/apiman/gateway/engine/policies/BasicAuthLDAPTest.java