@Test
 public void systemProperty() throws IOException {
   System.setProperty("p1", "sys");
   PropertiesLoader pl =
       new PropertiesLoader("classpath:/test1.properties", "classpath:/test2.properties");
   assertThat(pl.getProperty("p1")).isEqualTo("sys");
   System.clearProperty("p1");
 }
  @BeforeClass
  public static void beforeClass() throws Exception {

    VaultRule vaultRule = new VaultRule();
    vaultRule.before();

    assumeTrue(vaultRule.prepare().getVersion().isGreaterThanOrEqualTo(Version.parse("0.6.1")));

    VaultProperties vaultProperties = Settings.createVaultProperties();

    if (!vaultRule.prepare().hasAuth(vaultProperties.getAppRole().getAppRolePath())) {
      vaultRule.prepare().mountAuth(vaultProperties.getAppRole().getAppRolePath());
    }

    VaultOperations vaultOperations = vaultRule.prepare().getVaultOperations();

    String rules =
        "{ \"name\": \"testpolicy\",\n" //
            + "  \"path\": {\n" //
            + "    \"*\": {  \"policy\": \"read\" }\n" //
            + "  }\n" //
            + "}";

    vaultOperations.write("sys/policy/testpolicy", Collections.singletonMap("rules", rules));

    String appId = VaultConfigAppRoleTests.class.getSimpleName();

    vaultOperations.write(
        "secret/" + VaultConfigAppRoleTests.class.getSimpleName(),
        Collections.singletonMap("vault.value", "foo"));

    Map<String, String> withSecretId = new HashMap<String, String>();
    withSecretId.put("policies", "testpolicy"); // policy
    withSecretId.put("bound_cidr_list", "0.0.0.0/0");
    withSecretId.put("bind_secret_id", "true");

    vaultOperations.write("auth/approle/role/with-secret-id", withSecretId);

    String roleId =
        (String)
            vaultOperations
                .read("auth/approle/role/with-secret-id/role-id")
                .getData()
                .get("role_id");
    String secretId =
        (String)
            vaultOperations
                .write(
                    String.format("auth/approle/role/with-secret-id/secret-id", "with-secret-id"),
                    null)
                .getData()
                .get("secret_id");

    System.setProperty("spring.cloud.vault.app-role.role-id", roleId);
    System.setProperty("spring.cloud.vault.app-role.secret-id", secretId);
  }
 private File createCleanDir() throws IOException {
   String buildRoot = System.getProperty(TestCaseUtils.PROPERTY_BUILD_ROOT);
   String path =
       System.getProperty(TestCaseUtils.PROPERTY_BUILD_DIR, buildRoot + File.separator + "build");
   path = path + File.separator + "unit-tests" + File.separator + "JEReplicaDB";
   final File testRoot = new File(path);
   TestCaseUtils.deleteDirectory(testRoot);
   testRoot.mkdirs();
   return testRoot;
 }
  @Test
  public void testTwoDraws() throws Exception {
    // Test initialisation

    /*
     * find a seed for which the draw differs: we loop until the
     * first index drawn (Random(seed).nextInt(SilanisLottery.MAX_BALL))
     * is different from the first index drawn for
     * Random(seed + 1).SilanisLottery.MAX_BALL
     */
    long seed;
    do {
      seed = System.currentTimeMillis();
    } while (new Random(seed).nextInt(SilanisLottery.MAX_BALL)
        == new Random(seed + 1).nextInt(SilanisLottery.MAX_BALL));

    this.drawableInteger = new DrawableInteger(new Random(seed));

    final List<Integer> draws = new ArrayList<>();
    this.fillDraws(draws);

    final List<Integer> secondDraws = new ArrayList<>();
    this.drawableInteger = new DrawableInteger(new Random(seed + 1));
    this.fillDraws(secondDraws);
    assertThat(secondDraws).as("The sequence of draws is supposed").isNotEqualTo(draws);
  }
 @Before
 public void setUp() throws Exception {
   logger = new ConsoleAuditLogger();
   ace = mock(AuditableAccessControlEntry.class);
   console = System.out;
   System.setOut(new PrintStream(bytes));
 }
  @Test
  public void testTwoDrawsInitializedWithTheSameSeed() throws Exception {
    final long seed = System.currentTimeMillis();
    this.drawableInteger = new DrawableInteger(new Random(seed));

    final List<Integer> draws = new ArrayList<>();
    this.fillDraws(draws);

    final List<Integer> secondDraws = new ArrayList<>();
    this.drawableInteger = new DrawableInteger(new Random(seed));
    this.fillDraws(secondDraws);
    // secondDraws will be strictly equal to this.drawableInteger
    assertThat(secondDraws).isEqualTo(draws);
  }
 @After
 public void tearDown() throws Exception {
   System.setOut(console);
   bytes.reset();
 }
  private void testGetOldestNewestCSNs(final int max, final int counterWindow) throws Exception {
    String tn = "testDBCount(" + max + "," + counterWindow + ")";
    debugInfo(tn, "Starting test");

    File testRoot = null;
    ReplicationServer replicationServer = null;
    ReplicationDbEnv dbEnv = null;
    JEReplicaDB replicaDB = null;
    try {
      TestCaseUtils.startServer();
      replicationServer = configureReplicationServer(100000, 10);

      testRoot = createCleanDir();
      dbEnv = new ReplicationDbEnv(testRoot.getPath(), replicationServer);
      replicaDB = new JEReplicaDB(1, TEST_ROOT_DN, replicationServer, dbEnv);
      replicaDB.setCounterRecordWindowSize(counterWindow);

      // Populate the db with 'max' msg
      int mySeqnum = 1;
      CSN csns[] = new CSN[2 * (max + 1)];
      long now = System.currentTimeMillis();
      for (int i = 1; i <= max; i++) {
        csns[i] = new CSN(now + i, mySeqnum, 1);
        replicaDB.add(new DeleteMsg(TEST_ROOT_DN, csns[i], "uid"));
        mySeqnum += 2;
      }

      assertEquals(replicaDB.getOldestCSN(), csns[1], "Wrong oldest CSN");
      assertEquals(replicaDB.getNewestCSN(), csns[max], "Wrong newest CSN");

      // Now we want to test that after closing and reopening the db, the
      // counting algo is well reinitialized and when new messages are added
      // the new counter are correctly generated.
      debugInfo(tn, "SHUTDOWN replicaDB and recreate");
      replicaDB.shutdown();

      replicaDB = new JEReplicaDB(1, TEST_ROOT_DN, replicationServer, dbEnv);
      replicaDB.setCounterRecordWindowSize(counterWindow);

      assertEquals(replicaDB.getOldestCSN(), csns[1], "Wrong oldest CSN");
      assertEquals(replicaDB.getNewestCSN(), csns[max], "Wrong newest CSN");

      // Populate the db with 'max' msg
      for (int i = max + 1; i <= 2 * max; i++) {
        csns[i] = new CSN(now + i, mySeqnum, 1);
        replicaDB.add(new DeleteMsg(TEST_ROOT_DN, csns[i], "uid"));
        mySeqnum += 2;
      }

      assertEquals(replicaDB.getOldestCSN(), csns[1], "Wrong oldest CSN");
      assertEquals(replicaDB.getNewestCSN(), csns[2 * max], "Wrong newest CSN");

      replicaDB.purgeUpTo(new CSN(Long.MAX_VALUE, 0, 0));

      String testcase = "AFTER PURGE (oldest, newest)=";
      debugInfo(tn, testcase + replicaDB.getOldestCSN() + replicaDB.getNewestCSN());
      assertEquals(replicaDB.getNewestCSN(), csns[2 * max], "Newest=");

      // Clear ...
      debugInfo(tn, "clear:");
      replicaDB.clear();

      // Check the db is cleared.
      assertEquals(null, replicaDB.getOldestCSN());
      assertEquals(null, replicaDB.getNewestCSN());
      debugInfo(tn, "Success");
    } finally {
      shutdown(replicaDB);
      if (dbEnv != null) {
        dbEnv.shutdown();
      }
      remove(replicationServer);
      TestCaseUtils.deleteDirectory(testRoot);
    }
  }
  @DataProvider
  Object[][] cursorData() {
    // create 7 csns
    final CSN[] sevenCsns = generateCSNs(1, System.currentTimeMillis(), 7);
    CSN beforeCsn = sevenCsns[0];
    CSN middleCsn = sevenCsns[3]; // will be between csns[1] and csns[2]
    CSN afterCsn = sevenCsns[6];

    // but use only 4 of them for update msg
    // beforeCsn, middleCsn and afterCsn are not used
    // in order to test cursor generation from a key not present in the log (before, in the middle,
    // after)
    final List<CSN> usedCsns = new ArrayList<CSN>(Arrays.asList(sevenCsns));
    usedCsns.remove(beforeCsn);
    usedCsns.remove(middleCsn);
    usedCsns.remove(afterCsn);
    final CSN[] csns = usedCsns.toArray(new CSN[4]);

    return new Object[][] {
      // equal matching
      {csns, beforeCsn, EQUAL_TO_KEY, ON_MATCHING_KEY, -1, -1},
      {csns, csns[0], EQUAL_TO_KEY, ON_MATCHING_KEY, 0, 3},
      {csns, csns[1], EQUAL_TO_KEY, ON_MATCHING_KEY, 1, 3},
      {csns, middleCsn, EQUAL_TO_KEY, ON_MATCHING_KEY, -1, -1},
      {csns, csns[2], EQUAL_TO_KEY, ON_MATCHING_KEY, 2, 3},
      {csns, csns[3], EQUAL_TO_KEY, ON_MATCHING_KEY, 3, 3},
      {csns, afterCsn, EQUAL_TO_KEY, ON_MATCHING_KEY, -1, -1},
      {csns, beforeCsn, EQUAL_TO_KEY, AFTER_MATCHING_KEY, -1, -1},
      {csns, csns[0], EQUAL_TO_KEY, AFTER_MATCHING_KEY, 1, 3},
      {csns, csns[1], EQUAL_TO_KEY, AFTER_MATCHING_KEY, 2, 3},
      {csns, middleCsn, EQUAL_TO_KEY, AFTER_MATCHING_KEY, -1, -1},
      {csns, csns[2], EQUAL_TO_KEY, AFTER_MATCHING_KEY, 3, 3},
      {csns, csns[3], EQUAL_TO_KEY, AFTER_MATCHING_KEY, -1, -1},
      {csns, afterCsn, EQUAL_TO_KEY, AFTER_MATCHING_KEY, -1, -1},

      // less than or equal matching
      {csns, beforeCsn, LESS_THAN_OR_EQUAL_TO_KEY, ON_MATCHING_KEY, -1, -1},
      {csns, csns[0], LESS_THAN_OR_EQUAL_TO_KEY, ON_MATCHING_KEY, 0, 3},
      {csns, csns[1], LESS_THAN_OR_EQUAL_TO_KEY, ON_MATCHING_KEY, 1, 3},
      {csns, middleCsn, LESS_THAN_OR_EQUAL_TO_KEY, ON_MATCHING_KEY, 1, 3},
      {csns, csns[2], LESS_THAN_OR_EQUAL_TO_KEY, ON_MATCHING_KEY, 2, 3},
      {csns, csns[3], LESS_THAN_OR_EQUAL_TO_KEY, ON_MATCHING_KEY, 3, 3},
      {csns, afterCsn, LESS_THAN_OR_EQUAL_TO_KEY, ON_MATCHING_KEY, 3, 3},
      {csns, beforeCsn, LESS_THAN_OR_EQUAL_TO_KEY, AFTER_MATCHING_KEY, -1, -1},
      {csns, csns[0], LESS_THAN_OR_EQUAL_TO_KEY, AFTER_MATCHING_KEY, 1, 3},
      {csns, csns[1], LESS_THAN_OR_EQUAL_TO_KEY, AFTER_MATCHING_KEY, 2, 3},
      {csns, middleCsn, LESS_THAN_OR_EQUAL_TO_KEY, AFTER_MATCHING_KEY, 2, 3},
      {csns, csns[2], LESS_THAN_OR_EQUAL_TO_KEY, AFTER_MATCHING_KEY, 3, 3},
      {csns, csns[3], LESS_THAN_OR_EQUAL_TO_KEY, AFTER_MATCHING_KEY, -1, -1},
      {csns, afterCsn, LESS_THAN_OR_EQUAL_TO_KEY, AFTER_MATCHING_KEY, -1, -1},

      // greater than or equal matching
      {csns, beforeCsn, GREATER_THAN_OR_EQUAL_TO_KEY, ON_MATCHING_KEY, 0, 3},
      {csns, csns[0], GREATER_THAN_OR_EQUAL_TO_KEY, ON_MATCHING_KEY, 0, 3},
      {csns, csns[1], GREATER_THAN_OR_EQUAL_TO_KEY, ON_MATCHING_KEY, 1, 3},
      {csns, middleCsn, GREATER_THAN_OR_EQUAL_TO_KEY, ON_MATCHING_KEY, 2, 3},
      {csns, csns[2], GREATER_THAN_OR_EQUAL_TO_KEY, ON_MATCHING_KEY, 2, 3},
      {csns, csns[3], GREATER_THAN_OR_EQUAL_TO_KEY, ON_MATCHING_KEY, 3, 3},
      {csns, afterCsn, GREATER_THAN_OR_EQUAL_TO_KEY, ON_MATCHING_KEY, -1, -1},
      {csns, beforeCsn, GREATER_THAN_OR_EQUAL_TO_KEY, AFTER_MATCHING_KEY, 0, 3},
      {csns, csns[0], GREATER_THAN_OR_EQUAL_TO_KEY, AFTER_MATCHING_KEY, 1, 3},
      {csns, csns[1], GREATER_THAN_OR_EQUAL_TO_KEY, AFTER_MATCHING_KEY, 2, 3},
      {csns, middleCsn, GREATER_THAN_OR_EQUAL_TO_KEY, AFTER_MATCHING_KEY, 2, 3},
      {csns, csns[2], GREATER_THAN_OR_EQUAL_TO_KEY, AFTER_MATCHING_KEY, 3, 3},
      {csns, csns[3], GREATER_THAN_OR_EQUAL_TO_KEY, AFTER_MATCHING_KEY, -1, -1},
      {csns, afterCsn, GREATER_THAN_OR_EQUAL_TO_KEY, AFTER_MATCHING_KEY, -1, -1},
      {null, null, null, null, -1, -1} // stop line
    };
  }