SHARE
TWEET

Untitled

a guest Jul 21st, 2019 78 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*******************************************************************************************************************
  2. public without sharing class Telemetry {
  3.     @TestVisible
  4.     private FeatureManagementWrapper featureManager = new FeatureManagementWrapper();
  5.    
  6.     public void process() {
  7.         featureManager.setPackageIntegerValue(
  8.                 'NumberOfClosedOpportunities',
  9.             [SELECT Id FROM Opportunity WHERE IsClosed = true].size()
  10.         );
  11.        
  12.         featureManager.setPackageBooleanValue(
  13.             'HasOrders',
  14.             [SELECT Id FROM Order LIMIT 1].size() > 0
  15.         );
  16.     }
  17. }
  18. /*******************************************************************************************************************
  19. public with sharing class FeatureManagementWrapper {
  20.     /**
  21.      * @description Wrapper for System.FeatureManagement.setPackageBooleanValue
  22.      */
  23.     public void setPackageBooleanValue(String apiName, Boolean value) {
  24.         System.FeatureManagement.setPackageBooleanValue(apiName, value);
  25.     }
  26.  
  27.     /**
  28.      * @description Wrapper for System.FeatureManagement.setPackageIntegerValue
  29.      */
  30.     public void setPackageIntegerValue(String apiName, Integer value) {
  31.         System.FeatureManagement.setPackageIntegerValue(apiName, value);
  32.     }
  33. }
  34.  
  35. //TESTS
  36. /*******************************************************************************************************************
  37.  
  38. @IsTest
  39. private class Telemetry_TEST {
  40.     @IsTest
  41.     private static void shouldCaptureTelemetry() {
  42.         final Integer NUM_OPPS = 10;
  43.         TestFixtures.insertClosedOpportunities(NUM_OPPS);
  44.  
  45.         FeatureManagementWrapperMock mock = new FeatureManagementWrapperMock();
  46.  
  47.         Telemetry telemetryService = new Telemetry();
  48.         telemetryService.featureManager =
  49.             (FeatureManagementWrapper) Test.createStub(FeatureManagementWrapper.class, featureManagementMock);
  50.  
  51.         Test.startTest();
  52.         telemetryService.process();
  53.         Test.stopTest();
  54.  
  55.         System.assert(featureManagementMock.packageIntegerValuesByName.containsKey('NumberOfClosedOpportunities'),
  56.             'setPackageIntegerValue should be called with the feature "NumberOfClosedOpportunities"');
  57.         System.assertEquals(
  58.             NUM_OPPS,
  59.             featureManagementMock.packageIntegerValuesByName.get('NumberOfClosedOpportunities'),
  60.             'The feature "NumberOfClosedOpportunities" should be set with the correct number of opps'
  61.         );
  62.  
  63.         System.assert(featureManagementMock.setPackageBooleanValue.containsKey('HasOrders'),
  64.             'setPackageBooleanValue should be called with the feature "HasOrders"');
  65.         System.assertEquals(
  66.             false,
  67.             featureManagementMock.setPackageBooleanValue.get('HasOrders'),
  68.             'The feature "HasOrders" should be set to false if there are no Orders'
  69.         );
  70.     }
  71.    
  72.     private class FeatureManagementWrapperMock implements System.StubProvider {
  73.         private Map<String, Boolean> packageBooleanValuesByName = new Map<String, Boolean>();
  74.         private Map<String, Integer> packageIntegerValuesByName = new Map<String, Integer>();
  75.  
  76.         private Object handleMethodCall(
  77.             Object stubbedObject,
  78.             String stubbedMethodName,
  79.             Type returnType,
  80.             List<Type> listOfParamTypes,
  81.             List<String> listOfParamNames,
  82.             List<Object> listOfArgs
  83.         ) {
  84.             switch on stubbedMethodName {
  85.                 when 'setPackageBooleanValue' {
  86.                     packageBooleanValuesByName.put((String) listOfArgs[0], (Boolean) listOfArgs[1]);
  87.                 }
  88.  
  89.                 when 'setPackageIntegerValue' {
  90.                     packageIntegerValuesByName.put((String) listOfArgs[0], (Integer) listOfArgs[1]);
  91.                 }
  92.             }
  93.             return null;
  94.         }
  95.     }
  96. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top