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

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyObject;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.sagebionetworks.repo.manager.AccessRequirementManagerImpl.DEFAULT_LIMIT;
import static org.sagebionetworks.repo.manager.AccessRequirementManagerImpl.DEFAULT_OFFSET;

import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.sagebionetworks.repo.model.ACCESS_TYPE;
import org.sagebionetworks.repo.model.ACTAccessRequirement;
import org.sagebionetworks.repo.model.AccessApprovalDAO;
import org.sagebionetworks.repo.model.AccessRequirement;
import org.sagebionetworks.repo.model.AccessRequirementDAO;
import org.sagebionetworks.repo.model.AccessRequirementInfoForUpdate;
import org.sagebionetworks.repo.model.AccessRequirementStats;
import org.sagebionetworks.repo.model.ConflictingUpdateException;
import org.sagebionetworks.repo.model.LockAccessRequirement;
import org.sagebionetworks.repo.model.ManagedACTAccessRequirement;
import org.sagebionetworks.repo.model.NextPageToken;
import org.sagebionetworks.repo.model.NodeDAO;
import org.sagebionetworks.repo.model.ObjectType;
import org.sagebionetworks.repo.model.PostMessageContentAccessRequirement;
import org.sagebionetworks.repo.model.RestrictableObjectDescriptor;
import org.sagebionetworks.repo.model.RestrictableObjectDescriptorResponse;
import org.sagebionetworks.repo.model.RestrictableObjectType;
import org.sagebionetworks.repo.model.SelfSignAccessRequirement;
import org.sagebionetworks.repo.model.TermsOfUseAccessRequirement;
import org.sagebionetworks.repo.model.UnauthorizedException;
import org.sagebionetworks.repo.model.UserInfo;
import org.sagebionetworks.repo.model.auth.AuthorizationStatus;
import org.sagebionetworks.repo.model.dao.NotificationEmailDAO;
import org.sagebionetworks.repo.model.dataaccess.AccessRequirementConversionRequest;
import org.sagebionetworks.repo.util.jrjc.CreatedIssue;
import org.sagebionetworks.repo.util.jrjc.JiraClient;
import org.sagebionetworks.repo.util.jrjc.ProjectInfo;
import org.sagebionetworks.repo.web.NotFoundException;

@ExtendWith(MockitoExtension.class)
public class AccessRequirementManagerImplUnitTest {

	@Mock
	private JiraClient jiraClient;

	private static final String TEST_PRINCIPAL_ID = "1010101";
	private static final String TEST_ENTITY_ID = "syn98786543";

	@Mock
	private AccessRequirementDAO accessRequirementDAO;
	@Mock
	private AccessApprovalDAO accessApprovalDAO;
	@Mock
	private NodeDAO nodeDao;
	@Mock
	private AuthorizationManager authorizationManager;

	@InjectMocks
	private AccessRequirementManagerImpl arm;

	private UserInfo userInfo;
	@Mock
	private NotificationEmailDAO notificationEmailDao;
	@Mock
	CreatedIssue mockProject;
	@Mock
	ProjectInfo mockProjectInfo;

	private Map<String, String> fields;


	@BeforeEach
	public void setUp() throws Exception {
		userInfo = new UserInfo(false, TEST_PRINCIPAL_ID);
	}

	@Test
	public void testCreatePostMessageContentAccessRequirement() {
		Assertions.assertThrows(IllegalArgumentException.class, () -> {
			arm.createAccessRequirement(userInfo, new PostMessageContentAccessRequirement());
		}) ;
	}

	@Test
	public void testCreateNullAccessType() {
		AccessRequirement toCreate = createExpectedAR();
		toCreate.setAccessType(null);
		Assertions.assertThrows(IllegalArgumentException.class, () -> {
			arm.createAccessRequirement(userInfo, toCreate);
		});
	}

	@Test
	public void testCreateNullSubjectIds() {
		AccessRequirement toCreate = createExpectedAR();
		toCreate.setSubjectIds(null);
		Assertions.assertThrows(IllegalArgumentException.class, () -> {
			arm.createAccessRequirement(userInfo, toCreate);
		});
	}

	private AccessRequirement createExpectedAR() {
		ManagedACTAccessRequirement expectedAR = new ManagedACTAccessRequirement();
		expectedAR.setAccessType(ACCESS_TYPE.DOWNLOAD);
		RestrictableObjectDescriptor subjectId = new RestrictableObjectDescriptor();
		subjectId.setId(TEST_ENTITY_ID);
		subjectId.setType(RestrictableObjectType.ENTITY);
		expectedAR.setSubjectIds(Arrays.asList(new RestrictableObjectDescriptor[]{subjectId}));
		AccessRequirementManagerImpl.populateCreationFields(userInfo, expectedAR);
		return expectedAR;
	}

	@Test
	public void testCreateLockAccessRequirementWithNullUserInfo() {
		Assertions.assertThrows(IllegalArgumentException.class, () -> {
			arm.createLockAccessRequirement(null, TEST_ENTITY_ID);
		});
	}

	@Test
	public void testCreateLockAccessRequirementWithNullEntityId() {
		Assertions.assertThrows(IllegalArgumentException.class, () -> {
			arm.createLockAccessRequirement(userInfo, null);
		});
	}

	@Test
	public void testCreateLockAccessRequirementHappyPath() throws Exception {
		when(authorizationManager.canAccess(userInfo, TEST_ENTITY_ID, ObjectType.ENTITY, ACCESS_TYPE.CREATE)).thenReturn(AuthorizationStatus.authorized());
		when(authorizationManager.canAccess(userInfo, TEST_ENTITY_ID, ObjectType.ENTITY, ACCESS_TYPE.UPDATE)).thenReturn(AuthorizationStatus.authorized());
		when(mockProjectInfo.getProjectId()).thenReturn("projectId");
		when(mockProjectInfo.getIssueTypeId()).thenReturn(10000L);

		fields = new HashMap<String, String>();
		fields.put("Synapse Principal ID", "id1");
		fields.put("Synapse User Display Name", "id2");
		fields.put("Synapse Data Object", "id3");
		when(jiraClient.getFields()).thenReturn(fields);

		when(jiraClient.getProjectInfo(anyString(), anyString())).thenReturn(mockProjectInfo);

		when(mockProject.getKey()).thenReturn("SG-101");
		when(jiraClient.createIssue(anyObject())).thenReturn(mockProject);

		Set<String> ars = new HashSet<String>();<commit_msg>PLFM-5517<commit_after>package org.sagebionetworks.repo.manager;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyObject;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.sagebionetworks.repo.manager.AccessRequirementManagerImpl.DEFAULT_LIMIT;
import static org.sagebionetworks.repo.manager.AccessRequirementManagerImpl.DEFAULT_OFFSET;

import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.sagebionetworks.repo.model.ACCESS_TYPE;
import org.sagebionetworks.repo.model.ACTAccessRequirement;
import org.sagebionetworks.repo.model.AccessApprovalDAO;
import org.sagebionetworks.repo.model.AccessRequirement;
import org.sagebionetworks.repo.model.AccessRequirementDAO;
import org.sagebionetworks.repo.model.AccessRequirementInfoForUpdate;
import org.sagebionetworks.repo.model.AccessRequirementStats;
import org.sagebionetworks.repo.model.ConflictingUpdateException;
import org.sagebionetworks.repo.model.LockAccessRequirement;
import org.sagebionetworks.repo.model.ManagedACTAccessRequirement;
import org.sagebionetworks.repo.model.NextPageToken;
import org.sagebionetworks.repo.model.NodeDAO;
import org.sagebionetworks.repo.model.ObjectType;
import org.sagebionetworks.repo.model.PostMessageContentAccessRequirement;
import org.sagebionetworks.repo.model.RestrictableObjectDescriptor;
import org.sagebionetworks.repo.model.RestrictableObjectDescriptorResponse;
import org.sagebionetworks.repo.model.RestrictableObjectType;
import org.sagebionetworks.repo.model.SelfSignAccessRequirement;
import org.sagebionetworks.repo.model.TermsOfUseAccessRequirement;
import org.sagebionetworks.repo.model.UnauthorizedException;
import org.sagebionetworks.repo.model.UserInfo;
import org.sagebionetworks.repo.model.auth.AuthorizationStatus;
import org.sagebionetworks.repo.model.dao.NotificationEmailDAO;
import org.sagebionetworks.repo.model.dataaccess.AccessRequirementConversionRequest;
import org.sagebionetworks.repo.util.jrjc.CreatedIssue;
import org.sagebionetworks.repo.util.jrjc.JiraClient;
import org.sagebionetworks.repo.util.jrjc.ProjectInfo;
import org.sagebionetworks.repo.web.NotFoundException;

@ExtendWith(MockitoExtension.class)
public class AccessRequirementManagerImplUnitTest {

	@Mock
	private JiraClient jiraClient;

	private static final String TEST_PRINCIPAL_ID = "1010101";
	private static final String TEST_ENTITY_ID = "syn98786543";

	@Mock
	private AccessRequirementDAO accessRequirementDAO;
	@Mock
	private AccessApprovalDAO accessApprovalDAO;
	@Mock
	private NodeDAO nodeDao;
	@Mock
	private AuthorizationManager authorizationManager;

	@InjectMocks
	private AccessRequirementManagerImpl arm;

	private UserInfo userInfo;
	@Mock
	private NotificationEmailDAO notificationEmailDao;
	@Mock
	CreatedIssue mockProject;
	@Mock
	ProjectInfo mockProjectInfo;

	private Map<String, String> fields;


	@BeforeEach
	public void setUp() throws Exception {
		userInfo = new UserInfo(false, TEST_PRINCIPAL_ID);
	}

	@Test
	public void testCreatePostMessageContentAccessRequirement() {
		Assertions.assertThrows(IllegalArgumentException.class, () -> {
			arm.createAccessRequirement(userInfo, new PostMessageContentAccessRequirement());
		}) ;
	}

	@Test
	public void testCreateNullAccessType() {
		AccessRequirement toCreate = createExpectedAR();
		toCreate.setAccessType(null);
		Assertions.assertThrows(IllegalArgumentException.class, () -> {
			arm.createAccessRequirement(userInfo, toCreate);
		});
	}

	@Test
	public void testCreateNullSubjectIds() {
		AccessRequirement toCreate = createExpectedAR();
		toCreate.setSubjectIds(null);
		Assertions.assertThrows(IllegalArgumentException.class, () -> {
			arm.createAccessRequirement(userInfo, toCreate);
		});
	}

	private AccessRequirement createExpectedAR() {
		ManagedACTAccessRequirement expectedAR = new ManagedACTAccessRequirement();
		expectedAR.setAccessType(ACCESS_TYPE.DOWNLOAD);
		RestrictableObjectDescriptor subjectId = new RestrictableObjectDescriptor();
		subjectId.setId(TEST_ENTITY_ID);
		subjectId.setType(RestrictableObjectType.ENTITY);
		expectedAR.setSubjectIds(Arrays.asList(new RestrictableObjectDescriptor[]{subjectId}));
		AccessRequirementManagerImpl.populateCreationFields(userInfo, expectedAR);
		return expectedAR;
	}

	@Test
	public void testCreateLockAccessRequirementWithNullUserInfo() {
		Assertions.assertThrows(IllegalArgumentException.class, () -> {
			arm.createLockAccessRequirement(null, TEST_ENTITY_ID);
		});
	}

	@Test
	public void testCreateLockAccessRequirementWithNullEntityId() {
		Assertions.assertThrows(IllegalArgumentException.class, () -> {
			arm.createLockAccessRequirement(userInfo, null);
		});
	}

	@Test
	public void testCreateLockAccessRequirementHappyPath() throws Exception {
		when(authorizationManager.canAccess(userInfo, TEST_ENTITY_ID, ObjectType.ENTITY, ACCESS_TYPE.CREATE)).thenReturn(AuthorizationStatus.authorized());
		when(authorizationManager.canAccess(userInfo, TEST_ENTITY_ID, ObjectType.ENTITY, ACCESS_TYPE.UPDATE)).thenReturn(AuthorizationStatus.authorized());
		when(mockProjectInfo.getProjectId()).thenReturn("projectId");
		when(mockProjectInfo.getIssueTypeId()).thenReturn(10000L);

		fields = new HashMap<String, String>();
		fields.put("Synapse Principal ID", "id1");
		fields.put("Synapse User Display Name", "id2");
		fields.put("Synapse Data Object", "id3");
		fields.put("Components", "components");
		when(jiraClient.getFields()).thenReturn(fields);

		when(jiraClient.getProjectInfo(anyString(), anyString())).thenReturn(mockProjectInfo);

		when(mockProject.getKey()).thenReturn("SG-101");
		when(jiraClient.createIssue(anyObject())).thenReturn(mockProject);

		Set<String> ars = new HashSet<String>();
{'fraction_non_alphanumeric': 0.09799757281553398, 'fraction_numerical': 0.00424757281553398, 'mean_word_length': 7.468208092485549, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 21, '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': '5402068', 'n_tokens_mistral': 4275, 'n_tokens_neox': 4097, 'n_words': 605}