Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*******************************************************************************************************************
- public without sharing class Telemetry {
- @TestVisible
- private FeatureManagementWrapper featureManager = new FeatureManagementWrapper();
- public void process() {
- featureManager.setPackageIntegerValue(
- 'NumberOfClosedOpportunities',
- [SELECT Id FROM Opportunity WHERE IsClosed = true].size()
- );
- featureManager.setPackageBooleanValue(
- 'HasOrders',
- [SELECT Id FROM Order LIMIT 1].size() > 0
- );
- }
- }
- /*******************************************************************************************************************
- public with sharing class FeatureManagementWrapper {
- /**
- * @description Wrapper for System.FeatureManagement.setPackageBooleanValue
- */
- public void setPackageBooleanValue(String apiName, Boolean value) {
- System.FeatureManagement.setPackageBooleanValue(apiName, value);
- }
- /**
- * @description Wrapper for System.FeatureManagement.setPackageIntegerValue
- */
- public void setPackageIntegerValue(String apiName, Integer value) {
- System.FeatureManagement.setPackageIntegerValue(apiName, value);
- }
- }
- //TESTS
- /*******************************************************************************************************************
- @IsTest
- private class Telemetry_TEST {
- @IsTest
- private static void shouldCaptureTelemetry() {
- final Integer NUM_OPPS = 10;
- TestFixtures.insertClosedOpportunities(NUM_OPPS);
- FeatureManagementWrapperMock mock = new FeatureManagementWrapperMock();
- Telemetry telemetryService = new Telemetry();
- telemetryService.featureManager =
- (FeatureManagementWrapper) Test.createStub(FeatureManagementWrapper.class, featureManagementMock);
- Test.startTest();
- telemetryService.process();
- Test.stopTest();
- System.assert(featureManagementMock.packageIntegerValuesByName.containsKey('NumberOfClosedOpportunities'),
- 'setPackageIntegerValue should be called with the feature "NumberOfClosedOpportunities"');
- System.assertEquals(
- NUM_OPPS,
- featureManagementMock.packageIntegerValuesByName.get('NumberOfClosedOpportunities'),
- 'The feature "NumberOfClosedOpportunities" should be set with the correct number of opps'
- );
- System.assert(featureManagementMock.setPackageBooleanValue.containsKey('HasOrders'),
- 'setPackageBooleanValue should be called with the feature "HasOrders"');
- System.assertEquals(
- false,
- featureManagementMock.setPackageBooleanValue.get('HasOrders'),
- 'The feature "HasOrders" should be set to false if there are no Orders'
- );
- }
- private class FeatureManagementWrapperMock implements System.StubProvider {
- private Map<String, Boolean> packageBooleanValuesByName = new Map<String, Boolean>();
- private Map<String, Integer> packageIntegerValuesByName = new Map<String, Integer>();
- private Object handleMethodCall(
- Object stubbedObject,
- String stubbedMethodName,
- Type returnType,
- List<Type> listOfParamTypes,
- List<String> listOfParamNames,
- List<Object> listOfArgs
- ) {
- switch on stubbedMethodName {
- when 'setPackageBooleanValue' {
- packageBooleanValuesByName.put((String) listOfArgs[0], (Boolean) listOfArgs[1]);
- }
- when 'setPackageIntegerValue' {
- packageIntegerValuesByName.put((String) listOfArgs[0], (Integer) listOfArgs[1]);
- }
- }
- return null;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement