/** * Test class for SIMInventoryImporter * Provides code coverage for deployment to production. * * @author Customer Portal Team * @version 1.1 */ @isTest private class SIMInventoryImporterTest { /** * Helper method to create a ContentVersion (file) for testing */ private static String createTestFile(String csvContent) { ContentVersion cv = new ContentVersion(); cv.Title = 'Test SIM Import'; cv.PathOnClient = 'test_sims.csv'; cv.VersionData = Blob.valueOf(csvContent); insert cv; // Get the ContentDocumentId cv = [SELECT ContentDocumentId FROM ContentVersion WHERE Id = :cv.Id]; return cv.ContentDocumentId; } @isTest static void testSuccessfulImport() { // Prepare test CSV content (matches ASI_N6_PASI format - no header row) String csvContent = '1,02000002470001,PT0220024700010,PASI,20251229,,,,\n' + '2,02000002470002,PT0220024700020,PASI,20251229,,,,\n' + '3,02000002470003,PT0220024700030,PASI,20251229,,,,'; String contentDocId = createTestFile(csvContent); SIMInventoryImporter.ImportRequest request = new SIMInventoryImporter.ImportRequest(); request.contentDocumentIds = new List{ contentDocId }; Test.startTest(); List results = SIMInventoryImporter.importFromCSV(new List{ request }); Test.stopTest(); System.assertEquals(1, results.size(), 'Should return one result'); System.assertEquals(true, results[0].success, 'Import should succeed'); System.assertEquals(3, results[0].recordsCreated, 'Should create 3 records'); System.assertEquals(0, results[0].recordsFailed, 'Should have no failures'); // Verify records were created correctly List sims = [ SELECT Phone_Number__c, PT_Number__c, OEM_ID__c, Status__c, SIM_Type__c, Batch_Date__c FROM SIM_Inventory__c ORDER BY Phone_Number__c ]; System.assertEquals(3, sims.size(), 'Should have 3 SIM records'); System.assertEquals('02000002470001', sims[0].Phone_Number__c); System.assertEquals('PT0220024700010', sims[0].PT_Number__c); System.assertEquals('PASI', sims[0].OEM_ID__c); System.assertEquals('Available', sims[0].Status__c); System.assertEquals('Physical SIM', sims[0].SIM_Type__c); System.assertEquals(Date.newInstance(2025, 12, 29), sims[0].Batch_Date__c); } @isTest static void testDuplicateDetectionInDatabase() { // Create existing record insert new SIM_Inventory__c( Name = '02000002470001', Phone_Number__c = '02000002470001', PT_Number__c = 'PT0220024700010', Status__c = 'Available', SIM_Type__c = 'Physical SIM' ); // Try to import same phone number String csvContent = '1,02000002470001,PT0220024700010,PASI,20251229,,,,'; String contentDocId = createTestFile(csvContent); SIMInventoryImporter.ImportRequest request = new SIMInventoryImporter.ImportRequest(); request.contentDocumentIds = new List{ contentDocId }; Test.startTest(); List results = SIMInventoryImporter.importFromCSV(new List{ request }); Test.stopTest(); System.assertEquals(0, results[0].recordsCreated, 'Should not create duplicate'); System.assertEquals(1, results[0].recordsFailed, 'Should report 1 failure'); System.assert(results[0].errorMessages.contains('already exists'), 'Should mention duplicate'); } @isTest static void testDuplicateDetectionInCSV() { // CSV with duplicate phone numbers String csvContent = '1,02000002470001,PT0220024700010,PASI,20251229,,,,\n' + '2,02000002470001,PT0220024700010,PASI,20251229,,,,'; String contentDocId = createTestFile(csvContent); SIMInventoryImporter.ImportRequest request = new SIMInventoryImporter.ImportRequest(); request.contentDocumentIds = new List{ contentDocId }; Test.startTest(); List results = SIMInventoryImporter.importFromCSV(new List{ request }); Test.stopTest(); System.assertEquals(1, results[0].recordsCreated, 'Should create first record only'); System.assertEquals(1, results[0].recordsFailed, 'Should fail on duplicate'); } @isTest static void testEmptyPhoneNumber() { String csvContent = '1,,PT0220024700010,PASI,20251229,,,,'; String contentDocId = createTestFile(csvContent); SIMInventoryImporter.ImportRequest request = new SIMInventoryImporter.ImportRequest(); request.contentDocumentIds = new List{ contentDocId }; Test.startTest(); List results = SIMInventoryImporter.importFromCSV(new List{ request }); Test.stopTest(); System.assertEquals(0, results[0].recordsCreated, 'Should not create record without phone'); System.assertEquals(1, results[0].recordsFailed, 'Should report failure'); } @isTest static void testEmptyFile() { String csvContent = ''; String contentDocId = createTestFile(csvContent); SIMInventoryImporter.ImportRequest request = new SIMInventoryImporter.ImportRequest(); request.contentDocumentIds = new List{ contentDocId }; Test.startTest(); List results = SIMInventoryImporter.importFromCSV(new List{ request }); Test.stopTest(); System.assertEquals(0, results[0].recordsCreated, 'Should create no records'); System.assertEquals(0, results[0].recordsFailed, 'Should have no failures for empty file'); } @isTest static void testAlwaysPhysicalSIM() { // Verify that all imported SIMs are set to Physical SIM type String csvContent = '1,02000002470001,PT0220024700010,PASI,20251229,,,,'; String contentDocId = createTestFile(csvContent); SIMInventoryImporter.ImportRequest request = new SIMInventoryImporter.ImportRequest(); request.contentDocumentIds = new List{ contentDocId }; Test.startTest(); List results = SIMInventoryImporter.importFromCSV(new List{ request }); Test.stopTest(); SIM_Inventory__c sim = [SELECT SIM_Type__c FROM SIM_Inventory__c LIMIT 1]; System.assertEquals('Physical SIM', sim.SIM_Type__c, 'Should always be Physical SIM'); } @isTest static void testLargeImport() { // Test with 50 records (matches real CSV file size) String csvContent = ''; for (Integer i = 1; i <= 50; i++) { String phoneNum = '0200000247' + String.valueOf(i).leftPad(4, '0'); String ptNum = 'PT022002470' + String.valueOf(i).leftPad(4, '0') + '0'; csvContent += i + ',' + phoneNum + ',' + ptNum + ',PASI,20251229,,,,\n'; } String contentDocId = createTestFile(csvContent); SIMInventoryImporter.ImportRequest request = new SIMInventoryImporter.ImportRequest(); request.contentDocumentIds = new List{ contentDocId }; Test.startTest(); List results = SIMInventoryImporter.importFromCSV(new List{ request }); Test.stopTest(); System.assertEquals(50, results[0].recordsCreated, 'Should create all 50 records'); System.assertEquals(0, results[0].recordsFailed, 'Should have no failures'); Integer count = [SELECT COUNT() FROM SIM_Inventory__c]; System.assertEquals(50, count, 'Database should have 50 records'); } @isTest static void testInvalidDateFormat() { // Invalid date format should not fail the import, just leave date null String csvContent = '1,02000002470001,PT0220024700010,PASI,invalid_date,,,,'; String contentDocId = createTestFile(csvContent); SIMInventoryImporter.ImportRequest request = new SIMInventoryImporter.ImportRequest(); request.contentDocumentIds = new List{ contentDocId }; Test.startTest(); List results = SIMInventoryImporter.importFromCSV(new List{ request }); Test.stopTest(); System.assertEquals(1, results[0].recordsCreated, 'Should still create record'); SIM_Inventory__c sim = [SELECT Batch_Date__c FROM SIM_Inventory__c LIMIT 1]; System.assertEquals(null, sim.Batch_Date__c, 'Date should be null for invalid format'); } }