You need to sign in to do that
Don't have an account?
vicky rathi
testclass coverage is 88% but test class is fail how to pass the test class
Here is my class
public with sharing class XeroOAuthUtility {
public static HttpRequest signRequest(HttpRequest req, String consumerKey) {
String nonce = String.valueOf(Crypto.getRandomLong());
String timestamp = String.valueOf(DateTime.now().getTime() / 1000);
Map<String,String> parameters = new Map<String,String>();
parameters.put('oauth_version', '1.0');
parameters.put('oauth_nonce', nonce);
parameters.put('oauth_timestamp', timestamp);
// RSA-SHA1 is the signature method used for the Xero authentication
parameters.put('oauth_signature_method','RSA-SHA1');
parameters.put('oauth_consumer_key', consumerKey);
// As no token is requested, set the token as the consumer key. The Xero API doesn't use tokens, but rather uses 1-legged OAuth to make requests
parameters.put('oauth_token', consumerKey);
// Generate signature
String signature = generateSignature(req, consumerKey, parameters);
// Now take the generated signature add to the headers
req.setHeader('Authorization', generateHeader(signature, parameters));
return req;
}
private static String generateHeader(String signature, Map<String,String> parameters) {
// Add OAuth value to the start of the header
String header = 'OAuth ';
// Itererate of all paramneters
for (String key : parameters.keySet()) {
// Add each paramaeter to the header string
header = header + key + '="' +parameters.get(key)+ '", ';
}
return header + 'oauth_signature="' + EncodingUtil.urlEncode(signature, 'UTF-8') + '"';
}
private static String generateSignature(HttpRequest req, String consumerSecret, Map<String,String> parameters) {
String baseString = createBaseString(req, parameters);
Blob signatureBlob = System.Crypto.signWithCertificate('RSA-SHA1', Blob.valueOf(baseString), 'XeroCertificate');
return EncodingUtil.base64Encode(signatureBlob);
}
private static String createBaseString(HttpRequest req, Map<String,String> parameters) {
Map<String,String> p = parameters.clone();
if (req.getMethod().equalsIgnoreCase('post') && req.getBody() != null &&
req.getHeader('Content-Type') == 'application/x-www-form-urlencoded'
) {
p.putAll(getUrlParams(req.getBody()));
}
String host = req.getEndpoint();
Integer n = host.indexOf('?');
if (n > -1) {
p.putAll(getUrlParams(host.substring(n + 1)));
host = host.substring(0,n);
}
List<String> keys = new List<String>();
keys.addAll(p.keySet());
keys.sort();
String urlString = keys.get(0) + '=' + p.get(keys.get(0));
for (Integer i = 1; i < keys.size(); i++) {
urlString = urlString + '&' + keys.get(i) + '=' + p.get(keys.get(i));
}
return req.getMethod().toUpperCase() + '&' + EncodingUtil.urlEncode(host, 'UTF-8') + '&' + EncodingUtil.urlEncode(urlString, 'UTF-8');
}
private static Map<String,String> getUrlParams(String value) {
Map<String,String> res = new Map<String,String>();
if (value == null || value=='') {
return res;
}
for (String s : value.split('&')) {
List<String> kv = s.split('=');
if (kv.size() > 1) {
String encName = EncodingUtil.urlEncode(EncodingUtil.urlDecode(kv[0], 'UTF-8'), 'UTF-8').replace('+','%20');
String encValue = EncodingUtil.urlEncode(EncodingUtil.urlDecode(kv[1], 'UTF-8'), 'UTF-8').replace('+','%20');
res.put(encName,encValue);
}
}
return res;
}
}
my test class is
@isTest(SeeAllData=true)
public with sharing class XeroOAuthUtilityTest{
Public Static TestMethod Void TestOAuth(){
Test.StartTest();
Xero_Settings__c xeroseting = new Xero_Settings__c();
xeroseting.name = 'testName';
xeroseting.Consumer_Key__c = 'testConsumer';
xeroseting.Endpoint__c = 'www.xero.com';
insert xeroseting;
/* SingleRequestMock fakeResponse = new SingleRequestMock(200, 'Complete', '[{"Name": "sForceTest1"}]', null);
Test.setMock(HttpCalloutMock.class, fakeResponse);
XeroCalloutUtility.executeCallout(' request.setMethod','request.setEndpoint','request.setBody'); */
HttpRequest request = new HttpRequest();
request.setMethod('post');
request.setHeader('Content-Type', 'application/x-www-form-urlencoded');
request.setBody('abc');
//HttpResponse response = new HTTP().send(request);
XeroOAuthUtility.signRequest(request, 'testConsumer');
Test.StopTest();
}
how to pass the test class
public with sharing class XeroOAuthUtility {
public static HttpRequest signRequest(HttpRequest req, String consumerKey) {
String nonce = String.valueOf(Crypto.getRandomLong());
String timestamp = String.valueOf(DateTime.now().getTime() / 1000);
Map<String,String> parameters = new Map<String,String>();
parameters.put('oauth_version', '1.0');
parameters.put('oauth_nonce', nonce);
parameters.put('oauth_timestamp', timestamp);
// RSA-SHA1 is the signature method used for the Xero authentication
parameters.put('oauth_signature_method','RSA-SHA1');
parameters.put('oauth_consumer_key', consumerKey);
// As no token is requested, set the token as the consumer key. The Xero API doesn't use tokens, but rather uses 1-legged OAuth to make requests
parameters.put('oauth_token', consumerKey);
// Generate signature
String signature = generateSignature(req, consumerKey, parameters);
// Now take the generated signature add to the headers
req.setHeader('Authorization', generateHeader(signature, parameters));
return req;
}
private static String generateHeader(String signature, Map<String,String> parameters) {
// Add OAuth value to the start of the header
String header = 'OAuth ';
// Itererate of all paramneters
for (String key : parameters.keySet()) {
// Add each paramaeter to the header string
header = header + key + '="' +parameters.get(key)+ '", ';
}
return header + 'oauth_signature="' + EncodingUtil.urlEncode(signature, 'UTF-8') + '"';
}
private static String generateSignature(HttpRequest req, String consumerSecret, Map<String,String> parameters) {
String baseString = createBaseString(req, parameters);
Blob signatureBlob = System.Crypto.signWithCertificate('RSA-SHA1', Blob.valueOf(baseString), 'XeroCertificate');
return EncodingUtil.base64Encode(signatureBlob);
}
private static String createBaseString(HttpRequest req, Map<String,String> parameters) {
Map<String,String> p = parameters.clone();
if (req.getMethod().equalsIgnoreCase('post') && req.getBody() != null &&
req.getHeader('Content-Type') == 'application/x-www-form-urlencoded'
) {
p.putAll(getUrlParams(req.getBody()));
}
String host = req.getEndpoint();
Integer n = host.indexOf('?');
if (n > -1) {
p.putAll(getUrlParams(host.substring(n + 1)));
host = host.substring(0,n);
}
List<String> keys = new List<String>();
keys.addAll(p.keySet());
keys.sort();
String urlString = keys.get(0) + '=' + p.get(keys.get(0));
for (Integer i = 1; i < keys.size(); i++) {
urlString = urlString + '&' + keys.get(i) + '=' + p.get(keys.get(i));
}
return req.getMethod().toUpperCase() + '&' + EncodingUtil.urlEncode(host, 'UTF-8') + '&' + EncodingUtil.urlEncode(urlString, 'UTF-8');
}
private static Map<String,String> getUrlParams(String value) {
Map<String,String> res = new Map<String,String>();
if (value == null || value=='') {
return res;
}
for (String s : value.split('&')) {
List<String> kv = s.split('=');
if (kv.size() > 1) {
String encName = EncodingUtil.urlEncode(EncodingUtil.urlDecode(kv[0], 'UTF-8'), 'UTF-8').replace('+','%20');
String encValue = EncodingUtil.urlEncode(EncodingUtil.urlDecode(kv[1], 'UTF-8'), 'UTF-8').replace('+','%20');
res.put(encName,encValue);
}
}
return res;
}
}
my test class is
@isTest(SeeAllData=true)
public with sharing class XeroOAuthUtilityTest{
Public Static TestMethod Void TestOAuth(){
Test.StartTest();
Xero_Settings__c xeroseting = new Xero_Settings__c();
xeroseting.name = 'testName';
xeroseting.Consumer_Key__c = 'testConsumer';
xeroseting.Endpoint__c = 'www.xero.com';
insert xeroseting;
/* SingleRequestMock fakeResponse = new SingleRequestMock(200, 'Complete', '[{"Name": "sForceTest1"}]', null);
Test.setMock(HttpCalloutMock.class, fakeResponse);
XeroCalloutUtility.executeCallout(' request.setMethod','request.setEndpoint','request.setBody'); */
HttpRequest request = new HttpRequest();
request.setMethod('post');
request.setHeader('Content-Type', 'application/x-www-form-urlencoded');
request.setBody('abc');
//HttpResponse response = new HTTP().send(request);
XeroOAuthUtility.signRequest(request, 'testConsumer');
Test.StopTest();
}
how to pass the test class
Which line you are getting Error if my my findings are correct you will get error on line 83: if (n > -1) on this line Attempt to de-reference null object.
for that you have to add End poinf in your test class
kinldy use updated code:
If that is not the case .. kinldy post the error you are getting..
Hope this will help you
Mark Best ASNWER if its works for you.
Thanks
karthik