/**
 * 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 OFSHpLeaveRestTest {

    static testMethod void myUnitTest() {
        Event e = new Event(
        	Subject = 'test',
        	StartDateTime = Datetime.now().addHours(-1),
        	EndDateTime = Datetime.now().addHours(1),
        	OwnerId = Userinfo.getUserId(),
        	Main_Visit_Location__c = 'test_location'
        );
        //e.DurationInMinutes = Integer.valueOf((e.EndDateTime.getTime() - e.StartDateTime.getTime()) / 1000 / 60);
        insert e;
        
        String JsonMsg = '{"Eid" : "' + e.Id + '"}';
        Test.startTest();
        
        RestRequest req = new RestRequest();
   		RestResponse res = new RestResponse();
   		
   		req.requestURI = '/services/apexrest/OFSHpLeave';  //Request URL
   		req.httpMethod = 'POST';//HTTP Request Type
   		req.requestBody = Blob.valueof(JsonMsg);
   		
   		RestContext.request = req;
   		RestContext.response= res;
   		
   		e=[select id,StartDateTime from Event where Id =:e.Id];
        OFSHpLeaveRest.doPost(e.Id);
        
        Test.stopTest();
        
        String str = res.responseBody.toString();
        system.assert(str.contains(System.Label.OFSErrorSuccess));
        
        e=[select id,StartDateTime, HPLeaveFlg__c from Event where Id =:e.Id];
        system.assertEquals(true, e.HPLeaveFlg__c);
    }
    
    //no event been found exception test
    static testMethod void nofoundTest() {
        Event e = new Event(
        	Subject = 'test',
        	StartDateTime = Datetime.now().addHours(-1),
        	EndDateTime = Datetime.now().addHours(1),
        	OwnerId = Userinfo.getUserId(),
        	Main_Visit_Location__c = 'test_location'
        );
        
        
        String JsonMsg = '{"Eid" : "' + e.Id + '"}';
        Test.startTest();
        
        RestRequest req = new RestRequest();
   		RestResponse res = new RestResponse();
   		
   		req.requestURI = '/services/apexrest/OFSHpLeave';  //Request URL
   		req.httpMethod = 'POST';//HTTP Request Type
   		req.requestBody = Blob.valueof(JsonMsg);
   		
   		RestContext.request = req;
   		RestContext.response= res;
        OFSHpLeaveRest.doPost(e.Id);
        
        Test.stopTest();
        
        String str = res.responseBody.toString();
        system.assert(str.contains(System.Label.OFSErrorFailure));
    }
    
    /*
    //update exception test
     static testMethod void updateTest() {
     	DateTime et = Datetime.now().addDays(-4);
     	
     	Date t = Date.today();
        Date t1 = t.addMonths(6);
        
        Daily_Report__c dr1 = new Daily_Report__c(
            Reported_Date__c = t1,
            Status__c = '申請中',
            Reporter__c = Userinfo.getUserId()
        );
        insert dr1;
        
        Event__c ec1 = new Event__c(
            Daily_Report__c = dr1.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 = dr1.Reported_Date__c,
            Subject__c = 'eventc1'
        );
        insert ec1;
        
        //このパラメーターはどういう意味でしょうかわからないけど、必須です
        StaticParameter.NotUpdEventCFlg = false;
        
        List<Daily_Report__c> dlist = [select id from Daily_Report__c where Reported_Date__c =: t];
        if(dlist.size()<=0){
        	system.debug('**********OFSHpLeave*********:no dailyReport has been found');
        }
        
        // ①日報がない（Eventと日報の関連なし）、どこへも移動できます
	    // ②日報あり
	    // 移動元、移動先日報共に作成中、移動可能
        
        Event e1 = [select Id,ActivityDate,WhatId from Event where EventC_ID__c = :ec1.Id];
        
        String JsonMsg = '{"Eid" : "' + e1.Id + '"}';
        Test.startTest();
        
        RestRequest req = new RestRequest();
   		RestResponse res = new RestResponse();
   		
   		req.requestURI = '/services/apexrest/OFSHpLeave';  //Request URL
   		req.httpMethod = 'POST';//HTTP Request Type
   		req.requestBody = Blob.valueof(JsonMsg);
   		
   		RestContext.request = req;
   		RestContext.response= res;
   		
        OFSHpLeaveRest.doPost(e1.Id);
        e1=[select id,StartDateTime,ActivityDate from Event where Id =:e1.Id];
        Test.stopTest();
        
        String str = res.responseBody.toString();
        system.assert(str.contains(System.Label.OFSErrorFailure));
        
        e1=[select id,StartDateTime, HPLeaveFlg__c, ActivityDate from Event where Id =:e1.Id];
        system.assertEquals(false, e1.HPLeaveFlg__c);
        
    }*/
    
    ////到着重複のテスト
    static testMethod void duplicationTest() {
     	Event e = new Event(
        	Subject = 'test',
        	StartDateTime = Datetime.now().addHours(-1),
        	EndDateTime = Datetime.now().addHours(1),
        	OwnerId = Userinfo.getUserId(),
        	Main_Visit_Location__c = 'test_location'
        );
        //e.DurationInMinutes = Integer.valueOf((e.EndDateTime.getTime() - e.StartDateTime.getTime()) / 1000 / 60);
        insert e;
        
        String JsonMsg = '{"Eid" : "' + e.Id + '"}';
        Test.startTest();
        
        RestRequest req1 = new RestRequest();
   		RestResponse res1 = new RestResponse();
   		
   		req1.requestURI = '/services/apexrest/OFSHpLeave';  //Request URL
   		req1.httpMethod = 'POST';//HTTP Request Type
   		req1.requestBody = Blob.valueof(JsonMsg);
   		
   		RestContext.request = req1;
   		RestContext.response= res1;
   		
        OFSHpLeaveRest.doPost(e.Id);
        
        String str1 = res1.responseBody.toString();
        system.assert(str1.contains(System.Label.OFSErrorSuccess));
        
        e=[select id,StartDateTime, HPLeaveFlg__c from Event where Id =:e.Id];
        system.assertEquals(true, e.HPLeaveFlg__c);
        
        RestRequest req2 = new RestRequest();
   		RestResponse res2 = new RestResponse();
   		
   		req2.requestURI = '/services/apexrest/OFSHpLeave';  //Request URL
   		req2.httpMethod = 'POST';//HTTP Request Type
   		req2.requestBody = Blob.valueof(JsonMsg);
   		
   		RestContext.request = req2;
   		RestContext.response= res2;
   		
        OFSHpLeaveRest.doPost(e.Id);
        
        Test.stopTest();
        
        String str2 = res2.responseBody.toString();
        system.assert(str2.contains(System.Label.OFSErrorFailure));
        
    }
}