diff --git a/src/test/java/org/elasticsearch/shield/authc/ldap/ApacheDsRule.java b/src/test/java/org/elasticsearch/shield/authc/ldap/ApacheDsRule.java new file mode 100644 index 00000000000..a8ca40cb4f1 --- /dev/null +++ b/src/test/java/org/elasticsearch/shield/authc/ldap/ApacheDsRule.java @@ -0,0 +1,39 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ +package org.elasticsearch.shield.authc.ldap; + +import org.junit.rules.MethodRule; +import org.junit.runners.model.FrameworkMethod; +import org.junit.runners.model.Statement; + +/** + * + */ +public class ApacheDsRule implements MethodRule { + + private ApacheDsEmbedded ldap; + + @Override + public Statement apply(final Statement base, final FrameworkMethod method, final Object target) { + + return new Statement() { + @Override + public void evaluate() throws Throwable { + try { + ldap = new ApacheDsEmbedded("o=sevenSeas", "seven-seas.ldif", target.getClass().getName()); + ldap.startServer(); + base.evaluate(); + } finally { + ldap.stopAndCleanup(); + } + } + }; + } + + public String getUrl() { + return ldap.getUrl(); + } +} diff --git a/src/test/java/org/elasticsearch/shield/authc/ldap/LdapConnectionTests.java b/src/test/java/org/elasticsearch/shield/authc/ldap/LdapConnectionTests.java index 1a52d2da23e..f1e23cd38ff 100644 --- a/src/test/java/org/elasticsearch/shield/authc/ldap/LdapConnectionTests.java +++ b/src/test/java/org/elasticsearch/shield/authc/ldap/LdapConnectionTests.java @@ -8,8 +8,7 @@ package org.elasticsearch.shield.authc.ldap; import org.elasticsearch.common.settings.ImmutableSettings; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.test.ElasticsearchTestCase; -import org.junit.AfterClass; -import org.junit.BeforeClass; +import org.junit.Rule; import org.junit.Test; import java.util.List; @@ -20,20 +19,12 @@ import static org.hamcrest.Matchers.*; public class LdapConnectionTests extends ElasticsearchTestCase { public static String SETTINGS_PREFIX = LdapRealm.class.getPackage().getName().substring("com.elasticsearch.".length()) + '.'; - static ApacheDsEmbedded ldap = new ApacheDsEmbedded("o=sevenSeas", "seven-seas.ldif", LdapConnectionTests.class.getName()); - - @BeforeClass - public static void startServer() throws Exception { - ldap.startServer(); - } - @AfterClass - public static void stopServer() throws Exception { - ldap.stopAndCleanup(); - } + @Rule + public static ApacheDsRule apacheDsRule = new ApacheDsRule(); @Test public void testBindWithTemplates() { - String[] ldapUrls = new String[]{ldap.getUrl()}; + String[] ldapUrls = new String[]{apacheDsRule.getUrl()}; String groupSearchBase = "o=sevenSeas"; boolean isSubTreeSearch = true; String[] userTemplates = new String[]{ @@ -55,7 +46,7 @@ public class LdapConnectionTests extends ElasticsearchTestCase { } @Test public void testBindWithBogusTemplates() { - String[] ldapUrl = new String[]{ldap.getUrl()}; + String[] ldapUrl = new String[]{apacheDsRule.getUrl()}; String groupSearchBase = "o=sevenSeas"; boolean isSubTreeSearch = true; String[] userTemplates = new String[]{ @@ -85,7 +76,7 @@ public class LdapConnectionTests extends ElasticsearchTestCase { boolean isSubTreeSearch = true; StandardLdapConnectionFactory ldapFac = new StandardLdapConnectionFactory( - buildLdapSettings(ldap.getUrl(), userTemplate, groupSearchBase, isSubTreeSearch)); + buildLdapSettings(apacheDsRule.getUrl(), userTemplate, groupSearchBase, isSubTreeSearch)); String user = "Horatio Hornblower"; char[] userPass = "pass".toCharArray(); @@ -102,7 +93,7 @@ public class LdapConnectionTests extends ElasticsearchTestCase { String userTemplate = "cn={0},ou=people,o=sevenSeas"; boolean isSubTreeSearch = false; StandardLdapConnectionFactory ldapFac = new StandardLdapConnectionFactory( - buildLdapSettings(ldap.getUrl(), userTemplate, groupSearchBase, isSubTreeSearch)); + buildLdapSettings(apacheDsRule.getUrl(), userTemplate, groupSearchBase, isSubTreeSearch)); String user = "Horatio Hornblower"; LdapConnection ldap = ldapFac.bind(user, "pass".toCharArray()); diff --git a/src/test/java/org/elasticsearch/shield/authc/ldap/LdapRealmTest.java b/src/test/java/org/elasticsearch/shield/authc/ldap/LdapRealmTest.java index 3e1a9ff3f7d..5d2104cdbc9 100644 --- a/src/test/java/org/elasticsearch/shield/authc/ldap/LdapRealmTest.java +++ b/src/test/java/org/elasticsearch/shield/authc/ldap/LdapRealmTest.java @@ -13,9 +13,8 @@ import org.elasticsearch.shield.authc.support.UsernamePasswordToken; import org.elasticsearch.test.ElasticsearchTestCase; import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.watcher.ResourceWatcherService; -import org.junit.AfterClass; -import org.junit.BeforeClass; import org.junit.Ignore; +import org.junit.Rule; import org.junit.Test; import static org.hamcrest.Matchers.*; @@ -24,7 +23,7 @@ import static org.mockito.Matchers.anyString; import static org.mockito.Mockito.*; public class LdapRealmTest extends ElasticsearchTestCase { - static ApacheDsEmbedded ldap = new ApacheDsEmbedded("o=sevenSeas", "seven-seas.ldif", LdapRealmTest.class.getName()); + public static String AD_IP = "54.213.145.20"; public static String AD_URL = "ldap://" + AD_IP + ":389"; @@ -32,21 +31,15 @@ public class LdapRealmTest extends ElasticsearchTestCase { public static final String VALID_USERNAME = "Thomas Masterman Hardy"; public static final String PASSWORD = "pass"; - @BeforeClass - public static void startServer() throws Exception { - ldap.startServer(); - } - @AfterClass - public static void stopServer() throws Exception { - ldap.stopAndCleanup(); - } + @Rule + public static ApacheDsRule apacheDsRule = new ApacheDsRule(); @Test public void testAuthenticate_subTreeGroupSearch(){ String groupSearchBase = "o=sevenSeas"; boolean isSubTreeSearch = true; String userTemplate = VALID_USER_TEMPLATE; - Settings settings = LdapConnectionTests.buildLdapSettings(ldap.getUrl(), userTemplate, groupSearchBase, isSubTreeSearch); + Settings settings = LdapConnectionTests.buildLdapSettings(apacheDsRule.getUrl(), userTemplate, groupSearchBase, isSubTreeSearch); StandardLdapConnectionFactory ldapFactory = new StandardLdapConnectionFactory(settings); LdapRealm ldap = new LdapRealm(buildNonCachingSettings(), ldapFactory, buildGroupAsRoleMapper()); @@ -61,7 +54,7 @@ public class LdapRealmTest extends ElasticsearchTestCase { boolean isSubTreeSearch = false; String userTemplate = VALID_USER_TEMPLATE; StandardLdapConnectionFactory ldapFactory = new StandardLdapConnectionFactory( - LdapConnectionTests.buildLdapSettings(ldap.getUrl(), userTemplate, groupSearchBase, isSubTreeSearch)); + LdapConnectionTests.buildLdapSettings(apacheDsRule.getUrl(), userTemplate, groupSearchBase, isSubTreeSearch)); LdapRealm ldap = new LdapRealm(buildNonCachingSettings(), ldapFactory, buildGroupAsRoleMapper()); @@ -77,7 +70,7 @@ public class LdapRealmTest extends ElasticsearchTestCase { boolean isSubTreeSearch = true; String userTemplate = VALID_USER_TEMPLATE; StandardLdapConnectionFactory ldapFactory = new StandardLdapConnectionFactory( - LdapConnectionTests.buildLdapSettings( ldap.getUrl(), userTemplate, groupSearchBase, isSubTreeSearch) ); + LdapConnectionTests.buildLdapSettings( apacheDsRule.getUrl(), userTemplate, groupSearchBase, isSubTreeSearch) ); ldapFactory = spy(ldapFactory); LdapRealm ldap = new LdapRealm( buildCachingSettings(), ldapFactory, buildGroupAsRoleMapper()); @@ -94,7 +87,7 @@ public class LdapRealmTest extends ElasticsearchTestCase { boolean isSubTreeSearch = true; String userTemplate = VALID_USER_TEMPLATE; StandardLdapConnectionFactory ldapFactory = new StandardLdapConnectionFactory( - LdapConnectionTests.buildLdapSettings(ldap.getUrl(), userTemplate, groupSearchBase, isSubTreeSearch) ); + LdapConnectionTests.buildLdapSettings(apacheDsRule.getUrl(), userTemplate, groupSearchBase, isSubTreeSearch) ); ldapFactory = spy(ldapFactory); LdapRealm ldap = new LdapRealm( buildNonCachingSettings(), ldapFactory, buildGroupAsRoleMapper());