<commit_before>package org.sagebionetworks.repo.manager.file;

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

import java.io.InputStream;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.Assume;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.sagebionetworks.StackConfiguration;
import org.sagebionetworks.repo.manager.ProjectSettingsManager;
import org.sagebionetworks.repo.manager.UserManager;
import org.sagebionetworks.repo.model.AuthorizationConstants.BOOTSTRAP_PRINCIPAL;
import org.sagebionetworks.repo.model.UserInfo;
import org.sagebionetworks.repo.model.dao.FileHandleDao;
import org.sagebionetworks.repo.model.file.AddPartResponse;
import org.sagebionetworks.repo.model.file.AddPartState;
import org.sagebionetworks.repo.model.file.BatchPresignedUploadUrlRequest;
import org.sagebionetworks.repo.model.file.BatchPresignedUploadUrlResponse;
import org.sagebionetworks.repo.model.file.MultipartUploadRequest;
import org.sagebionetworks.repo.model.file.MultipartUploadState;
import org.sagebionetworks.repo.model.file.MultipartUploadStatus;
import org.sagebionetworks.repo.model.file.PartPresignedUrl;
import org.sagebionetworks.repo.model.file.PartUtils;
import org.sagebionetworks.repo.model.file.UploadType;
import org.sagebionetworks.repo.model.project.ExternalGoogleCloudStorageLocationSetting;
import org.sagebionetworks.simpleHttpClient.SimpleHttpClient;
import org.sagebionetworks.simpleHttpClient.SimpleHttpClientImpl;
import org.sagebionetworks.simpleHttpClient.SimpleHttpRequest;
import org.sagebionetworks.simpleHttpClient.SimpleHttpResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.amazonaws.util.BinaryUtils;
import com.amazonaws.util.Md5Utils;
import com.amazonaws.util.StringInputStream;
import com.google.common.collect.Lists;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:test-context.xml" })
public class MultipartManagerV2ImplAutowireTest {

	@Autowired
	StackConfiguration stackConfiguration;

	@Autowired
	MultipartManagerV2 multipartManagerV2;

	@Autowired
	private FileHandleDao fileHandleDao;

	@Autowired
	public UserManager userManager;

	@Autowired
	public ProjectSettingsManager projectSettingsManager;

	@Value("${dev-googlecloud-bucket}")
	private String googleCloudBucket;

	static SimpleHttpClient simpleHttpClient;

	private UserInfo adminUserInfo;
	private List<String> fileHandlesToDelete;

	MultipartUploadRequest request;
	String fileDataString;
	byte[] fileDataBytes;
	String fileMD5Hex;

	ExternalGoogleCloudStorageLocationSetting googleCloudStorageLocationSetting;

	@BeforeClass
	public static void beforeClass() {
		simpleHttpClient = new SimpleHttpClientImpl();
	}

	@Before
	public void before() throws Exception {
		// used to put data to a pre-signed url.
		fileHandlesToDelete = new LinkedList<>();
		adminUserInfo = userManager
				.getUserInfo(BOOTSTRAP_PRINCIPAL.THE_ADMIN_USER
						.getPrincipalId());

		fileDataString = "The text of this file";
		fileDataBytes = fileDataString.getBytes("UTF-8");
		request = new MultipartUploadRequest();
		request.setContentType("plain/text");
		request.setFileName("foo.txt");
		request.setFileSizeBytes(new Long(fileDataBytes.length));
		request.setPartSizeBytes(PartUtils.MIN_PART_SIZE_BYTES);
		request.setStorageLocationId(null);
		// calculate the MD5
		byte[] md5 = Md5Utils.computeMD5Hash(fileDataBytes);
		fileMD5Hex = BinaryUtils.toHex(md5);
		request.setContentMD5Hex(fileMD5Hex);

		if (stackConfiguration.getGoogleCloudEnabled()) {
			googleCloudStorageLocationSetting = new ExternalGoogleCloudStorageLocationSetting();
			googleCloudStorageLocationSetting.setBucket(googleCloudBucket);
			googleCloudStorageLocationSetting.setUploadType(UploadType.GOOGLECLOUDSTORAGE);
			googleCloudStorageLocationSetting = projectSettingsManager.createStorageLocationSetting(adminUserInfo, googleCloudStorageLocationSetting);
		}
	}

	@After
	public void after() {
		if (fileHandlesToDelete != null) {
			for (String id : fileHandlesToDelete) {
				try {
					fileHandleDao.delete(id);<commit_msg>Write a new owner.txt in a new folder for GC MultipartUpload autowired<commit_after>package org.sagebionetworks.repo.manager.file;

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

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.junit.After;
import org.junit.Assume;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.sagebionetworks.StackConfiguration;
import org.sagebionetworks.googlecloud.SynapseGoogleCloudStorageClient;
import org.sagebionetworks.repo.manager.ProjectSettingsManager;
import org.sagebionetworks.repo.manager.UserManager;
import org.sagebionetworks.repo.model.AuthorizationConstants.BOOTSTRAP_PRINCIPAL;
import org.sagebionetworks.repo.model.UserInfo;
import org.sagebionetworks.repo.model.dao.FileHandleDao;
import org.sagebionetworks.repo.model.file.AddPartResponse;
import org.sagebionetworks.repo.model.file.AddPartState;
import org.sagebionetworks.repo.model.file.BatchPresignedUploadUrlRequest;
import org.sagebionetworks.repo.model.file.BatchPresignedUploadUrlResponse;
import org.sagebionetworks.repo.model.file.MultipartUploadRequest;
import org.sagebionetworks.repo.model.file.MultipartUploadState;
import org.sagebionetworks.repo.model.file.MultipartUploadStatus;
import org.sagebionetworks.repo.model.file.PartPresignedUrl;
import org.sagebionetworks.repo.model.file.PartUtils;
import org.sagebionetworks.repo.model.file.UploadType;
import org.sagebionetworks.repo.model.principal.PrincipalAliasDAO;
import org.sagebionetworks.repo.model.project.ExternalGoogleCloudStorageLocationSetting;
import org.sagebionetworks.simpleHttpClient.SimpleHttpClient;
import org.sagebionetworks.simpleHttpClient.SimpleHttpClientImpl;
import org.sagebionetworks.simpleHttpClient.SimpleHttpRequest;
import org.sagebionetworks.simpleHttpClient.SimpleHttpResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.amazonaws.util.BinaryUtils;
import com.amazonaws.util.Md5Utils;
import com.amazonaws.util.StringInputStream;
import com.google.common.collect.Lists;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:test-context.xml" })
public class MultipartManagerV2ImplAutowireTest {

	@Autowired
	StackConfiguration stackConfiguration;

	@Autowired
	MultipartManagerV2 multipartManagerV2;

	@Autowired
	private FileHandleDao fileHandleDao;

	@Autowired
	private UserManager userManager;

	@Autowired
	private ProjectSettingsManager projectSettingsManager;

	@Autowired
	private PrincipalAliasDAO principalAliasDao;

	@Autowired
	private SynapseGoogleCloudStorageClient googleCloudStorageClient;

	@Value("${dev-googlecloud-bucket}")
	private String googleCloudBucket;

	static SimpleHttpClient simpleHttpClient;

	private UserInfo adminUserInfo;
	private List<String> fileHandlesToDelete;

	MultipartUploadRequest request;
	String fileDataString;
	byte[] fileDataBytes;
	String fileMD5Hex;

	ExternalGoogleCloudStorageLocationSetting googleCloudStorageLocationSetting;

	@BeforeClass
	public static void beforeClass() {
		simpleHttpClient = new SimpleHttpClientImpl();
	}

	@Before
	public void before() throws Exception {
		// used to put data to a pre-signed url.
		fileHandlesToDelete = new LinkedList<>();
		adminUserInfo = userManager
				.getUserInfo(BOOTSTRAP_PRINCIPAL.THE_ADMIN_USER
						.getPrincipalId());

		fileDataString = "The text of this file";
		fileDataBytes = fileDataString.getBytes("UTF-8");
		request = new MultipartUploadRequest();
		request.setContentType("plain/text");
		request.setFileName("foo.txt");
		request.setFileSizeBytes(new Long(fileDataBytes.length));
		request.setPartSizeBytes(PartUtils.MIN_PART_SIZE_BYTES);
		request.setStorageLocationId(null);
		// calculate the MD5
		byte[] md5 = Md5Utils.computeMD5Hash(fileDataBytes);
		fileMD5Hex = BinaryUtils.toHex(md5);
		request.setContentMD5Hex(fileMD5Hex);

		if (stackConfiguration.getGoogleCloudEnabled()) {
			// Create the owner.txt on the bucket
			String baseKey = "MultipartManagerV2AutowiredTest-" + UUID.randomUUID().toString();
			googleCloudStorageClient.putObject(googleCloudBucket, baseKey + "owner.txt", new ByteArrayInputStream(principalAliasDao.getUserName(adminUserInfo.getId()).getBytes(StandardCharsets.UTF_8)));

			googleCloudStorageLocationSetting = new ExternalGoogleCloudStorageLocationSetting();
			googleCloudStorageLocationSetting.setBucket(googleCloudBucket);
			googleCloudStorageLocationSetting.setBaseKey(baseKey);
			googleCloudStorageLocationSetting.setUploadType(UploadType.GOOGLECLOUDSTORAGE);
			googleCloudStorageLocationSetting = projectSettingsManager.createStorageLocationSetting(adminUserInfo, googleCloudStorageLocationSetting);
		}
	}

	@After
	public void after() {
		if (fileHandlesToDelete != null) {
			for (String id : fileHandlesToDelete) {
				try {
					fileHandleDao.delete(id);
{'fraction_non_alphanumeric': 0.0869832173557102, 'fraction_numerical': 0.0036839950880065493, 'mean_word_length': 7.157762938230384, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '3409448', 'n_tokens_mistral': 3044, 'n_tokens_neox': 2950, 'n_words': 470}