<commit_before>package org.hive2hive.processes.test.implementations.files.add;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

import org.apache.commons.io.FileUtils;
import org.hive2hive.core.H2HConstants;
import org.hive2hive.core.H2HSession;
import org.hive2hive.core.IFileConfiguration;
import org.hive2hive.core.exceptions.GetFailedException;
import org.hive2hive.core.exceptions.IllegalFileLocation;
import org.hive2hive.core.exceptions.NoSessionException;
import org.hive2hive.core.file.FileManager;
import org.hive2hive.core.model.FileTreeNode;
import org.hive2hive.core.model.MetaFile;
import org.hive2hive.core.model.UserProfile;
import org.hive2hive.core.network.NetworkManager;
import org.hive2hive.core.network.data.UserProfileManager;
import org.hive2hive.core.process.ProcessManager;
import org.hive2hive.core.security.EncryptionUtil;
import org.hive2hive.core.security.H2HEncryptionUtil;
import org.hive2hive.core.security.UserCredentials;
import org.hive2hive.core.test.H2HJUnitTest;
import org.hive2hive.core.test.H2HWaiter;
import org.hive2hive.core.test.file.FileTestUtil;
import org.hive2hive.core.test.integration.TestFileConfiguration;
import org.hive2hive.core.test.network.NetworkTestUtil;
import org.hive2hive.core.test.process.ProcessTestUtil;
import org.hive2hive.processes.ProcessFactory;
import org.hive2hive.processes.framework.exceptions.InvalidProcessStateException;
import org.hive2hive.processes.framework.interfaces.IProcessComponent;
import org.hive2hive.processes.test.util.TestProcessComponentListener;
import org.hive2hive.processes.test.util.UseCaseTestUtil;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

/**
 * Tests uploading a new version of a file.
 * 
 * @author Nico
 * 
 */
public class UpdateFileTest extends H2HJUnitTest {

	private final int networkSize = 5;
	private final IFileConfiguration config = new TestFileConfiguration();
	private List<NetworkManager> network;
	private UserCredentials userCredentials;
	private FileManager fileManagerUploader;
	private File file;

	private NetworkManager uploader;
	private NetworkManager downloader;
	private FileManager fileManagerDownloader;

	@BeforeClass
	public static void initTest() throws Exception {
		testClass = UpdateFileTest.class;
		beforeClass();

	}

	@Before
	public void createProfileUploadBaseFile() throws IOException, IllegalFileLocation, NoSessionException {
		network = NetworkTestUtil.createNetwork(networkSize);
		NetworkManager registrar = network.get(0);
		uploader = network.get(1);
		downloader = network.get(2);

		userCredentials = NetworkTestUtil.generateRandomCredentials();

		// create the roots and the filemanagers
		File rootUploader = new File(System.getProperty("java.io.tmpdir"), NetworkTestUtil.randomString());
		fileManagerUploader = new FileManager(rootUploader.toPath());
		File rootDownloader = new File(System.getProperty("java.io.tmpdir"), NetworkTestUtil.randomString());
		fileManagerDownloader = new FileManager(rootDownloader.toPath());

		// register a user
		UseCaseTestUtil.register(userCredentials, registrar);
		UseCaseTestUtil.login(userCredentials, uploader, rootUploader);
		UseCaseTestUtil.login(userCredentials, downloader, rootDownloader);

		// create a file
		file = FileTestUtil.createFileRandomContent(3, fileManagerUploader, config);
		UseCaseTestUtil.uploadNewFile(uploader, file);
	}

	@Test
	public void testUploadNewVersion() throws IOException, GetFailedException, NoSessionException {
		// overwrite the content in the file
		String newContent = NetworkTestUtil.randomString();
		FileUtils.write(file, newContent, false);
		byte[] md5UpdatedFile = EncryptionUtil.generateMD5Hash(file);

		// upload the new version
		UseCaseTestUtil.uploadNewVersion(uploader, file);

		// download the file and check if version is newer
		UserProfileManager profileManager = new UserProfileManager(downloader, userCredentials);
		UserProfile userProfile = profileManager.getUserProfile(UUID.randomUUID().toString(), false);
		FileTreeNode fileNode = userProfile.getFileByPath(file, fileManagerUploader);
		File downloaded = ProcessTestUtil.downloadFile(downloader, fileNode, profileManager,
				fileManagerDownloader, config);

		// new content should be latest one
		Assert.assertEquals(newContent, FileUtils.readFileToString(downloaded));

		// check the md5 hash
		Assert.assertTrue(H2HEncryptionUtil.compareMD5(downloaded, md5UpdatedFile));
	}

	@Test
	public void testUploadSameVersion() throws IllegalFileLocation, GetFailedException, IOException,
			NoSessionException, InvalidProcessStateException {
		// upload the same content again
		IProcessComponent process = ProcessFactory.instance().createUpdateFileProcess(file, uploader);
		TestProcessComponentListener listener = new TestProcessComponentListener();
		process.attachListener(listener);
		process.start();

		H2HWaiter waiter = new H2HWaiter(60);
		do {
			waiter.tickASecond();
		} while (!listener.hasFailed());

		// verify if the md5 hash did not change
		UserProfile userProfile = UseCaseTestUtil.getUserProfile(downloader, userCredentials);
		FileTreeNode fileNode = userProfile.getFileByPath(file, fileManagerUploader);
		Assert.assertTrue(H2HEncryptionUtil.compareMD5(file, fileNode.getMD5()));
<commit_msg>Switch to UseCaseUtil in one test<commit_after>package org.hive2hive.processes.test.implementations.files.add;

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.hive2hive.core.H2HConstants;
import org.hive2hive.core.H2HSession;
import org.hive2hive.core.IFileConfiguration;
import org.hive2hive.core.exceptions.GetFailedException;
import org.hive2hive.core.exceptions.IllegalFileLocation;
import org.hive2hive.core.exceptions.NoSessionException;
import org.hive2hive.core.file.FileManager;
import org.hive2hive.core.model.FileTreeNode;
import org.hive2hive.core.model.MetaFile;
import org.hive2hive.core.model.UserProfile;
import org.hive2hive.core.network.NetworkManager;
import org.hive2hive.core.process.ProcessManager;
import org.hive2hive.core.security.EncryptionUtil;
import org.hive2hive.core.security.H2HEncryptionUtil;
import org.hive2hive.core.security.UserCredentials;
import org.hive2hive.core.test.H2HJUnitTest;
import org.hive2hive.core.test.H2HWaiter;
import org.hive2hive.core.test.file.FileTestUtil;
import org.hive2hive.core.test.integration.TestFileConfiguration;
import org.hive2hive.core.test.network.NetworkTestUtil;
import org.hive2hive.core.test.process.ProcessTestUtil;
import org.hive2hive.processes.ProcessFactory;
import org.hive2hive.processes.framework.exceptions.InvalidProcessStateException;
import org.hive2hive.processes.framework.interfaces.IProcessComponent;
import org.hive2hive.processes.test.util.TestProcessComponentListener;
import org.hive2hive.processes.test.util.UseCaseTestUtil;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

/**
 * Tests uploading a new version of a file.
 * 
 * @author Nico
 * 
 */
public class UpdateFileTest extends H2HJUnitTest {

	private final int networkSize = 5;
	private final IFileConfiguration config = new TestFileConfiguration();
	private List<NetworkManager> network;
	private UserCredentials userCredentials;
	private FileManager fileManagerUploader;
	private File file;

	private NetworkManager uploader;
	private NetworkManager downloader;

	@BeforeClass
	public static void initTest() throws Exception {
		testClass = UpdateFileTest.class;
		beforeClass();

	}

	@Before
	public void createProfileUploadBaseFile() throws IOException, IllegalFileLocation, NoSessionException {
		network = NetworkTestUtil.createNetwork(networkSize);
		NetworkManager registrar = network.get(0);
		uploader = network.get(1);
		downloader = network.get(2);

		userCredentials = NetworkTestUtil.generateRandomCredentials();

		// create the roots and the filemanagers
		File rootUploader = new File(System.getProperty("java.io.tmpdir"), NetworkTestUtil.randomString());
		fileManagerUploader = new FileManager(rootUploader.toPath());
		File rootDownloader = new File(System.getProperty("java.io.tmpdir"), NetworkTestUtil.randomString());

		// register a user
		UseCaseTestUtil.register(userCredentials, registrar);
		UseCaseTestUtil.login(userCredentials, uploader, rootUploader);
		UseCaseTestUtil.login(userCredentials, downloader, rootDownloader);

		// create a file
		file = FileTestUtil.createFileRandomContent(3, fileManagerUploader, config);
		UseCaseTestUtil.uploadNewFile(uploader, file);
	}

	@Test
	public void testUploadNewVersion() throws IOException, GetFailedException, NoSessionException {
		// overwrite the content in the file
		String newContent = NetworkTestUtil.randomString();
		FileUtils.write(file, newContent, false);
		byte[] md5UpdatedFile = EncryptionUtil.generateMD5Hash(file);

		// upload the new version
		UseCaseTestUtil.uploadNewVersion(uploader, file);

		// download the file and check if version is newer
		UseCaseTestUtil.login(userCredentials, downloader, FileUtils.getTempDirectory());
		FileTreeNode fileNode = UseCaseTestUtil.getUserProfile(downloader, userCredentials).getFileByPath(
				file, fileManagerUploader);
		File downloaded = UseCaseTestUtil.downloadFile(downloader, fileNode.getKeyPair().getPublic());

		// new content should be latest one
		Assert.assertEquals(newContent, FileUtils.readFileToString(downloaded));

		// check the md5 hash
		Assert.assertTrue(H2HEncryptionUtil.compareMD5(downloaded, md5UpdatedFile));
	}

	@Test
	public void testUploadSameVersion() throws IllegalFileLocation, GetFailedException, IOException,
			NoSessionException, InvalidProcessStateException {
		// upload the same content again
		IProcessComponent process = ProcessFactory.instance().createUpdateFileProcess(file, uploader);
		TestProcessComponentListener listener = new TestProcessComponentListener();
		process.attachListener(listener);
		process.start();

		H2HWaiter waiter = new H2HWaiter(60);
		do {
			waiter.tickASecond();
		} while (!listener.hasFailed());

		// verify if the md5 hash did not change
		UserProfile userProfile = UseCaseTestUtil.getUserProfile(downloader, userCredentials);
		FileTreeNode fileNode = userProfile.getFileByPath(file, fileManagerUploader);
		Assert.assertTrue(H2HEncryptionUtil.compareMD5(file, fileNode.getMD5()));

{'fraction_non_alphanumeric': 0.09218512898330804, 'fraction_numerical': 0.009958270106221547, 'mean_word_length': 6.358688066992324, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 31, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4363168', 'n_tokens_mistral': 3271, 'n_tokens_neox': 3151, 'n_words': 626}