@isTest
private class NFM609RestTest {
    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 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;


        Contact con = new Contact();
        con.AccountId = depart.Id;
        con.LastName = '测试0001';
        insert con;

        Profile p = [select Id from Profile where id =:System.Label.ProfileId_SystemAdmin];
        User hpOwner = new User(Test_staff__c = true, LastName = 'hp', Employee_No__c = 'SFDC-02000',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;


        // 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 = 'om008003';
        // insert user;

        Case c = new Case();
        
        c.complaintID__c = 'test202193';
        c.Status = '对应中';
        insert c;        
        

    }
    @isTest static void test_method1() {

        Test.startTest();
        User us = [select Employee_No__c from User where Employee_No__c = 'SFDC-02000'];

        RestRequest req = new RestRequest();
        RestResponse res = new RestResponse();
        List < Contact > contactList = [
                SELECT Id, AccountId, Account.Name, Account.ParentId, Account.Parent.ParentId, CManageCode__c, Employee_No_manual__c
                FROM Contact
            ];
        String JsonMsg = '[{"unifiedUserID":"2547",'+
                            '"trackType":"委托IT HelpDesk对应",'+
                            '"status":"回答完成&确认",'+
                            '"remarks":"无",'+
                            '"questionType":"投诉",'+
                            '"QuestionList":["于梦辉[2021-12-08 17:03:51]1208测试2;"],'+
                            '"province":"北京市",'+
                            '"mngCd":"'+ contactList[0].CManageCode__c +'",'+
                            '"InquirerType":"客户",'+
                            '"InquirerDate":"2021-12-08 17:03:51",'+
                            '"InquireProduct":"1220测试",'+
                            '"InquirePlatform":null,'+
                            '"InquireContent":"1208测试2；",'+
                            '"field":"产品咨询",'+
                            '"faultJudgment":"解决",'+
                            '"dealMethod":"当场回答",'+
                            '"complaintID":"CP-20211208-00009",'+
                            '"CICstaff":"SFDC-02000",'+
                            '"attachmentName":"609/4775b661c8f044b98b358be0d3e5e777.zip",'+
                            '"attachmentfile":"https://albsylfw.s3.cn-northwest-1.amazonaws.com.cn/20211220/14/11/30/5e8cb69e-f935-48f5-825e-b8344d60f6d4.zip",'+
                            '"AnswerList":["服务二号[2021-12-17 15:02:57]123;"],'+
                            '"answerDate":"20211217",'+
                            '"answerContent":"123；"}]';
        req.requestURI = 'services/apexrest/NFM609/doPost';
        req.httpMethod = 'POST';
        req.requestBody = Blob.valueof(JsonMsg);
        RestContext.request = req;
        RestContext.response= res;

        NFM609Rest.doPost();
        Test.setMock(HttpCalloutMock.class, new FiledDownloadHttpCallMock());
        Test.stopTest();
    }
    
    
    
    @isTest static void test_method2() {

        Test.startTest();
        //进异常
        //User us = [select Employee_No__c from User where Employee_No__c = 'SFDC-02000'];

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

        String JsonMsg = '[{"unifiedUserID":"2547",'+
                            '"trackType":"委托IT HelpDesk对应",'+
                            '"status":"回答完成&确认",'+
                            '"remarks":"无",'+
                            '"questionType":"",'+
                            '"QuestionList":["于梦辉[2021-12-08 17:03:51]1208测试2;"],'+
                            '"province":"北京市",'+
                            '"mngCd":null,'+
                            '"InquirerType":"客户",'+
                            '"InquirerDate":"2021-12-08 17:03:51",'+
                            '"InquireProduct":"",'+
                            '"InquirePlatform":null,'+
                            '"InquireContent":"1208测试2；\\n",'+
                            '"field":"产品咨询",'+
                            '"faultJudgment":"解决",'+
                            '"dealMethod":"当场回答",'+
                            '"complaintID":"",'+
                            '"CICstaff":"",'+
                            '"attachmentName":"609/4775b661c8f044b98b358be0d3e5e777.zip",'+
                            '"attachmentfile":"https://albsylfw.s3.cn-northwest-1.amazonaws.com.cn/20211220/14/11/30/5e8cb69e-f935-48f5-825e-b8344d60f6d4.zip",'+
                            '"AnswerList":null,'+
                            '"answerDate":"20211217",'+
                            '"answerContent":"123；\\n"}]';
        // String JsonMsg = '[{"unifiedUserID":"2547","trackType":"委托IT HelpDesk对应","status":"回答完成&确认","remarks":"无","questionType":"投诉","QuestionList":["于梦辉[2021-12-08 17:03:51]1208测试2;"],"province":"北京市","mngCd":null,"InquirerType":"客户","InquirerDate":"2021-12-08 17:03:51","InquireProduct":"1220测试","InquirePlatform":null,"InquireContent":"1208测试2；\n","field":"产品咨询","faultJudgment":"解决","dealMethod":"当场回答","complaintID":"CP-20211208-00009","CICstaff":"SFDC-02000","attachmentName":"609/4775b661c8f044b98b358be0d3e5e777.zip","attachmentfile":"https://albsylfw.s3.cn-northwest-1.amazonaws.com.cn/20211220/14/11/30/5e8cb69e-f935-48f5-825e-b8344d60f6d4.zip","AnswerList":["服务二号[2021-12-17 15:02:57]123;"],"answerDate":"20211217","answerContent":"123；\n"}]';
        // String JsonMsg = '[{"unifiedUserID":"sa_ceshi001","trackType":"委托办事处跟进","status":"确认111111","remarks":"备注","questionType":"产品规格","QuestionList":"李师 2021年9月1日09点18分 ???111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111","province":"北京市","mngCd":null,"InquirerType":"客户","InquirerDate":"202101093000","InquireProduct":"01t10000000ThtJAAS","InquirePlatform":"医院客户端","InquireContent":null,"field":"产品咨询","faultJudgment":"故障判断","dealMethod":"应对方法","complaintID":"test202193","CICstaff":"SFDC-02000","attachmentName":null,"attachmentfile":null,"AnswerList":"张三 2021年9月1日09点18分 ???","answerDate":"fs2fss0df21dsfd0109sdfsf3000sfsfsdfds","answerContent":null}]';
        req.requestURI = 'services/apexrest/NFM609/doPost';
        req.httpMethod = 'POST';
        req.requestBody = Blob.valueof(JsonMsg);
        RestContext.request = req;
        RestContext.response= res;

        NFM609Rest.doPost();
        Test.setMock(HttpCalloutMock.class, new FiledDownloadHttpCallMock());
        NFM609Schedule.assignOneMinute('RepairSubOrder__c');
        Test.stopTest();
    }
    
    @isTest static void test_method3() {

        Test.startTest();
        //数据为空
        RestRequest req = new RestRequest();
        RestResponse res = new RestResponse();

        String JsonMsg = '[{}]';
        req.requestURI = 'services/apexrest/NFM609/doPost';
        req.httpMethod = 'POST';
        req.requestBody = Blob.valueof(JsonMsg);
        RestContext.request = req;
        RestContext.response= res;

        NFM609Rest.doPost();
        // NFM609Schedule.assignOneMinute('RepairSubOrder__c');
        Test.setMock(HttpCalloutMock.class, new FiledDownloadHttpCallMock());
        Test.stopTest();
    }
}