/*********************************************************************************************************** * Name : AWAL_SduHelperTest * Purpose : Test class for AWAL_SduHelper **********************************************************************************************************/ @IsTest private class AWAL_SduHelperTest { /************************************************************************************ * APEX method tested : AWAL_SduHelper.getFields * Description : Ensures FieldsWrapper is built correctly — oppFields equals OPP_FIELDS + OPP_FORMULA_FIELDS + OPP_TO_LABEL (order), and list sizes/membership match their constants. *************************************************************************************/ @IsTest static void testGetFields() { List expectedOppFields = new List(); expectedOppFields.addAll(AWAL_SduConstants.OPP_FIELDS); expectedOppFields.addAll(AWAL_SduConstants.OPP_FORMULA_FIELDS); expectedOppFields.addAll(AWAL_SduConstants.OPP_TO_LABEL); Test.startTest(); AWAL_SduHelper.FieldsWrapper result = AWAL_SduHelper.getFields(); Test.stopTest(); Assert.areNotEqual(null, result, 'Wrapper should not be null.'); Assert.areEqual(AWAL_SduConstants.SDU_FIELDS.size(), result.sduFields.size(), 'Unexpected SDU_FIELDS size.'); Assert.areEqual(expectedOppFields.size(), result.oppFields.size(), 'Unexpected oppFields size.'); Assert.areEqual( AWAL_SduConstants.GROUPED_FIELDS.size(), result.groupedFields.size(), 'Unexpected GROUPED_FIELDS size.' ); Assert.areEqual( AWAL_SduConstants.OPP_BOOLEAN_FIELDS.size(), result.oppBooleanFields.size(), 'Unexpected OPP_BOOLEAN_FIELDS size.' ); Assert.areEqual( AWAL_SduConstants.SDU_PICKLISTS.size(), result.sduPicklists.size(), 'Unexpected SDU_PICKLISTS size.' ); for (String f : AWAL_SduConstants.OPP_FIELDS) { Assert.isTrue(result.oppFields.contains(f), 'Missing field from OPP_FIELDS: ' + f); } for (String f : AWAL_SduConstants.OPP_FORMULA_FIELDS) { Assert.isTrue(result.oppFields.contains(f), 'Missing field from OPP_FORMULA_FIELDS: ' + f); } for (String f : AWAL_SduConstants.OPP_TO_LABEL) { Assert.isTrue(result.oppFields.contains(f), 'Missing field from OPP_TO_LABEL: ' + f); } } /************************************************************************************ * APEX method tested : AWAL_SduHelper.getSDU * Description : Validates that getSDU() returns the correct Opportunity_SDU__c record for a given Opportunity and returns null when no SDU exists. *************************************************************************************/ @IsTest static void testGetSDU() { Opportunity opp1 = TestFactory.createAwalOpp(); Opportunity opp2 = TestFactory.createAwalOpp(); Opportunity_SDU__c sdu = new Opportunity_SDU__c(Opportunity__c = opp2.Id); insert sdu; Test.startTest(); Opportunity_SDU__c resultOpp1 = AWAL_SduHelper.getSDU(opp1.Id); Opportunity_SDU__c resultOpp2 = AWAL_SduHelper.getSDU(opp2.Id); Test.stopTest(); Assert.areEqual(null, resultOpp1, 'Opp1 should not receive the SDU created for Opp2.'); Assert.areNotEqual(null, resultOpp2, 'Opp2 should receive its SDU.'); Assert.areEqual(opp2.Id, resultOpp2.Opportunity__c, 'Opp2 SDU should be correctly linked.'); } /************************************************************************************ * APEX method tested : AWAL_SduHelper.saveSDU * Description : Verifies insert/upsert behavior and the rule: keep Relevant_Recordings_Details__c when value is "Other", clear it otherwise. *************************************************************************************/ @IsTest static void testSaveSDU() { Opportunity opp = TestFactory.createAwalOpp(); Map> picklists = TestFactory.getPicklistValuesMapByObjName('Opportunity_SDU__c'); String otherVal = 'Other'; String notOtherVal = null; for (String value : picklists.get('Relevant_Recordings__c')) { if (value != otherVal) { notOtherVal = value; break; } } Opportunity_SDU__c sdu = new Opportunity_SDU__c( Opportunity__c = opp.Id, Relevant_Recordings__c = otherVal, Relevant_Recordings_Details__c = 'Details should remain' ); Test.startTest(); Id firstId = AWAL_SduHelper.saveSDU(sdu); Opportunity_SDU__c sduAfterInsert = [ SELECT Id, Opportunity__c, Relevant_Recordings__c, Relevant_Recordings_Details__c FROM Opportunity_SDU__c WHERE Id = :firstId ]; Assert.areNotEqual(null, firstId, 'Insert should return a non-null Id.'); Assert.areEqual(otherVal, sduAfterInsert.Relevant_Recordings__c, 'Should keep "Other" value.'); Assert.areEqual( 'Details should remain', sduAfterInsert.Relevant_Recordings_Details__c, 'Details must be preserved when Relevant_Recordings__c == "Other".' ); sduAfterInsert.Relevant_Recordings__c = notOtherVal; sduAfterInsert.Relevant_Recordings_Details__c = 'Should be cleared'; Id secondId = AWAL_SduHelper.saveSDU(sduAfterInsert); Test.stopTest(); Opportunity_SDU__c sduAfterUpdate = [ SELECT Id, Opportunity__c, Relevant_Recordings__c, Relevant_Recordings_Details__c FROM Opportunity_SDU__c WHERE Id = :secondId ]; Assert.areEqual(firstId, secondId, 'Upsert must keep the same Id.'); Assert.areEqual(notOtherVal, sduAfterUpdate.Relevant_Recordings__c, 'Should persist the non-"Other" value.'); Assert.areEqual( null, sduAfterUpdate.Relevant_Recordings_Details__c, 'Details must be cleared when Relevant_Recordings__c != "Other".' ); } /************************************************************************************ * APEX method tested : AWAL_SduHelper.sendEmail * Description : Ensures a single email send occurs with provided recipients, subject, body, and PDF attachment derived from the Base64 input. *************************************************************************************/ @IsTest static void testSendEmail() { Test.startTest(); AWAL_SduHelper.sendEmail( new Map(), EncodingUtil.base64Encode(Blob.valueOf('fake pdf bytes')), 'SDU Document', new List{ 'primary.to@example.com' }, new List{ 'cc.one@example.com' } ); Integer invocations = Limits.getEmailInvocations(); Test.stopTest(); Assert.areEqual(1, invocations, 'Exactly one email invocation should have occurred.'); } /************************************************************************************ * APEX method tested : AWAL_SduHelper.upsertPdfAsContentVersion * Description : Verifies Base64 matches injected PDF, and that first call publishes a ContentDocument, while the second creates a new version on the same document with an incremented VersionNumber. *************************************************************************************/ @IsTest static void testUpsertPdfAsContentVersion() { Opportunity opp = TestFactory.createAwalOpp(); String oppName = opp.Name; Blob pdfV1 = Blob.valueOf('PDF BYTES V1'); Blob pdfV2 = Blob.valueOf('PDF BYTES V2 - UPDATED'); AWAL_SduHelper.testPdfBlob = pdfV1; Test.startTest(); String returnedB64_V1 = AWAL_SduHelper.upsertPdfAsContentVersion(opp.Id, oppName); Assert.areEqual( EncodingUtil.base64Encode(pdfV1), returnedB64_V1, 'Returned Base64 must match the injected PDF V1.' ); ContentVersion cv1 = [ SELECT Id, Title, PathOnClient, VersionData, ContentDocumentId, VersionNumber FROM ContentVersion WHERE Title = :('SDU - ' + oppName) AND PathOnClient = :String.valueOf(opp.Id) + '.pdf' ORDER BY VersionNumber ASC LIMIT 1 ]; Assert.areEqual('SDU - ' + oppName, cv1.Title, 'Unexpected Title on first version.'); Assert.areEqual(String.valueOf(opp.Id) + '.pdf', cv1.PathOnClient, 'Unexpected PathOnClient on first version.'); Assert.areEqual(pdfV1.size(), cv1.VersionData.size(), 'VersionData size of first version must match V1.'); Assert.areEqual( returnedB64_V1, EncodingUtil.base64Encode(cv1.VersionData), 'Persisted VersionData (v1) must match returned Base64.' ); Assert.areNotEqual(null, cv1.ContentDocumentId, 'ContentDocumentId should be set after first publish.'); Id docId = cv1.ContentDocumentId; Integer firstVersionNumber = Integer.valueOf(String.valueOf(cv1.VersionNumber)); AWAL_SduHelper.testPdfBlob = pdfV2; String returnedB64_V2 = AWAL_SduHelper.upsertPdfAsContentVersion(opp.Id, oppName); Assert.areEqual( EncodingUtil.base64Encode(pdfV2), returnedB64_V2, 'Returned Base64 must match the injected PDF V2.' ); List versions = [ SELECT Id, ContentDocumentId, VersionNumber, VersionData FROM ContentVersion WHERE ContentDocumentId = :docId ORDER BY VersionNumber ASC ]; Assert.isTrue(versions.size() == 2, 'There should be two versions on the same ContentDocument.'); ContentVersion last = versions[versions.size() - 1]; Integer lastVersionNumber = Integer.valueOf(String.valueOf(last.VersionNumber)); Assert.areEqual(docId, last.ContentDocumentId, 'Last version must belong to the same ContentDocument.'); Assert.isTrue(lastVersionNumber > firstVersionNumber, 'VersionNumber should increase on new version.'); Assert.areEqual(pdfV2.size(), last.VersionData.size(), 'Latest VersionData size must match V2.'); Assert.areEqual( returnedB64_V2, EncodingUtil.base64Encode(last.VersionData), 'Persisted VersionData (latest) must match returned Base64 V2.' ); Test.stopTest(); } /************************************************************************************ * APEX method tested : AWAL_SduHelper.getSnapshot * Description : Ensures snapshot includes a field populated only in the amendment (original null), by verifying the field value is present in the snapshot. *************************************************************************************/ @IsTest static void testGetSnapshot() { Account acc = TestFactory.getAwalAccount(); Opportunity baseOpp = TestFactory.getAwalOpp(); insert baseOpp; TriggerControl.disableTrigger(OpportunityTriggerHandler.SELF); baseOpp.StageName = AWAL_Constants.OPPORTUNITY_STAGE_CLOSED_WON; baseOpp.RecordingFund__c = null; baseOpp.LegalEntityNameId__c = acc.Id; update baseOpp; TriggerControl.enableTrigger(OpportunityTriggerHandler.SELF); Opportunity amendOpp = TestFactory.getAwalOpp(); amendOpp.Is_Amendment__c = true; amendOpp.Parent_Opportunity__c = baseOpp.Id; amendOpp.RecordingFund__c = -100; insert amendOpp; Test.startTest(); AWAL_SduHelper.SnapshotWrapper wrapper = AWAL_SduHelper.getSnapshot(amendOpp.Id); Test.stopTest(); Assert.areNotEqual(null, wrapper, 'SnapshotWrapper should not be null.'); Assert.areNotEqual(null, wrapper.snapshot, 'Snapshot (Opportunity) inside the wrapper should not be null.'); Decimal snapshotVal = (Decimal) wrapper.snapshot.get('RecordingFund__c'); Assert.areEqual( amendOpp.RecordingFund__c, snapshotVal, 'Snapshot must carry the amendment value for RecordingFund__c when original was null.' ); } }