@isTest
private class RentalApplyControllerTest {
	@TestSetup
	static void makeData(){
		List<String> sobjList = new List<String>();
    	sobjList.add('Rental_Apply__c');
    	sobjList.add('Contact');
    	TestDataUtility.CreatePIPolicyConfigurations(sobjList);
		

	}
    static testMethod void testMethod1() {
    	

        String userMode = 'classic';//classic  lightning
    	String objectType = 'Contact';
    	String recordTypeId = Schema.SObjectType.Contact.getRecordTypeInfosByDeveloperName().get('Doctor').getRecordTypeId();

    	Map<String,object> detailsMap = new Map<String,object>();
    	Map<String,object> detailsMap2 = new Map<String,object>();
    	Map<String,object> detailsMap3 = new Map<String,object>();
    	detailsMap.put('calculated',true);
    	detailsMap.put('autoNumber',false);
    	detailsMap.put('type','address');
    	detailsMap2.put('calculated',false);
    	detailsMap2.put('autoNumber',false);
    	detailsMap2.put('type','repair');
    	detailsMap3.put('calculated',false);
    	detailsMap3.put('autoNumber',false);
    	detailsMap3.put('type','repair');

    	List<Map<String, object>> componentMapList = new List<Map<String, object>>();
    	List<Map<String, object>> componentMapList2 = new List<Map<String, object>>();
    	List<Map<String, object>> componentMapList3 = new List<Map<String, object>>();
    	Map<String, object> componentMap = new Map<String, object>();
    	Map<String, object> componentMap2 = new Map<String, object>();
    	Map<String, object> componentMap3 = new Map<String, object>();
    	componentMap.put('value','Name');
    	componentMap.put('type','Field');
    	componentMap.put('details',detailsMap);
    	componentMapList.add(componentMap);
    	componentMap2.put('value','Name');
    	componentMap2.put('type','Field');
    	componentMap2.put('details',detailsMap2);
    	componentMapList2.add(componentMap2);
    	componentMap3.put('value','LastName');
    	componentMap3.put('type','Field');
    	componentMap3.put('details',detailsMap3);
    	componentMapList3.add(componentMap3);

    	List<Map<String, object>> layoutComponentsList = new List<Map<String, object>>();
    	Map<String, object> layoutComponents = new Map<String, object>();
    	layoutComponents.put('layoutComponents',componentMapList);
    	layoutComponents.put('label','Address');
    	layoutComponents.put('placeholder',true);
    	layoutComponents.put('editableForNew',true);
    	layoutComponents.put('required',false);
    	layoutComponentsList.add(layoutComponents);
    	Map<String, object> layoutComponents2 = new Map<String, object>();
    	layoutComponents2.put('layoutComponents',componentMapList2);
    	layoutComponents2.put('label','Repair');
    	layoutComponents2.put('placeholder',true);
    	layoutComponents2.put('editableForNew',true);
    	layoutComponents2.put('required',false);
    	layoutComponentsList.add(layoutComponents2);
    	Map<String, object> layoutComponents3 = new Map<String, object>();
    	layoutComponents3.put('layoutComponents',componentMapList3);
    	layoutComponents3.put('label','Repair');
    	layoutComponents3.put('placeholder',true);
    	layoutComponents3.put('editableForNew',true);
    	layoutComponents3.put('required',false);
    	layoutComponentsList.add(layoutComponents3);


    	List<Map<String,object>> itemList = new List<Map<String,object>>();
    	Map<String,object> item = new Map<String,object>();
    	item.put('layoutItems',layoutComponentsList);
    	itemList.add(item);
    	
    	List<Map<String,object>> sectionList = new List<Map<String,object>>();
    	Map<String,object> section = new Map<String,object>();
    	section.put('heading','');
    	section.put('useHeading',true);
    	section.put('columns',1);
    	section.put('useCollapsibleSection',true);
    	section.put('layoutRows',itemList);
    	sectionList.add(section);

		Map<String, Object> layoutSection = new Map<String,Object>();
    	layoutSection.put('layouts','123');
    	layoutSection.put('editLayoutSections',sectionList);
		String layoutSectionJson = JSON.serialize(layoutSection);

        Test.setMock(HttpCalloutMock.class, new TestDataUtility.BaseHttpMock(layoutSectionJson,'OK','200'));

        Test.startTest();

		String url = ApexPages.currentPage().getParameters().put('Name','test');
		url = ApexPages.currentPage().getParameters().put('Name','test');
		url = ApexPages.currentPage().getParameters().put('CF00N10000008ps61_lkid','test');
		url = ApexPages.currentPage().getParameters().put('CF00N10000003Mp1d_lkid','test');
		url = ApexPages.currentPage().getParameters().put('CF00N10000003O3V6_lkid','test');
		url = ApexPages.currentPage().getParameters().put('CF00N10000003Mp2R_lkid','test');
		url = ApexPages.currentPage().getParameters().put('00N10000003Msk0','test');
		url = ApexPages.currentPage().getParameters().put('00N10000003Msk5','test');
		url = ApexPages.currentPage().getParameters().put('00N100000098amW','test');
		url = ApexPages.currentPage().getParameters().put('00N10000003OJzc','test');
        ApexPages.StandardController scc = new ApexPages.StandardController(new Rental_Apply__c());
        RentalApplyController racc = new RentalApplyController(scc);


        Test.stopTest();
    }
    static testMethod void testMethod2() {

        String raJson = '{\"HP_received_sign_rich__c\":\"\",\"Name\":\"123456\",\"OPDPlan__c\":\"000000000000000\",\"GI_Diff__c\":\"\",\"Add_Approval_Status__c\":\"\",\"ApplyPerson_Phone__c\":\"\",\"Person_In_Charge__c\":\"0050l000005vPbu\",\"Loaner_centre_mail_address__c\":\"\",\"applyUser__c\":\"0050l000005vPbu\",\"Cross_Region_Assign__c\":\"\",\"Priority_Inspect__c\":false,\"SalesdeptSelect__c\":\"医疗东北营业本部\",\"RecordTypeId\":\"01210000000RHIi\",\"Zsq_Rental_Apply__c\":\"000000000000000\",\"Asset_return_time_OK__c\":\"\",\"Hospital__c\":\"0010l00001PQFkh\",\"Loaner_medical_Staff__c\":\"0030l00000mFW7G\",\"Strategic_dept__c\":\"0010l00001PQFkh\",\"Phone_number__c\":\"***********\",\"Account__c\":\"0010l00001PQFkh\",\"Demo_purpose1__c\":\"产品试用\",\"Repair__c\":\"000000000000000\",\"NoOpp_Reason__c\":\"\",\"Product_category__c\":\"GI\",\"Follow_UP_Opp__c\":\"000000000000000\",\"Campaign__c\":\"000000000000000\",\"Follow_pcl_status2_Text__c\":\"\",\"QIS_number__c\":\"000000000000000\",\"Statu_Achievements__c\":\"000000000000000\",\"QISRepair__c\":\"000000000000000\",\"CrinicalTrialName__c\":\"\",\"DB_loaner_request__c\":\"\",\"IsJump__c\":false,\"AgreementBorrowingExtensionDate__c\":\"\",\"JumpCause__c\":\"\",\"NewRepair__c\":\"000000000000000\",\"IsJump_Rental__c\":false,\"JumpCause_Rental__c\":\"\",\"JumpDate_Rental__c\":\"\",\"Demo_purpose_text__c\":\"dfasvq\",\"Hope_Lonaer_date_Num__c\":\"3\",\"Request_return_day__c\":\"2022/03/27\",\"Shipment_address__c\":\"000000000000000\",\"direct_send__c\":\"上门自提\",\"Dealer__c\":\"000000000000000\",\"pickup_time__c\":\"2022/03/24 17:44\",\"Loaner_received_staff__c\":\"test\",\"Loaner_received_staff_phone__c\":\"1234567890\",\"direct_shippment_address__c\":\"**********\",\"Post_Code__c\":\"12345-123\",\"Old_Rental_Apply__c\":\"000000000000000\",\"Split_Apply_Reason__c\":\"\",\"HP_received_sign_day__c\":\"\",\"HP_received_sign_NG__c\":false,\"HP_received_sign_NG_Reason__c\":\"\",\"AssetManageConfirm__c\":false,\"Cancel_Reason__c\":\"\",\"Loaner_cancel_request__c\":\"\",\"Cancel_time__c\":\"\",\"HP_received_sign_NewDate__c\":\"\",\"Rental_Received_Day__c\":\"\",\"Add_Request_demo_time__c\":\"\",\"Add_Request_approval_time__c\":\"\",\"ExtensionStatus__c\":\"\",\"ExtensionSuccessTimes__c\":\"\",\"ExtensionApplicationTime_Initial__c\":\"\",\"ExtensionApprovalTime_Initial__c\":\"\",\"ExtensionApplicationTime_Final__c\":\"\",\"ExtensionApprovalTime_Final__c\":\"\",\"Repair_Final_Inspection_Date__c\":\"\",\"All_Delivery_Flag_c__c\":false,\"Request_demo_time__c\":\"\",\"Request_approval_time__c\":\"\",\"Application_accept_time__c\":\"\",\"Request_answer_time__c\":\"\",\"HP_City__c\":\"Abejones\",\"OwnerId\":\"0050l000005vPbu\",\"OPD__c\":\"000000000000000\",\"OPDAmount__c\":\"\",\"IsOPD_Account__c\":\"\",\"OriginalOpdPlanApplication__c\":\"\",\"demo_purpose2__c\":\"试用（无询价）\",\"ToAgency__c\":\"大连\",\"Loaner_cancel_reason__c\":\"_\\u0001_\",\"Phone_Number_Encrypt__c\":\"415f2dab7eb6b2a866e02d823080a845\",\"Direct_Shippment_Address_Encrypt__c\":\"2df1bc4bf3800c5e05e3d9f394c3446567d1f05482d2295650b7b50e9e4aa97a92338985c9693f576e1e6df667aaee46\",\"AWS_Data_Id__c\":\"956621521466425345\"}';
        // Map<String,Object> fieldValueMap = (Map<String,Object>)JSON.deserializeStrict(raJson, Map<String,Object>.class);

        // Map<String,Object> fieldValueMap = (Map<String,Object>)JSON.deserializeUntyped(raJson);
        // Rental_Apply__c rentalApplyInfo = new Rental_Apply__c();

        // Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
        // Schema.SObjectType rentalApplySchema = schemaMap.get('Rental_Apply__c');
        // Map<String, Schema.SObjectField> fieldAPIToTypeMap = rentalApplySchema.getDescribe().fields.getMap();
        // for(String fieldAPI :fieldValueMap.keySet()){
        //     Schema.DisplayType fielddataType = fieldAPIToTypeMap.get(fieldAPI).getDescribe().getType();  
        //     String fieldValue = String.valueOf(fieldValueMap.get(fieldAPI)); 
        //     if(String.isBlank(fieldValue)){
        //         continue;
        //     }
        //     if(String.valueOf(fielddataType)=='DATE'){
        //         System.debug('DATE fieldAPI = '+fieldAPI+' filedData = '+String.valueOf(fieldValueMap.get(fieldAPI)));
        //         // rentalApplyInfo.put(fieldAPI, Date.valueOf(String.valueOf(fieldValueMap.get(fieldAPI)).replace('/', '-')));
        //         rentalApplyInfo.put(fieldAPI,(String.isBlank(fieldValue)||String.isEmpty(fieldValue))? null:Date.valueOf(fieldValue.replace('/', '-'))); 
        //     }else if(String.valueOf(fielddataType)=='DATETIME'){
        //         String dt = String.valueOf(fieldValueMap.get(fieldAPI));
        //         if(String.isNotBlank(dt)&&dt.contains('T')){
        //             dt = dt.replace('T',' ');
        //             rentalApplyInfo.put(fieldAPI, Datetime.valueOfGmt(dt));
        //         }else if(String.isNotBlank(dt))  {
        //             //rentalApplyInfo.put(fieldAPI, Datetime.valueOf(dt));
        //             dt = dt.replace('/', '-') + ':00';
        //             System.debug('dt = ' + dt);
        //             rentalApplyInfo.put(fieldAPI, Datetime.valueOf(dt));
        //         }else{
        //             rentalApplyInfo.put(fieldAPI, null);
        //         }          
        //     }else if(String.valueOf(fielddataType)=='Number'||String.valueOf(fielddataType)=='DOUBLE' ){
        //         rentalApplyInfo.put(fieldAPI, Decimal.valueOf(String.valueOf(fieldValueMap.get(fieldAPI))));
        //     } else if(String.valueof(fielddataType)=='BOOLEAN'){
        //         rentalApplyInfo.put(fieldAPI, fieldValueMap.get(fieldAPI));
        //     }else {
        //         rentalApplyInfo.put(fieldAPI, String.valueOf(fieldValueMap.get(fieldAPI)));
        //     }
        // }
        // insert rentalApplyInfo;

        Test.startTest();
        RentalApplyController.saveRentalApply(raJson,'savdqerva',true);
        Test.stopTest();
    }
	static testMethod void testMethod3() {
    	
        Rental_Apply__c ra = TestDataUtility.CreateRentalApplys(1)[0];
        String raJson = JSON.serialize(ra);

		Test.setMock(HttpCalloutMock.class, new TestDataUtility.CreateMetaDataUtilityHttpMock());
		Test.startTest();
		System.debug('ra--------'+ra);
        ApexPages.StandardController sc = new ApexPages.StandardController(ra);
        RentalApplyController rac = new RentalApplyController(sc);
		Test.stopTest();
	}
	static testMethod void testMethod4() {
		Rental_Apply__c ra = TestDataUtility.CreateRentalApplys(1)[0];
        String raJson = '{\"HP_received_sign_rich__c\":\"\",\"Name\":\"123456\",\"OPDPlan__c\":\"000000000000000\",\"GI_Diff__c\":\"\",\"Add_Approval_Status__c\":\"\",\"ApplyPerson_Phone__c\":\"\",\"Person_In_Charge__c\":\"0050l000005vPbu\",\"Loaner_centre_mail_address__c\":\"\",\"applyUser__c\":\"0050l000005vPbu\",\"Cross_Region_Assign__c\":\"\",\"Priority_Inspect__c\":false,\"SalesdeptSelect__c\":\"医疗东北营业本部\",\"RecordTypeId\":\"01210000000RHIi\",\"Zsq_Rental_Apply__c\":\"000000000000000\",\"Asset_return_time_OK__c\":\"\",\"Hospital__c\":\"0010l00001PQFkh\",\"Loaner_medical_Staff__c\":\"0030l00000mFW7G\",\"Strategic_dept__c\":\"0010l00001PQFkh\",\"Phone_number__c\":\"***********\",\"Account__c\":\"0010l00001PQFkh\",\"Demo_purpose1__c\":\"产品试用\",\"Repair__c\":\"000000000000000\",\"NoOpp_Reason__c\":\"\",\"Product_category__c\":\"GI\",\"Follow_UP_Opp__c\":\"000000000000000\",\"Campaign__c\":\"000000000000000\",\"Follow_pcl_status2_Text__c\":\"\",\"QIS_number__c\":\"000000000000000\",\"Statu_Achievements__c\":\"000000000000000\",\"QISRepair__c\":\"000000000000000\",\"CrinicalTrialName__c\":\"\",\"DB_loaner_request__c\":\"\",\"IsJump__c\":false,\"AgreementBorrowingExtensionDate__c\":\"\",\"JumpCause__c\":\"\",\"NewRepair__c\":\"000000000000000\",\"IsJump_Rental__c\":false,\"JumpCause_Rental__c\":\"\",\"JumpDate_Rental__c\":\"\",\"Demo_purpose_text__c\":\"dfasvq\",\"Hope_Lonaer_date_Num__c\":\"3\",\"Request_return_day__c\":\"2022/03/27\",\"Shipment_address__c\":\"000000000000000\",\"direct_send__c\":\"上门自提\",\"Dealer__c\":\"000000000000000\",\"pickup_time__c\":\"2022/03/24 17:44\",\"Loaner_received_staff__c\":\"test\",\"Loaner_received_staff_phone__c\":\"1234567890\",\"direct_shippment_address__c\":\"**********\",\"Post_Code__c\":\"12345-123\",\"Old_Rental_Apply__c\":\"000000000000000\",\"Split_Apply_Reason__c\":\"\",\"HP_received_sign_day__c\":\"\",\"HP_received_sign_NG__c\":false,\"HP_received_sign_NG_Reason__c\":\"\",\"AssetManageConfirm__c\":false,\"Cancel_Reason__c\":\"\",\"Loaner_cancel_request__c\":\"\",\"Cancel_time__c\":\"\",\"HP_received_sign_NewDate__c\":\"\",\"Rental_Received_Day__c\":\"\",\"Add_Request_demo_time__c\":\"\",\"Add_Request_approval_time__c\":\"\",\"ExtensionStatus__c\":\"\",\"ExtensionSuccessTimes__c\":\"\",\"ExtensionApplicationTime_Initial__c\":\"\",\"ExtensionApprovalTime_Initial__c\":\"\",\"ExtensionApplicationTime_Final__c\":\"\",\"ExtensionApprovalTime_Final__c\":\"\",\"Repair_Final_Inspection_Date__c\":\"\",\"All_Delivery_Flag_c__c\":false,\"Request_demo_time__c\":\"\",\"Request_approval_time__c\":\"\",\"Application_accept_time__c\":\"\",\"Request_answer_time__c\":\"\",\"HP_City__c\":\"Abejones\",\"OwnerId\":\"0050l000005vPbu\",\"OPD__c\":\"000000000000000\",\"OPDAmount__c\":\"\",\"IsOPD_Account__c\":\"\",\"OriginalOpdPlanApplication__c\":\"\",\"demo_purpose2__c\":\"试用（无询价）\",\"ToAgency__c\":\"大连\",\"Loaner_cancel_reason__c\":\"_\\u0001_\",\"Phone_Number_Encrypt__c\":\"415f2dab7eb6b2a866e02d823080a845\",\"Direct_Shippment_Address_Encrypt__c\":\"2df1bc4bf3800c5e05e3d9f394c3446567d1f05482d2295650b7b50e9e4aa97a92338985c9693f576e1e6df667aaee46\",\"AWS_Data_Id__c\":\"'+ra.AWS_Data_Id__c+'\"}';
		System.debug('ra.AWS_Data_Id__c------'+ra.AWS_Data_Id__c);
		Test.startTest();
        RentalApplyController.saveRentalApply(raJson,'savdqerva',false);
        Test.stopTest();
	}
}