0

Hi i create the class to generate document:

     public with sharing class GenerateDocumentApiHandler {
//here is what i failed
            public Static GenerateResult generateDocId(String parentAzureId, String bodyRequest){
                system.debug('@@@ -------------------- GenerateDocumentApiHandler.generateDocId --------------------');
                system.debug('@@@ parentAzureId= ' + parentAzureId);
                system.debug('@@@ bodyRequest= ' + bodyRequest);

                GenerateResult result = new GenerateResult();
                API_Configuration__mdt apiSettingGenerateDoc = CommonUtil.getAPIconfiguration('Generate_Document');
                if (apiSettingGenerateDoc == null){
                    result.errMsg = 'API Configuration Generate Document are missing';
                    return result;
                }
                try {
                    HttpRequest reqGenerateDoc = getHttpRequest(apiSettingGenerateDoc, parentAzureId, 'Generate_Document');
                    reqGenerateDoc.setBody(bodyRequest);
                    Http httpGenerateDoc = new Http();
                    HttpResponse resGenerateDoc = httpGenerateDoc.send(reqGenerateDoc);
                    system.debug('@@@ >> resGenerateDoc= ' + resGenerateDoc);
                    system.debug('@@@ >> resGenerateDoc.getBody()= ' + resGenerateDoc.getBody());

                    if (resGenerateDoc.getStatusCode() == 200) {
                        Map<String, Object> resGenerateDocBody = (Map<String, Object>) JSON.deserializeUntyped(resGenerateDoc.getBody());
                        String docAzureId = (String) resGenerateDocBody.get('documentId');
                        system.debug('@@@ >> docAzureId= ' + docAzureId);

                        result.docAzureId = docAzureId;    
                    } else {
                        result.errMsg = resGenerateDoc.getBody();
                    }     

                } catch (Exception ex) {
                    result.errMsg = CommonUtil.getExceptionMessage(ex);
                }

                return result;
            }
        //till here
            public Static GenerateResult generateDocId(String parentObjectField, String parentId, String parentAzureId, 
                                                       String documentType, String bodyRequest){
                system.debug('@@@ -------------------- GenerateDocumentApiHandler.generateDocId --------------------');
                system.debug('@@@ parentObjectField= ' + parentObjectField);
                system.debug('@@@ parentId= ' + parentId);
                system.debug('@@@ parentAzureId= ' + parentAzureId);
                system.debug('@@@ documentType= ' + documentType);
                system.debug('@@@ bodyRequest= ' + bodyRequest);

                GenerateResult result = new GenerateResult();
                API_Configuration__mdt apiSettingGenerateDoc = CommonUtil.getAPIconfiguration('Generate_Document');
                if (apiSettingGenerateDoc == null){
                    result.errMsg = 'API Configuration Generate Document are missing';
                    return result;
                }
                try {
                    HttpRequest reqGenerateDoc = getHttpRequest(apiSettingGenerateDoc, parentAzureId, 'Generate_Document');
                    reqGenerateDoc.setBody(bodyRequest);
                    Http httpGenerateDoc = new Http();
                    HttpResponse resGenerateDoc = httpGenerateDoc.send(reqGenerateDoc);
                    system.debug('@@@ >> resGenerateDoc= ' + resGenerateDoc);
                    system.debug('@@@ >> resGenerateDoc.getBody()= ' + resGenerateDoc.getBody());

                    if (resGenerateDoc.getStatusCode() == 200) {
                        Map<String, Object> resGenerateDocBody = (Map<String, Object>) JSON.deserializeUntyped(resGenerateDoc.getBody());
                        String docAzureId = (String) resGenerateDocBody.get('documentId');
                        system.debug('@@@ >> docAzureId= ' + docAzureId);

                        result.docAzureId = docAzureId;

                        String recordTypeId = '';
                        if (parentObjectField == 'Contract__c') {
                            recordTypeId = Schema.SObjectType.Document_Revision__c.getRecordTypeInfosByDeveloperName().get('Contract').getRecordTypeId();
                        } else {
                            recordTypeId = Schema.SObjectType.Document_Revision__c.getRecordTypeInfosByDeveloperName().get('Quote').getRecordTypeId();
                        }

                        // insert new Quote Document Record
                        Document_Revision__c newDoc = new Document_Revision__c(
                            Name = documentType + '_' + String.valueOf(Datetime.now()),
                            Document_Azure_Id__c = docAzureId,
                            Document_Type__c = documentType,
                            RecordTypeId = recordTypeId
                        );
                        newDoc.put(parentObjectField, parentId);
                        system.debug('@@@ newDoc= ' + newDoc);
                        insert newDoc;

                    } else {
                        result.errMsg = resGenerateDoc.getBody();
                    }
                    ApiUtil.logAPICall(reqGenerateDoc, resGenerateDoc, 'Generate_Document Callout','GenerateDocumentApiHandler.generateDocId', apiSettingGenerateDoc.HeaderParams__c, 'Outbound');

                } catch (Exception ex) {
                    result.errMsg = CommonUtil.getExceptionMessage(ex);
                }
                return result;
            }


          //i failed here
            public static Document_Revision__c generateDocRevisionContract(String parentId, String parentAzureId, 
                                                                            String documentType, String bodyRequest){

                system.debug('@@@ -------------------- GenerateDocumentApiHandler.generateDocResultBatch --------------------');
                API_Configuration__mdt apiSettingGenerateDoc = CommonUtil.getAPIconfiguration('Generate_Document');
                if (apiSettingGenerateDoc == null){
                    return null;
                }
                HttpRequest reqGenerateDoc = null;
                HttpResponse resGenerateDoc = null;
                try {
                    reqGenerateDoc = getHttpRequest(apiSettingGenerateDoc, parentAzureId, 'Generate_Document');
                    reqGenerateDoc.setBody(bodyRequest);
                    Http httpGenerateDoc = new Http();
                    resGenerateDoc = httpGenerateDoc.send(reqGenerateDoc);

                    if (resGenerateDoc.getStatusCode() == 200) {
                        Map<String, Object> resGenerateDocBody = (Map<String, Object>) JSON.deserializeUntyped(resGenerateDoc.getBody());
                        String docAzureId = (String) resGenerateDocBody.get('documentId');
                        system.debug('@@@ >> docAzureId= ' + docAzureId);

                        String recordTypeId = Schema.SObjectType.Document_Revision__c.getRecordTypeInfosByDeveloperName().get('Contract').getRecordTypeId();

                        // insert new Quote Document Record
                        Document_Revision__c newDoc = new Document_Revision__c(
                            Name = documentType + '_' + String.valueOf(Datetime.now()),
                            Document_Azure_Id__c = docAzureId,
                            Document_Type__c = documentType,
                            RecordTypeId = recordTypeId,
                            Contract__c = parentId
                        );
                        return newDoc;      
                    }
                    ApiUtil.logAPICall(reqGenerateDoc, resGenerateDoc, 'Generate_Document Callout','GenerateDocumentApiHandler.generateDocResultBatch', apiSettingGenerateDoc.HeaderParams__c, 'Outbound');         
                } catch (Exception ex) {
                    ApiUtil.logAPICall(new HttpRequest(), new HttpResponse(), 'Generate_Document Callout','GenerateDocumentApiHandler.generateDocResultBatch', apiSettingGenerateDoc.HeaderParams__c, 'Outbound');
                    return null;
                }
                return null;
            }
        //till here
            public static GenerateResult getDocumentEncode(String docAzureId){
                system.debug('@@@ -------------------- GenerateDocumentApiHandler.getDocumentEndcode --------------------');
                system.debug('@@@ docAzureId= ' + docAzureId);
                GenerateResult result = new GenerateResult();

                try {
                    Blob responseBodyBlob = null;
                    API_Configuration__mdt apiSettingGetDoc = CommonUtil.getAPIconfiguration('Get_Document');
                    if (apiSettingGetDoc == null) {
                        //throw new AuraHandledException('API Configuration Get Document is missing');
                        result.errMsg = 'API Configuration Get Document is missing';
                        return result;
                    }
                    HttpRequest reqGetDoc = getHttpRequest(apiSettingGetDoc, docAzureId, 'Get_Document');
                    Http httpGetDoc = new Http();
                    HttpResponse resGetDoc = httpGetDoc.send(reqGetDoc);
                    system.debug('@@@ resGetDoc= ' + resGetDoc);
                    system.debug('@@@ resGetDoc body= ' + resGetDoc.getBody());
                    if (resGetDoc.getStatusCode() == 200) {
                        responseBodyBlob = resGetDoc.getBodyAsBlob();
                    } else { 
                        result.errMsg = Label.SYSTEM_ERROR_MSG + ': ' + resGetDoc.getBody();
                    }
                    system.debug('@@@ responseBodyBlob= ' + responseBodyBlob);
                    //Commented by Siva as Log API throwing exception as the message is huge.
                  //  ApiUtil.logAPICall(reqGetDoc, resGetDoc, 'Get_Document Callout','GenerateDocumentApiHandler.getDocumentEndcode', apiSettingGetDoc.HeaderParams__c, 'Outbound');
                    result.encodeBlobResponse = (responseBodyBlob == null) ? null : EncodingUtil.base64Encode(responseBodyBlob);

                } catch (Exception ex) {
                    result.errMsg = CommonUtil.getExceptionMessage(ex);
                }
                return result;
            }

            public static HttpRequest getHttpRequest(API_Configuration__mdt apiSetting, String idData, String nameApi) {
                String baseUri = apiSetting.Base_Url__c + apiSetting.Endpoint__c;
                system.debug('@@@ baseUri= ' + baseUri);

                Map<String, String> headerParamMap = new Map<String, String>();
                for (String headerParam : apiSetting.HeaderParams__c.split(';')) {
                    List<String> headerKeyValue = headerParam.split(':');
                    headerParamMap.put(headerKeyValue[0], headerKeyValue[1]);
                }
                system.debug('@@@ headerParamMap= ' + headerParamMap);

                if(nameApi == 'Get_Document'){
                    baseUri += '/' + idData;
                }

                HttpRequest req = new HttpRequest();
                req.setEndpoint(baseUri);
                req.setMethod(apiSetting.Method__c);
                req.setTimeout(Integer.valueOf(Label.ApiTimeOut));
                for (String headerParamKey : headerParamMap.keySet()) {
                    req.setHeader(headerParamKey, headerParamMap.get(headerParamKey));
                }
                system.debug('@@@ req= ' + req);
                return req;
            }

            public class GenerateResult {
                @AuraEnabled public String errMsg               {get; set;}
                @AuraEnabled public String docAzureId           {get; set;}
                @AuraEnabled public String encodeBlobResponse   {get; set;}
                @AuraEnabled public String docName              {get; set;}
            }

        // failed here
            public class GenerateDocumentResultBatch {

                @AuraEnabled public String docName              {get; set;}
                @AuraEnabled public String docAzureId           {get; set;}
                @AuraEnabled public String docType              {get; set;}
                @AuraEnabled public String recordTypeId         {get; set;}
                @AuraEnabled public String parentObjectField         {get; set;}
                @AuraEnabled public String errMsg               {get; set;}

                public GenerateDocumentResultBatch() {

                }

                public GenerateDocumentResultBatch(String docName, String docAzureId, 
                                    String docType, String recordTypeId, String parentObjectField, String errMsg) {
                    this.docName = docName;
                    this.docAzureId = docAzureId;
                    this.docType = docType;
                    this.recordTypeId = recordTypeId;
                    this.parentObjectField = parentObjectField;
                    this.errMsg = errMsg;

                }

            }

        }

and this is my test class:

@isTest
private class GenerateDocumentApiHandlerTest
{

    static TestMethod void testGenerateDocId() {
        String docType = 'test';
        String contractAzureId = 'contact-azure';
        String bodyRequest = '{"documentType":"' + docType + '","contractId":"' + contractAzureId + '"}';
        Test.setMock(HttpCalloutMock.class, new HttpCalloutMockImpl());

        Test.startTest();
        GenerateDocumentApiHandler.GenerateResult result = GenerateDocumentApiHandler.generateDocId(
                        'Contract__c', 
                        'contactId', 
                        contractAzureId, 
                        docType, 
                        bodyRequest);
        Test.stopTest();

        System.assertEquals(true, result.docAzureId != null);
    }

    static TestMethod void testGetDocumentEncode() {
        String docType = 'test';
        String contractAzureId = 'contact-azure';
        String bodyRequest = '{"documentType":"' + docType + '","contractId":"' + contractAzureId + '"}';
        Test.setMock(HttpCalloutMock.class, new HttpCalloutMockImpl());

        Test.startTest();
        GenerateDocumentApiHandler.GenerateResult result = GenerateDocumentApiHandler.getDocumentEncode('docAzureId');
        Test.stopTest();

        System.assertEquals(true, result.encodeBlobResponse != null);
    }

}

Could you please help me to improve it? Thank you

marked as duplicate by Vijay Ganji, Derek F, glls, David Reed apex Jun 11 at 15:29

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 3
    Hi Duy, welcome to SFSE. Please see How to Ask for getting the most from the Community. You can start BT editing your question to shorten the code dump by stripping out all those debug lines, then showing where you are having coverage issues and then think about better asserts that truly test what the code is trying to do. – cropredy Jun 11 at 4:22

Browse other questions tagged or ask your own question.