@isTest
public class AgencyAccountCmpTest {

	public static Account account1 = new Account();
	public static Account account2 = new Account();
    public static Contact contact1 = new Contact();
    public static Contact contact2 = new Contact();
    public static User user = new User();
    public static Agency_Hospital_Link__c agency_hospital_link = new Agency_Hospital_Link__c();

    private static void testInit() {
        // 取引先
        account1.Name = 'test1医院';
        account1.RecordTypeId = '01210000000QemG';
        insert account1;

        account2.Name = 'test1经销商';
        account2.RecordTypeId = '01210000000Qem1';
        insert account2;
        
        // 取引先責任者
        contact1.AccountId = account1.Id;
        contact1.FirstName = '責任者';
        contact1.LastName = 'test1医院';
        insert contact1;

        contact2.AccountId = account1.Id;
        contact2.FirstName = '責任者';
        contact2.LastName = 'test2医院';
        insert contact2;

        // ユーザー
        Profile p = [select Id from Profile where Name = '901_经销商周报'];
        user.ProfileId = p.Id;
        user.ContactId = contact2.Id;
        user.FirstName = 'ユーザー';
        user.LastName = 'テスト';
        user.Email = 'test_user@example.com';
        user.emailencodingkey='UTF-8';
        user.languagelocalekey='zh_CN';
        user.localesidkey='ja_JP';
        user.timezonesidkey='Asia/Shanghai';
        user.Username = 'test_user@example.com';
        user.Alias = 'テユ';
        user.CommunityNickname = 'テストユーザー';
        insert user;

        // 代理店医院
        agency_hospital_link.Name = 'test1代理店医院';
        agency_hospital_link.Hospital__c = account1.Id;
        agency_hospital_link.Agency__c = account2.Id;
        agency_hospital_link.OwnerId = user.Id;
        insert agency_hospital_link;

        Agency_Contact__c ac1 = new Agency_Contact__c();
        ac1.Name = 'test1のび太';
        ac1.Agency_Hospital__c = agency_hospital_link.Id;
        insert ac1;

        List<Account> depts = [select Id from Account where Hospital_Department_Class__c = :account1.Id];
        Account dept = depts[0];

        Agency_Contact__c ac2 = new Agency_Contact__c();
        ac2.Name = 'test2のび太';
        ac2.Department_Class__c = dept.Id;
        insert ac2;
    }
	
	@isTest public static void getAccountsTest() {
		testInit();
		// Implement test code
		System.runAs(user) {
			List<Agency_Contact__c> res = AgencyAccountCmp.getAccounts();
			System.assertEquals(2, res.size());
		}
	}
	
	@isTest public static void getfiledsmapTest() {
		// Implement test cod
		testInit();
		System.runAs(user) {
			Map<String,String> res = AgencyAccountCmp.getfiledsmap();
            System.assertEquals('客户人员名', res.get('Name'));
			//System.assertEquals('.客户人员名', res.get('Name'));
			System.assertEquals('医院名', res.get('Hospital_Name__c'));
		}
	}
	
	@isTest public static void searchAccountsTest() {
		// Implement test code
		testInit();
		// Implement test code
		System.runAs(user) {
			List<Agency_Contact__c> res = AgencyAccountCmp.searchAccounts('', 'test1');
			System.assertEquals(1, res.size());
		}
	}
}