/**
|
* 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 OFSHpArriveRestTest {
|
|
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 reqst = '{"Eid" : "' + e.Id + '"}';
|
|
//String JsonMsg=JSON.serialize(reqst);
|
/*
|
e=[select id, ActivityDate, OwnerId, Subject, whatid__c, EventC_ID__c, NextEventC_ID__c, AppCdId__c, SyncCreatedDate__c,
|
StartDateTime, EndDateTime, DurationInMinutes, Main_Visit_Location__c, Activity_Type2__c, IsScheduled__c, BeforeActivityDate__c,
|
Purpose_Type__c, Location, Related_Opportunity1__c, Related_Service1__c, Related_Opportunity1_ID__c, Related_Service1_ID__c,
|
WS_flg__c, HPArriveFlg__c, EndDateTime_org__c, StartDateTime_org__c from Event where Id =:e.Id];
|
System.assertEquals(e.StartDateTime_org__c,null);
|
*/
|
String JsonMsg = '{"Eid" : "' + e.Id + '"}';
|
Test.startTest();
|
|
RestRequest req = new RestRequest();
|
RestResponse res = new RestResponse();
|
|
req.requestURI = '/services/apexrest/OFSHpArrive'; //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];
|
OFSHpArriveRest.doPost(e.Id);
|
|
Test.stopTest();
|
|
String str = res.responseBody.toString();
|
system.assert(str.contains(System.Label.OFSErrorSuccess));
|
|
e=[select id,StartDateTime, HPArriveFlg__c from Event where Id =:e.Id];
|
system.assertEquals(true, e.HPArriveFlg__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/OFSHpArrive'; //Request URL
|
req.httpMethod = 'POST';//HTTP Request Type
|
req.requestBody = Blob.valueof(JsonMsg);
|
|
RestContext.request = req;
|
RestContext.response= res;
|
OFSHpArriveRest.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];
|
System.assertEquals(0, dlist.size());
|
if(dlist.size()<=0){
|
system.debug('**********OFSHpArrive*********: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/OFSHpArrive'; //Request URL
|
req.httpMethod = 'POST';//HTTP Request Type
|
req.requestBody = Blob.valueof(JsonMsg);
|
|
RestContext.request = req;
|
RestContext.response= res;
|
|
OFSHpArriveRest.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, HPArriveFlg__c, ActivityDate from Event where Id =:e1.Id];
|
system.assertEquals(false, e1.HPArriveFlg__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/OFSHpArrive'; //Request URL
|
req1.httpMethod = 'POST';//HTTP Request Type
|
req1.requestBody = Blob.valueof(JsonMsg);
|
|
RestContext.request = req1;
|
RestContext.response= res1;
|
|
OFSHpArriveRest.doPost(e.Id);
|
|
String str1 = res1.responseBody.toString();
|
system.assert(str1.contains(System.Label.OFSErrorSuccess));
|
|
e=[select id,StartDateTime, HPArriveFlg__c from Event where Id =:e.Id];
|
system.assertEquals(true, e.HPArriveFlg__c);
|
|
RestRequest req2 = new RestRequest();
|
RestResponse res2 = new RestResponse();
|
|
req2.requestURI = '/services/apexrest/OFSHpArrive'; //Request URL
|
req2.httpMethod = 'POST';//HTTP Request Type
|
req2.requestBody = Blob.valueof(JsonMsg);
|
|
RestContext.request = req2;
|
RestContext.response= res2;
|
|
OFSHpArriveRest.doPost(e.Id);
|
|
Test.stopTest();
|
|
String str2 = res2.responseBody.toString();
|
system.assert(str2.contains(System.Label.OFSErrorFailure));
|
|
}
|
}
|