Search in sources :

Example 1 with LDAPSDKUsageException

use of com.unboundid.util.LDAPSDKUsageException in project ldapsdk by pingidentity.

the class UpdateConnectionPoolBindRequestAndServerSetTestCase method testSetServerSetForLDAPConnectionPool.

/**
 * Tests the behavior of the {@link LDAPConnectionPool#setServerSet} method.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testSetServerSetForLDAPConnectionPool() throws Exception {
    // Create three different in-memory directory server instances to use for
    // testing.
    final InMemoryDirectoryServerConfig cfg = new InMemoryDirectoryServerConfig("dc=example,dc=com");
    cfg.addAdditionalBindCredentials("cn=Directory Manager", "password");
    final InMemoryDirectoryServer ds1 = new InMemoryDirectoryServer(new InMemoryDirectoryServerConfig(cfg));
    ds1.startListening();
    final InMemoryDirectoryServer ds2 = new InMemoryDirectoryServer(new InMemoryDirectoryServerConfig(cfg));
    ds2.startListening();
    final InMemoryDirectoryServer ds3 = new InMemoryDirectoryServer(new InMemoryDirectoryServerConfig(cfg));
    ds3.startListening();
    try (LDAPConnectionPool pool = new LDAPConnectionPool(new SingleServerSet("127.0.0.1", ds1.getListenPort()), new SimpleBindRequest("cn=Directory Manager", "password"), 1, 1)) {
        // Make sure that the connection is initially established to ds1.
        LDAPConnection conn = pool.getConnection();
        assertEquals(conn.getConnectedPort(), ds1.getListenPort());
        pool.releaseConnection(conn);
        // Update the server so that new connections will be established to ds2.
        pool.setServerSet(new SingleServerSet("127.0.0.1", ds2.getListenPort()));
        // Make sure that the existing connection is still connected to ds1.
        conn = pool.getConnection();
        assertEquals(conn.getConnectedPort(), ds1.getListenPort());
        // Release the connection as defunct and make sure that the new connection
        // is connected to ds2.
        pool.releaseDefunctConnection(conn);
        conn = pool.getConnection();
        assertEquals(conn.getConnectedPort(), ds2.getListenPort());
        // Try to set a null server set.  That should be rejected.
        try {
            pool.setServerSet(null);
            fail("Expected an exception when setting a null server set.");
        } catch (final LDAPSDKUsageException e) {
        // This was expected.
        }
        // It should still be possible to get new connections, and they should
        // still be connected to ds2.
        pool.releaseDefunctConnection(conn);
        conn = pool.getConnection();
        assertEquals(conn.getConnectedPort(), ds2.getListenPort());
        // Set the server set so that new connections will be established to ds3.
        pool.setServerSet(new SingleServerSet("127.0.0.1", ds3.getListenPort()));
        // Make sure that new connections are established to ds3.
        pool.releaseDefunctConnection(conn);
        conn = pool.getConnection();
        assertEquals(conn.getConnectedPort(), ds3.getListenPort());
        pool.releaseConnection(conn);
    } finally {
        ds1.shutDown(true);
        ds2.shutDown(true);
        ds3.shutDown(true);
    }
}
Also used : InMemoryDirectoryServer(com.unboundid.ldap.listener.InMemoryDirectoryServer) InMemoryDirectoryServerConfig(com.unboundid.ldap.listener.InMemoryDirectoryServerConfig) LDAPSDKUsageException(com.unboundid.util.LDAPSDKUsageException) Test(org.testng.annotations.Test)

Example 2 with LDAPSDKUsageException

use of com.unboundid.util.LDAPSDKUsageException in project ldapsdk by pingidentity.

the class UpdateConnectionPoolBindRequestAndServerSetTestCase method testSetServerSetForLDAPThreadLocalConnectionPoolFromConn.

/**
 * Tests the behavior of the
 * {@link LDAPThreadLocalConnectionPool#setServerSet} method for a connection
 * pool created with an already-established connection rather than a server
 * set and a bind request.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testSetServerSetForLDAPThreadLocalConnectionPoolFromConn() throws Exception {
    // Create three different in-memory directory server instances to use for
    // testing.
    final InMemoryDirectoryServerConfig cfg = new InMemoryDirectoryServerConfig("dc=example,dc=com");
    cfg.addAdditionalBindCredentials("cn=Directory Manager", "password");
    final InMemoryDirectoryServer ds1 = new InMemoryDirectoryServer(new InMemoryDirectoryServerConfig(cfg));
    ds1.startListening();
    final InMemoryDirectoryServer ds2 = new InMemoryDirectoryServer(new InMemoryDirectoryServerConfig(cfg));
    ds2.startListening();
    final InMemoryDirectoryServer ds3 = new InMemoryDirectoryServer(new InMemoryDirectoryServerConfig(cfg));
    ds3.startListening();
    try (LDAPConnection connection = new LDAPConnection("127.0.0.1", ds1.getListenPort(), "cn=Directory Manager", "password");
        LDAPConnectionPool pool = new LDAPConnectionPool(connection, 1, 1)) {
        // Make sure that the connection is initially established to ds1.
        LDAPConnection conn = pool.getConnection();
        assertEquals(conn.getConnectedPort(), ds1.getListenPort());
        pool.releaseConnection(conn);
        // Update the server so that new connections will be established to ds2.
        pool.setServerSet(new SingleServerSet("127.0.0.1", ds2.getListenPort()));
        // Make sure that the existing connection is still connected to ds1.
        conn = pool.getConnection();
        assertEquals(conn.getConnectedPort(), ds1.getListenPort());
        // Release the connection as defunct and make sure that the new connection
        // is connected to ds2.
        pool.releaseDefunctConnection(conn);
        conn = pool.getConnection();
        assertEquals(conn.getConnectedPort(), ds2.getListenPort());
        // Try to set a null server set.  That should be rejected.
        try {
            pool.setServerSet(null);
            fail("Expected an exception when setting a null server set.");
        } catch (final LDAPSDKUsageException e) {
        // This was expected.
        }
        // It should still be possible to get new connections, and they should
        // still be connected to ds2.
        pool.releaseDefunctConnection(conn);
        conn = pool.getConnection();
        assertEquals(conn.getConnectedPort(), ds2.getListenPort());
        // Set the server set so that new connections will be established to ds3.
        pool.setServerSet(new SingleServerSet("127.0.0.1", ds3.getListenPort()));
        // Make sure that new connections are established to ds3.
        pool.releaseDefunctConnection(conn);
        conn = pool.getConnection();
        assertEquals(conn.getConnectedPort(), ds3.getListenPort());
        pool.releaseConnection(conn);
    } finally {
        ds1.shutDown(true);
        ds2.shutDown(true);
        ds3.shutDown(true);
    }
}
Also used : InMemoryDirectoryServer(com.unboundid.ldap.listener.InMemoryDirectoryServer) InMemoryDirectoryServerConfig(com.unboundid.ldap.listener.InMemoryDirectoryServerConfig) LDAPSDKUsageException(com.unboundid.util.LDAPSDKUsageException) Test(org.testng.annotations.Test)

Example 3 with LDAPSDKUsageException

use of com.unboundid.util.LDAPSDKUsageException in project ldapsdk by pingidentity.

the class EqualsAnyJSONObjectFilterTestCase method testGetAndSetValues.

/**
 * Provides test coverage for the methods that can be used to get and set the
 * target values for a filter.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testGetAndSetValues() throws Exception {
    final EqualsAnyJSONObjectFilter f = new EqualsAnyJSONObjectFilter("test-field-name", JSONNull.NULL);
    assertNotNull(f.getValues());
    assertEquals(f.getValues(), Collections.<JSONValue>singletonList(JSONNull.NULL));
    f.setValues(new JSONString("foo"));
    assertNotNull(f.getValues());
    assertEquals(f.getValues(), Collections.<JSONValue>singletonList(new JSONString("foo")));
    f.setValues(new JSONString("foo"), new JSONString("bar"));
    assertNotNull(f.getValues());
    assertEquals(f.getValues(), Arrays.<JSONValue>asList(new JSONString("foo"), new JSONString("bar")));
    f.setValues("abc", "def", "ghi");
    assertNotNull(f.getValues());
    assertEquals(f.getValues(), Arrays.<JSONValue>asList(new JSONString("abc"), new JSONString("def"), new JSONString("ghi")));
    try {
        f.setValues(new JSONValue[0]);
        fail("Expected an exception with setValues of empty");
    } catch (final LDAPSDKUsageException e) {
    // This was expected
    }
}
Also used : LDAPSDKUsageException(com.unboundid.util.LDAPSDKUsageException) JSONString(com.unboundid.util.json.JSONString) Test(org.testng.annotations.Test)

Example 4 with LDAPSDKUsageException

use of com.unboundid.util.LDAPSDKUsageException in project ldapsdk by pingidentity.

the class EqualsAnyJSONObjectFilterTestCase method testGetAndSetField.

/**
 * Provides test coverage for the methods that can be used to get and set the
 * target field name for a filter.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testGetAndSetField() throws Exception {
    final EqualsAnyJSONObjectFilter f = new EqualsAnyJSONObjectFilter("test-field-name", JSONNull.NULL);
    assertNotNull(f.getField());
    assertEquals(f.getField(), Collections.singletonList("test-field-name"));
    assertEquals(f.toJSONObject(), new JSONObject(new JSONField("filterType", "equalsAny"), new JSONField("field", "test-field-name"), new JSONField("values", new JSONArray(JSONNull.NULL))));
    f.setField("different-field-name");
    assertNotNull(f.getField());
    assertEquals(f.getField(), Collections.singletonList("different-field-name"));
    assertEquals(f.toJSONObject(), new JSONObject(new JSONField("filterType", "equalsAny"), new JSONField("field", "different-field-name"), new JSONField("values", new JSONArray(JSONNull.NULL))));
    f.setField("first", "second", "third");
    assertNotNull(f.getField());
    assertEquals(f.getField(), Arrays.asList("first", "second", "third"));
    assertEquals(f.toJSONObject(), new JSONObject(new JSONField("filterType", "equalsAny"), new JSONField("field", new JSONArray(new JSONString("first"), new JSONString("second"), new JSONString("third"))), new JSONField("values", new JSONArray(JSONNull.NULL))));
    try {
        f.setField();
        fail("Expected an exception with setFieldName of empty");
    } catch (final LDAPSDKUsageException e) {
    // This was expected
    }
}
Also used : JSONObject(com.unboundid.util.json.JSONObject) JSONArray(com.unboundid.util.json.JSONArray) JSONField(com.unboundid.util.json.JSONField) LDAPSDKUsageException(com.unboundid.util.LDAPSDKUsageException) JSONString(com.unboundid.util.json.JSONString) Test(org.testng.annotations.Test)

Example 5 with LDAPSDKUsageException

use of com.unboundid.util.LDAPSDKUsageException in project ldapsdk by pingidentity.

the class NegateJSONObjectFilterTestCase method testNegateFilter.

/**
 * Tests the behavior of a negate filter under normal conditions.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testNegateFilter() throws Exception {
    final EqualsJSONObjectFilter equalsFilter = new EqualsJSONObjectFilter("a", new JSONString("b"));
    NegateJSONObjectFilter f = new NegateJSONObjectFilter(equalsFilter);
    assertNotNull(f.toJSONObject());
    assertEquals(f.toJSONObject(), new JSONObject(new JSONField("filterType", "negate"), new JSONField("negateFilter", equalsFilter.toJSONObject())));
    f = (NegateJSONObjectFilter) JSONObjectFilter.decode(f.toJSONObject());
    assertNotNull(f);
    assertNotNull(f.getNegateFilter());
    assertEquals(f.getNegateFilter(), equalsFilter);
    assertNotNull(f.getFilterType());
    assertEquals(f.getFilterType(), "negate");
    assertNotNull(f.getRequiredFieldNames());
    assertEquals(f.getRequiredFieldNames(), new HashSet<String>(Collections.singletonList("negateFilter")));
    assertNotNull(f.getOptionalFieldNames());
    assertEquals(f.getOptionalFieldNames(), Collections.emptySet());
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("a", "b"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("a", 1234))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("a", true))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("b", "a"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("b", "b"))));
    assertTrue(f.matchesJSONObject(new JSONObject()));
    final ANDJSONObjectFilter andFilter = new ANDJSONObjectFilter();
    f.setNegateFilter(andFilter);
    assertNotNull(f.toJSONObject());
    assertEquals(f.toJSONObject(), new JSONObject(new JSONField("filterType", "negate"), new JSONField("negateFilter", andFilter.toJSONObject())));
    f = (NegateJSONObjectFilter) JSONObjectFilter.decode(f.toJSONObject());
    assertNotNull(f);
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("a", "b"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("a", 1234))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("a", true))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("b", "a"))));
    assertFalse(f.matchesJSONObject(new JSONObject(new JSONField("b", "b"))));
    assertFalse(f.matchesJSONObject(new JSONObject()));
    final ORJSONObjectFilter orFilter = new ORJSONObjectFilter();
    f.setNegateFilter(orFilter);
    assertNotNull(f.toJSONObject());
    assertEquals(f.toJSONObject(), new JSONObject(new JSONField("filterType", "negate"), new JSONField("negateFilter", orFilter.toJSONObject())));
    f = (NegateJSONObjectFilter) JSONObjectFilter.decode(f.toJSONObject());
    assertNotNull(f);
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("a", "b"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("a", 1234))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("a", true))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("b", "a"))));
    assertTrue(f.matchesJSONObject(new JSONObject(new JSONField("b", "b"))));
    assertTrue(f.matchesJSONObject(new JSONObject()));
    try {
        f.setNegateFilter(null);
        fail("Expected an exception from setNegateFilter(null)");
    } catch (final LDAPSDKUsageException e) {
    // This was expected
    }
}
Also used : JSONObject(com.unboundid.util.json.JSONObject) JSONField(com.unboundid.util.json.JSONField) LDAPSDKUsageException(com.unboundid.util.LDAPSDKUsageException) JSONString(com.unboundid.util.json.JSONString) JSONString(com.unboundid.util.json.JSONString) Test(org.testng.annotations.Test)

Aggregations

LDAPSDKUsageException (com.unboundid.util.LDAPSDKUsageException)17 Test (org.testng.annotations.Test)17 JSONString (com.unboundid.util.json.JSONString)12 JSONField (com.unboundid.util.json.JSONField)8 JSONObject (com.unboundid.util.json.JSONObject)8 JSONArray (com.unboundid.util.json.JSONArray)5 InMemoryDirectoryServer (com.unboundid.ldap.listener.InMemoryDirectoryServer)4 InMemoryDirectoryServerConfig (com.unboundid.ldap.listener.InMemoryDirectoryServerConfig)4 JSONNumber (com.unboundid.util.json.JSONNumber)4 JSONException (com.unboundid.util.json.JSONException)1