/force-app/main/schema/classes/user/UserMapper_TEST.cls

https://github.com/SalesforceFoundation/HEDAP · Visual Basic for Applications · 179 lines · 154 code · 18 blank · 7 comment · 3 complexity · d736691cbff4951010671537ebe84606 MD5 · raw file

  1. /*
  2. Copyright (c) 2021, Salesforce.org
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are met:
  6. * Redistributions of source code must retain the above copyright
  7. notice, this List of conditions and the following disclaimer.
  8. * Redistributions in binary form must reproduce the above copyright
  9. notice, this List of conditions and the following disclaimer in the
  10. documentation and/or other materials provided with the distribution.
  11. * Neither the name of Salesforce.org nor the names of
  12. its contributors may be used to endorse or promote products derived
  13. from this software without specific prior written permission.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  15. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  16. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  17. FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  18. COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  19. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  20. BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  21. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  23. LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  24. ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  25. POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. /**
  28. * @author Salesforce.org
  29. * @date 2021
  30. * @group
  31. * @group-content ../../ApexDocContent/
  32. * @description Test class for UserMapper class.
  33. */
  34. @isTest
  35. private with sharing class UserMapper_TEST {
  36. /**************************************************************************************************************************
  37. * @description Test method to verify that the getInstance method returns a new instance of the
  38. * UserMapper class when one does not already exist.
  39. ***************************************************************************************************************************/
  40. @isTest
  41. private static void getInstanceNew() {
  42. Test.startTest();
  43. UserMapper mapperInstance = UserMapper.getInstance();
  44. Test.stopTest();
  45. System.assertEquals(
  46. UserMapper.instance,
  47. mapperInstance,
  48. 'Instance of mapper class returned should match static instance variable.'
  49. );
  50. System.assertEquals(true, mapperInstance != null, 'Instance of mapper class should not be null.');
  51. }
  52. /**************************************************************************************************************************
  53. * @description Test method to verify that the getInstance method returns the existing instance of the
  54. * UserMapper class when one already exists.
  55. ***************************************************************************************************************************/
  56. @isTest
  57. private static void getInstanceExisting() {
  58. UserMapper mapperInstance1 = UserMapper.getInstance();
  59. Test.startTest();
  60. UserMapper mapperInstance2 = UserMapper.getInstance();
  61. Test.stopTest();
  62. System.assertEquals(
  63. UserMapper.instance,
  64. mapperInstance1,
  65. 'Instance of mapper class returned should match static instance variable.'
  66. );
  67. System.assertEquals(
  68. mapperInstance1,
  69. mapperInstance2,
  70. 'Subsequent retrievals of mapper class instance should return existing instance.'
  71. );
  72. System.assertEquals(true, mapperInstance2 != null, 'Instance of mapper class should not be null.');
  73. }
  74. /**************************************************************************************************************************
  75. * @description Test method to verify that a user model is retrieved by an Id
  76. ***************************************************************************************************************************/
  77. @isTest
  78. private static void getUserModelById() {
  79. UserMapper userMapperInstance = UserMapper.getInstance();
  80. Id userToTestId = UTIL_UnitTestData_TEST.createNewUserForTests(System.now().getTime() + '@testerson.com').id;
  81. User userToTest = [SELECT Id, Name, isActive, Email, SmallPhotoUrl FROM User WHERE Id = :userToTestId];
  82. Test.startTest();
  83. UserModel userModel = userMapperInstance.getUserModelById(userToTest.id);
  84. Test.stopTest();
  85. System.assertEquals(userToTest.Id, userModel.Id, 'User Model id should match user id.');
  86. System.assertEquals(userToTest.Name, userModel.name, 'User Model name should match user name.');
  87. System.assertEquals(
  88. userToTest.IsActive,
  89. userModel.isActive,
  90. 'User Model is active should match user is active.'
  91. );
  92. System.assertEquals(userToTest.Email, userModel.email, 'User Model email should match user email.');
  93. System.assertEquals(
  94. userToTest.SmallPhotoUrl,
  95. userModel.smallPhotoUrl,
  96. 'User Model small photo url should match user small photo url.'
  97. );
  98. }
  99. /**************************************************************************************************************************
  100. * @description Test method to verify that getting user models like a name is functional
  101. ***************************************************************************************************************************/
  102. @isTest
  103. private static void getUserModelsLikeName() {
  104. UserMapper userMapperInstance = UserMapper.getInstance();
  105. List<Id> userToTestIdList = new List<Id>();
  106. userToTestIdList.add(
  107. UTIL_UnitTestData_TEST.createNewUserForTests(System.now().getTime() + '@testerson.com').id
  108. );
  109. userToTestIdList.add(
  110. UTIL_UnitTestData_TEST.createNewUserForTests(System.now().getTime() + '@testerson.com2').id
  111. );
  112. Map<Id, User> usersByIds = new Map<Id, User>(
  113. [SELECT Id, Name, isActive, Email, SmallPhotoUrl FROM User WHERE Id IN :userToTestIdList]
  114. );
  115. Test.startTest();
  116. List<UserModel> userModelList = userMapperInstance.getUserModelsLikeName('Smith', 1);
  117. Test.stopTest();
  118. System.assertEquals(1, userModelList.size(), 'Only 1 user model should be retrieved.');
  119. System.assertNotEquals(
  120. null,
  121. usersByIds.get(userModelList[0].id),
  122. 'The user model retrieved should be within the map of those created.'
  123. );
  124. }
  125. /**************************************************************************************************************************
  126. * @description Test method to verify that getting user models like a name is functional
  127. ***************************************************************************************************************************/
  128. @isTest
  129. private static void getUserModelsLikeNameNoneRetrieved() {
  130. UserMapper userMapperInstance = UserMapper.getInstance();
  131. List<Id> userToTestIdList = new List<Id>();
  132. userToTestIdList.add(
  133. UTIL_UnitTestData_TEST.createNewUserForTests(System.now().getTime() + '@testerson.com').id
  134. );
  135. userToTestIdList.add(
  136. UTIL_UnitTestData_TEST.createNewUserForTests(System.now().getTime() + '@testerson.com2').id
  137. );
  138. Map<Id, User> usersByIds = new Map<Id, User>(
  139. [SELECT Id, Name, isActive, Email, SmallPhotoUrl FROM User WHERE Id IN :userToTestIdList]
  140. );
  141. Test.startTest();
  142. List<UserModel> userModelList = userMapperInstance.getUserModelsLikeName('Not Smith', 1);
  143. Test.stopTest();
  144. System.assert(userModelList.isEmpty(), 'No user models should be retrieved.');
  145. }
  146. /**************************************************************************************************************************
  147. * @description Test method to verify that getting the model label is functional
  148. ***************************************************************************************************************************/
  149. @isTest
  150. private static void getModelObjectLabel() {
  151. UserMapper userMapperInstance = UserMapper.getInstance();
  152. Test.startTest();
  153. String userObjectLabel = userMapperInstance.getModelObjectLabel();
  154. Test.stopTest();
  155. System.assertEquals(
  156. UTIL_Describe.getObjectDescribe('User').getLabel(),
  157. userObjectLabel,
  158. 'Label retrieved should be that of the user object label'
  159. );
  160. }
  161. }