@isTest
private class NFM608RestTest {
    static testMethod void testMethod1() {
        
    }
    @testSetup
    static void makeTestRepair() {
        List<RecordType> rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        if (rectCo.size() == 0) {
            throw new ControllerUtil.myException('not found 病院 recodetype');
        }
        List<RecordType> rectSct = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '戦略科室分類 消化科'];
        if (rectSct.size() == 0) {
            throw new ControllerUtil.myException('not found 戦略科室分類 呼吸科 recodetype');
        }
        List<RecordType> rectDpt = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '診療科 消化科'];
        if (rectDpt.size() == 0) {
            throw new ControllerUtil.myException('not found 診療科 消化科 recodetype');
        }
        // テストデータ
        Account company = new Account();

        company.RecordTypeId = rectCo[0].Id;
        company.Name         = 'NFM105TestCompany';
        upsert company;
        // Account section = [Select Management_Code__c, Management_Code_Auto__c, Name, Id from Account where ParentId = :company.Id and RecordTypeId = :rectSct[0].Id];
        Account section = [Select Management_Code__c,  Name, Id from Account where ParentId = :company.Id and RecordTypeId = :rectSct[0].Id];

        Account depart = new Account();
        depart.RecordTypeId        = rectDpt[0].Id;
        depart.Name                = '*';
        depart.Department_Name__c  = 'NFM105TestDepart';
        depart.ParentId            = section.Id;
        depart.Department_Class__c = section.Id;
        depart.Hospital__c         = company.Id;
        upsert depart;


        List<Product2> prdList = new List<Product2>();
        Product2 prd1 = new Product2();
        prd1.ProductCode_Ext__c     = 'NFM105Prd1';
        prd1.ProductCode            = 'NFM105Prd1';
        prd1.Repair_Product_Code__c = 'NFM105Prd1_RP';
        prd1.Name                   = 'NFM105Prd1';
        prd1.Manual_Entry__c        = false;
        prdList.add(prd1);
        Product2 prd2 = new Product2();
        prd2.ProductCode_Ext__c     = 'NFM105Prd2';
        prd2.ProductCode            = 'NFM105Prd2';
        prd2.Repair_Product_Code__c = 'NFM105Prd2_RP';
        prd2.Name                   = 'NFM105Prd2';
        prd2.Manual_Entry__c        = false;
        prdList.add(prd2);
        insert prdList;



		Account keshi = new Account();

        Profile p = [select Id from Profile where id =:System.Label.ProfileId_SystemAdmin];
		/*User hpOwner = new User(Test_staff__c = true, LastName = 'hp', FirstName = 'owner', Alias = 'hp', CommunityNickname = 'hpOwner', Email = 'olympus_hpowner@sunbridge.com', Username = 'olympus_hpowner@sunbridge.com', IsActive = true, EmailEncodingKey = 'ISO-2022-JP', TimeZoneSidKey = 'Asia/Tokyo', LocaleSidKey = 'ja_JP', LanguageLocaleKey = 'ja', ProfileId = p.id);
        insert hpOwner;*/
        
            //Profile p = [select Id from Profile where id =:System.Label.ProfileId_SystemAdmin];
            // ユーザー作成
        User local = new User(Test_staff__c = true,Employee_No__c ='ocm99999', LastName = 'hp', FirstName = 'owner', Alias = 'hp', CommunityNickname = 'hpOwner', Email = 'olympus_hpowner@sunbridge.com', Username = 'olympus_hpowner@sunbridge.com', IsActive = true, EmailEncodingKey = 'ISO-2022-JP', TimeZoneSidKey = 'Asia/Tokyo', LocaleSidKey = 'ja_JP', LanguageLocaleKey = 'ja', ProfileId = p.id);
        insert local;

        Daily_Report__c drc = new Daily_Report__c();
        drc.Reported_Date__c = NFMUtil.parseDateTimeStr2Date('20210911000000');
        drc.Reporter__c = local.Id;

        insert drc;



        User user = new User(Test_staff__c = true);
        user.LastName = '_サンブリッジ';
        user.FirstName = 'う';
        user.Alias = 'う';
        user.Email = 'olympustest03@sunbridge.com';
        user.Username = 'olympustest03@sunbridge.com';
        user.CommunityNickname = 'う';
        user.IsActive = true;
        user.EmailEncodingKey = 'ISO-2022-JP';
        user.TimeZoneSidKey = 'Asia/Tokyo';
        user.LocaleSidKey = 'ja_JP';
        user.LanguageLocaleKey = 'ja';
        user.ProfileId = System.Label.ProfileId_SystemAdmin;
        user.Job_Category__c = '销售推广';
        user.Province__c = '上海市';
        user.Use_Start_Date__c = Date.today().addMonths(-6);
        user.Employee_No__c = '0000022174';
        user.Stay_or_not__c = '在职';
        user.IsActive = true;
        insert user;

        User user1 = new User(Test_staff__c = true);
        user1.LastName = '测试';
        user1.FirstName = '测';
        user1.Alias = '测';
        user1.Email = 'olympustest031@sunbridge.com';
        user1.Username = 'olympustest031@sunbridge.com';
        user1.CommunityNickname = '测';
        user1.IsActive = true;
        user1.EmailEncodingKey = 'ISO-2022-JP';
        user1.TimeZoneSidKey = 'Asia/Tokyo';
        user1.LocaleSidKey = 'ja_JP';
        user1.LanguageLocaleKey = 'ja';
        user1.ProfileId = System.Label.ProfileId_SystemAdmin;
        user1.Job_Category__c = '销售推广';
        user1.Province__c = '上海市';
        user1.Use_Start_Date__c = Date.today().addMonths(-6);
        user1.Employee_No__c = '0000022175';
        user1.Stay_or_not__c = '已离职';
        user1.IsActive = true;
        insert user1;

        Id rtId = '01210000000Qtky';
        Contact tmp = new Contact();
        tmp.RecordTypeId = rtId;
        tmp.AccountId = depart.Id;
        //tmp.User__c = local.Id;
        tmp.FirstName = local.FirstName;
        tmp.LastName = local.LastName;
        tmp.Notes_File_Name__c = local.Notes_File_Name__c;//20170220新增
        tmp.Email = local.Email;
        tmp.MobilePhone = local.Mobile_Phone__c;
        tmp.Employee_No_manual__c = local.Employee_No__c;
        tmp.Work_Location_manual__c = local.Work_Location__c;
        tmp.Post_picklist__c = local.Post__c;
        tmp.Job_Category_picklist__c = local.Job_Category__c;
        tmp.Hire_date_text__c = local.Hire_Date__c; 
        tmp.Gender_text__c = local.Gender__c; 
        tmp.Dept__c = local.Dept__c;
        tmp.Pregnant_Rest__c = local.Pregnant_Rest__c;
        tmp.Salesdepartment_Text__c = local.Salesdepartment__c;
        tmp.Stay_or_not__c = local.Stay_or_not__c;

        insert tmp;
        
        /*Event__c report = new Event__c();
        report.Account_ID__c = keshi.Id;
        report.ActivityDate__c = NFMUtil.parseStr2Date('20210911');
        report.StartDateTime__c = NFMUtil.parseStr2DateTime('20210911000000');
        report.ActivityDate__c = NFMUtil.parseDateTimeStr2Date('20210911000000');
        report.EndDateTime__c = NFMUtil.parseStr2DateTime('20210911000000');
        report.Description__c = '无';
        report.Activity_Type2__c = '学术会议';
        report.Activity_Purpose__c = '休假';
        report.Purpose_Type__c = '会议对应';
        // report.Daily_Report__c = 'a0A9D000003IHflUAG';
        report.Daily_Report__c = drc.Id;
        insert report;*/

    }
    @isTest
    static void test_1(){


        Test.startTest();

        List<RecordType> rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        if (rectCo.size() == 0) {
            throw new ControllerUtil.myException('not found 病院 recodetype');
        }
        List<RecordType> rectSct = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '戦略科室分類 消化科'];
        if (rectSct.size() == 0) {
            throw new ControllerUtil.myException('not found 戦略科室分類 呼吸科 recodetype');
        }
        List<RecordType> rectDpt = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '診療科 消化科'];
        if (rectDpt.size() == 0) {
            throw new ControllerUtil.myException('not found 診療科 消化科 recodetype');
        }

        Account depart = [select Management_Code__c from Account where RecordTypeId = :rectDpt[0].Id];

        RestRequest req = new RestRequest();
        RestResponse res = new RestResponse();

        String JsonMsg = '{"visitStartDate":"20211221033551",'+
                        '"visitPurpose":"维修跟进",'+
                        '"visitInfo":"报价任务",'+
                        '"visitEndDate":"20211221033554",'+
                        '"visitDistinction":"用户拜访",'+
                        '"synchroDate":"20211221153601",'+
                        '"replyResult":"报价跟进",'+
                        '"replyID":"88",'+
                        '"mngCd":"0000022174",'+
                            '"departmentCd":"'+depart.Management_Code__c+'",'+
                        // '"departmentCd":"667538",'+
                        '"applicantId":"5",'+
                        '"activityDifferentiation":"报价跟进"}';
        req.requestURI = 'services/apexrest/NFM608/execute';
        req.httpMethod = 'POST';
        req.requestBody = Blob.valueof(JsonMsg);
        RestContext.request = req;
        RestContext.response= res;

        NFM608Rest.doPost();
        Test.stopTest();
    }
    // mngCd无效
    @isTest
    static void test_7(){


        Test.startTest();

        List<RecordType> rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        if (rectCo.size() == 0) {
            throw new ControllerUtil.myException('not found 病院 recodetype');
        }
        List<RecordType> rectSct = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '戦略科室分類 消化科'];
        if (rectSct.size() == 0) {
            throw new ControllerUtil.myException('not found 戦略科室分類 呼吸科 recodetype');
        }
        List<RecordType> rectDpt = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '診療科 消化科'];
        if (rectDpt.size() == 0) {
            throw new ControllerUtil.myException('not found 診療科 消化科 recodetype');
        }

        Account depart = [select Management_Code__c from Account where RecordTypeId = :rectDpt[0].Id];

        RestRequest req = new RestRequest();
        RestResponse res = new RestResponse();

        String JsonMsg = '{"visitStartDate":"20211221033551",'+
                        '"visitPurpose":"维修跟进",'+
                        '"visitInfo":"报价任务",'+
                        '"visitEndDate":"20211221033554",'+
                        '"visitDistinction":"用户拜访",'+
                        '"synchroDate":"20211221153601",'+
                        '"replyResult":"报价跟进",'+
                        '"replyID":"88",'+
                        '"mngCd":"0000022175",'+
                            '"departmentCd":"'+depart.Management_Code__c+'",'+
                        // '"departmentCd":"667538",'+
                        '"applicantId":"5",'+
                        '"activityDifferentiation":"报价跟进"}';
        req.requestURI = 'services/apexrest/NFM608/execute';
        req.httpMethod = 'POST';
        req.requestBody = Blob.valueof(JsonMsg);
        RestContext.request = req;
        RestContext.response= res;

        NFM608Rest.doPost();
        Test.stopTest();
    }
    // departmentCd无效
    @isTest
    static void test_6(){


        Test.startTest();

        List<RecordType> rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        if (rectCo.size() == 0) {
            throw new ControllerUtil.myException('not found 病院 recodetype');
        }
        List<RecordType> rectSct = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '戦略科室分類 消化科'];
        if (rectSct.size() == 0) {
            throw new ControllerUtil.myException('not found 戦略科室分類 呼吸科 recodetype');
        }
        List<RecordType> rectDpt = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '診療科 消化科'];
        if (rectDpt.size() == 0) {
            throw new ControllerUtil.myException('not found 診療科 消化科 recodetype');
        }

        Account depart = [select Management_Code__c from Account where RecordTypeId = :rectDpt[0].Id];

        RestRequest req = new RestRequest();
        RestResponse res = new RestResponse();

        String JsonMsg = '{"visitStartDate":"20211221033551",'+
                        '"visitPurpose":"维修跟进",'+
                        '"visitInfo":"报价任务",'+
                        '"visitEndDate":"20211221033554",'+
                        '"visitDistinction":"用户拜访",'+
                        '"synchroDate":"20211221153601",'+
                        '"replyResult":"报价跟进",'+
                        '"replyID":"88",'+
                        '"mngCd":"0000022174",'+
                            '"departmentCd":"11111111",'+
                        // '"departmentCd":"667538",'+
                        '"applicantId":"5",'+
                        '"activityDifferentiation":"报价跟进"}';
        req.requestURI = 'services/apexrest/NFM608/execute';
        req.httpMethod = 'POST';
        req.requestBody = Blob.valueof(JsonMsg);
        RestContext.request = req;
        RestContext.response= res;

        NFM608Rest.doPost();
        Test.stopTest();
    }
    // 进catch
    @isTest
    static void test_5(){


        Test.startTest();

        List<RecordType> rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        if (rectCo.size() == 0) {
            throw new ControllerUtil.myException('not found 病院 recodetype');
        }
        List<RecordType> rectSct = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '戦略科室分類 消化科'];
        if (rectSct.size() == 0) {
            throw new ControllerUtil.myException('not found 戦略科室分類 呼吸科 recodetype');
        }
        List<RecordType> rectDpt = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '診療科 消化科'];
        if (rectDpt.size() == 0) {
            throw new ControllerUtil.myException('not found 診療科 消化科 recodetype');
        }

        Account depart = [select Management_Code__c from Account where RecordTypeId = :rectDpt[0].Id];

        RestRequest req = new RestRequest();
        RestResponse res = new RestResponse();

        String JsonMsg = '{"visitStartDate":"20211221033551",'+
                        '"visitPurpose":"维修跟进",'+
                        '"visitInfo":"报价任务",'+
                        '"visitEndDate":"20211221033554",'+
                        '"visitDistinction":"用户拜访0000",'+
                        '"synchroDate":"20211221153601",'+
                        '"replyResult":"报价跟进",'+
                        '"replyID":"88",'+
                        '"mngCd":"0000022174",'+
                            '"departmentCd":"'+depart.Management_Code__c+'",'+
                        // '"departmentCd":"667538",'+
                        '"applicantId":"5",'+
                        '"activityDifferentiation":"报价跟进"}';
        req.requestURI = 'services/apexrest/NFM608/execute';
        req.httpMethod = 'POST';
        req.requestBody = Blob.valueof(JsonMsg);
        RestContext.request = req;
        RestContext.response= res;

        NFM608Rest.doPost();
        Test.stopTest();
    }
    @isTest
    static void test_3(){
        Test.startTest();

        List<RecordType> rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        if (rectCo.size() == 0) {
            throw new ControllerUtil.myException('not found 病院 recodetype');
        }
        List<RecordType> rectSct = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '戦略科室分類 消化科'];
        if (rectSct.size() == 0) {
            throw new ControllerUtil.myException('not found 戦略科室分類 呼吸科 recodetype');
        }
        List<RecordType> rectDpt = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '診療科 消化科'];
        if (rectDpt.size() == 0) {
            throw new ControllerUtil.myException('not found 診療科 消化科 recodetype');
        }

        Account depart = [select Management_Code__c from Account where RecordTypeId = :rectDpt[0].Id];
        RestRequest req = new RestRequest();
        RestResponse res = new RestResponse();

        String JsonMsg = '{"visitStartDate":"20211221033551",'+
                        '"visitPurpose":"维修跟进",'+
                        '"visitInfo":"报价任务",'+
                        '"visitEndDate":"20211221033554",'+
                        '"visitDistinction":"用户拜访",'+
                        '"synchroDate":"20211221153601",'+
                        '"replyResult":"报价跟进",'+
                        '"replyID":"88",'+
                        '"mngCd":null,'+
                            '"departmentCd":"'+depart.Management_Code__c+'",'+
                        '"applicantId":"5",'+
                        '"activityDifferentiation":"报价跟进"}';
        req.requestURI = 'services/apexrest/NFM608/execute';
        req.httpMethod = 'POST';
        req.requestBody = Blob.valueof(JsonMsg);
        RestContext.request = req;
        RestContext.response= res;

        NFM608Rest.doPost();
        Test.stopTest();
    }
    @isTest
    static void test_4(){
        Test.startTest();

        List<RecordType> rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        if (rectCo.size() == 0) {
            throw new ControllerUtil.myException('not found 病院 recodetype');
        }
        List<RecordType> rectSct = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '戦略科室分類 消化科'];
        if (rectSct.size() == 0) {
            throw new ControllerUtil.myException('not found 戦略科室分類 呼吸科 recodetype');
        }
        List<RecordType> rectDpt = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '診療科 消化科'];
        if (rectDpt.size() == 0) {
            throw new ControllerUtil.myException('not found 診療科 消化科 recodetype');
        }

        Account depart = [select Management_Code__c from Account where RecordTypeId = :rectDpt[0].Id];
        RestRequest req = new RestRequest();
        RestResponse res = new RestResponse();


        String JsonMsg = '{"visitStartDate":"20211221033551",'+
                        '"visitPurpose":"维修跟进",'+
                        '"visitInfo":"报价任务",'+
                        '"visitEndDate":"20211221033554",'+
                        '"visitDistinction":"用户拜访",'+
                        '"synchroDate":"20211221153601",'+
                        '"replyResult":"报价跟进",'+
                        '"replyID":"88",'+
                        '"mngCd":"0000022174",'+
                            '"departmentCd":"'+depart.Management_Code__c+'111",'+
                        // '"departmentCd":"667538",'+
                        '"applicantId":"5",'+
                        '"activityDifferentiation":"报价跟进"}';
        req.requestURI = 'services/apexrest/NFM608/execute';
        req.httpMethod = 'POST';
        req.requestBody = Blob.valueof(JsonMsg);
        RestContext.request = req;
        RestContext.response= res;

        NFM608Rest.doPost();
        Test.stopTest();
    }
    @isTest
    static void test_2(){
    	Test.startTest();

        RestRequest req = new RestRequest();
        RestResponse res = new RestResponse();
		String JsonMsg = '{}';
        req.requestURI = 'services/apexrest/NFM608/execute';
    	req.httpMethod = 'POST';
        req.requestBody = Blob.valueof(JsonMsg);
        RestContext.request = req;
        RestContext.response= res;

        NFM608Rest.doPost();
        Test.stopTest();
    }
}