diff --git a/framework/default/ortoo-core/default/classes/search/ISearchConfiguration.cls b/framework/default/ortoo-core/default/classes/search/ISearchConfiguration.cls index d165c78f20b..dfc8ceeb4cb 100644 --- a/framework/default/ortoo-core/default/classes/search/ISearchConfiguration.cls +++ b/framework/default/ortoo-core/default/classes/search/ISearchConfiguration.cls @@ -7,7 +7,7 @@ * * How to map from the result set fields to internal SObject fields * * Which Base SObject is used to derive a record in the result set */ -public interface ISearchConfiguration +public interface ISearchConfiguration extends IToSobjectFieldMapper { List getRequiredFields(); List getSortableFields(); diff --git a/framework/default/ortoo-core/default/classes/search/IToSobjectFieldMapper.cls b/framework/default/ortoo-core/default/classes/search/IToSobjectFieldMapper.cls new file mode 100644 index 00000000000..d752ccac7b1 --- /dev/null +++ b/framework/default/ortoo-core/default/classes/search/IToSobjectFieldMapper.cls @@ -0,0 +1,13 @@ +/** + * Interface that defines classes that describe the configuration of searches. + * + * This includes the ability to define: + * * The fields that should be included in the result set + * * Which fields are sortable + * * How to map from the result set fields to internal SObject fields + * * Which Base SObject is used to derive a record in the result set + */ +public interface IToSobjectFieldMapper +{ + String getMappedSobjectField( String resultField ); +} \ No newline at end of file diff --git a/framework/default/ortoo-core/default/classes/search/IToSobjectFieldMapper.cls-meta.xml b/framework/default/ortoo-core/default/classes/search/IToSobjectFieldMapper.cls-meta.xml new file mode 100644 index 00000000000..dd61d1f917e --- /dev/null +++ b/framework/default/ortoo-core/default/classes/search/IToSobjectFieldMapper.cls-meta.xml @@ -0,0 +1,5 @@ + + + 52.0 + Active + diff --git a/framework/default/ortoo-core/default/classes/search/SearchOrderBy.cls b/framework/default/ortoo-core/default/classes/search/SearchOrderBy.cls index e84d32f27bc..404fa24a7d9 100644 --- a/framework/default/ortoo-core/default/classes/search/SearchOrderBy.cls +++ b/framework/default/ortoo-core/default/classes/search/SearchOrderBy.cls @@ -17,32 +17,28 @@ public inherited sharing class SearchOrderBy { Contract.requires( properties != null, 'configure called with a null properties' ); - return configure( properties, NullSearchConfiguration.class ); + return configure( properties, NullToSobjectFieldMapper.class ); } - // TODO: add the interface IToSobjectFieldMapper - // TODO: Have ISearchConfiguration extend that - // TODO: Simplify NullSearchConfiguration and change to NullToSobjectFieldMapper - /** * Configures the order by, defining its properties and the search configuration that defines the mappability * * @param Map The properties of the window. Should contain 'fieldName' and 'offset' properties as Strings - * @param Type The type of the SearchConfiguration object that should be used to determine the field mappings + * @param Type The type of the IToSobjectFieldMapper object that should be used to determine the field mappings * @return SearchOrderBy Itself, allowing for a fluent interface */ - public SearchOrderBy configure( Map properties, Type searchConfigurationType ) + public SearchOrderBy configure( Map properties, Type toSobjectFieldMapperType ) { Contract.requires( properties != null, 'configure called with a null properties' ); - Contract.requires( searchConfigurationType != null, 'configure called with a null searchConfigurationType' ); + Contract.requires( toSobjectFieldMapperType != null, 'configure called with a null toSobjectFieldMapperType' ); - ISearchConfiguration searchConfiguration; + IToSobjectFieldMapper toSobjectFieldMapper; try { - searchConfiguration = (ISearchConfiguration)Application.APP_LOGIC.newInstance( searchConfigurationType ); + toSobjectFieldMapper = (IToSobjectFieldMapper)Application.APP_LOGIC.newInstance( toSobjectFieldMapperType ); } catch ( Exception e ) { - Contract.assert( false, 'configure called with a searchConfigurationType ('+searchConfigurationType+') that does not implement ISearchConfiguration or does not have a parameterless constructor' ); + Contract.assert( false, 'configure called with an Sobject Field Mapper ('+toSobjectFieldMapperType+') that does not implement IToSobjectFieldMapper or does not have a parameterless constructor' ); } String resultsFieldName = (String)properties.get( 'fieldName' ); @@ -58,7 +54,7 @@ public inherited sharing class SearchOrderBy if ( String.isNotBlank( resultsFieldName ) ) { - String mappedFieldName = searchConfiguration.getMappedSobjectField( resultsFieldName ); + String mappedFieldName = toSobjectFieldMapper.getMappedSobjectField( resultsFieldName ); if ( String.isNotBlank( mappedFieldName ) ) { @@ -82,26 +78,11 @@ public inherited sharing class SearchOrderBy /** * Null object implementation of the SearchConfiguration which allows unmapped Sobject fields to be used. */ - public class NullSearchConfiguration implements ISearchConfiguration + public class NullToSobjectFieldMapper implements IToSobjectFieldMapper { public String getMappedSobjectField( String resultField ) { return resultField; } - - public List getRequiredFields() - { - return new List(); - } - - public List getSortableFields() - { - return new List(); - } - - public SobjectType getBaseSobjectType() - { - return null; - } } } \ No newline at end of file diff --git a/framework/default/ortoo-core/default/classes/search/tests/SearchOrderByTest.cls b/framework/default/ortoo-core/default/classes/search/tests/SearchOrderByTest.cls index 05f29823919..3e818b9d4dd 100644 --- a/framework/default/ortoo-core/default/classes/search/tests/SearchOrderByTest.cls +++ b/framework/default/ortoo-core/default/classes/search/tests/SearchOrderByTest.cls @@ -26,16 +26,16 @@ public inherited sharing class SearchOrderByTest 'fieldName' => 'sourceFieldName', 'direction' => 'asc' }; - Type configurationType = ISearchConfiguration.class; + Type fieldMapperType = IToSobjectFieldMapper.class; - Amoss_Instance configurationController = ApplicationMockRegistrar.registerMockAppLogic( configurationType ); + Amoss_Instance configurationController = ApplicationMockRegistrar.registerMockAppLogic( fieldMapperType ); configurationController .expects( 'getMappedSobjectField' ) .withParameter( 'sourceFieldName' ) .returning( 'targetFieldName' ); Test.startTest(); - SearchOrderBy orderBy = new SearchOrderBy().configure( properties, configurationType ); + SearchOrderBy orderBy = new SearchOrderBy().configure( properties, fieldMapperType ); Test.stopTest(); configurationController.verify(); @@ -52,14 +52,14 @@ public inherited sharing class SearchOrderByTest Map properties = new Map{ 'direction' => 'asc' }; - Type configurationType = ISearchConfiguration.class; + Type fieldMapperType = IToSobjectFieldMapper.class; - Amoss_Instance configurationController = ApplicationMockRegistrar.registerMockAppLogic( configurationType ); + Amoss_Instance configurationController = ApplicationMockRegistrar.registerMockAppLogic( fieldMapperType ); configurationController .expectsNoCalls(); Test.startTest(); - SearchOrderBy orderBy = new SearchOrderBy().configure( properties, configurationType ); + SearchOrderBy orderBy = new SearchOrderBy().configure( properties, fieldMapperType ); Test.stopTest(); configurationController.verify(); @@ -77,14 +77,14 @@ public inherited sharing class SearchOrderByTest 'fieldName' => '', 'direction' => 'asc' }; - Type configurationType = ISearchConfiguration.class; + Type fieldMapperType = IToSobjectFieldMapper.class; - Amoss_Instance configurationController = ApplicationMockRegistrar.registerMockAppLogic( configurationType ); + Amoss_Instance configurationController = ApplicationMockRegistrar.registerMockAppLogic( fieldMapperType ); configurationController .expectsNoCalls(); Test.startTest(); - SearchOrderBy orderBy = new SearchOrderBy().configure( properties, configurationType ); + SearchOrderBy orderBy = new SearchOrderBy().configure( properties, fieldMapperType ); Test.stopTest(); configurationController.verify(); @@ -102,15 +102,15 @@ public inherited sharing class SearchOrderByTest 'fieldName' => 'unmapped', 'direction' => 'asc' }; - Type configurationType = ISearchConfiguration.class; + Type fieldMapperType = IToSobjectFieldMapper.class; - Amoss_Instance configurationController = ApplicationMockRegistrar.registerMockAppLogic( configurationType ); + Amoss_Instance configurationController = ApplicationMockRegistrar.registerMockAppLogic( fieldMapperType ); configurationController .when( 'getMappedSobjectField' ) .returns( null ); Test.startTest(); - SearchOrderBy orderBy = new SearchOrderBy().configure( properties, configurationType ); + SearchOrderBy orderBy = new SearchOrderBy().configure( properties, fieldMapperType ); Test.stopTest(); configurationController.verify(); @@ -127,15 +127,15 @@ public inherited sharing class SearchOrderByTest Map properties = new Map{ 'fieldName' => 'source' }; - Type configurationType = ISearchConfiguration.class; + Type fieldMapperType = IToSobjectFieldMapper.class; - Amoss_Instance configurationController = ApplicationMockRegistrar.registerMockAppLogic( configurationType ); + Amoss_Instance configurationController = ApplicationMockRegistrar.registerMockAppLogic( fieldMapperType ); configurationController .when( 'getMappedSobjectField' ) .returns( 'target' ); Test.startTest(); - SearchOrderBy orderBy = new SearchOrderBy().configure( properties, configurationType ); + SearchOrderBy orderBy = new SearchOrderBy().configure( properties, fieldMapperType ); Test.stopTest(); configurationController.verify(); @@ -153,15 +153,15 @@ public inherited sharing class SearchOrderByTest 'fieldName' => 'source', 'direction' => 'asc' }; - Type configurationType = ISearchConfiguration.class; + Type fieldMapperType = IToSobjectFieldMapper.class; - Amoss_Instance configurationController = ApplicationMockRegistrar.registerMockAppLogic( configurationType ); + Amoss_Instance configurationController = ApplicationMockRegistrar.registerMockAppLogic( fieldMapperType ); configurationController .when( 'getMappedSobjectField' ) .returns( 'target' ); Test.startTest(); - SearchOrderBy orderBy = new SearchOrderBy().configure( properties, configurationType ); + SearchOrderBy orderBy = new SearchOrderBy().configure( properties, fieldMapperType ); Test.stopTest(); configurationController.verify(); @@ -177,15 +177,15 @@ public inherited sharing class SearchOrderByTest 'fieldName' => 'source', 'direction' => 'desc' }; - Type configurationType = ISearchConfiguration.class; + Type fieldMapperType = IToSobjectFieldMapper.class; - Amoss_Instance configurationController = ApplicationMockRegistrar.registerMockAppLogic( configurationType ); + Amoss_Instance configurationController = ApplicationMockRegistrar.registerMockAppLogic( fieldMapperType ); configurationController .when( 'getMappedSobjectField' ) .returns( 'target' ); Test.startTest(); - SearchOrderBy orderBy = new SearchOrderBy().configure( properties, configurationType ); + SearchOrderBy orderBy = new SearchOrderBy().configure( properties, fieldMapperType ); Test.stopTest(); configurationController.verify(); @@ -201,9 +201,9 @@ public inherited sharing class SearchOrderByTest 'fieldName' => 'source', 'direction' => 'invalid' }; - Type configurationType = ISearchConfiguration.class; + Type fieldMapperType = IToSobjectFieldMapper.class; - Amoss_Instance configurationController = ApplicationMockRegistrar.registerMockAppLogic( configurationType ); + Amoss_Instance configurationController = ApplicationMockRegistrar.registerMockAppLogic( fieldMapperType ); configurationController .when( 'getMappedSobjectField' ) .returns( 'target' ); @@ -212,7 +212,7 @@ public inherited sharing class SearchOrderByTest String exceptionMessage; try { - new SearchOrderBy().configure( properties, configurationType ); + new SearchOrderBy().configure( properties, fieldMapperType ); } catch ( Contract.AssertException e ) { @@ -230,7 +230,7 @@ public inherited sharing class SearchOrderByTest String exceptionMessage; try { - new SearchOrderBy().configure( null, ISearchConfiguration.class ); + new SearchOrderBy().configure( null, IToSobjectFieldMapper.class ); } catch ( Contract.RequiresException e ) { @@ -256,7 +256,7 @@ public inherited sharing class SearchOrderByTest } Test.stopTest(); - ortoo_Asserts.assertContains( 'configure called with a null searchConfigurationType', exceptionMessage, 'configure, when passed a null searchConfigurationType, will throw an exception' ); + ortoo_Asserts.assertContains( 'configure called with a null toSobjectFieldMapperType', exceptionMessage, 'configure, when passed a null toSobjectFieldMapperType, will throw an exception' ); } @isTest @@ -274,7 +274,7 @@ public inherited sharing class SearchOrderByTest } Test.stopTest(); - ortoo_Asserts.assertContains( 'configure called with a searchConfigurationType ('+SearchOrderByTest.class+') that does not implement ISearchConfiguration or does not have a parameterless constructor', exceptionMessage, 'configure, when passed an invalid searchConfigurationType, will throw an exception' ); + ortoo_Asserts.assertContains( 'configure called with an Sobject Field Mapper ('+SearchOrderByTest.class+') that does not implement IToSobjectFieldMapper or does not have a parameterless constructor', exceptionMessage, 'configure, when passed an invalid toSobjectFieldMapperType, will throw an exception' ); } @isTest @@ -292,29 +292,26 @@ public inherited sharing class SearchOrderByTest } Test.stopTest(); - ortoo_Asserts.assertContains( 'configure called with a searchConfigurationType ('+SearchOrderByTest.NoParameterlessConstructor.class+') that does not implement ISearchConfiguration or does not have a parameterless constructor', exceptionMessage, 'configure, when passed an invalid searchConfigurationType, will throw an exception' ); + ortoo_Asserts.assertContains( 'configure called with an Sobject Field Mapper ('+SearchOrderByTest.NoParameterlessConstructor.class+') that does not implement IToSobjectFieldMapper or does not have a parameterless constructor', exceptionMessage, 'configure, when passed an invalid toSobjectFieldMapperType, will throw an exception' ); } + @isTest + private static void nullToSobjectFieldMapper_getMappedSobjectField_whenCalled_returnsWhatWasPassedIn() // NOPMD: Test method name format + { + Test.startTest(); + String got = new SearchOrderBy.NullToSobjectFieldMapper().getMappedSobjectField( 'expected' ); + Test.stopTest(); - class NoParameterlessConstructor implements ISearchConfiguration + System.assertEquals( 'expected', got, 'NullToSobjectFieldMapper_getMappedSobjectField, when called, will return what was passed in' ); + } + + class NoParameterlessConstructor implements IToSobjectFieldMapper { public NoParameterlessConstructor( String parameter ) {} - public List getRequiredFields() - { - return null; - } - public List getSortableFields() - { - return null; - } public String getMappedSobjectField( String resultField ) { return null; } - public SobjectType getBaseSobjectType() - { - return null; - } } } \ No newline at end of file