-1

我尝试使用下面的 .json 得到错误的请求错误,但我没有得到。我想发送这个响应。

例如:

  1. 验证错误:{ "StatusCode":"400", "id":"", "error":"xxxx" } 2.Internal Server error: { "StatusCode":"500", "id":"", "错误":"xxxx" }

    Apex REST API 类:

    @RestResource(urlMapping='/IoT_Case__c/*')
    全局共享类 IOT_CaseManager { 全局类 ResponseWrapper{

            public String StatusCode;
            public String StatusMessage;
            public String ErrorMessage;
            public String ID;
            //public List<IoT_Case__c> Details;
    
    
        }  
       @HttpPost
        global static ResponseWrapper createIOT_Case(String IOT_Case_Type,
                                       String Name,String Email,String Phone,String Facility_Name,
                                       String Address,String case_Reason,string Device_ID,
                                       String Date_time, String Pool_Name){
    
           RestRequest request = RestContext.request; 
           ResponseWrapper resp = new ResponseWrapper(); 
           system.debug('%%%%%% the response is######'+resp);  
    
    
           List<IoT_Case__c>  caseList = new List< IoT_Case__c> ();
      try{ 
                 IoT_Case__c   Iot_case = new IoT_Case__c (); 
    
                 Iot_case.IOT_Case_Type__c=IOT_Case_Type;   
                 Iot_case.Name__c= Name;
                 Iot_case.Email__c= Email;     
                 Iot_case.Phone__c= Phone;
                 Iot_case.Address__c= Address;
                 Iot_case.Pool_Name__c= Pool_Name;     
                 Iot_case.Device_ID__c= Device_ID;
                 Iot_case.case_Reason__c=case_Reason;
                 Iot_case.Facility_Name__c=Facility_Name;
                 Iot_case.Date_time__c=Date.valueOf(Date_time); 
              caseList.add(Iot_case);         
    
         if (caseList.size() > 0){                  
         List<IoT_Case__c> Iotcase_List = [SELECT ID,Name__c,case_Reason__c,Device_ID__c,IOT_Case_status__c from IoT_Case__c]; 
        // system.debug('@@@@the value is &&&&&'+ Iotcase_List);
         For (Iot_case__c ic:Iotcase_List){            
    
         if ((ic.IOT_Case_status__c == 'Open'|| ic.IOT_Case_status__c == 'In -Progress') && (ic.case_Reason__c == caseList[0].case_Reason__c && ic.Device_ID__c == caseList[0].Device_ID__c)){                
             resp.errorMessage='Duplicate case'; 
             system.debug('@@@@the value is &&&&&'+resp.errorMessage );            
           }
     } 
       if(resp.errorMessage!='Duplicate case'){
    
              insert caseList; 
               resp.statusCode ='201';
               resp.statusMessage ='Success' ;
    
         }
           else{
               resp.statusCode ='409';
               resp.statusMessage ='Duplicate' ;
             }
         }          
     } 
        catch (Exception e) {  
    
          system.debug('&&&&&&& The errro message is *****'+ e);
    
          if(e.getMessage().contains('Validation'))
            {
            resp.statusCode = '400';
            resp.statusMessage = 'Exception : ' + e.getMessage();   
            } 
            if(e.getMessage().contains('Internal server error '))
            {
            resp.statusCode = '500';
            resp.statusMessage = 'Exception : ' + e.getMessage();   
            }  
        } 
         string Outputget= JSON.serialize((caseList));
    
               //resp.Details = caseList;
               resp.ID = caseList[0].Id; 
               system.debug('@@@@@@the response id is&&&&&'+resp.ID );
               //resp.ErrorMessage= 'Exception ' ;
    
        return resp; 
       } 
    }
    
    json :
    
    {
        "IOT_Case_Type": "Equipment_Replacement",
        "Name": "test123",
        "Email": "test1@gmail.com",
        "Phone": "9086978010",
        "Address": "Chennai",
        "Pool_Name": "Pool-1",
        "Facility_Name": "Facility-1",
        "Device_ID" : "D1110" ,
        "case_Reason"  : "FAC Sensor Expired,ORP Sensor Expired,pH Sensor Expired",
        "Date_time": "2021-02-01T03:42:22Z" 
    }
    
    endpoint Post url :/services/apexrest/IoT_Case__c/*
    
4

2 回答 2

0
 if(e.getMessage().contains('FIELD_CUSTOM_VALIDATION_EXCEPTION'))
        {
        resp.statusCode = '400';
        resp.statusMessage = 'Exception : ' + e.getMessage();
        resp.ErrorMessage= 'Bad Request Exception' ;   
        } 
       else{
        resp.statusCode = '500';
        resp.statusMessage = 'Exception :' + e.getMessage(); 
        resp.ErrorMessage= 'Internal server Exception ' ;  
       }  
于 2021-08-27T14:13:01.390 回答
0
I need test class with 80% code coverage for above class . 
I tried this but i am getting 58% code coverage only .
@isTest
Public class IOT_CaseManagerTest{
    static testMethod void testDoPost(){
      
      IoT_Case__c  Iot_case = new IoT_Case__c(); 
            Iot_case.IOT_Case_Type__c='Equipment_Replacement';   
             Iot_case.Name__c='';
             Iot_case.Email__c='test1@gmail.com';     
             Iot_case.Phone__c='9573678567';
             Iot_case.Address__c='Chennai';
             Iot_case.Pool_Name__c='Pool-1';     
             Iot_case.Device_ID__c='CAS968878';
             Iot_case.case_Reason__c='FAC Sensor Expired,ORP Sensor Expired,pH Sensor Expired';
             Iot_case.Facility_Name__c='Facility-1';
             Iot_case.Date_time__c=system.Today(); 
          insert Iot_case;
           Test.startTest();
             IOT_CaseManager.createIOT_Case('Equipment_Replacement','sreenitest','test1@gmail.com','9573678567','Chennai','Pool-1','CAS968878','FAC Sensor Expired','Facility-1','8/25/2021');
           Test.stopTest();
    }      
    static testMethod void testPostMethod()
     {
        RestRequest request = new RestRequest();
        request.requestUri ='/services/apexrest/IoT_Case__c/*';
        request.httpMethod = 'POST';
        RestContext.request = request;
        IOT_CaseManager.createIOT_Case('Equipment_Replacement','sreenitest','test1@gmail.com','9997877','Chennai-2','Pool-1','CAS968898','FAC Sensor Expired','Facility-1','8/25/2021');
        //System.assert(strId !=null );
     }          
 }
于 2021-08-27T14:21:35.857 回答