/**
   * Tests sampling with different probabilities. This method has a change of false positives but
   * with <code>PROBABILISTIC_TRIES</code> number of tries the probability should be small.
   */
  public void testSampleProbabilistic() throws Exception {
    MockedCertificateSamplerCustomPublisher publisher;
    boolean success;

    // Test that with p=0.0 no certificate is stored
    Properties default0 = new Properties();
    default0.setProperty("outputfolder", TEMP_DIR.getAbsolutePath());
    default0.setProperty("default.samplingmethod", "SAMPLE_PROBABILISTIC");
    default0.setProperty("default.pvalue", "0.0");
    for (int i = 0; i < PROBABILISTIC_TRIES; i++) {
      publisher = createMockedPublisher(default0);
      success = storeCertificate(publisher, SecConst.CERT_ACTIVE, ANY_PROFILEID);
      assertTrue("Status should be success", success);
      assertFalse(
          "Certificate should not have been stored, i=" + i, publisher.isWriteCertificateCalled());
    }

    // Test that with pvalue=1.0 all certificates are stored
    Properties default1 = new Properties();
    default1.setProperty("outputfolder", TEMP_DIR.getAbsolutePath());
    default1.setProperty("default.samplingmethod", "SAMPLE_PROBABILISTIC");
    default1.setProperty("default.pvalue", "1.0");
    for (int i = 0; i < PROBABILISTIC_TRIES; i++) {
      publisher = createMockedPublisher(default1);
      success = storeCertificate(publisher, SecConst.CERT_ACTIVE, ANY_PROFILEID);
      assertTrue("Status should be success", success);
      assertTrue(
          "Certificate should have been stored, i=" + i, publisher.isWriteCertificateCalled());
      publisher.reset();
    }

    // Test that with pvalue=0.5 at least some certificates are stored
    Properties default05 = new Properties();
    default05.setProperty("outputfolder", TEMP_DIR.getAbsolutePath());
    default05.setProperty("default.samplingmethod", "SAMPLE_PROBABILISTIC");
    default05.setProperty("default.pvalue", "0.5");
    int stored = 0;
    for (int i = 0; i < PROBABILISTIC_TRIES; i++) {
      publisher = createMockedPublisher(default05);
      success = storeCertificate(publisher, SecConst.CERT_ACTIVE, ANY_PROFILEID);
      assertTrue("Status should be success", success);

      if (publisher.isWriteCertificateCalled()) {
        stored++;
      }
      publisher.reset();
    }
    assertTrue("At least some should have been stored", stored > 0);
  }