I am trying to cover my class Api_Http_DealerProfileEmail_1
using a test class which covers some other classes too,for some reason it keeps on giving me 0% code coverage for the class.
Here is the part of test class, I am using to cover my class.
Can anyone please tell me whats wrong here.
Thanks.
private static void insertAndChangeAddress() {
Integer r = Crypto.getRandomInteger();
String rs = String.valueOf(r);
String email = 'jb-'+rs+'@some.org';
String newEmail = 'jb-'+rs+'@other.org';
Id id;
Id testId;
User owner = [select Id, Name from User where Name = 'OEM Sales' limit 1];
id = Api_Http_DealerRegister_0.postNewCustomer('JoeBloe','GM','12345',email,'1212 some place'
,'Bouler','CO','12121','US','3033539876','Joe','Bloe');
System.assertNotEquals(null, id);
Account acct = [select Id, OwnerId, Dealer_Email__c from Account where Id = :id limit 1];
System.assertEquals(owner.Id, acct.OwnerId);
System.assertEquals(email, acct.Dealer_Email__c);
// Change the email address to a duplicate email address, and fail
Api_Result_0 result = Api_Http_DealerProfileEmail_1.setEmail(user1Email, newEmail);
System.assertEquals(false, result.success);
// Change the email address correctly now
result = Api_Http_DealerProfileEmail_1.setEmail(email, newEmail);
System.assertEquals(true, result.success);
// Make sure we can look up the account by the modified address
acct = [select Id, OwnerId, Dealer_Email__c from Account where Dealer_Email__c = :newEmail limit 1];
System.assertEquals(id, acct.Id);
// The same rename operation tried again should fail, because the original email no longer exists
result = Api_Http_DealerProfileEmail_1.setEmail(email, newEmail);
System.assertEquals(false, result.success);
}
private static void updateNoContactRecord() {
String email = '[email protected]';
RecordType customerType = [Select Id, Name, sObjectType
from RecordType
where sObjectType = 'Account' and Name='Company OEM'
limit 1
];
Account acct = new Account();
acct.RecordTypeId = customerType.Id;
acct.Name = 'Joe Shmoe';
acct.ShippingStreet = '2234 Mercury Way';
acct.ShippingCity = 'Santa Rosa';
acct.ShippingState = 'CA';
acct.ShippingCountry = 'US';
acct.ShippingPostalCode = '94928';
acct.Dealer_Email__c = email;
acct.Type = 'Customer';
insert acct;
String encEmail = EncodingUtil.urlEncode(email, 'utf-8');
System.RestContext.request = new RestRequest();
RestContext.request.requestURI = '/Api/DealerProfile_0/' + encEmail;
Api_Result_0 result = Api_Http_DealerProfile_0.setProfile(
'JaneShmoe',
'1121 Some Other Place','Denver', 'CO', '12345', 'US',
'7075551212', 'Jane','Shmoe'
);
Contact cont = [select Id, Email, Name, FirstName, LastName, MailingStreet, MailingCity, MailingState,
MailingCountry, MailingPostalCode from Contact where email = :email limit 1];
System.assertNotEquals(null, cont);
System.assertEquals(email, cont.Email);
System.assertEquals('Shmoe', cont.LastName);
}
private static void readdressNoContactRecord() {
String email = '[email protected]';
String newEmail = '[email protected]';
Contact cont;
RecordType customerType = [Select Id, Name, sObjectType
from RecordType
where sObjectType = 'Account' and Name='Subscriber(new)'
limit 1
];
Account acct = new Account();
acct.RecordTypeId = customerType.Id;
acct.Name = 'Pliny A Younger';
acct.ShippingStreet = '2234 Mercury Way';
acct.ShippingCity = 'Santa Rosa';
acct.ShippingState = 'CA';
acct.ShippingCountry = 'US';
acct.ShippingPostalCode = '94928';
acct.Dealer_Email__c = email;
acct.Type = 'Customer';
insert acct;
Api_Result_0 result = Api_Http_DealerProfileEmail_1.setEmail(email, newEmail);
System.assertEquals(true, result.success);
// make sure we can't retrieve the original contact
try {
cont = [select Id from Contact where email = :email limit 1];
System.assert(false, 'We should not be able to retrieve the contact by the old email.');
} catch (QueryException qex) {
}
cont = [select Id, Email, Name, FirstName, LastName, MailingStreet, MailingCity, MailingState,
MailingCountry, MailingPostalCode from Contact where email = :newEmail limit 1];
System.assertNotEquals(null, cont);
System.assertEquals(newEmail, cont.Email);
System.assertEquals('Younger', cont.LastName);
System.assertEquals(acct.ShippingStreet, cont.MailingStreet);
System.assertEquals(acct.ShippingPostalCode, cont.MailingPostalCode);
}
And my class is :
@RestResource(urlMapping='/Api/DealerProfileEmail_1/*')
global with sharing class Api_Http_DealerProfileEmail_1 {
@HttpPost
global static Api_Result_0 setEmail(
String oldEmail,
String newEmail
) {
Api_Result_0 result = new Api_Result_0();
try {
Api_DealerProfile_0 profile = new Api_DealerProfile_0(oldEmail);
profile.changeEmail(newEmail);
result.success = true;
} catch(Exception ex) {
result.success = false;
result.error = ex.getMessage();
}
return result;
}
}