<commit_before>package gov.nih.nci.cabig.caaers.domain.repository;

import static org.easymock.EasyMock.expect;
import gov.nih.nci.cabig.caaers.AbstractTestCase;
import gov.nih.nci.cabig.caaers.dao.ResearchStaffDao;
import gov.nih.nci.cabig.caaers.domain.Fixtures;
import gov.nih.nci.cabig.caaers.domain.Organization;
import gov.nih.nci.cabig.caaers.domain.ResearchStaff;
import gov.nih.nci.cabig.caaers.domain.SiteResearchStaff;
import gov.nih.nci.cabig.caaers.domain.SiteResearchStaffRole;
import gov.nih.nci.cabig.caaers.domain.UserGroupType;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.poi.hssf.record.formula.functions.Month;

import com.ibm.icu.util.Calendar;

public class ResearchStaffRepositoryTest extends AbstractTestCase {
	
	ResearchStaffRepository repository;
	CSMUserRepository csmUserRepository;
	ResearchStaffDao researchStaffDao;
	StudyRepository studyRepository;
	
	protected void setUp() throws Exception {
		super.setUp();
		repository = new ResearchStaffRepository();
		csmUserRepository = registerMockFor(CSMUserRepository.class);
		studyRepository = registerMockFor(StudyRepository.class);
		repository.setCsmUserRepository(csmUserRepository);
		researchStaffDao = registerDaoMockFor(ResearchStaffDao.class);
		repository.setResearchStaffDao(researchStaffDao);
		repository.setAuthenticationMode("local");
		repository.setStudyRepository(studyRepository);
	}

	public void testSave() throws Exception {
		Organization org = Fixtures.createOrganization("NCI");
		List<UserGroupType> groupList = new ArrayList<UserGroupType>();
		groupList.add(UserGroupType.caaers_study_cd);
		ResearchStaff staff = Fixtures.createResearchStaff(org, groupList, "Joel");
		SiteResearchStaff siteResearchStaff = new SiteResearchStaff();
		siteResearchStaff.setEmailAddress("<EMAIL>");
		siteResearchStaff.setOrganization(org);
		SiteResearchStaffRole siteResearchStaffRole = new SiteResearchStaffRole();
		siteResearchStaffRole.setRoleCode("caaers_study_cd");
		siteResearchStaffRole.setStartDate(new Date());
		siteResearchStaff.addSiteResearchStaffRole(siteResearchStaffRole);
		siteResearchStaff.setAssociateAllStudies(Boolean.TRUE);
		staff.addSiteResearchStaff(siteResearchStaff);
		staff.setLoginId("<EMAIL>");
		String changeUrl = "/pages/url";
		expect(researchStaffDao.merge(staff)).andReturn(staff).anyTimes();
		csmUserRepository.createOrUpdateCSMUserAndGroupsForResearchStaff(staff, changeUrl);
		studyRepository.associateStudyPersonnel(staff);
		replayMocks();
		repository.save(staff, changeUrl);
		verifyMocks();
		assertEquals("<EMAIL>", staff.getLoginId());
		
	}
	
	public void testSaveWebSso() throws Exception {
		repository.setAuthenticationMode("webSSO");
		Organization org = Fixtures.createOrganization("NCI");
		List<UserGroupType> groupList = new ArrayList<UserGroupType>();
		groupList.add(UserGroupType.caaers_study_cd);
		ResearchStaff staff = Fixtures.createResearchStaff(org, groupList, "Joel");
		SiteResearchStaff siteResearchStaff = new SiteResearchStaff();
		siteResearchStaff.setEmailAddress("<EMAIL>");
		siteResearchStaff.setOrganization(org);
		SiteResearchStaffRole siteResearchStaffRole = new SiteResearchStaffRole();
		siteResearchStaffRole.setRoleCode("caaers_study_cd");
		siteResearchStaffRole.setStartDate(new Date());
		siteResearchStaff.addSiteResearchStaffRole(siteResearchStaffRole);
		staff.addSiteResearchStaff(siteResearchStaff);
		staff.setLoginId("<EMAIL>");
		String changeUrl = "/pages/url";
		expect(researchStaffDao.merge(staff)).andReturn(staff).anyTimes();
		csmUserRepository.createOrUpdateCSMUserAndGroupsForResearchStaff(staff, changeUrl);
		studyRepository.associateStudyPersonnel(staff);
		replayMocks();
		repository.save(staff, changeUrl);
		verifyMocks();
		assertEquals("<EMAIL>", staff.getLoginId());
	}
	
	public void testSaveWebSso_NoLoginId() throws Exception{
		repository.setAuthenticationMode("webSSO");
		Organization org = Fixtures.createOrganization("NCI");
		List<UserGroupType> groupList = new ArrayList<UserGroupType>();
		groupList.add(UserGroupType.caaers_study_cd);
		ResearchStaff staff = Fixtures.createResearchStaff(org, groupList, "Joel");
		SiteResearchStaff siteResearchStaff = new SiteResearchStaff();
		siteResearchStaff.setEmailAddress("<EMAIL>");
		siteResearchStaff.setOrganization(org);
		SiteResearchStaffRole siteResearchStaffRole = new SiteResearchStaffRole();
		siteResearchStaffRole.setRoleCode("caaers_study_cd");
		siteResearchStaffRole.setStartDate(new Date());
		siteResearchStaff.addSiteResearchStaffRole(siteResearchStaffRole);
		staff.addSiteResearchStaff(siteResearchStaff);
		//staff.setLoginId("<EMAIL>");
		String changeUrl = "/pages/url";
		expect(researchStaffDao.merge(staff)).andReturn(staff).anyTimes();
		csmUserRepository.createOrUpdateCSMUserAndGroupsForResearchStaff(staff, changeUrl);
		studyRepository.associateStudyPersonnel(staff);
		replayMocks();
		repository.save(staff, changeUrl);
		verifyMocks();
		assertNull(staff.getLoginId());
	}
	
	public void testUnlockUser() {
		Organization org = Fixtures.createOrganization("NCI");
		List<UserGroupType> groupList = new ArrayList<UserGroupType>();
		groupList.add(UserGroupType.caaers_study_cd);
		ResearchStaff staff = Fixtures.createResearchStaff(org, groupList, "Joel");
		SiteResearchStaff siteResearchStaff = new SiteResearchStaff();
		siteResearchStaff.setEmailAddress("<EMAIL>");
		siteResearchStaff.setOrganization(org);
		SiteResearchStaffRole siteResearchStaffRole = new SiteResearchStaffRole();
		siteResearchStaffRole.setRoleCode("caaers_study_cd");
		siteResearchStaffRole.setStartDate(new Date());
		siteResearchStaff.addSiteResearchStaffRole(siteResearchStaffRole);
		siteResearchStaff.setAssociateAllStudies(Boolean.TRUE);
		staff.addSiteResearchStaff(siteResearchStaff);
		staff.setLoginId("<EMAIL>");
		staff.setFailedLoginAttempts(-1);
		Timestamp now = new Timestamp(new Date().getTime());<commit_msg>Modified testSave_NoLoginId testcase.<commit_after>package gov.nih.nci.cabig.caaers.domain.repository;

import static org.easymock.EasyMock.expect;
import gov.nih.nci.cabig.caaers.AbstractTestCase;
import gov.nih.nci.cabig.caaers.dao.ResearchStaffDao;
import gov.nih.nci.cabig.caaers.domain.Fixtures;
import gov.nih.nci.cabig.caaers.domain.Organization;
import gov.nih.nci.cabig.caaers.domain.ResearchStaff;
import gov.nih.nci.cabig.caaers.domain.SiteResearchStaff;
import gov.nih.nci.cabig.caaers.domain.SiteResearchStaffRole;
import gov.nih.nci.cabig.caaers.domain.UserGroupType;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.poi.hssf.record.formula.functions.Month;

import com.ibm.icu.util.Calendar;

public class ResearchStaffRepositoryTest extends AbstractTestCase {
	
	ResearchStaffRepository repository;
	CSMUserRepository csmUserRepository;
	ResearchStaffDao researchStaffDao;
	StudyRepository studyRepository;
	
	protected void setUp() throws Exception {
		super.setUp();
		repository = new ResearchStaffRepository();
		csmUserRepository = registerMockFor(CSMUserRepository.class);
		studyRepository = registerMockFor(StudyRepository.class);
		repository.setCsmUserRepository(csmUserRepository);
		researchStaffDao = registerDaoMockFor(ResearchStaffDao.class);
		repository.setResearchStaffDao(researchStaffDao);
		repository.setAuthenticationMode("local");
		repository.setStudyRepository(studyRepository);
	}

	public void testSave() throws Exception {
		Organization org = Fixtures.createOrganization("NCI");
		List<UserGroupType> groupList = new ArrayList<UserGroupType>();
		groupList.add(UserGroupType.caaers_study_cd);
		ResearchStaff staff = Fixtures.createResearchStaff(org, groupList, "Joel");
		SiteResearchStaff siteResearchStaff = new SiteResearchStaff();
		siteResearchStaff.setEmailAddress("<EMAIL>");
		siteResearchStaff.setOrganization(org);
		SiteResearchStaffRole siteResearchStaffRole = new SiteResearchStaffRole();
		siteResearchStaffRole.setRoleCode("caaers_study_cd");
		siteResearchStaffRole.setStartDate(new Date());
		siteResearchStaff.addSiteResearchStaffRole(siteResearchStaffRole);
		siteResearchStaff.setAssociateAllStudies(Boolean.TRUE);
		staff.addSiteResearchStaff(siteResearchStaff);
		staff.setLoginId("<EMAIL>");
		String changeUrl = "/pages/url";
		expect(researchStaffDao.merge(staff)).andReturn(staff).anyTimes();
		csmUserRepository.createOrUpdateCSMUserAndGroupsForResearchStaff(staff, changeUrl);
		studyRepository.associateStudyPersonnel(staff);
		replayMocks();
		repository.save(staff, changeUrl);
		verifyMocks();
		assertEquals("<EMAIL>", staff.getLoginId());
		
	}
	
	public void testSaveWebSso() throws Exception {
		repository.setAuthenticationMode("webSSO");
		Organization org = Fixtures.createOrganization("NCI");
		List<UserGroupType> groupList = new ArrayList<UserGroupType>();
		groupList.add(UserGroupType.caaers_study_cd);
		ResearchStaff staff = Fixtures.createResearchStaff(org, groupList, "Joel");
		SiteResearchStaff siteResearchStaff = new SiteResearchStaff();
		siteResearchStaff.setEmailAddress("<EMAIL>");
		siteResearchStaff.setOrganization(org);
		SiteResearchStaffRole siteResearchStaffRole = new SiteResearchStaffRole();
		siteResearchStaffRole.setRoleCode("caaers_study_cd");
		siteResearchStaffRole.setStartDate(new Date());
		siteResearchStaff.addSiteResearchStaffRole(siteResearchStaffRole);
		staff.addSiteResearchStaff(siteResearchStaff);
		staff.setLoginId("<EMAIL>");
		String changeUrl = "/pages/url";
		expect(researchStaffDao.merge(staff)).andReturn(staff).anyTimes();
		csmUserRepository.createOrUpdateCSMUserAndGroupsForResearchStaff(staff, changeUrl);
		studyRepository.associateStudyPersonnel(staff);
		replayMocks();
		repository.save(staff, changeUrl);
		verifyMocks();
		assertEquals("<EMAIL>", staff.getLoginId());
	}
	
	public void testSave_NoLoginId() throws Exception{
		repository.setAuthenticationMode("webSSO");
		Organization org = Fixtures.createOrganization("NCI");
		List<UserGroupType> groupList = new ArrayList<UserGroupType>();
		groupList.add(UserGroupType.caaers_study_cd);
		ResearchStaff staff = Fixtures.createResearchStaff(org, groupList, "Joel");
		SiteResearchStaff siteResearchStaff = new SiteResearchStaff();
		siteResearchStaff.setEmailAddress("<EMAIL>");
		siteResearchStaff.setOrganization(org);
		SiteResearchStaffRole siteResearchStaffRole = new SiteResearchStaffRole();
		siteResearchStaffRole.setRoleCode("caaers_study_cd");
		siteResearchStaffRole.setStartDate(new Date());
		siteResearchStaff.addSiteResearchStaffRole(siteResearchStaffRole);
		staff.addSiteResearchStaff(siteResearchStaff);
		String changeUrl = "/pages/url";
		expect(researchStaffDao.merge(staff)).andReturn(staff).anyTimes();
		studyRepository.associateStudyPersonnel(staff);
		replayMocks();
		repository.save(staff, changeUrl);
		verifyMocks();
		assertNull(staff.getLoginId());
	}
	
	public void testUnlockUser() {
		Organization org = Fixtures.createOrganization("NCI");
		List<UserGroupType> groupList = new ArrayList<UserGroupType>();
		groupList.add(UserGroupType.caaers_study_cd);
		ResearchStaff staff = Fixtures.createResearchStaff(org, groupList, "Joel");
		SiteResearchStaff siteResearchStaff = new SiteResearchStaff();
		siteResearchStaff.setEmailAddress("<EMAIL>");
		siteResearchStaff.setOrganization(org);
		SiteResearchStaffRole siteResearchStaffRole = new SiteResearchStaffRole();
		siteResearchStaffRole.setRoleCode("caaers_study_cd");
		siteResearchStaffRole.setStartDate(new Date());
		siteResearchStaff.addSiteResearchStaffRole(siteResearchStaffRole);
		siteResearchStaff.setAssociateAllStudies(Boolean.TRUE);
		staff.addSiteResearchStaff(siteResearchStaff);
		staff.setLoginId("<EMAIL>");
		staff.setFailedLoginAttempts(-1);
		Timestamp now = new Timestamp(new Date().getTime());
{'fraction_non_alphanumeric': 0.10797132011809363, 'fraction_numerical': 0.0001687051876845213, 'mean_word_length': 8.002277904328018, 'pattern_counts': {'":': 0, '<': 38, '<?xml version=': 0, '>': 38, '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': '3146965', 'n_tokens_mistral': 4089, 'n_tokens_neox': 3508, 'n_words': 492}