|
| 1 | +/** |
| 2 | + * @File Name : LocationAnimalControllerTest.cls |
| 3 | + * @Description : Test Class for LocationAnimalController |
| 4 | + * @Author : Chris Rolfe |
| 5 | + * @Last Modified By : |
| 6 | + * @Last Modified On : |
| 7 | + * @Modification Log : |
| 8 | + * Ver Date Author Modification |
| 9 | + * 1.0 22/08/2024 Chris Rolfe Initial Version |
| 10 | +**/ |
| 11 | +@isTest |
| 12 | +public class LocationAnimalControllerTest { |
| 13 | + |
| 14 | + @testSetup |
| 15 | + static void setupTestData() { |
| 16 | + // Get Unit RecordType for Locations |
| 17 | + Id locRecordTypeUnit = Schema.SObjectType.animalshelters__Locations__c.getRecordTypeInfosByName().get('Unit').getRecordTypeId(); |
| 18 | + Id locRecordTypeBlock = Schema.SObjectType.animalshelters__Locations__c.getRecordTypeInfosByName().get('Block').getRecordTypeId(); |
| 19 | + |
| 20 | + |
| 21 | + // Create a Location of type Unit |
| 22 | + animalshelters__Locations__c unitLocation = new animalshelters__Locations__c(animalshelters__Name__c = 'Unit 1', RecordTypeId = locRecordTypeUnit); |
| 23 | + insert unitLocation; |
| 24 | + |
| 25 | + // Create another Location of type Block |
| 26 | + animalshelters__Locations__c blockLocation = new animalshelters__Locations__c(animalshelters__Name__c = 'Block 1', RecordTypeId = locRecordTypeBlock); |
| 27 | + insert blockLocation; |
| 28 | + |
| 29 | + // Create an Animal |
| 30 | + animalshelters__Animal__c animal1 = new animalshelters__Animal__c(animalshelters__Animal_Name__c = 'Molly', animalshelters__Date_of_Arrival__c = Date.today()); |
| 31 | + animalshelters__Animal__c animal2 = new animalshelters__Animal__c(animalshelters__Animal_Name__c = 'Furby', animalshelters__Date_of_Arrival__c = Date.today()); |
| 32 | + insert new List<animalshelters__Animal__c>{animal1, animal2}; |
| 33 | + |
| 34 | + // Create Movement records for the Unit Location |
| 35 | + animalshelters__Movement__c movement1 = new animalshelters__Movement__c(animalshelters__Animal__c = animal1.Id, animalshelters__Location__c = unitLocation.Id, animalshelters__Current__c = true); |
| 36 | + animalshelters__Movement__c movement2 = new animalshelters__Movement__c(animalshelters__Animal__c = animal2.Id, animalshelters__Location__c = unitLocation.Id, animalshelters__Current__c = true); |
| 37 | + insert new List<Movement__c>{movement1, movement2}; |
| 38 | + } |
| 39 | + |
| 40 | + @isTest |
| 41 | + static void testGetAnimalsInLocation_UnitWithAnimals() { |
| 42 | + // Fetch the Unit Location created in test setup |
| 43 | + animalshelters__Locations__c unitLocation = [SELECT Id FROM animalshelters__Locations__c WHERE animalshelters__Name__c = 'Unit 1' LIMIT 1]; |
| 44 | + |
| 45 | + // Call the method |
| 46 | + Test.startTest(); |
| 47 | + List<animalshelters__Movement__c> movements = LocationAnimalController.getAnimalsInLocation(unitLocation.Id); |
| 48 | + Test.stopTest(); |
| 49 | + |
| 50 | + // Verify the result |
| 51 | + System.assertEquals(2, movements.size(), 'There should be two movements for Unit 1'); |
| 52 | + System.assertEquals('Molly', movements[0].animalshelters__Animal__r.animalshelters__Animal_Name__c, 'First animal should be Lion'); |
| 53 | + System.assertEquals('Furby', movements[1].animalshelters__Animal__r.animalshelters__Animal_Name__c, 'Second animal should be Tiger'); |
| 54 | + } |
| 55 | + |
| 56 | + @isTest |
| 57 | + static void testGetAnimalsInLocation_UnitWithoutAnimals() { |
| 58 | + // Create a new Unit Location without any animals |
| 59 | + RecordType unitRecordType = [SELECT Id FROM RecordType WHERE DeveloperName = 'Unit' AND SObjectType = 'animalshelters__Locations__c' LIMIT 1]; |
| 60 | + animalshelters__Locations__c emptyUnitLocation = new animalshelters__Locations__c(animalshelters__Name__c = 'Empty Unit', RecordTypeId = unitRecordType.Id); |
| 61 | + insert emptyUnitLocation; |
| 62 | + |
| 63 | + // Call the method |
| 64 | + Test.startTest(); |
| 65 | + List<animalshelters__Movement__c> movements = LocationAnimalController.getAnimalsInLocation(emptyUnitLocation.Id); |
| 66 | + Test.stopTest(); |
| 67 | + |
| 68 | + // Verify the result |
| 69 | + System.assertEquals(0, movements.size(), 'There should be no movements for the empty unit'); |
| 70 | + } |
| 71 | + |
| 72 | + @isTest |
| 73 | + static void testGetAnimalsInLocation_NonUnitLocation() { |
| 74 | + // Fetch the Block Location created in test setup |
| 75 | + animalshelters__Locations__c blockLocation = [SELECT Id FROM animalshelters__Locations__c WHERE animalshelters__Name__c = 'Block 1' LIMIT 1]; |
| 76 | + |
| 77 | + // Call the method |
| 78 | + Test.startTest(); |
| 79 | + List<animalshelters__Movement__c> movements = LocationAnimalController.getAnimalsInLocation(blockLocation.Id); |
| 80 | + Test.stopTest(); |
| 81 | + |
| 82 | + // Verify the result |
| 83 | + System.assertEquals(0, movements.size(), 'There should be no movements because the location is not a Unit'); |
| 84 | + } |
| 85 | +} |
0 commit comments