package eu.europa.esig.dss.asic.signature.asice;

import static org.junit.Assert.assertTrue;

import java.util.Date;

import org.junit.Assert;
import org.junit.Test;

import eu.europa.esig.dss.DSSDocument;
import eu.europa.esig.dss.DigestAlgorithm;
import eu.europa.esig.dss.InMemoryDocument;
import eu.europa.esig.dss.SignatureAlgorithm;
import eu.europa.esig.dss.SignatureForm;
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.asic.ASiCSignatureParameters;
import eu.europa.esig.dss.asic.signature.ASiCService;
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.policy.rules.Indication;
import eu.europa.esig.dss.validation.reports.Reports;
import eu.europa.esig.dss.validation.reports.wrapper.DiagnosticData;

/**
 * @author axel.abinet
 *
 */
public class ASiCELevelBXAdESSignFourTimeTest {

	@Test
	public void test() throws Exception {
		DSSDocument documentToSign = new InMemoryDocument("Hello Wolrd !".getBytes(), "test.text");

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

		ASiCSignatureParameters signatureParameters = new ASiCSignatureParameters();
		signatureParameters.bLevel().setSigningDate(new Date());
		signatureParameters.setSigningCertificate(privateKeyEntry.getCertificate());
		signatureParameters.setCertificateChain(privateKeyEntry.getCertificateChain());
		signatureParameters.setSignaturePackaging(SignaturePackaging.ENVELOPING);
		signatureParameters.setSignatureLevel(SignatureLevel.ASiC_E_BASELINE_B);
		signatureParameters.aSiC().setUnderlyingForm(SignatureForm.XAdES);

		CertificateVerifier certificateVerifier = new CommonCertificateVerifier();
		ASiCService service = new ASiCService(certificateVerifier);

		ToBeSigned dataToSign = service.getDataToSign(documentToSign, signatureParameters);
		SignatureValue signatureValue = TestUtils.sign(SignatureAlgorithm.RSA_SHA256, privateKeyEntry, dataToSign);
		DSSDocument signedDocument = service.signDocument(documentToSign, signatureParameters, signatureValue);

		privateKeyEntry = certificateService.generateCertificateChain(SignatureAlgorithm.RSA_SHA256);
		signatureParameters.bLevel().setSigningDate(new Date());
		signatureParameters.setSigningCertificate(privateKeyEntry.getCertificate());
		signatureParameters.setCertificateChain(privateKeyEntry.getCertificateChain());
		signatureParameters.setSignaturePackaging(SignaturePackaging.ENVELOPING);
		signatureParameters.setSignatureLevel(SignatureLevel.ASiC_E_BASELINE_B);
		signatureParameters.aSiC().setUnderlyingForm(SignatureForm.XAdES);
		signatureParameters.setDetachedContent(documentToSign);

		certificateVerifier = new CommonCertificateVerifier();
		service = new ASiCService(certificateVerifier);

		dataToSign = service.getDataToSign(signedDocument, signatureParameters);
		signatureValue = TestUtils.sign(SignatureAlgorithm.RSA_SHA256, privateKeyEntry, dataToSign);
		DSSDocument resignedDocument = service.signDocument(signedDocument, signatureParameters, signatureValue);
		
		privateKeyEntry = certificateService.generateCertificateChain(SignatureAlgorithm.RSA_SHA256);
		signatureParameters.bLevel().setSigningDate(new Date());
		signatureParameters.setSigningCertificate(privateKeyEntry.getCertificate());
		signatureParameters.setCertificateChain(privateKeyEntry.getCertificateChain());
		signatureParameters.setSignaturePackaging(SignaturePackaging.ENVELOPING);
		signatureParameters.setSignatureLevel(SignatureLevel.ASiC_E_BASELINE_B);
		signatureParameters.aSiC().setUnderlyingForm(SignatureForm.XAdES);
		signatureParameters.setDetachedContent(documentToSign);

		certificateVerifier = new CommonCertificateVerifier();
		service = new ASiCService(certificateVerifier);

		dataToSign = service.getDataToSign(resignedDocument, signatureParameters);
		signatureValue = TestUtils.sign(SignatureAlgorithm.RSA_SHA256, privateKeyEntry, dataToSign);
		resignedDocument = service.signDocument(resignedDocument, signatureParameters, signatureValue);
		
		privateKeyEntry = certificateService.generateCertificateChain(SignatureAlgorithm.RSA_SHA256);
		signatureParameters.bLevel().setSigningDate(new Date());
		signatureParameters.setSigningCertificate(privateKeyEntry.getCertificate());
		signatureParameters.setCertificateChain(privateKeyEntry.getCertificateChain());
		signatureParameters.setSignaturePackaging(SignaturePackaging.ENVELOPING);
		signatureParameters.setSignatureLevel(SignatureLevel.ASiC_E_BASELINE_B);
		signatureParameters.aSiC().setUnderlyingForm(SignatureForm.XAdES);
		signatureParameters.setDetachedContent(documentToSign);

		certificateVerifier = new CommonCertificateVerifier();
		service = new ASiCService(certificateVerifier);

		dataToSign = service.getDataToSign(resignedDocument, signatureParameters);
		signatureValue = TestUtils.sign(SignatureAlgorithm.RSA_SHA256, privateKeyEntry, dataToSign);
		resignedDocument = service.signDocument(resignedDocument, signatureParameters, signatureValue);

		SignedDocumentValidator validator = SignedDocumentValidator.fromDocument(resignedDocument);
		validator.setCertificateVerifier(new CommonCertificateVerifier());
		resignedDocument.save("C://Users/axel.abinet/Desktop/test.zip");

		Reports reports = validator.validateDocument();

		// reports.print();

		while (reports != null) {
			DiagnosticData diagnosticData = reports.getDiagnosticData();
			assertTrue(diagnosticData.isBLevelTechnicallyValid(diagnosticData.getFirstSignatureId()));
			Assert.assertNotEquals(Indication.INVALID, reports.getSimpleReport().getIndication(diagnosticData.getFirstSignatureId()));
			reports = reports.getNextReports();
		}
	}
}

@@ -203,51 +203,8 @@
 nt;%0A
-import eu.europa.esig.dss.DigestAlgorithm;%0A
 impo
@@ -1326,10 +1326,10 @@
 o Wo
-l
 r
+l
 d !%22
@@ -3477,34 +3477,32 @@
 ignatureValue);%0A
-%09%09
 %0A%09%09privateKeyEnt
@@ -4459,18 +4459,16 @@
 Value);%0A
-%09%09
 %0A%09%09priva
@@ -5600,75 +5600,8 @@
 ());
-%0A%09%09resignedDocument.save(%22C://Users/axel.abinet/Desktop/test.zip%22);
 %0A%0A%09%09
@@ -5653,31 +5653,8 @@
 );%0A%0A
-%09%09// reports.print();%0A%0A
 %09%09wh

Fix for jenkins