You need to sign in to do that
Don't have an account?
AmberTaylor
Test Class For a Get request
Hello,
Is someone available to help me with a test class ?
here is my code :
Here My starting TestClass :
Any help on this topic would be appreciated ! TY in advance
Amber
Is someone available to help me with a test class ?
here is my code :
public class TokenGenerator_CTL { private static final String CNONCE = String.valueOf(Crypto.getRandomLong()); //random number ; public String finalUrl { get; set; } public TokenGenerator_CTL() { //retrieve custom settings TokenSettings__c that contains credentials to access insideBoard Widget InsideboardSettings__c oIBSettings = [ SELECT PRIVATE_KEY__c, PUBLIC_KEY__c, SUBDOMAIN__c, WIDGET_PATH__c FROM TokenSettings__c LIMIT 1 ]; System.debug('oIBSettings Value = ' + oIBSettings); // Get current user ID String userId = UserInfo.getUserId(); // path to access the Widget and custom settings fields to retrieve keys String Widget_Path = '/query-auth-access/project/__/user/UID/one/time/only/to/widget/show'; String WP = oIBSettings.WIDGET_PATH__c; if (!string.isBlank(WP)){ Widget_Path = '/query-auth-access/project/__/user/UID/one/time/only/redirect/project/' + WP + '/to/widget/show' ; } System.debug('WidgetPATH Value = ' + Widget_Path); String Client_Private = oIBSettings.PRIVATE_KEY__c; String Client_Public = oIBSettings.PUBLIC_KEY__c; String Base_URL = 'https://' + oIBSettings.SUBDOMAIN__c + '.thisSite.com'; //this allows to change UID string with retrieved userId variable value String path = Widget_Path.replace('UID', getUserFederationId (userId)); // complete url String url = Base_URL + path; String strippedURL = Base_URL.removeStart('https://'); long timestamp = Datetime.now().getTime(); String cnonce = CNONCE + timestamp; String tmpParamString = 'cnonce=' + EncodingUtil.urlEncode (cnonce, 'UTF-8') + '&key=' + EncodingUtil.urlEncode(Client_Public, 'UTF-8') + '×tamp=' + timestamp/1000 ; String sign1 = 'GET\nhttps\n' + strippedURL + '\n'+ path.toLowerCase() + '\n' + tmpParamString; String paramString = tmpParamString + '&signature=' + EncodingUtil.urlEncode(getSignature(sign1, Client_Private), 'UTF-8'); finalUrl = url + '?' + paramString; System.debug('<======== Final URL Value = ' + finalURL + ' ========>'); } // function that returns signature, that will be use to get final URL private static String getSignature (String toSign, String privateKey) { Blob signatureBlob = Crypto.generateMac('HmacSHA256', Blob.valueOf(toSign) , Blob.valueOf(privateKey)); String signature = EncodingUtil.base64Encode(signatureBlob); System.debug('Signature value = ' + signature + ' ========>'); return signature; } // function that returns current user Federation Id private String getUserFederationId(String userId) { String federationId = [SELECT FederationIdentifier FROM User WHERE Id = :userId].FederationIdentifier; System.debug('<======== FederationId value = ' + federationId + ' ========>') ; return (federationId == null) ? '' : federationId; } }
Here My starting TestClass :
public with sharing class TokenGenerator_CTL_TST { @isTest public static void TestTokenGenerator() { Profile profileId = [SELECT Id FROM Profile WHERE Name = 'Standard User' LIMIT 1]; User usr = new User(LastName = 'Test', FirstName='EML', Alias = 'EmlT', Email = 'eml.test@gmail.com', Username = 'eml.test@gmail.com', ProfileId = profileId.id, TimeZoneSidKey = 'GMT', LanguageLocaleKey = 'en_US', EmailEncodingKey = 'UTF-8', LocaleSidKey = 'en_US', FederationIdentifier = 'IJK435' ); insert usr; TokenGenerator_CTL testTry = new TokenGenerator_CTL(); System.runAs(usr) { Test.startTest(); RestRequest req = new RestRequest(); RestResponse res = new RestResponse(); req.requestURI = testTry.finalURL; req.httpMethod = 'GET'; req.addHeader('Content-Type', 'application/json'); RestContext.request = req; RestContext.response = res; Test.stopTest(); } } }
Any help on this topic would be appreciated ! TY in advance
Amber
Though not exact, the below posts give a good insight into how to begin writing test class and how coverage can be improved and should help you get started.
https://salesforce.stackexchange.com/questions/244788/how-do-i-write-an-apex-unit-test
https://salesforce.stackexchange.com/questions/244794/how-do-i-increase-my-code-coverage-or-why-cant-i-cover-these-lines
Examples of test classes that cover Crypto.generateMac() and federationIdentifier :
https://salesforce.stackexchange.com/questions/193281/really-stuck-need-some-help-creating-apex-test-class-for-jit-class-auth-samut
https://salesforce.stackexchange.com/questions/325010/attempt-to-de-reference-a-null-object-email-service-test-class
If it helps, please mark this answer as best.Thank you