package eu.europa.esig.dss.pades;

import java.io.File;
import java.util.Date;

import org.junit.Assert;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import eu.europa.esig.dss.DSSDocument;
import eu.europa.esig.dss.FileDocument;
import eu.europa.esig.dss.SignatureAlgorithm;
import eu.europa.esig.dss.SignatureLevel;
import eu.europa.esig.dss.SignaturePackaging;
import eu.europa.esig.dss.SignatureValue;
import eu.europa.esig.dss.ToBeSigned;
import eu.europa.esig.dss.pades.signature.PAdESService;
import eu.europa.esig.dss.signature.DocumentSignatureService;
import eu.europa.esig.dss.test.TestUtils;
import eu.europa.esig.dss.test.gen.CertificateService;
import eu.europa.esig.dss.test.mock.MockPrivateKeyEntry;
import eu.europa.esig.dss.validation.CertificateVerifier;
import eu.europa.esig.dss.validation.CommonCertificateVerifier;
import eu.europa.esig.dss.validation.SignedDocumentValidator;
import eu.europa.esig.dss.validation.reports.Reports;

public class TwoPAdESSigniatureMustHaveDifferentIdTest {

	private static final Logger logger = LoggerFactory.getLogger(TwoPAdESSigniatureMustHaveDifferentIdTest.class);

	@Test
	public void test() throws Exception {
		DSSDocument documentToSign = new FileDocument(new File("src/test/resources/sample.pdf"));

		CertificateService certificateService = new CertificateService();
		MockPrivateKeyEntry privateKeyEntry = certificateService.generateCertificateChain(SignatureAlgorithm.RSA_SHA256);

		PAdESSignatureParameters signatureParameters = new PAdESSignatureParameters();
		signatureParameters.bLevel().setSigningDate(new Date());
		signatureParameters.setSigningCertificate(privateKeyEntry.getCertificate());
		signatureParameters.setCertificateChain(privateKeyEntry.getCertificateChain());
		signatureParameters.setSignaturePackaging(SignaturePackaging.ENVELOPING);
		signatureParameters.setSignatureLevel(SignatureLevel.PAdES_BASELINE_B);
		signatureParameters.setLocation("Luxembourg");
		signatureParameters.setReason("DSS testing");
		signatureParameters.setContactInfo("Jira");

		CertificateVerifier certificateVerifier = new CommonCertificateVerifier();
		DocumentSignatureService<PAdESSignatureParameters> service = new PAdESService(certificateVerifier);

		ToBeSigned dataToSign = service.getDataToSign(documentToSign, signatureParameters);
		SignatureValue signatureValue = TestUtils.sign(signatureParameters.getSignatureAlgorithm(), privateKeyEntry, dataToSign);
		DSSDocument firstSignedDocument = service.signDocument(documentToSign, signatureParameters, signatureValue);

		SignedDocumentValidator validator = SignedDocumentValidator.fromDocument(firstSignedDocument);
		validator.setCertificateVerifier(new CommonCertificateVerifier());
		Reports reports = validator.validateDocument();
		String firstId = reports.getSimpleReport().getFirstSignatureId();

		// milli seconds are splitted
		Thread.sleep(500);

		signatureParameters.bLevel().setSigningDate(new Date());
		dataToSign = service.getDataToSign(documentToSign, signatureParameters);
		signatureValue = TestUtils.sign(signatureParameters.getSignatureAlgorithm(), privateKeyEntry, dataToSign);
		DSSDocument secondSignedDocument = service.signDocument(documentToSign, signatureParameters, signatureValue);

		validator = SignedDocumentValidator.fromDocument(secondSignedDocument);
		validator.setCertificateVerifier(new CommonCertificateVerifier());
		reports = validator.validateDocument();
		String secondId = reports.getSimpleReport().getFirstSignatureId();

		logger.info("First signature id  : " + firstId);
		logger.info("Second signature id  : " + secondId);

		Assert.assertNotEquals(firstId, secondId);
	}
}

@@ -71,16 +71,65 @@
 il.Date;
+%0Aimport java.util.HashSet;%0Aimport java.util.List;
 %0A%0Aimport
@@ -2913,62 +2913,8 @@
 );%0A%0A
-%09%09// milli seconds are splitted%0A%09%09Thread.sleep(500);%0A%0A
 %09%09si
@@ -3001,38 +3001,43 @@
 tDataToSign(
-d
+firstSignedD
 ocument
-ToSign
 , signatureP
@@ -3211,38 +3211,43 @@
 ignDocument(
-d
+firstSignedD
 ocument
-ToSign
 , signatureP
@@ -3464,25 +3464,39 @@
 ();%0A
+%0A
 %09%09
+List%3C
 String
+%3E
  s
-econdId
+ignatureIdList
  = r
@@ -3515,37 +3515,32 @@
 mpleReport().get
-First
 SignatureId();%0A%0A
@@ -3538,118 +3538,113 @@
 reId
+List
 ();%0A%0A%09%09
-logger.info(%22First signature id  : %22 + firstId);%0A%09%09logger.info(%22Second signature id  : %22 + secondId
+Assert.assertEquals(2, new HashSet%3C%3E(reports.getSimpleReport().getSignatureIdList()).size()
 );%0A
-%0A
 %09%09As
@@ -3668,28 +3668,58 @@
 als(
-firstId, secondId
+signatureIdList.get(0), signatureIdList.get(1)
 );%0A
+%0A
 %09%7D%0A%7D

Fix unit test