@isTest
private class NFM704RestTest {
    @testSetup
    static void test_method_one(){

        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.Is_Active__c = '有効';
        company.Name = 'NFM105TestCompany';
        company.AwaitToSendAWS__c = true;
        company.AgentCode_Ext__c = '358180';
        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;
        depart.AgentCode_Ext__c = 'Test202201';
        upsert depart;
    
    }

    @isTest static void test_method_two(){

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

        String JsonMsg = '{"Monitoring":{"Tag":"","Sender":"OBPM","Receiver":"SFDC","MessageGroupNumber":"111","MessageType":"1","NumberOfRecord":"1","TransmissionDateTime":"","Text":""},"GeData":[{"WorkingSeniority":"0","TechnicalTitle":"副主任医师","State":"内蒙古自治区","StaffMCode":"","Speciality":"消化内科系统常见病多发病的诊治；消化内镜下的诊断及ESD、EMR、POEM等相关治疗.","Society1":"中华医学会","ProfessionalField":"消化","OfficeMCode":"消化内科","Name":"刘琳","Mobile":"","HospitalMCode":"358180","HcpNo":"153P4959","HCPLevel":"Tier3","EventStatus":"成员","City":"包头市"}]}';
        req.requestURI = 'services/apexrest/NFM704/execute';
        req.httpMethod = 'POST';
        req.requestBody = Blob.valueOf(JsonMsg);

        RestContext.request = req;
        RestContext.response = res;
        
        NFM704Rest.doPost();
    }
}