• dlatte
  • NEWBIE
  • 10 Points
  • Member since 2014
  • Developer
  • MathWorks


  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 5
    Questions
  • 1
    Replies
//Test HttpPOST ping method
    static testMethod void postPingTest1() {
        RestRequest req = new RestRequest();
        RestResponse res = new RestResponse();
        String postData = '{"email":"ping"}';

        String JsonMsg=JSON.serialize(postData);
        System.debug('Serialized Json ' + JsonMsg);
        req.requestBody = Blob.valueOf(JsonMsg);

        req.requestURI = '/services/apexrest/gdpr/v1/GDPRContactMask/';
        req.httpMethod = 'POST';
        RestContext.request = req;
        RestContext.response = res;

        Test.startTest();
        GDPRContactMaskWSv1.maskDetails();
        Test.stopTest();

        System.assertEquals(201, RestContext.response.statusCode);
        Map<String, Object> m = (Map<String, Object>) JSON.deserializeUntyped(RestContext.response.responseBody.toString());
        System.assertEquals('Working', m.get('message'));
    }
Below I am just displaying the logic from the @RestResource(urlMapping='/gdpr/v1/GDPRContactMask/*') global with sharing class GDPRContactMaskWSv1 { that pertains to the @HttpPost method I am testing.   
@HttpPost
    global static void maskDetails() {

        String strJson;
        String responseMsg;
        String email;

        Logger.push('gdprContactMask', 'GDPRContactMaskWSv1');

        RestContext.response.addHeader('Content-Type', 'application/json');
        RestRequest request = RestContext.request;

        strJson = System.RestContext.request.requestBody.toString();
        System.debug('restBody ' + strJson);

        try {
            // deserialize the maskGDPRData from the JSON string.
            GDPRDataWrapper maskGDPRData = GDPRDataWrapper.getParsedMaskDataJson(strJson);
            System.debug('maskDetails after deserialized');
            if (maskGDPRData != null) {

                email = maskGDPRData.email;

                // Valid email is required data
                if (String.isBlank(email)) {
                    RestContext.response.statuscode = 400;
                    RestContext.response.responseBody = Blob.valueOf('{ "errorCode" : "400", "message" : "' + System.Label.GDPR_ContactMaskRequiredFields + '" }');
                    Logger.pop();
                    return;
                }

                if (String.isNotBlank(email)) {
                    System.debug('GDPR Email ' + email);

                    // Ping request
                    if (email.toLowerCase() == 'ping') {
                        System.debug('In POST Ping');
                        processPing();
                        Logger.pop();
                        return;
                    } else { // mask Request

                        GDPR_UtilityHelper utilityHelper = new GDPR_UtilityHelper();
                        responseMsg = utilityHelper.GDPRUtilityHelper(maskGDPRData);
                    }
                }

                // Grab the responseMsg and send back to consumer

                if (String.isNotBlank(responseMsg)) {
                    RestContext.response.statuscode = 500;
                    RestContext.response.responseBody = Blob.valueOf(responseMsg);
                } else {
                    RestContext.response.statuscode = 201;
                    RestContext.response.responseBody = Blob.valueOf(strJson); // including original strJson but may change to just return Success
                }
            }
        } catch (DmlException dmlEx) {
            Logger.debugException(dmlEx.getMessage());
            system.debug('****Exception in GDPR Contact Mask****' + dmlEx.getMessage());
            strJson = 'Failure:' + (dmlEx.getMessage().replace('GDPR Mask Contact Update failed.,', '')).replace(': []', '');
            RestContext.response.statuscode = 400;
            RestContext.response.responseBody = Blob.valueOf('{ "errorCode" : "400", "message" : "' + strJson + '" }');

        } catch (Exception ex) {
            Logger.debugException(ex.getMessage());
            system.debug('****Exception in GDPR Contact Mask:****' + ex.getMessage());
            strJson = 'Failure:' + ex.getMessage();
            RestContext.response.statuscode = 400;
            RestContext.response.responseBody = Blob.valueOf('{ "errorCode" : "400", "message" : "' + strJson + '" }');
        }
Output from my log showing the JSON has the '{' at the beginning. 
18:14:18.2 (7452161)|USER_DEBUG|[160]|DEBUG|Serialized Json "{\"email\":\"ping\"}"
18:14:18.2 (45303165)|USER_DEBUG|[34]|DEBUG|restBody "{\"email\":\"ping\"}"
18:14:18.2 (47385844)|USER_DEBUG|[30]|DEBUG|In GDPRDataWrapper parse Json "{\"email\":\"ping\"}"
18:14:18.2 (52017541)|USER_DEBUG|[87]|DEBUG|****Exception in GDPR Contact Mask:****Malformed JSON: Expected '{' at the beginning of object
/**
 * Created by dlatte on 10/9/2018.
 */

public class GDPRDataWrapper {

    public String email { get; set; }
    public String maskedEmail { get; set; }
    public String firstName { get; set; }
    public String middleName { get; set; }
    public String lastName { get; set; }
    public String firstNameLatin { get; set; }
    public String lastNameLatin { get; set; }
    public String mailingStreet { get; set; }
    public String mailingCity { get; set; }
    public String mailingPostalCode { get; set; }
    public String latinStreet { get; set; }
    public String latinCity { get; set; }
    public String workPhone { get; set; }
    public String fax { get; set; }
    public String mobilePhone { get; set; }
    public String homePhone { get; set; }
    public String otherPhone { get; set; }
    public String contactStatus { get; set; }


// parse JSon Method
    public static GDPRDataWrapper getParsedMaskDataJson(String jsonToParse) {

        System.debug('In GDPRDataWrapper parse Json ' + jsonToParse);
        return (GDPRDataWrapper) JSON.deserialize(jsonToParse, GDPRDataWrapper.class);
    }
}

The above logic works when I run my Postman collection.
My test is where I'm having the issue with the Malformed JSON, even though the JSON request is exactly like my postman Body..
 
{  
    "email": "cc@gmail.com.mwtest",
    "maskedEmail": "unique2MaskedEmail@test.com",
    "firstName": "Delete",
    "middleName": "Delete",
    "lastName": "Delete",
    "firstNameLatin": "",
    "lastNameLatin": "",
    "mailingStreet": "Delete",
    "mailingCity": "Delete",
    "mailingPostalCode": "000000",
    "latinStreet": "",
    "latinCity": "Delete",
    "workPhone": "",
    "fax": "Delete",
    "mobilePhone": "Delete",
    "homePhone": "Delete",
    "otherPhone": "",
    "contactStatus": "Junk"
}

 
  • October 15, 2018
  • Like
  • 0
I would like direction on approaches to write Test Classes for UserManagement.obfuscateUser(uid) please.
My design includes a List of Portal User Ids to Obfuscate and System.enqueueJob to delete the related Contact Records.

Would it be accurate to build my test data for Portal User and related Contact in the Test Method and then issue the UserManagement.obfuscateUser(uid) method to obfuscate the Portal User and then Delete the related Contact?
Or
Would it be more accurate to use Mock Test Data instead?

Also my ideas for assertEquals would be to see if the Contact was deleted.   My knowledge of obfuscated Portal User is once they are obfuscated there is no way to access them, so I'm not certain what I would test with assertEquals for UserManagement.obfuscateUser(uid) method. 

I am developing with Test Driven Development process.
Thank you,
Donna
 
  • August 06, 2018
  • Like
  • 0
Advanced Formula question:

This formula works fine.  It checkes the country is CN, or JP, or KR.  Then it checks if any of the fields are blank.  This works fine:
IF(OR (ISPICKVAL ( MailingCountryCode , "CN"), 
ISPICKVAL(MailingCountryCode, "JP"), 
ISPICKVAL(MailingCountryCode, "KR")), 
(IF( ISBLANK( First_Name_Latin__c ) , 0, 1) + 
IF( ISBLANK( Last_Name_Latin__c ) , 0, 1) + IF( ISBLANK( Job_Title_Latin__c ) , 0, 1) + 
IF( ISBLANK( Department_Latin__c ) , 0, 1) + IF( ISBLANK( City_Latin__c ) , 0, 1) + IF( ISBLANK( Title ) , 0, 1) + 
IF( ISBLANK( Department ) , 0, 1) + IF( AND(ISBLANK(Phone),ISBLANK(MobilePhone)), 0, 1)) / 8, 1)


My question:
The Title, Department, and (Phone,MobilePhone ) fields need to be check regardless of the Country.
How can I modify the above Advanced Formula for this?     




The second condition is *not* dependent on the country, and only 3 fields are blank.

 
  • January 08, 2016
  • Like
  • 0
The page 'cancel' button has an onclick redirectpage.
Now, when I click the cancel button nothing happens.

<apex:commandbutton id="cmdCancel" value="{!$Label.Button_Cancel}" onclick="javascript: RedirectPage();"/>  

From Chrome: Uncaught TypeError: Cannot read property 'focus' of undefined  

From Mozilla Firefox: Instance 'backToSuggestionsLnk_500e0000006P9rj-18ed8e4b6c5de6fb024519190e0bef26150816a7a90zf04126a57303ae4d' is uninitialized and type was undefined"
  • October 19, 2015
  • Like
  • 0
I am sending the exact same attachment from Outlook or from Gmail.

My debug log files are showing my method that checks the body.length() of the TextAttachment yields different results.

In my code, I set the maxSize for my test toInteger =  5242880.

My method tests the body.length() < maxSize.

If the method correctly determines the attachment size is less than 5 MB, then I insert the attachment.

I have observed when my message and attachment is sent from  Outlook, the method 'incorrectly' determines the body.length < maxSize, and thus when the attachment tries to insert, Salesforce throws the error:

FATAL_ERROR|System.DmlException: Insert failed. First exception on row 0; first error: MAXIMUM_SIZE_OF_ATTACHMENT, attachment data exceeded maximum size: [BodyLength]


However, if I send the exact same attachment via GMail, the body.length < maxSize method correctly assesses the size to be 5 MB, and then goes into my proper code so it does not try to insert the attachment.
  • January 06, 2014
  • Like
  • 0
Advanced Formula question:

This formula works fine.  It checkes the country is CN, or JP, or KR.  Then it checks if any of the fields are blank.  This works fine:
IF(OR (ISPICKVAL ( MailingCountryCode , "CN"), 
ISPICKVAL(MailingCountryCode, "JP"), 
ISPICKVAL(MailingCountryCode, "KR")), 
(IF( ISBLANK( First_Name_Latin__c ) , 0, 1) + 
IF( ISBLANK( Last_Name_Latin__c ) , 0, 1) + IF( ISBLANK( Job_Title_Latin__c ) , 0, 1) + 
IF( ISBLANK( Department_Latin__c ) , 0, 1) + IF( ISBLANK( City_Latin__c ) , 0, 1) + IF( ISBLANK( Title ) , 0, 1) + 
IF( ISBLANK( Department ) , 0, 1) + IF( AND(ISBLANK(Phone),ISBLANK(MobilePhone)), 0, 1)) / 8, 1)


My question:
The Title, Department, and (Phone,MobilePhone ) fields need to be check regardless of the Country.
How can I modify the above Advanced Formula for this?     




The second condition is *not* dependent on the country, and only 3 fields are blank.

 
  • January 08, 2016
  • Like
  • 0