/*
 * Author: Wang,Xueqin
 * Created Date: 08/07/2023
 * Purpose:Test Class
 * */
@isTest
private class LexNewAndEditCasePIPLControllerTest {
    public static String ReRecordTypeId = Schema.SObjectType.Case.getRecordTypeInfosByDeveloperName().get('Probrems').getRecordTypeId();
    public static String rectCoAg = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Agency').getRecordTypeId();
    public static String rectAg = Schema.SObjectType.Contact.getRecordTypeInfosByDeveloperName().get('Agency').getRecordTypeId();
    public static String SystemRecord = Schema.SObjectType.Case.getRecordTypeInfosByDeveloperName().get('Probrems').getRecordTypeId();
    @testSetup
    static void setupTestData(){
        Case ca = new Case();
        ca.RecordTypeId = ReRecordTypeId;
        ca.Task_category__c = 'test';
        insert ca;
        List<RecordType> rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        Account account0 = new Account(RecordTypeId = rectCo[0].Id, AgentCode_Ext__c = '9999909', Name = 'olympus0',Is_Active__c = '有効');
        insert account0;
        Account testRectHpAccounts = new Account(Name = 'testRectHpAccount', RecordTypeId = rectCoAg, Hospital__c = account0.Id);
        insert testRectHpAccounts;
        Contact testContacts = new Contact(email='jplumber@salesforce.com', firstname='Joe', lastname='Plumber',RecordTypeId = rectAg,  AWS_Data_Id__c = 'TestContact', accountId = testRectHpAccounts.id);
        insert testContacts;

        PI_Policy_Configuration__c config1 = new PI_Policy_Configuration__c(Sobject_Type__c  = 'Case');
        insert config1;
        PI_Policy_Configuration__c config2 = new PI_Policy_Configuration__c(Sobject_Type__c  = 'CaseV2');
        insert config2;
        PI_Policy_Configuration__c config3 = new PI_Policy_Configuration__c(Sobject_Type__c  = 'Contact');
        insert config3;
        PI_Field_Policy_Detail__c pfpd1 = new PI_Field_Policy_Detail__c(AWS_Field_API__c = 'phone1', SF_Field_API_Name__c = 'Phone1',PI_Policy_Configuration__c = config1.Id, Enable_Encrypt__c=true);
        insert pfpd1;
        PI_Field_Policy_Detail__c pfpd2 = new PI_Field_Policy_Detail__c(AWS_Field_API__c = 'phone2', SF_Field_API_Name__c = 'Phone2',PI_Policy_Configuration__c = config2.Id, Enable_Encrypt__c=true);
        insert pfpd2;
        PI_Field_Policy_Detail__c pfpd3 = new PI_Field_Policy_Detail__c(AWS_Field_API__c = 'phone2', SF_Field_API_Name__c = 'Phone2',PI_Policy_Configuration__c = config3.Id, Enable_Encrypt__c=true);
        insert pfpd3;


    }
    @isTest
    static void tesMethod1(){
        Case ca = [SELECT Id FROM Case WHERE Task_category__c = 'test' LIMIT 1];
        Contact con = [SELECT Id,AWS_Data_Id__c FROM Contact WHERE AWS_Data_Id__c = 'TestContact' LIMIT 1];
        List<Profile> p = [SELECT Id FROM Profile WHERE Name = '系统管理员'];
        if(p.size() == 0) {
            p = [SELECT Id FROM Profile WHERE Name = 'System Administrator'];
        }
        Id systemAdminProfileId = p[0].Id;
        String body='{"size":1,"totalSize":1,"done":true,"queryLocator":null,"entityTypeName":"ProfileLayout","records":[{"attributes":{"type":"ProfileLayout","url":"/services/data/v41.0/tooling/sobjects/ProfileLayout/01G1000000AKdQREA1"},"Layout":{"attributes":{"type":"Layout","url":"/services/data/v41.0/tooling/sobjects/Layout/00h10000005ziI1AAI"},"Name":"管理者课题页面布局","TableEnumOrId":"Case"},"ProfileId":"'+systemAdminProfileId+'","Profile":{"attributes":{"type":"Profile","url":"/services/data/v41.0/tooling/sobjects/Profile/'+systemAdminProfileId+'"},"Name":"系统管理员"},"RecordTypeId":"'+SystemRecord+'"}]}';
        Test.setMock(HttpCalloutMock.class, new BaseHttpMock(body,'1','200'));
        System.Test.startTest();
        String record_type_id = ReRecordTypeId;
        String objectType = 'Case';
        List<Metadata.LayoutSection> layout = MetaDataUtility.GetRecordTypePageLayout(record_type_id,objectType);
        LexNewAndEditCasePIPLController.initData(ca.Id,ReRecordTypeId,'Case');
        LexNewAndEditCasePIPLController.getContactByAWSId(con.AWS_Data_Id__c);
        System.Test.stopTest(); 
    }
    public class BaseHttpMock implements HttpCalloutMock {
        String body ='';
        String status = '';
        String statusCode = '';
        public BaseHttpMock(String body,String status,String statusCode){
            this.body = body;
            this.status = status;
            this.statusCode = statusCode;
        }
        public HTTPResponse respond(HTTPRequest req) {
            HttpResponse res = new HttpResponse();
            res.setHeader('Content-Type', 'application/json');
            res.setBody(body); 
            res.setStatus('OK');
            res.setStatusCode(200);
            return res;
        }
    }


}