@isTest
private class Test_CurrencyConversionFeature {
// Helper: Expected conversion rates
private static final Decimal USD_TO_EUR_RATE = 0.85;
private static final Decimal EUR_TO_USD_RATE = 1.18;
// Test for zero input amount handling
@isTest static void testZeroInputHandling() {
CurrencyConverterVFCtrl ctrl = new CurrencyConverterVFCtrl();
[Link] = 0;
[Link]();
[Link](0, [Link], 'Zero input should result in 0
output.');
[Link]();
[Link](0, [Link], 'Zero input should result in 0
output.');
// Ensure no record with zero input is created in the database
List<Currency_Conversion__c> records = [
SELECT Id FROM Currency_Conversion__c WHERE Input_Amount__c = 0
];
[Link](0, [Link](), 'No record should be created for
zero input.');
}
// Test conversion controller with fresh data
@isTest static void testConversionControllerBehavior() {
insert new Currency_Conversion__c(
Input_Amount__c = 100,
Conversion_Type__c = 'USD to EUR',
Converted_Amount__c = 85
);
CurrencyConverterVFCtrl ctrl = new CurrencyConverterVFCtrl();
[Link]();
[Link] = 100;
[Link]();
[Link](85, [Link], 'Cached USD to EUR failed.');
[Link] = 85;
[Link]();
[Link](85 * EUR_TO_USD_RATE, [Link], 'Fresh EUR to
USD failed.');
[Link] = 200;
[Link]();
[Link](200 * USD_TO_EUR_RATE, [Link], 'Fresh USD to
EUR failed.');
[Link]();
}
// Test validation for duplicate entries
@isTest static void testDuplicateConversionValidation() {
Currency_Conversion__c record = new Currency_Conversion__c(
Input_Amount__c = 100,
Conversion_Type__c = 'USD to EUR',
Converted_Amount__c = 85
);
insert record;
Currency_Conversion__c duplicate = [Link](false);
[Link]();
try {
insert duplicate;
[Link](false, 'Expected duplicate insertion to fail.');
} catch (DmlException e) {
[Link]([Link]().contains('Duplicate combination of Input
Amount and Conversion Type is not allowed.'));
}
[Link]();
}
// Test helper method logic
@isTest static void testHelperMethods() {
[Link](85, [Link](100), 'USD to EUR
helper failed.');
[Link](100.3, [Link](85), 'EUR to
USD helper failed.');
}
// Test negative input validation
/* @isTest static void testNegativeInputValidation() {
Currency_Conversion__c invalid = new Currency_Conversion__c(
Input_Amount__c = -100,
Conversion_Type__c = 'USD to EUR',
Converted_Amount__c = 0
);
[Link]();
try {
insert invalid;
[Link](false, 'Expected failure for negative input.');
} catch (DmlException e) {
[Link]([Link]().contains('Input Amount must be greater
than 0.'));
}
[Link]();
}*/
// Test valid record insertion in the object
@isTest static void testValidConversionInsert() {
Currency_Conversion__c conversion = new Currency_Conversion__c(
Input_Amount__c = 200,
Conversion_Type__c = 'USD to EUR',
Converted_Amount__c = 170
);
[Link]();
insert conversion;
[Link]();
Currency_Conversion__c fetched = [SELECT Converted_Amount__c FROM
Currency_Conversion__c WHERE Id = :[Link]];
[Link](170, fetched.Converted_Amount__c, 'Converted amount
should match.');
}
// Test reset functionality in controller
@isTest static void testResetControllerFields() {
CurrencyConverterVFCtrl ctrl = new CurrencyConverterVFCtrl();
[Link] = 100;
[Link] = 85;
[Link]();
[Link](null, [Link], 'inputAmount should be
reset.');
[Link](0, [Link], 'outputAmount should be reset.');
}
// Test null input validation from controller
@isTest static void testValidateNullInput() {
CurrencyConverterVFCtrl ctrl = new CurrencyConverterVFCtrl();
[Link]();
List<[Link]> messages = [Link]();
[Link](1, [Link](), 'Expected one error message.');
[Link]('Please enter an amount to convert.',
messages[0].getSummary(), 'Error message mismatch.');
}
}
/*public class CurrencyConverterVFCtrl {
// User input and output variables
public Decimal inputAmount { get; set; }
public Decimal outputAmount { get; set; }
// Validate input and add errors if needed
private Boolean validateInput() {
// Check for null value
if (inputAmount == null) {
[Link] msg = new [Link](
[Link], 'Please enter an amount to convert.');
[Link](msg);
return false;
}
// Check for zero or negative value
if (inputAmount <= 0) {
[Link] msg = new [Link](
[Link], 'Amount must be greater than zero.');
[Link](msg);
return false;
}
return true;
}
// Main logic to fetch or calculate the converted amount
private Decimal getConvertedAmount(Decimal input, String type) {
List<Currency_Conversion__c> existingRecords = [
SELECT Input_Amount__c, Conversion_Type__c, Converted_Amount__c
FROM Currency_Conversion__c
WHERE Input_Amount__c = :input AND Conversion_Type__c = :type
LIMIT 1
];
Decimal result;
if (![Link]()) {
result = existingRecords[0].Converted_Amount__c;
} else {
result = (type == 'USD to EUR') ?
[Link](input) :
[Link](input);
// Trigger handles inserting new record
Currency_Conversion__c newConversion = new Currency_Conversion__c(
Input_Amount__c = input,
Conversion_Type__c = type,
Converted_Amount__c = result
);
insert newConversion;
}
return result;
}
// Called when "USD to EUR" button is clicked
public void convertUSDtoEUR() {
if (validateInput()) {
outputAmount = getConvertedAmount(inputAmount, 'USD to EUR');
} else {
outputAmount = 0; // Important: set to 0 if input is invalid
}
}
// Called when "EUR to USD" button is clicked
public void convertEURtoUSD() {
if (validateInput()) {
outputAmount = getConvertedAmount(inputAmount, 'EUR to USD');
} else {
outputAmount = 0; // Important: set to 0 if input is invalid
}
}
public void resetFields() {
inputAmount = null;
outputAmount = 0;
}
}*/