/** * File Name: ArrayTest * Description: * * @author: architect ir. Wilhelmus G.J. Velzeboer | wvelzebo@its.jnj.com */ @IsTest(IsParallel=true) private class ArrayUtilsTest { @IsTest static void testObjectToString() { System.assertEquals( new List {'123', '456', '789'}, ArrayUtils.objectToString( new List {123, 456, 789} ) ); } @IsTest static void testObjectStringToString() { System.assertEquals( new List {'123', '456', '789'}, ArrayUtils.objectToString( new List {'123', '456', '789'} ) ); } @IsTest static void itShouldReturnEmptyListOnEmptyObjectToString() { System.assertEquals( new List(), ArrayUtils.objectToString( new List() ) ); } @IsTest static void itShouldConvertSetToListOfString() { System.assertEquals( new List {'1', '2', '3'}, ArrayUtils.objectToString( new Set {1, 2, 3} ) ); } @IsTest static void itShouldConvertSetObjectToSetOfIds() { Id idA = fflib_IDGenerator.generate(Account.SObjectType); Id idB = fflib_IDGenerator.generate(Account.SObjectType); System.assertEquals( new Set {idA, idB}, ArrayUtils.objectToIds( new Set { (Object) idA, (Object) idB } ) ); } @IsTest static void itShouldReturnReversedObjectList() { System.assertEquals( new List { (Object) 'C', (Object) 'B', (Object) 'A' }, ArrayUtils.reverse( new List { (Object) 'A', (Object) 'B', (Object) 'C' } ) ); } @IsTest static void itShouldReturnReversedSObjectList() { System.assertEquals( new List { (SObject) new Account(Name = 'C'), (SObject) new Account(Name = 'B'), (SObject) new Account(Name = 'A') }, ArrayUtils.reverse( new List { (SObject) new Account(Name = 'A'), (SObject) new Account(Name = 'B'), (SObject) new Account(Name = 'C') } ) ); } @IsTest static void itShouldConvertListOfStringsIntoUpperCase() { System.assertEquals( new List { 'TEST A', 'TEST B', 'TEST C' }, ArrayUtils.upperCase( new List { 'test a', 'test b', 'test c' } ) ); } @IsTest static void testLowerCase() { System.assertEquals( new List {'abc', 'def'}, ArrayUtils.lowerCase( new List {'ABC', 'DEF'} ) ); } @IsTest static void testIsEmpty() { List aNull = null; System.assertEquals(true, ArrayUtils.isEmpty(aNull)); System.assertEquals(true, ArrayUtils.isEmpty(new List {})); System.assertEquals(false, ArrayUtils.isEmpty(new List {'a'})); System.assertEquals(false, ArrayUtils.isEmpty(new List {'a', 'b'})); System.assertEquals(true, ArrayUtils.isEmpty(new List {})); System.assertEquals(false, ArrayUtils.isEmpty(new List {1})); System.assertEquals(false, ArrayUtils.isEmpty(new List {1, 2})); } @IsTest static void testIsNotEmpty() { List aNull = null; System.assertEquals(false, ArrayUtils.isNotEmpty(aNull)); System.assertEquals(false, ArrayUtils.isNotEmpty(new List {})); System.assertEquals(true, ArrayUtils.isNotEmpty(new List {'a'})); System.assertEquals(true, ArrayUtils.isNotEmpty(new List {'a', 'b'})); System.assertEquals(false, ArrayUtils.isNotEmpty(new List {})); System.assertEquals(true, ArrayUtils.isNotEmpty(new List {1})); System.assertEquals(true, ArrayUtils.isNotEmpty(new List {1, 2})); System.assertEquals(false, ArrayUtils.isNotEmpty(new List {})); System.assertEquals(true, ArrayUtils.isNotEmpty(new List {new Account(Name = 'test')})); } @IsTest static void itShouldReturnMostRecentDatetime() { // GIVEN - a list of unsorted Datetime values List datetimes = generateDateTimes(); // WHEN - we request the maxDatetime Datetime maxDatetime = ArrayUtils.maxDatetime(datetimes); // THEN - the highest Datetime should be returned System.assertEquals(datetimes.get(1), maxDatetime); } @IsTest static void itShouldReturnOldestDatetime() { // GIVEN - a list of unsorted Datetime values List datetimes = generateDateTimes(); // WHEN - we request the maxDatetime Datetime maxDatetime = ArrayUtils.minDatetime(datetimes); // THEN - the highest Datetime should be returned System.assertEquals(datetimes.get(4), maxDatetime); } @IsTest static void itShouldHandleNullInputsForMaxDatatime() { System.assertEquals( null, ArrayUtils.maxDatetime(new List()), 'maxDatetime method was unable to handle an empty list as input' ); System.assertEquals( null, ArrayUtils.maxDatetime(null), 'maxDatetime method was unable to handle null as input' ); } @IsTest static void itShouldCloneAndSortTheList() { // GIVEN - a list of unsorted numbers List unsorted = new List { 1, 30, 2, 76, -7 }; // WHEN - we request the a cloned and sorted list List sorted = ArrayUtils.cloneAndSort(unsorted); System.assertNotEquals(unsorted, sorted, 'The original object was modified, something went wrong with the cloning'); System.assertEquals(new List {-7, 1, 2, 30, 76}, sorted, 'Incorrect sorted list returned'); } @IsTest static void itShouldReturnTheLastValueInTheList() { // GIVEN - a list of unsorted values List objects = new List {1, 2, 3, 9, 4, 7, 8}; // WHEN - we request the last value Object result = ArrayUtils.lastValue(objects); // THEN - the highest Datetime should be returned System.assertEquals(8, result, 'Did not return the last value in the list of objects'); } private static List generateDateTimes() { return new List { Datetime.newInstance(2020, 01, 01, 07, 59, 7), Datetime.newInstance(2020, 02, 01, 07, 40, 7), Datetime.newInstance(2020, 02, 01, 07, 00, 7), Datetime.newInstance(2019, 02, 01, 07, 59, 7), Datetime.newInstance(2018, 07, 07, 07, 7, 7) }; } }