mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

Jean-Noël Rouvignac
03.41.2015 a11a0561f324860f1381a3b57212e20ede1ad05f
Fix randomness in ChangelogBackendTestCase

ChangelogBackendTestCase.java:
In searchChangelog(), move the assert on the SearchOperation ResultCode inside the loop that asserts the number of returned search entries.

ExternalChangelogRequestControl.java:
In getCookie(), copied the cookie using new MultiDomainServerState constructor. This prevents unit tests from mutating the cookie coming from the control and making repeated calls with the same SearchRequest fail.

MultiDomainServerState.java, MultiDomainServerStateTest.java:
Added copy constructor + tests
4 files modified
90 ■■■■ changed files
opendj-server-legacy/src/main/java/org/opends/server/controls/ExternalChangelogRequestControl.java 7 ●●●●● patch | view | raw | blame | history
opendj-server-legacy/src/main/java/org/opends/server/replication/common/MultiDomainServerState.java 24 ●●●● patch | view | raw | blame | history
opendj-server-legacy/src/test/java/org/opends/server/backends/ChangelogBackendTestCase.java 16 ●●●●● patch | view | raw | blame | history
opendj-server-legacy/src/test/java/org/opends/server/replication/common/MultiDomainServerStateTest.java 43 ●●●● patch | view | raw | blame | history
opendj-server-legacy/src/main/java/org/opends/server/controls/ExternalChangelogRequestControl.java
@@ -110,12 +110,13 @@
  }
  /**
   * Returns the cookie value.
   * @return The cookie value.
   * Returns a copy of the cookie value.
   *
   * @return a copy of the cookie value
   */
  public MultiDomainServerState getCookie()
  {
    return this.cookie;
    return new MultiDomainServerState(cookie);
  }
  @Override
opendj-server-legacy/src/main/java/org/opends/server/replication/common/MultiDomainServerState.java
@@ -64,13 +64,31 @@
  }
  /**
   * Copy constructor.
   *
   * @param cookie
   *          the cookie to copy
   */
  public MultiDomainServerState(MultiDomainServerState cookie)
  {
    list = new ConcurrentSkipListMap<>();
    for (Map.Entry<DN, ServerState> mapEntry : cookie.list.entrySet())
    {
      DN dn = mapEntry.getKey();
      ServerState state = mapEntry.getValue();
      list.put(dn, state.duplicate());
    }
  }
  /**
   * Create an object from a string representation.
   * @param mdss The provided string representation of the state.
   * @param cookie The provided string representation of the state.
   * @throws DirectoryException when the string has an invalid format
   */
  public MultiDomainServerState(String mdss) throws DirectoryException
  public MultiDomainServerState(String cookie) throws DirectoryException
  {
    list = new ConcurrentSkipListMap<>(splitGenStateToServerStates(mdss));
    list = new ConcurrentSkipListMap<>(splitGenStateToServerStates(cookie));
  }
  /**
opendj-server-legacy/src/test/java/org/opends/server/backends/ChangelogBackendTestCase.java
@@ -52,6 +52,7 @@
import java.util.SortedSet;
import java.util.concurrent.Callable;
import org.assertj.core.api.SoftAssertions;
import org.forgerock.i18n.slf4j.LocalizedLogger;
import org.forgerock.opendj.ldap.ByteString;
import org.forgerock.opendj.ldap.ModificationType;
@@ -1119,7 +1120,7 @@
  }
  private InternalSearchOperation searchChangelog(final SearchRequest request, final int expectedNbEntries,
      ResultCode expectedResultCode, String testName) throws Exception
      final ResultCode expectedResultCode, String testName) throws Exception
  {
    TestTimer timer = new TestTimer.Builder()
      .maxSleep(5, SECONDS)
@@ -1131,15 +1132,18 @@
      public InternalSearchOperation call() throws Exception
      {
        InternalSearchOperation searchOp = connection.processSearch(request);
        assertThat(searchOp.getSearchEntries()).hasSize(expectedNbEntries);
        final SoftAssertions softly = new SoftAssertions();
        softly.assertThat(searchOp.getResultCode()).as(searchOp.getErrorMessage().toString())
            .isEqualTo(expectedResultCode);
        softly.assertThat(searchOp.getSearchEntries()).hasSize(expectedNbEntries);
        softly.assertAll();
        return searchOp;
      }
    });
    final List<SearchResultEntry> entries = searchOp.getSearchEntries();
    assertThat(entries).hasSize(expectedNbEntries);
    debugAndWriteEntries(getLDIFWriter(), entries, testName);
    waitForSearchOpResult(searchOp, expectedResultCode);
    debugAndWriteEntries(getLDIFWriter(), searchOp.getSearchEntries(), testName);
    return searchOp;
  }
opendj-server-legacy/src/test/java/org/opends/server/replication/common/MultiDomainServerStateTest.java
@@ -33,6 +33,7 @@
import org.opends.server.replication.ReplicationTestCase;
import org.opends.server.types.DN;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
@SuppressWarnings("javadoc")
@@ -55,21 +56,43 @@
    dn3 = DN.valueOf("o=test3");
  }
  @Test
  public void testDecodeAndEncode1() throws Exception
  @DataProvider
  public Object[][] decodeAndEncodeData()
  {
    final String cookie = "o=test1:" + csn1 + ";o=test2:" + csn2 + ";o=test6:";
    final MultiDomainServerState state = new MultiDomainServerState(cookie);
    assertEquals(state.toString(), cookie + ";");
    String cookie = "o=test1:" + csn1 + ";o=test2:" + csn2 + ";o=test6:";
    return new Object[][] {
      { "", "" },
      { "o=test1:", "o=test1:;" },
      { ";;o=test1:;;", ":;o=test1:;" },
      { cookie, cookie + ";" },
      { "o=test1:" + csn1 + ";o=test2:" + csn2 + ";;o=test6:", ":;o=test1:" + csn1 + ";o=test2:" + csn2 + ";o=test6:;" },
    };
  }
  @Test
  public void testDecodeAndEncode2() throws Exception
  @Test(dataProvider = "decodeAndEncodeData")
  public void decodeAndEncode(String cookie, String expectedCookie) throws Exception
  {
    final String cookie = "o=test1:" + csn1 + ";o=test2:" + csn2 + ";;o=test6:";
    final MultiDomainServerState state = new MultiDomainServerState(cookie);
    final String expected = ":;o=test1:" + csn1 + ";o=test2:" + csn2 + ";o=test6:;";
    assertEquals(state.toString(), expected);
    assertEquals(state.toString(), expectedCookie);
  }
  @DataProvider
  public Object[][] copyCtorData()
  {
    return new Object[][] {
      { "" },
      { "o=test1:" + csn1 + ";" },
      { "o=test1:" + csn1 + ";o=test2:" + csn2 + ";" },
      { "o=test1:" + csn1 + ";o=test2:" + csn2 + ";o=test6:;" },
    };
  }
  @Test(dataProvider = "copyCtorData")
  public void copyCtor(String cookie) throws Exception
  {
    final MultiDomainServerState state = new MultiDomainServerState(cookie);
    final MultiDomainServerState copy = new MultiDomainServerState(state);
    assertEquals(copy.toString(), cookie);
  }
  @Test