<commit_before>package org.sagebionetworks.repo.web.controller;

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

import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;

import org.json.JSONObject;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.sagebionetworks.reflection.model.PaginatedResults;
import org.sagebionetworks.repo.manager.UserManager;
import org.sagebionetworks.repo.manager.file.FileHandleManager;
import org.sagebionetworks.repo.model.AuthorizationConstants.BOOTSTRAP_PRINCIPAL;
import org.sagebionetworks.repo.model.UnauthorizedException;
import org.sagebionetworks.repo.model.UserInfo;
import org.sagebionetworks.repo.model.auth.NewUser;
import org.sagebionetworks.repo.model.file.ExternalFileHandle;
import org.sagebionetworks.repo.model.message.MessageBundle;
import org.sagebionetworks.repo.model.message.MessageRecipientSet;
import org.sagebionetworks.repo.model.message.MessageSortBy;
import org.sagebionetworks.repo.model.message.MessageStatus;
import org.sagebionetworks.repo.model.message.MessageStatusType;
import org.sagebionetworks.repo.model.message.MessageToUser;
import org.sagebionetworks.repo.model.message.cloudmailin.Envelope;
import org.sagebionetworks.repo.model.message.cloudmailin.Message;
import org.sagebionetworks.repo.model.principal.PrincipalAliasDAO;
import org.sagebionetworks.repo.web.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**
 * Simplistic test to see if things are wired up correctly
 * All messages are retrieved oldest first
 */
public class MessageControllerAutowiredTest extends AbstractAutowiredControllerTestBase {
	
	@Autowired
	private UserManager userManager;
	
	@Autowired
	private MessageService messageService;
	
	@Autowired
	private FileHandleManager fileHandleManager;
	
	private String fileHandleId;
	
	private static final MessageSortBy SORT_ORDER = MessageSortBy.SEND_DATE;
	private static final boolean DESCENDING = false;
	private static final long LIMIT = 100;
	private static final long OFFSET = 0;

	private UserInfo adminUserInfo;
	private Long alice;
	private Long bob;
	private Long eve;
	
	private String aliceId;
	private String bobId;
	private String eveId;
	private Set<String> toAlice;
	private Set<String> toBob;
	private Set<String> toEve;
	
	private String aliceEmail;
	private String bobUsername;
	
	private List<String> cleanup;
	
	@SuppressWarnings("serial")
	private static List<MessageStatusType> inboxFilter = new ArrayList<MessageStatusType>() {{add(MessageStatusType.UNREAD);}};

	@SuppressWarnings("serial")
	@Before
	public void before() throws Exception {
		cleanup = new ArrayList<String>();
		
		adminUserInfo = userManager.getUserInfo(BOOTSTRAP_PRINCIPAL.THE_ADMIN_USER.getPrincipalId());
		
		// Need 3 users
		NewUser user = new NewUser(); 
		aliceEmail = UUID.randomUUID().toString() + "@test.com";
		user.setEmail(aliceEmail);
		user.setUserName(UUID.randomUUID().toString());
		alice = userManager.createUser(user);
		aliceId = "" + alice;
		
		user.setEmail(UUID.randomUUID().toString() + "@test.com");
		bobUsername = UUID.randomUUID().toString();
		user.setUserName(bobUsername);
		bob = userManager.createUser(user);
		bobId = "" + bob;
		
		user.setEmail(UUID.randomUUID().toString() + "@test.com");
		user.setUserName(UUID.randomUUID().toString());
		eve = userManager.createUser(user);
		eveId = "" + eve;
		
		toAlice = new HashSet<String>() {{
			add(aliceId);
		}};
		toBob = new HashSet<String>() {{
			add(bobId);
		}};
		toEve = new HashSet<String>() {{
			add(eveId);
		}};
		
		// We need a file handle to satisfy a foreign key constraint
		// And so that sent messages can be "downloaded"
		// Alice creates the file and the other users get access when the file is sent to them via a message
		ExternalFileHandle handle = new ExternalFileHandle();
		handle.setContentType("text/plain");
		handle.setFileName("foobar");
		URL url = MessageControllerAutowiredTest.class.getClassLoader().getResource("images/notAnImage.txt");
		handle.setExternalURL(url.toString());
		handle = fileHandleManager.createExternalFileHandle(userManager.getUserInfo(Long.parseLong(aliceId)), handle);
		fileHandleId = handle.getId();
	}
	
	@After
	public void after() throws Exception {
		try {
			for (String id : cleanup) {
				messageService.deleteMessage(adminUserInfo.getId(), id);
			}
			
			fileHandleManager.deleteFileHandle(adminUserInfo, fileHandleId);
			
			userManager.deletePrincipal(adminUserInfo, Long.parseLong(aliceId));
			userManager.deletePrincipal(adminUserInfo, Long.parseLong(bobId));
			userManager.deletePrincipal(adminUserInfo, Long.parseLong(eveId));
		} catch (Exception e) {
			
		}
	}
	
	/**
	 * Fills in a MessageToUser object with the minimal number of fields
	 */
	private MessageToUser getMessageDTO(Set<String> recipients, String inReplyTo) {
		MessageToUser message = new MessageToUser();
		message.setFileHandleId(fileHandleId);
		message.setRecipients(recipients);
		message.setInReplyTo(inReplyTo);<commit_msg>test fix<commit_after>package org.sagebionetworks.repo.web.controller;

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

import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;

import org.json.JSONObject;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.sagebionetworks.reflection.model.PaginatedResults;
import org.sagebionetworks.repo.manager.UserManager;
import org.sagebionetworks.repo.manager.file.FileHandleManager;
import org.sagebionetworks.repo.model.AuthorizationConstants.BOOTSTRAP_PRINCIPAL;
import org.sagebionetworks.repo.model.UnauthorizedException;
import org.sagebionetworks.repo.model.UserInfo;
import org.sagebionetworks.repo.model.auth.NewUser;
import org.sagebionetworks.repo.model.file.ExternalFileHandle;
import org.sagebionetworks.repo.model.file.S3FileHandle;
import org.sagebionetworks.repo.model.message.MessageBundle;
import org.sagebionetworks.repo.model.message.MessageRecipientSet;
import org.sagebionetworks.repo.model.message.MessageSortBy;
import org.sagebionetworks.repo.model.message.MessageStatus;
import org.sagebionetworks.repo.model.message.MessageStatusType;
import org.sagebionetworks.repo.model.message.MessageToUser;
import org.sagebionetworks.repo.model.message.cloudmailin.Envelope;
import org.sagebionetworks.repo.model.message.cloudmailin.Message;
import org.sagebionetworks.repo.model.principal.PrincipalAliasDAO;
import org.sagebionetworks.repo.web.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**
 * Simplistic test to see if things are wired up correctly
 * All messages are retrieved oldest first
 */
public class MessageControllerAutowiredTest extends AbstractAutowiredControllerTestBase {
	
	@Autowired
	private UserManager userManager;
	
	@Autowired
	private MessageService messageService;
	
	@Autowired
	private FileHandleManager fileHandleManager;
	
	private String fileHandleId;
	
	private static final MessageSortBy SORT_ORDER = MessageSortBy.SEND_DATE;
	private static final boolean DESCENDING = false;
	private static final long LIMIT = 100;
	private static final long OFFSET = 0;

	private UserInfo adminUserInfo;
	private Long alice;
	private Long bob;
	private Long eve;
	
	private String aliceId;
	private String bobId;
	private String eveId;
	private Set<String> toAlice;
	private Set<String> toBob;
	private Set<String> toEve;
	
	private String aliceEmail;
	private String bobUsername;
	
	private List<String> cleanup;
	
	@SuppressWarnings("serial")
	private static List<MessageStatusType> inboxFilter = new ArrayList<MessageStatusType>() {{add(MessageStatusType.UNREAD);}};

	@SuppressWarnings("serial")
	@Before
	public void before() throws Exception {
		cleanup = new ArrayList<String>();
		
		adminUserInfo = userManager.getUserInfo(BOOTSTRAP_PRINCIPAL.THE_ADMIN_USER.getPrincipalId());
		
		// Need 3 users
		NewUser user = new NewUser(); 
		aliceEmail = UUID.randomUUID().toString() + "@test.com";
		user.setEmail(aliceEmail);
		user.setUserName(UUID.randomUUID().toString());
		alice = userManager.createUser(user);
		aliceId = "" + alice;
		
		user.setEmail(UUID.randomUUID().toString() + "@test.com");
		bobUsername = UUID.randomUUID().toString();
		user.setUserName(bobUsername);
		bob = userManager.createUser(user);
		bobId = "" + bob;
		
		user.setEmail(UUID.randomUUID().toString() + "@test.com");
		user.setUserName(UUID.randomUUID().toString());
		eve = userManager.createUser(user);
		eveId = "" + eve;
		
		toAlice = new HashSet<String>() {{
			add(aliceId);
		}};
		toBob = new HashSet<String>() {{
			add(bobId);
		}};
		toEve = new HashSet<String>() {{
			add(eveId);
		}};
		
		// We need a file handle to satisfy a foreign key constraint
		String fileContents = "This is text and not an image.";
		S3FileHandle handle = fileHandleManager.createCompressedFileFromString(aliceId, new Date(),
				fileContents);
		fileHandleId = handle.getId();
	}
	
	@After
	public void after() throws Exception {
		try {
			for (String id : cleanup) {
				messageService.deleteMessage(adminUserInfo.getId(), id);
			}
			
			fileHandleManager.deleteFileHandle(adminUserInfo, fileHandleId);
			
			userManager.deletePrincipal(adminUserInfo, Long.parseLong(aliceId));
			userManager.deletePrincipal(adminUserInfo, Long.parseLong(bobId));
			userManager.deletePrincipal(adminUserInfo, Long.parseLong(eveId));
		} catch (Exception e) {
			
		}
	}
	
	/**
	 * Fills in a MessageToUser object with the minimal number of fields
	 */
	private MessageToUser getMessageDTO(Set<String> recipients, String inReplyTo) {
		MessageToUser message = new MessageToUser();
		message.setFileHandleId(fileHandleId);
		message.setRecipients(recipients);
		message.setInReplyTo(inReplyTo);
{'fraction_non_alphanumeric': 0.10491304760999715, 'fraction_numerical': 0.001520478950869524, 'mean_word_length': 5.532588454376164, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2776070', 'n_tokens_mistral': 3477, 'n_tokens_neox': 3255, 'n_words': 662}