From 132dd3160f68d191687de3ac1d61ae29f4fc7e9d Mon Sep 17 00:00:00 2001
From: Jean-Noel Rouvignac <jean-noel.rouvignac@forgerock.com>
Date: Thu, 17 Oct 2013 13:50:45 +0000
Subject: [PATCH] Fixed GenerationIdTest.generationIdTest(): test was failing precisely because the directory server's broker was not connected to a replication domain, hence it was impossible to add the changes to the changelogDB and then find them when querying the changelogDB.

---
 opendj-sdk/opends/src/server/org/opends/server/replication/service/ReplicationBroker.java                            |   22 +++++++++--
 opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/replication/ReplicationTestCase.java          |    4 -
 opendj-sdk/opends/src/server/org/opends/server/replication/plugin/LDAPReplicationDomain.java                         |   19 +++------
 opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/replication/server/ReplicationServerTest.java |    1 
 opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/replication/GenerationIdTest.java             |   26 ++++++++++---
 5 files changed, 45 insertions(+), 27 deletions(-)

diff --git a/opendj-sdk/opends/src/server/org/opends/server/replication/plugin/LDAPReplicationDomain.java b/opendj-sdk/opends/src/server/org/opends/server/replication/plugin/LDAPReplicationDomain.java
index 4265759..21767cc 100644
--- a/opendj-sdk/opends/src/server/org/opends/server/replication/plugin/LDAPReplicationDomain.java
+++ b/opendj-sdk/opends/src/server/org/opends/server/replication/plugin/LDAPReplicationDomain.java
@@ -1778,10 +1778,8 @@
          */
         if (fractionalFilterOperation(addOperation, false))
         {
-          StringBuilder sb = new StringBuilder();
-          addOperation.toString(sb);
           Message msg = NOTE_ERR_FRACTIONAL_FORBIDDEN_OPERATION.get(
-            getBaseDNString(), sb.toString());
+            getBaseDNString(), addOperation.toString());
           return new SynchronizationProviderResult.StopProcessing(
             ResultCode.UNWILLING_TO_PERFORM, msg);
         }
@@ -1907,10 +1905,8 @@
          */
         if (fractionalFilterOperation(modifyDNOperation, false))
         {
-          StringBuilder sb = new StringBuilder();
-          modifyDNOperation.toString(sb);
           Message msg = NOTE_ERR_FRACTIONAL_FORBIDDEN_OPERATION.get(
-            getBaseDNString(), sb.toString());
+            getBaseDNString(), modifyDNOperation.toString());
           return new SynchronizationProviderResult.StopProcessing(
             ResultCode.UNWILLING_TO_PERFORM, msg);
         }
@@ -2042,10 +2038,8 @@
           case FRACTIONAL_HAS_FRACTIONAL_FILTERED_ATTRIBUTES:
             // Some attributes not compliant with fractional configuration :
             // forbid the operation
-            StringBuilder sb = new StringBuilder();
-            modifyOperation.toString(sb);
             Message msg = NOTE_ERR_FRACTIONAL_FORBIDDEN_OPERATION.get(
-              getBaseDNString(), sb.toString());
+              getBaseDNString(), modifyOperation.toString());
             return new SynchronizationProviderResult.StopProcessing(
               ResultCode.UNWILLING_TO_PERFORM, msg);
         }
@@ -3811,10 +3805,10 @@
       }
 
       //  Release the shared lock on the backend.
-      String lockFile = LockFileManager.getBackendLockFileName(backend);
-      StringBuilder failureReason = new StringBuilder();
       try
       {
+        String lockFile = LockFileManager.getBackendLockFileName(backend);
+        StringBuilder failureReason = new StringBuilder();
         if (! LockFileManager.releaseLock(lockFile, failureReason))
         {
           Message message = WARN_LDIFEXPORT_CANNOT_UNLOCK_BACKEND.get(
@@ -3976,8 +3970,7 @@
    * @param backend The backend implied in the import.
    * @exception DirectoryException Thrown when an error occurs.
    */
-  protected void closeBackendImport(Backend backend)
-  throws DirectoryException
+  protected void closeBackendImport(Backend backend) throws DirectoryException
   {
     String lockFile = LockFileManager.getBackendLockFileName(backend);
     StringBuilder failureReason = new StringBuilder();
diff --git a/opendj-sdk/opends/src/server/org/opends/server/replication/service/ReplicationBroker.java b/opendj-sdk/opends/src/server/org/opends/server/replication/service/ReplicationBroker.java
index 9b3dc97..585a7d2 100644
--- a/opendj-sdk/opends/src/server/org/opends/server/replication/service/ReplicationBroker.java
+++ b/opendj-sdk/opends/src/server/org/opends/server/replication/service/ReplicationBroker.java
@@ -3042,9 +3042,23 @@
   @Override
   public String toString()
   {
-    return getClass().getSimpleName() + " \"" + baseDN + " " + serverId + "\","
-        + " groupId=" + groupId + ", genId=" + generationID
-        + ", bestRS(serverId=" + rsServerId + ", serverUrl=" + rsServerUrl
-        + ", groupId=" + rsGroupId + ")";
+    final StringBuilder sb = new StringBuilder();
+    sb.append(getClass().getSimpleName())
+      .append(" \"").append(baseDN).append(" ").append(serverId).append("\",")
+      .append(" groupId=").append(groupId)
+      .append(", genId=").append(generationID)
+      .append(", connected=").append(connected).append(", ");
+    if (rsServerId == -1)
+    {
+      sb.append("no RS");
+    }
+    else
+    {
+      sb.append("bestRS(serverId=").append(rsServerId)
+        .append(", serverUrl=").append(rsServerUrl)
+        .append(", groupId=").append(rsGroupId)
+        .append(")");
+    }
+    return sb.toString();
   }
 }
diff --git a/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/replication/GenerationIdTest.java b/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/replication/GenerationIdTest.java
index f5ccd53..43a04de 100644
--- a/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/replication/GenerationIdTest.java
+++ b/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/replication/GenerationIdTest.java
@@ -660,7 +660,7 @@
       debugInfo(testCase + " ** TEST ** The part of the topology with the right gen ID should work well");
 
       // Now create a change that must be replicated
-      assertConnectedToReplicationDomain();
+      waitConnectionToReplicationDomain(baseDN, 1000);
       addTestEntriesToDB(createEntry(UUID.randomUUID()));
 
       // Verify that RS1 does contain the change related to this ADD.
@@ -766,7 +766,7 @@
           "Expecting that DS3 with old gen ID is in bad gen id from RS1");
 
       debugInfo("Add entries to DS1, update should not be sent to DS2 and DS3 that are in bad gen id");
-      assertConnectedToReplicationDomain();
+      waitConnectionToReplicationDomain(baseDN, 1000);
       addTestEntriesToDB(createEntry(UUID.randomUUID()));
 
       debugInfo("RS1 must have stored that update.");
@@ -861,11 +861,25 @@
     }
   }
 
-  private void assertConnectedToReplicationDomain()
+  /**
+   * Waits for the connection from server1 to the replication domain to
+   * establish itself up automagically.
+   */
+  private void waitConnectionToReplicationDomain(DN baseDN, int timeout)
   {
+    long start = System.currentTimeMillis();
+    while (System.currentTimeMillis() - start <= timeout)
+    {
+      LDAPReplicationDomain domain = MultimasterReplication.findDomain(baseDN, null);
+      if (domain != null && domain.isConnected())
+      {
+        break;
+      }
+    }
     assertTrue(MultimasterReplication.findDomain(baseDN, null).isConnected(),
-        "The server should be connected to replication domain" + baseDN
-            + " at this point");
+        "After waiting " + (System.currentTimeMillis() - start)
+            + " ms, server should have been connected to replication domain "
+            + baseDN);
   }
 
   private Entry createSetGenerationIdTask(Long genId, String additionalAttribute) throws Exception
@@ -1065,7 +1079,7 @@
       assertEquals(readGenIdFromSuffixRootEntry(), -1,
           "genId attribute should not be retrievable since there are NO entry in the backend");
 
-      assertConnectedToReplicationDomain();
+      waitConnectionToReplicationDomain(baseDN, 1000);
       addTestEntriesToDB(updatedEntries);
       assertEquals(readGenIdFromSuffixRootEntry(), EMPTY_DN_GENID,
           "genId attribute should be retrievable since there IS one entry in the backend");
diff --git a/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/replication/ReplicationTestCase.java b/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/replication/ReplicationTestCase.java
index fb52c58..fba2fcf 100644
--- a/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/replication/ReplicationTestCase.java
+++ b/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/replication/ReplicationTestCase.java
@@ -386,8 +386,7 @@
       "Found unexpected replication server config left");
 
     // Be sure that no replication server instance is left
-    List<ReplicationServer> allRSInstances = ReplicationServer.getAllInstances();
-    assertEquals(allRSInstances.size(), 0, "Some replication servers left: " + allRSInstances);
+    Assertions.assertThat(ReplicationServer.getAllInstances()).isEmpty();
 
     // Check for config entries for replication domain
     assertNoConfigEntriesWithFilter("(objectclass=ds-cfg-replication-domain)",
@@ -834,7 +833,6 @@
    */
   protected void addTestEntriesToDB(String... ldifEntries) throws Exception
   {
-    // Change config of DS to launch the total update task
     for (String ldifEntry : ldifEntries)
     {
       Entry entry = TestCaseUtils.entryFromLdifString(ldifEntry);
diff --git a/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/replication/server/ReplicationServerTest.java b/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/replication/server/ReplicationServerTest.java
index 204bf84..6f26f6c 100644
--- a/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/replication/server/ReplicationServerTest.java
+++ b/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/replication/server/ReplicationServerTest.java
@@ -133,7 +133,6 @@
         "--set", "replication-port:" + replicationServerPort,
         "--set", "replication-server-id:71");
 
-    DirectoryServer.getSynchronizationProviders();
     for (SynchronizationProvider<?> provider : DirectoryServer
         .getSynchronizationProviders()) {
       if (provider instanceof MultimasterReplication) {

--
Gitblit v1.10.0