/**
 * DSS - Digital Signature Services
 * Copyright (C) 2015 European Commission, provided under the CEF programme
 * 
 * This file is part of the "DSS - Digital Signature Services" project.
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package eu.europa.esig.dss.cades.signature;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.util.List;

import org.junit.Before;

import eu.europa.esig.dss.DSSDocument;
import eu.europa.esig.dss.InMemoryDocument;
import eu.europa.esig.dss.SignatureLevel;
import eu.europa.esig.dss.SignaturePackaging;
import eu.europa.esig.dss.cades.CAdESSignatureParameters;
import eu.europa.esig.dss.jaxb.diagnostic.XmlCertificate;
import eu.europa.esig.dss.jaxb.diagnostic.XmlRevocation;
import eu.europa.esig.dss.jaxb.diagnostic.XmlSignature;
import eu.europa.esig.dss.jaxb.diagnostic.XmlTimestamp;
import eu.europa.esig.dss.signature.DocumentSignatureService;
import eu.europa.esig.dss.utils.Utils;
import eu.europa.esig.dss.validation.CertificateVerifier;
import eu.europa.esig.dss.validation.reports.wrapper.CertificateWrapper;
import eu.europa.esig.dss.validation.reports.wrapper.DiagnosticData;
import eu.europa.esig.dss.validation.reports.wrapper.RevocationWrapper;
import eu.europa.esig.dss.validation.reports.wrapper.TimestampWrapper;

public class CAdESLevelLTTest extends AbstractCAdESTestSignature {

	private DocumentSignatureService<CAdESSignatureParameters> service;
	private CAdESSignatureParameters signatureParameters;
	private DSSDocument documentToSign;

	@Before
	public void init() throws Exception {
		documentToSign = new InMemoryDocument("Hello World".getBytes());

		signatureParameters = new CAdESSignatureParameters();
		signatureParameters.setSigningCertificate(getSigningCert());
		signatureParameters.setCertificateChain(getCertificateChain());
		signatureParameters.setSignaturePackaging(SignaturePackaging.ENVELOPING);
		signatureParameters.setSignatureLevel(SignatureLevel.CAdES_BASELINE_LT);

		service = new CAdESService(getCompleteCertificateVerifier());
		service.setTspSource(getGoodTsa());
	}

	@Override
	protected CertificateVerifier getOfflineCertificateVerifier() {
		CertificateVerifier certificateVerifier = super.getOfflineCertificateVerifier();
		certificateVerifier.setIncludeCertificateRevocationValues(true);
		certificateVerifier.setIncludeTimestampTokenValues(true);
		return certificateVerifier;
	}

	@Override
	protected void verifyDiagnosticDataJaxb(eu.europa.esig.dss.jaxb.diagnostic.DiagnosticData diagnosticDataJaxb) {
		super.verifyDiagnosticDataJaxb(diagnosticDataJaxb);

		List<XmlCertificate> usedCertificates = diagnosticDataJaxb.getUsedCertificates();
		for (XmlCertificate xmlCertificate : usedCertificates) {
			if (!xmlCertificate.isTrusted() && !xmlCertificate.isIdPkixOcspNoCheck() && !xmlCertificate.isSelfSigned()) {
				List<XmlRevocation> revocations = xmlCertificate.getRevocations();
				assertTrue(Utils.isCollectionNotEmpty(revocations));
				for (XmlRevocation xmlRevocation : revocations) {
					assertNotNull(xmlRevocation.getBase64Encoded());
				}

			}
		}

		List<XmlSignature> signatures = diagnosticDataJaxb.getSignatures();
		for (XmlSignature xmlSignature : signatures) {
			List<XmlTimestamp> timestamps = xmlSignature.getTimestamps();
			for (XmlTimestamp xmlTimestamp : timestamps) {
				assertNotNull(xmlTimestamp.getBase64Encoded());
			}
		}
		
		
		DiagnosticData dd = new DiagnosticData(diagnosticDataJaxb);
		for(CertificateWrapper cert : dd.getUsedCertificates()) {
			assertNotNull(dd.getUsedCertificateById(cert.getId()));
		}
		for(TimestampWrapper tst : dd.getAllTimestamps()) {
			assertNotNull(dd.getTimestampById(tst.getId()));
		}
		for(RevocationWrapper revocation : dd.getAllRevocationData()) {
			assertNotNull(dd.getRevocationDataById(revocation.getId()));
		}
	}

	@Override
	protected DocumentSignatureService<CAdESSignatureParameters> getService() {
		return service;
	}

	@Override
	protected CAdESSignatureParameters getSignatureParameters() {
		return signatureParameters;
	}

	@Override
	protected DSSDocument getDocumentToSign() {
		return documentToSign;
	}

	@Override
	protected String getSigningAlias() {
		return GOOD_USER;
	}

}

@@ -2970,16 +2970,78 @@
 fier();%0A
+%09%09certificateVerifier.setIncludeCertificateTokenValues(true);%0A
 %09%09certif
@@ -4176,21 +4176,16 @@
 %09%09%7D%0A%09%09%7D%0A
-%09%09%0A%09%09
 %0A%09%09Diagn
@@ -4244,16 +4244,17 @@
 );%0A%09%09for
+ 
 (Certifi
@@ -4307,60 +4307,175 @@
 %0A%09%09%09
-assertNotNull(dd.getUsedCertificateById(cert.getId()
+CertificateWrapper certificateWrapper = dd.getUsedCertificateById(cert.getId());%0A%09%09%09assertNotNull(certificateWrapper);%0A%09%09%09assertNotNull(certificateWrapper.getBinaries(
 ));%0A
@@ -4483,16 +4483,17 @@
 %09%7D%0A%09%09for
+ 
 (Timesta
@@ -4540,53 +4540,160 @@
 %0A%09%09%09
-assertNotNull(dd.getTimestampById(tst.getId()
+TimestampWrapper timestampWrapper = dd.getTimestampById(tst.getId());%0A%09%09%09assertNotNull(timestampWrapper);%0A%09%09%09assertNotNull(timestampWrapper.getBinaries(
 ));%0A
@@ -4701,16 +4701,17 @@
 %09%7D%0A%09%09for
+ 
 (Revocat
@@ -4766,30 +4766,54 @@
 )) %7B%0A%09%09%09
-assertNotNull(
+RevocationWrapper revocationWrapper = 
 dd.getRe
@@ -4848,16 +4848,103 @@
 getId())
+;%0A%09%09%09assertNotNull(revocationWrapper);%0A%09%09%09assertNotNull(revocationWrapper.getBinaries()
 );%0A%09%09%7D%0A%09

Improve unit test