function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Greg CooganGreg Coogan 

Why is Salesforce Shield's Transaction Security automatically creating Policy Condition Apex Classes without Test Classes?

Please help! We have Salesforce Shield. The Transaction Security feature automatically creates two Apex Classes that are used for Transaction Security Policies. They are:
  1. ConcurrentSessionsPolicyCondition
  2. DataLoaderLeadExportCondition
These are two of the three Apex Classes I have in a new org. I am trying to deploy the test class of the third Apex Class, which is a controller I wrote, but forget to deploy the Test Class with it.

Those two Apex Classes created by Salesforce Shield do not have Test Classes created with them. Why is this? It results in a very low code coverage and I cannot deploy to our Production org as a result.
Best Answer chosen by Greg Coogan
Gordon EngelGordon Engel
An internal Salesforce Bug already exists for this, but it is very low priority and unlikely to get fixed.  Surprisingly, there are no Cases  where any other user has complained about this issue.  Perhaps most orgs that use Salesforce Shield have a lot of Apex classes such that the addition of these two classes don't affect code coverage significantly?

All Answers

Gordon EngelGordon Engel
An internal Salesforce Bug already exists for this, but it is very low priority and unlikely to get fixed.  Surprisingly, there are no Cases  where any other user has complained about this issue.  Perhaps most orgs that use Salesforce Shield have a lot of Apex classes such that the addition of these two classes don't affect code coverage significantly?
This was selected as the best answer
Greg CooganGreg Coogan
I was able to deploy by only running "specific" Apex tests, instead of the default of all tests. However, its clearly a bug that lowers my code coverage. I now have 100% for all my development, but these generated classes have none.
Greg CooganGreg Coogan
Gordon,
Is there any update on this bug? We purchases Event Monitoring for another instance and before I enable Transaction Security, I'd like to know if this is resolved.
Gordon EngelGordon Engel
Sorry for my slow response - I had to search for the Bug again.  For future reference:  W-2656173

I posted a link to this developer forum entry and asked for an update in the Bug.  There hasn't been any change since my last post here.

If you can file a case and ask your customer support agent to follow up, it would at the very least make it clear that there is someone out there waiting for this to be fixed.  Right now the Bug has not cases associated with it.
Greg CooganGreg Coogan
I got Support to create a known issue.

https://success.salesforce.com/issues_view?id=a1p3A0000001CoZQAU
Greg CooganGreg Coogan
I was able to create a Test class for "DataLoaderLeadExportCondition" which Salesforce might want to consider using. It gets 100% code coverage. As for "ConcurrentSessionsPolicyCondition," this may not be possible to get 100% coverage on since we cannot create records on the AuthSession object. We can only read and delete.
 
@isTest
public class DataLoaderLeadExportConditionTest {
    public static testMethod void testDataLoaderLeadExportTrue() {
        /* Create a map for the event we’re going to build. */
        /* Insert relative info that will meet criteria of the policy */
        Map<String, String> eventData = new Map<String, String>();
        eventData.put('NumberOfRecords', '2001');
        eventData.put('ExecutionTime', '1001');
        eventData.put('EntityName', 'Lead');

        /* We’re not going to cause a real event in the org.
        Instead, we’re going to create a Transaction Security
        event object and “feed” it to the Policy Engine. */
        TxnSecurity.Event e = new TxnSecurity.Event(
            UserInfo.getOrganizationId(),			/* organizationId */
            UserInfo.getUserId(),					/* userId */
            'Lead',									/* entityName */
            'DataExport',							/* action */
            'Lead',									/* resourceType */
            '01pR00000009D2H',						/* entityId */
            Datetime.newInstance(2018, 8, 16),		/* timeStamp */
            eventData);								/* data - Map with info about this event. */
        DataLoaderLeadExportCondition condition = new DataLoaderLeadExportCondition();
        /* Assert that the condition is triggered by evaluating
        the event e. The Transaction Security PolicyCondition
        interface returns True if the policy is triggered. */
        System.assertEquals(true, condition.evaluate(e));
    }
    public static testMethod void testDataLoaderLeadExportFalse() {
        /* Create a map for the event we’re going to build. */
        /* Insert relative info that will meet criteria of the policy */
        Map<String, String> eventData = new Map<String, String>();
        eventData.put('NumberOfRecords', '2000');
        eventData.put('ExecutionTime', '1000');
        eventData.put('EntityName', 'Lead');

        /* We’re not going to cause a real event in the org.
        Instead, we’re going to create a Transaction Security
        event object and “feed” it to the Policy Engine. */
        TxnSecurity.Event e = new TxnSecurity.Event(
            UserInfo.getOrganizationId(),			/* organizationId */
            UserInfo.getUserId(),					/* userId */
            'Lead',								/* entityName */
            'DataExport',						/* action */
            'Lead',								/* resourceType */
            '01pR00000009D2H',					/* entityId */
            Datetime.newInstance(2018, 8, 16),		/* timeStamp */
            eventData);							/* data - Map with info about this event. */
        DataLoaderLeadExportCondition condition = new DataLoaderLeadExportCondition();
        /* Assert that the condition is triggered by evaluating
        the event e. The Transaction Security PolicyCondition
        interface returns True if the policy is triggered.
        We expect it to return false due to NumberOfRecords and ExecutionTime being below threshold.*/
        System.assertEquals(false, condition.evaluate(e));
    }
}