<commit_before>package eu.europa.esig.dss.tsl.job;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;

import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import eu.europa.esig.dss.model.DSSDocument;
import eu.europa.esig.dss.model.FileDocument;
import eu.europa.esig.dss.service.http.commons.FileCacheDataLoader;
import eu.europa.esig.dss.spi.DSSUtils;
import eu.europa.esig.dss.spi.tsl.LOTLInfo;
import eu.europa.esig.dss.spi.tsl.TLInfo;
import eu.europa.esig.dss.spi.tsl.TLValidationJobSummary;
import eu.europa.esig.dss.spi.tsl.TrustedListsCertificateSource;
import eu.europa.esig.dss.spi.x509.CertificateSource;
import eu.europa.esig.dss.spi.x509.CommonCertificateSource;
import eu.europa.esig.dss.tsl.cache.CacheCleaner;
import eu.europa.esig.dss.tsl.source.LOTLSource;

public class LOTLChangesTest {

	// Diff LOTL 248 / 250
	// URL change for SI
	// Certs change for FR

	@TempDir
	File cacheDirectory;

	@Test
	public void test() {
		FileCacheDataLoader offlineFileLoader = getOfflineFileLoader(originalFiles());

		TLValidationJob job = new TLValidationJob();
		job.setListOfTrustedListSources(getLOTLSource());
		job.setOfflineDataLoader(offlineFileLoader);
		job.setTrustedListCertificateSource(new TrustedListsCertificateSource());

		CacheCleaner cacheCleaner = new CacheCleaner();
		cacheCleaner.setCleanFileSystem(true);
		cacheCleaner.setCleanMemory(true);
		cacheCleaner.setDSSFileLoader(offlineFileLoader);
		job.setCacheCleaner(cacheCleaner);

//		job.setDebug(true);

		job.offlineRefresh();

		TLValidationJobSummary summary = job.getSummary();
		List<LOTLInfo> lotlInfos = summary.getLOTLInfos();
		assertEquals(1, lotlInfos.size());
		LOTLInfo lotlInfo = lotlInfos.get(0);
		List<TLInfo> tlInfos = lotlInfo.getTLInfos();
		assertEquals(31, tlInfos.size());

		TLInfo france = getFrance(tlInfos);
		assertNotNull(france);
		assertTrue(france.getValidationCacheInfo().isValid());
		Date firstSigningTime = france.getValidationCacheInfo().getSigningTime();
		Date firstIssueDate = france.getParsingCacheInfo().getIssueDate();

		job.setOnlineDataLoader(getOnlineFileLoader(refreshFiles()));

		job.onlineRefresh();

		summary = job.getSummary();
		lotlInfos = summary.getLOTLInfos();
		assertEquals(1, lotlInfos.size());
		lotlInfo = lotlInfos.get(0);

		tlInfos = lotlInfo.getTLInfos();
		assertEquals(31, tlInfos.size()); // same TLs number after cleaner

		france = getFrance(tlInfos);
		assertNotNull(france);

		assertTrue(france.getValidationCacheInfo().isValid());
		assertNotEquals(firstSigningTime, france.getValidationCacheInfo().getSigningTime());
		assertNotEquals(firstIssueDate, france.getParsingCacheInfo().getIssueDate());

	}

	private TLInfo getFrance(List<TLInfo> tlInfos) {
		for (TLInfo tlInfo : tlInfos) {
			if ("FR".equals(tlInfo.getParsingCacheInfo().getTerritory())) {
				return tlInfo;
			}
		}
		return null;
	}

	private FileCacheDataLoader getOfflineFileLoader(Map<String, DSSDocument> urlMap) {
		FileCacheDataLoader offlineFileLoader = new FileCacheDataLoader();
		offlineFileLoader.setCacheExpirationTime(Long.MAX_VALUE);
		offlineFileLoader.setDataLoader(new MockDataLoader(urlMap));
		offlineFileLoader.setFileCacheDirectory(cacheDirectory);
		return offlineFileLoader;<commit_msg>DSS-1836 : Introduce a synchronization strategy<commit_after>package eu.europa.esig.dss.tsl.job;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;

import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import eu.europa.esig.dss.model.DSSDocument;
import eu.europa.esig.dss.model.FileDocument;
import eu.europa.esig.dss.model.x509.CertificateToken;
import eu.europa.esig.dss.service.http.commons.FileCacheDataLoader;
import eu.europa.esig.dss.spi.DSSUtils;
import eu.europa.esig.dss.spi.tsl.LOTLInfo;
import eu.europa.esig.dss.spi.tsl.TLInfo;
import eu.europa.esig.dss.spi.tsl.TLValidationJobSummary;
import eu.europa.esig.dss.spi.tsl.TrustedListsCertificateSource;
import eu.europa.esig.dss.spi.x509.CertificateSource;
import eu.europa.esig.dss.spi.x509.CommonCertificateSource;
import eu.europa.esig.dss.tsl.cache.CacheCleaner;
import eu.europa.esig.dss.tsl.source.LOTLSource;
import eu.europa.esig.dss.tsl.sync.SynchronizationStrategy;

public class LOTLChangesTest {

	// Diff LOTL 248 / 250
	// URL change for SI
	// Certs change for FR

	@TempDir
	File cacheDirectory;

	@Test
	public void test() {
		FileCacheDataLoader offlineFileLoader = getOfflineFileLoader(originalFiles());

		TLValidationJob job = new TLValidationJob();
		job.setListOfTrustedListSources(getLOTLSource());
		job.setOfflineDataLoader(offlineFileLoader);
		TrustedListsCertificateSource trustedListCertificateSource = new TrustedListsCertificateSource();
		job.setTrustedListCertificateSource(trustedListCertificateSource);

		CacheCleaner cacheCleaner = new CacheCleaner();
		cacheCleaner.setCleanFileSystem(true);
		cacheCleaner.setCleanMemory(true);
		cacheCleaner.setDSSFileLoader(offlineFileLoader);
		job.setCacheCleaner(cacheCleaner);

		SynchronizationStrategy rejectAll = new SynchronizationStrategy() {

			@Override
			public boolean canBeSynchronized(LOTLInfo listOfTrustedList) {
				return false;
			}

			@Override
			public boolean canBeSynchronized(TLInfo trustedList) {
				return false;
			}
		};
		job.setSynchronizationStrategy(rejectAll);

//		job.setDebug(true);

		job.offlineRefresh();

		TLValidationJobSummary summary = job.getSummary();
		List<LOTLInfo> lotlInfos = summary.getLOTLInfos();
		assertEquals(1, lotlInfos.size());
		LOTLInfo lotlInfo = lotlInfos.get(0);
		List<TLInfo> tlInfos = lotlInfo.getTLInfos();
		assertEquals(31, tlInfos.size());

		TLInfo france = getFrance(tlInfos);
		assertNotNull(france);
		assertTrue(france.getValidationCacheInfo().isValid());
		Date firstSigningTime = france.getValidationCacheInfo().getSigningTime();
		Date firstIssueDate = france.getParsingCacheInfo().getIssueDate();

		job.setOnlineDataLoader(getOnlineFileLoader(refreshFiles()));

		job.onlineRefresh();

		summary = job.getSummary();
		lotlInfos = summary.getLOTLInfos();
		assertEquals(1, lotlInfos.size());
		lotlInfo = lotlInfos.get(0);

		tlInfos = lotlInfo.getTLInfos();
		assertEquals(31, tlInfos.size()); // same TLs number after cleaner

		france = getFrance(tlInfos);
		assertNotNull(france);

		assertTrue(france.getValidationCacheInfo().isValid());
		assertNotEquals(firstSigningTime, france.getValidationCacheInfo().getSigningTime());
		assertNotEquals(firstIssueDate, france.getParsingCacheInfo().getIssueDate());

		// validate rejectAll
		List<CertificateToken> certificates = trustedListCertificateSource.getCertificates();
		assertEquals(0, certificates.size());
	}

	private TLInfo getFrance(List<TLInfo> tlInfos) {
		for (TLInfo tlInfo : tlInfos) {
			if ("FR".equals(tlInfo.getParsingCacheInfo().getTerritory())) {
				return tlInfo;
			}
		}
		return null;
	}

	private FileCacheDataLoader getOfflineFileLoader(Map<String, DSSDocument> urlMap) {
		FileCacheDataLoader offlineFileLoader = new FileCacheDataLoader();
		offlineFileLoader.setCacheExpirationTime(Long.MAX_VALUE);
		offlineFileLoader.setDataLoader(new MockDataLoader(urlMap));
		offlineFileLoader.setFileCacheDirectory(cacheDirectory);
		return offlineFileLoader;
{'fraction_non_alphanumeric': 0.11383156005640302, 'fraction_numerical': 0.006153057300346109, 'mean_word_length': 5.978533094812165, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5939409', 'n_tokens_mistral': 2836, 'n_tokens_neox': 2628, 'n_words': 392}