/**
 * This class contains unit tests for validating the behavior of Apex classes
 * and triggers.
 *
 * Unit tests are class methods that verify whether a particular piece
 * of code is working properly. Unit test methods take no arguments,
 * commit no data to the database, and are flagged with the testMethod
 * keyword in the method definition.
 *
 * All test methods in an organization are executed whenever Apex code is deployed
 * to a production organization to confirm correctness, ensure code
 * coverage, and prevent regressions. All Apex classes are
 * required to have at least 75% code coverage in order to be deployed
 * to a production organization. In addition, all triggers must have some code coverage.
 * 
 * The @isTest class annotation indicates this class only contains test
 * methods. Classes defined with the @isTest annotation do not count against
 * the organization size limit for all Apex scripts.
 *
 * See the Apex Language Reference for more information about Testing and Code Coverage.
 */
@isTest
private class OFSEventEditRestTest {

    static testMethod void myUnitTest() {
        List<RecordType> rectHp = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and DeveloperName = 'HP'];
        RecordType rtDep = [select id from RecordType where IsActive = true and SobjectType = 'Account' and DeveloperName =:'Department_ENT'];
        
        Account hp = new Account(RecordTypeId = rectHp[0].Id, Name = 'HP1');
        insert hp;
        
        Account accDepClass = [select Id from Account where ParentId = :hp.Id and Department_Class_Label__c = '耳鼻喉科'];
        Account accDep = new Account();
        accDep.Name = '診療科1';
        accDep.Department_Class_Label__c = '診療科1';
        accDep.Hospital__c = hp.id;
        accDep.ParentId = accDepClass.id;
        accDep.Department_Class__c = accDepClass.id;
        accDep.Department_Name__c = '診療科1';
        accDep.CurrencyIsoCode = 'CNY';
        accDep.RecordTypeId = rtDep.id;
        insert accDep;
        
        Opportunity opp1 = new Opportunity(
            AccountId = accDep.Id,
            StageName = '引合',
            CloseDate = Date.today(),
            Name = 'opp1'
        );
        Opportunity opp2 = new Opportunity(
            AccountId = accDep.Id,
            StageName = '引合',
            CloseDate = Date.today(),
            Name = 'opp2'
        );
        Opportunity opp3 = new Opportunity(
            AccountId = accDep.Id,
            StageName = '引合',
            CloseDate = Date.today(),
            Name = 'opp3'
        );
        insert new Opportunity[] {opp1, opp2, opp3};
        
        Maintenance_Contract__c contract = new Maintenance_Contract__c();
        contract.Name = 'test contract';
        contract.Department__c = accDep.Id;
        contract.Contract_Start_Date__c = Date.today() - 10;
        contract.Contract_End_Date__c = Date.today() + 10;
        insert contract;
        
        Daily_Report__c dr = new Daily_Report__c();
        dr.Reported_Date__c = date.today();
        dr.Reporter__c = Userinfo.getUserId();
        insert dr;
        
        Date t = Date.today();
        Date t1 = t.addMonths(6);
        
        Event__c ec1 = new Event__c(
            Daily_Report__c = dr.Id,
            StartDateTime__c = Datetime.newInstance(t1.year(), t1.month(), t1.day(), 10, 0, 0),
            EndDateTime__c = Datetime.newInstance(t1.year(), t1.month(), t1.day(), 11, 0, 0),
            ActivityDate__c = dr.Reported_Date__c,
            Subject__c = 'eventc1'
        );
        insert ec1;
        
        Event e = [select Id from Event where EventC_ID__c = :ec1.Id];
        
        //OFSEventEditRest er = new OFSEventEditRest();
        Long st = Datetime.now().getTime();
        Long ed = Datetime.now().addHours(1).getTime();
        RestContext.request = new RestRequest();
        Map<String, String> requestMap = new Map<String, String>();
        requestMap.put('sub', 't1');
        requestMap.put('at2', 't2');
        requestMap.put('pt', 't3');
        requestMap.put('stime', '' + st);
        requestMap.put('etime', '' + ed);
        requestMap.put('eid', '' + e.Id);
        requestMap.put('result', '');
        requestMap.put('accId', '');
        requestMap.put('accName', 't4');
        requestMap.put('opp1Id', opp1.Id);
        requestMap.put('opp1', 'opp1');
        requestMap.put('opp2Id', '');
        requestMap.put('opp2', '');
        requestMap.put('opp3Id', '');
        requestMap.put('opp3', '');
        requestMap.put('mcId', contract.Id);
        requestMap.put('mcName', 'test contract');
        RestContext.request.requestBody = blob.valueOf(JSON.serialize(requestMap));
        RestContext.response = new RestResponse();
        OFSEventEditRest.doPost();
        
        Event e2 = new Event(
            StartDateTime = Datetime.newInstance(t1.year(), t1.month(), t1.day(), 12, 0, 0),
            EndDateTime = Datetime.newInstance(t1.year(), t1.month(), t1.day(), 13, 0, 0),
            Subject = 'event1'
        );
        insert e2;
        
        st = Datetime.now().addYears(1).getTime();
        ed = Datetime.now().addYears(1).addHours(1).getTime();
        requestMap.put('stime', '' + st);
        requestMap.put('etime', '' + ed);
        RestContext.request.requestBody = blob.valueOf(JSON.serialize(requestMap));
        OFSEventEditRest.doPost();
    }
}