/**
 * This class contains unit tests for validating the behavior of Apex classes
 * and triggers.
 *
 * Unit tests are class methods that verify whether a particular piece
 * of code is working properly. Unit test methods take no arguments,
 * commit no data to the database, and are flagged with the testMethod
 * keyword in the method definition.
 *
 * All test methods in an organization are executed whenever Apex code is deployed
 * to a production organization to confirm correctness, ensure code
 * coverage, and prevent regressions. All Apex classes are
 * required to have at least 75% code coverage in order to be deployed
 * to a production organization. In addition, all triggers must have some code coverage.
 * 
 * The @isTest class annotation indicates this class only contains test
 * methods. Classes defined with the @isTest annotation do not count against
 * the organization size limit for all Apex scripts.
 *
 * See the Apex Language Reference for more information about Testing and Code Coverage.
 */
@isTest
private class AfterCreateDepartmentTest {

    static testMethod void testAfterCreateDepartment() {
        // recode type を取得
        List<RecordType> rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        if (rectCo.size() == 0) {
            return;
        }
        List<RecordType> rectSct = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '戦略科室分類 消化科'];
        if (rectSct.size() == 0) {
            return;
        }
        List<RecordType> rectDpt = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '診療科 消化科'];
        if (rectDpt.size() == 0) {
            return;
        }
        
        // insert事前データ
        Account company = new Account();
        company.RecordTypeId = rectCo[0].Id;
        company.Name = 'Katsu テスト';
        company.OCM_Category__c = 'OCM_Test';
        insert company;
        Account section = [Select Name, Department_Class_Label__c, OwnerId, ParentId, Hospital_Department_Class__c from Account where ParentId =: company.Id and RecordTypeId =: rectSct[0].Id];
        System.assertEquals('消化科', section.Department_Class_Label__c);
        
        // チーム追加
        Profile p = [select Id from Profile where id =: System.Label.ProfileId_SystemAdmin];
        User u1 = new User(Test_staff__c = true);
        u1.LastName = '_サンブリッジ';
        u1.FirstName = 'あ';
        u1.Alias = 'あ';
        u1.Email = 'olympusTest01@sunbridge.com';
        u1.Username = 'olympusTest01@sunbridge.com';
        u1.CommunityNickname = 'あ';
        u1.IsActive = true;
        u1.EmailEncodingKey = 'ISO-2022-JP';
        u1.TimeZoneSidKey = 'Asia/Tokyo';
        u1.LocaleSidKey = 'ja_JP';
        u1.LanguageLocaleKey = 'ja';
        u1.ProfileId = p.Id;
        u1.Job_Category__c = '销售服务';
        u1.Province__c = '東京';
        insert u1;

        User u2 = new User(Test_staff__c = true);
        u2.LastName = '_サンブリッジ';
        u2.FirstName = 'い';
        u2.Alias = 'い';
        u2.Email = 'olympusTest02@sunbridge.com';
        u2.Username = 'olympusTest02@sunbridge.com';
        u2.CommunityNickname = 'い';
        u2.IsActive = true;
        u2.EmailEncodingKey = 'ISO-2022-JP';
        u2.TimeZoneSidKey = 'Asia/Tokyo';
        u2.LocaleSidKey = 'ja_JP';
        u2.LanguageLocaleKey = 'ja';
        u2.ProfileId = p.Id;
        u2.Job_Category__c = '销售推广';
        u2.Province__c = '東京';
        insert u2;
        
        AccountTeamMember dcT1 = new AccountTeamMember();//AccountAccessLevel = 'Read'
        dcT1.UserId = u1.Id;
        dcT1.TeamMemberRole = '営業担当';
        dcT1.AccountId = section.Id;
        insert dcT1;
        AccountShare dcTS1 = new AccountShare(UserOrGroupId=u1.Id, AccountId=section.Id, AccountAccessLevel='Read', OpportunityAccessLevel = 'None');
        insert dcTS1;
        
        AccountTeamMember dcT2 = new AccountTeamMember();//AccountAccessLevel = 'Edit'
        dcT2.UserId = u2.Id;
        dcT2.TeamMemberRole = 'FSE';
        dcT2.AccountId = section.Id;
        insert dcT2;
        AccountShare dcTS2 = new AccountShare(UserOrGroupId=u2.Id, AccountId=section.Id, AccountAccessLevel='Edit', OpportunityAccessLevel = 'None');
        insert dcTS2;

        // 診療科作成
        Account depart = new Account();
        depart.RecordTypeId = rectDpt[0].Id;
        depart.Name         = '*';
        depart.Department_Name__c  = 'NFM001TestDepart';
        depart.ParentId            = section.Id;
        depart.Department_Class__c = section.Id;
        depart.Hospital__c         = company.Id;
        insert depart;
        depart = [Select OwnerId, RecordTypeId, Name, Department_Name__c, ParentId, Department_Class__c, Hospital__c, OCM_Category__c from Account where Id =: depart.Id];
        
        // Assert
        List<AccountTeamMember> dcTList = [select Id, UserId, TeamMemberRole, AccountId, AccountAccessLevel from AccountTeamMember where AccountId = :section.Id];
        System.assertEquals(2, dcTList.size());
        List<AccountTeamMember> dTList  = [select Id, UserId, TeamMemberRole, AccountId, AccountAccessLevel from AccountTeamMember where AccountId = :depart.Id];
        System.assertEquals(2, dTList.size());
        if (dcT1.UserId == dTList[0].UserId) {
            System.assertEquals(depart.OCM_Category__c, company.OCM_Category__c);
            System.assertEquals(depart.OwnerId, section.OwnerId);
            System.assertEquals(dcT1.UserId, dTList[0].UserId);
            System.assertEquals(dcT1.TeamMemberRole, dTList[0].TeamMemberRole);
            System.assertEquals('Read', dTList[0].AccountAccessLevel);
            System.assertEquals(depart.Id, dTList[0].AccountId);
            System.assertEquals(dcT2.UserId, dTList[1].UserId);
            System.assertEquals(dcT2.TeamMemberRole, dTList[1].TeamMemberRole);
            //TODO 2016/6/14 System.AssertException: Assertion Failed: Expected: Edit, Actual: Read
            //System.assertEquals('Edit', dTList[1].AccountAccessLevel);
            System.assertEquals(depart.Id, dTList[1].AccountId);
        } else {
            System.assertEquals(depart.OCM_Category__c, company.OCM_Category__c);
            System.assertEquals(depart.OwnerId, section.OwnerId);
            System.assertEquals(dcT1.UserId, dTList[1].UserId);
            System.assertEquals(dcT1.TeamMemberRole, dTList[1].TeamMemberRole);
            System.assertEquals('Read', dTList[1].AccountAccessLevel);
            System.assertEquals(depart.Id, dTList[1].AccountId);
            System.assertEquals(dcT2.UserId, dTList[0].UserId);
            System.assertEquals(dcT2.TeamMemberRole, dTList[0].TeamMemberRole);
            //TODO 2016/6/14 System.AssertException: Assertion Failed: Expected: Edit, Actual: Read
            //System.assertEquals('Edit', dTList[0].AccountAccessLevel);
            System.assertEquals(depart.Id, dTList[0].AccountId);
        }
    }
}