C# Client Library
A C# Client Library for the AnalyzeRe REST API
Loading...
Searching...
No Matches
AnalyzeReTesting.Utilities.Reflection Class Reference

A collection of filthy hacks to populate some fields of APIResources objects of any type. More...

Classes

class  RecursionContext
 Used to keep track of the recursive random generation process for objects and their attributes. When an object or its properties are being randomly generated, the recursion context indicates what the parent object (if any) is. When a property to generate has additional nested properties that must also be randomized, the recursion context will point back to the original root object. More...
 
class  TimedRequest
 Small class to track a few properties of a request. More...
 

Public Member Functions

 Reflection (Samples samplesInstance=null, int? seedForRandom=null)
 Instantiate a class that can be used to generate random resources via reflection.
 
bool AnyAnalysisMatchesCriteria (IAPIAnalysis analysis, Func< IAPIAnalysis, bool > test)
 Recursively determines if the layer, or any inner layers it references match the specified criteria.
 
bool AnyLayerMatchesCriteria (ILayerSource rootlayerSource, Func< ILayer, bool > test, bool allocationSourcesOnly=false)
 Recursively determines if the layer, or any inner layers it references match the specified criteria.
 
bool AnyLayerSourceMatchesCriteria (ILayerSource rootLayerSource, Func< ILayerSource, bool > test, bool allocationSourcesOnly=false)
 Recursively determines if the layer, or any inner layers it references match the specified criteria.
 
bool AnyLossSetMatchesCriteria (LossSet root, Func< LossSet, bool > test)
 Recursively determines if the loss set, or any inner loss sets it references match the specified criteria.
 
IReference< TCacheResource< T > (IReference< T > reference)
 Cache some known posted resource reference so that it can be reused.
 
IReference< TCacheResource< T > (T resource)
 Cache some known posted resource so that it can be reused.
 
object ChangePropertyValueRandomly (IAPIType obj, PropertyInfo prop, int max_attempts=Max_ReRandomize_Attempts, RecursionContext parentRecursionInfo=null)
 Randomly change the specified property to some different value.
 
object CreateRandomizedInstance (Type desiredType, RecursionContext parentRecursionInfo=null)
 Generates a new randomized object of the specified type.
 
T CreateRandomizedInstance< T > (RecursionContext parentRecursionInfo=null)
 Overload of CreateRandomizedInstance that uses a compile-time type parameter and returns a strong type.
 
T FinalizePostedResource< T > (T posted)
 Helper method for ensuring a newly posted resource is left in a finalized state.
 
object GenerateAttributeValue (AttributeFilter owner, PropertyInfo prop)
 
string GetRandomCurrency ()
 Random currency must be a 3-character currency. In most cases, it must also be a currency for which we have entries in the FX table, so we pull from a list of currencies in the sample FX table we upload.
 
IReference< IAPIResourceGetRandomExistingResource (Type T, RecursionContext currentRecursion)
 Tries to find an existing (POSTed) resource of the specified type without generating a new one.
 
IReference< IAPIResourceGetSampleOfType (Type T)
 Get a standard IInjectableResource<T> value matching the given type.
 
HashSet< stringGetValidCurrenciesForAnalysisProfile (AnalysisProfile analysis_profile)
 
bool LayerSourceIsValidForAnalysisProfile (ILayerSource to_validate, AnalysisProfile analysis_profile)
 Validate that this ILayerSource is compatible with the current analysis profile, including recursive checks of inner layer and loss set definitions.
 
bool LossSetIsValidForAnalysisProfile (LossSet to_validate, AnalysisProfile analysis_profile)
 Validate that this LossSet is compatible with the current analysis profile, including recursive checks of inner layer and loss set definitions.
 
string PickBackAllocatedLayerSourceId (IReference< ILayerSource > sinkReference)
 Finds a random source loss set or layer_view id within the specified sinkReference which can be used as a BackAllocatedLayer.source_id.
 
T Resolve< T > (IReference< T > reference)
 Resolves a reference in a way that benefits from caching, so that the same reference value never has to be retrieved more than once within an integration test run.
 
bool ResourceIsValidForAnalysisProfile (IAPIResource to_validate, AnalysisProfile rootAp)
 Validate that the specified resource is compatible with the current analysis profile (e.g. that it doesn't reference any other event catalogs or loss filters).
 
T TryGenerate< T > (Func< T > generator, Func< T, bool > validation_test, Func< string > get_error, PropertyInfo property, bool exception_on_failure=true, RecursionContext recursionInfo=null)
 Attempt to generate a resource randomly, with some validation to see if the attempt was successful. Throws an exception if it fails.
 
T UploadResourceData< T > (T resource)
 If this resource has data endpoints, try uploading some data!
 

Static Public Member Functions

static IEnumerable< Type > GetAllInstantiableSubtypes (Type type)
 Creates a list of types that can be derived from a given type.
 
static ILayer GetLayerDefinition (ILayerSource layerSource)
 Convert the ILayerSource to a layer instance by extracting the ILayerView.layer property if it is a ILayerView.
 
static bool IsProperty< TPropertyOwner > (PropertyInfo property, Expression< Func< TPropertyOwner, object > > propertyExpression)
 Determines whether the object instance that returned the property is of the type TPropertyOwner and the property matches the specified propertyExpression.
 
static T LimitAttempts< T > (Func< T > generator, Func< T, bool > validation_test, Func< string > get_error, PropertyInfo property=null, int max_attempts=Max_ReRandomize_Attempts, bool exception_on_failure=true, RecursionContext recursionInfo=null)
 Attempt to generate a resource randomly, with some validation to see if the attempt was successful. Limit the number of attempts, then return null if we continue to fail.
 

Properties

AnalysisProfile Target_AnalysisProfile [get, set]
 The default AnalysisProfile to use for various validation assurances.
 
bool Validation_Enabled = true [get, set]
 When true, randomly generated properties will be validated to verify that they can be POSTed successfully. If set to false, properties will be more random, validation will be ignored, and invalid resources may be generated.
 

Detailed Description

A collection of filthy hacks to populate some fields of APIResources objects of any type.

TODO: This class and all that use it should implement disposable, since ThreadLocal{Random} thread_safe_random is disposable. This isn't too important, since test classes only live as long as a test run.

Definition at line 40 of file Reflection.cs.

Constructor & Destructor Documentation

◆ Reflection()

AnalyzeReTesting.Utilities.Reflection.Reflection ( Samples  samplesInstance = null,
int seedForRandom = null 
)
inline

Instantiate a class that can be used to generate random resources via reflection.

Parameters
samplesInstanceA Samples instance used to provide sample known valid resources and data to upload.
seedForRandomA seed to use when initializing the random number generator, which is used for all randomized behaviour.

Note that if tests are being run in parallel, the order in which tests are called is not deterministic and will subvert any attempts to reproduce tests by supplying the same seed. There is also some server-generated random behaviour that cannot be controlled, such as ids assigned to POSTed resources, and parametric distribution values generated.

Definition at line 96 of file Reflection.cs.

Member Function Documentation

◆ AnyAnalysisMatchesCriteria()

bool AnalyzeReTesting.Utilities.Reflection.AnyAnalysisMatchesCriteria ( IAPIAnalysis  analysis,
Func< IAPIAnalysis, bool test 
)
inline

Recursively determines if the layer, or any inner layers it references match the specified criteria.

Parameters
analysisThe IAPIAnalysis object to recurse on.
testThe criteria to test against each object encountered.
Returns
True if any of the layer matches the criteria.

Definition at line 2432 of file Reflection.cs.

◆ AnyLayerMatchesCriteria()

bool AnalyzeReTesting.Utilities.Reflection.AnyLayerMatchesCriteria ( ILayerSource  rootlayerSource,
Func< ILayer, bool test,
bool  allocationSourcesOnly = false 
)

Recursively determines if the layer, or any inner layers it references match the specified criteria.

Parameters
rootlayerSourceThe root layer or layer_view instance which may contain references to other layers.
testThe criteria to test against each layer encountered.
Returns
True if any of the layer matches the criteria.
Parameters
allocationSourcesOnlyIf set to true, only nested properties that qualify as allocation sources will be traversed.

◆ AnyLayerSourceMatchesCriteria()

bool AnalyzeReTesting.Utilities.Reflection.AnyLayerSourceMatchesCriteria ( ILayerSource  rootLayerSource,
Func< ILayerSource, bool test,
bool  allocationSourcesOnly = false 
)
inline

Recursively determines if the layer, or any inner layers it references match the specified criteria.

Parameters
rootLayerSourceThe root layer or layer_view instance which may contain references to other layers.
testThe criteria to test against each layerSource encountered.
allocationSourcesOnlyIf set to true, only nested properties that qualify as allocation sources will be traversed.
Returns
True if any of the layer matches the criteria.

TODO: The allocationSourcesOnly mechanic is scope creep, but it beats duplicating all the logic herein just to exclude "target" and "group" properties of the ValueAllocator layer. Think of other approaches.

Definition at line 2495 of file Reflection.cs.

◆ AnyLossSetMatchesCriteria()

bool AnalyzeReTesting.Utilities.Reflection.AnyLossSetMatchesCriteria ( LossSet  root,
Func< LossSet, bool test 
)
inline

Recursively determines if the loss set, or any inner loss sets it references match the specified criteria.

Parameters
rootThe root loss set which may contain references to other loss sets.
testThe criteria to test against each loss set encountered.
Returns
True if any of the loss sets matches the criteria.

Definition at line 2545 of file Reflection.cs.

◆ CacheResource< T >() [1/2]

IReference< T > AnalyzeReTesting.Utilities.Reflection.CacheResource< T > ( IReference< T reference)

Cache some known posted resource reference so that it can be reused.

Type Constraints
T :IAPIResource 
T :reference.ResourceType 
T :typeof 
T :T 
T :CacheResource 
T :typeof 
T :T 
T :reference 
T :reference.resolved 
T :CacheResource 
T :reference.GetValue() 
T :CacheResource 
T :reference.ResourceType 
T :reference 

◆ CacheResource< T >() [2/2]

IReference< T > AnalyzeReTesting.Utilities.Reflection.CacheResource< T > ( T  resource)

Cache some known posted resource so that it can be reused.

Type Constraints
T :IAPIResource 
T :CacheResource 
T :resource.GetType() 
T :resource.ToReference 
T :true 

◆ ChangePropertyValueRandomly()

object AnalyzeReTesting.Utilities.Reflection.ChangePropertyValueRandomly ( IAPIType  obj,
PropertyInfo  prop,
int  max_attempts = Max_ReRandomize_Attempts,
RecursionContext  parentRecursionInfo = null 
)
inline

Randomly change the specified property to some different value.

Parameters
objThe object whose property should be changed.
propThe property whose value should be randomly changed.
max_attemptsIf we randomly generate a value for this property which is equivalent to the initial value, or is otherwise invalid, we can attempt to generate a new value. This is the maximum number of times we should attempt to re-generate this property before raising an exception. (Some limit is recommended to avoid infinite loops caused by generation routines only generating one possible value.)
parentRecursionInfoThe recursion context (if applicable) indicating the parent object(s) owns the obj instance whose property is being randomized.
Returns
The new property value that has been set to the object (for convenience, so the caller doesn't need to invoke PropertyInfo.GetValue(Object).

Definition at line 1449 of file Reflection.cs.

◆ CreateRandomizedInstance()

object AnalyzeReTesting.Utilities.Reflection.CreateRandomizedInstance ( Type  desiredType,
RecursionContext  parentRecursionInfo = null 
)
inline

Generates a new randomized object of the specified type.

Parameters
desiredTypeThe type of object to generate.
parentRecursionInfoA context object that informs the random generation routines what parent object this resource is being generated for, which can shape its decisions.
Returns
A new random object of the desired type.

Definition at line 178 of file Reflection.cs.

◆ CreateRandomizedInstance< T >()

Overload of CreateRandomizedInstance that uses a compile-time type parameter and returns a strong type.

◆ FinalizePostedResource< T >()

T AnalyzeReTesting.Utilities.Reflection.FinalizePostedResource< T > ( T  posted)
inline

Helper method for ensuring a newly posted resource is left in a finalized state.

Type Constraints
T :IAPIResource 

Definition at line 781 of file Reflection.cs.

◆ GenerateAttributeValue()

object AnalyzeReTesting.Utilities.Reflection.GenerateAttributeValue ( AttributeFilter  owner,
PropertyInfo  prop 
)
inline

Definition at line 1934 of file Reflection.cs.

◆ GetAllInstantiableSubtypes()

static IEnumerable< Type > AnalyzeReTesting.Utilities.Reflection.GetAllInstantiableSubtypes ( Type  type)
inlinestatic

Creates a list of types that can be derived from a given type.

Parameters
typeThe base type for which to get all instantiable sub-types.
Returns
A list of all instantiable sub-types assignable to the type T.

Definition at line 2408 of file Reflection.cs.

◆ GetLayerDefinition()

static ILayer AnalyzeReTesting.Utilities.Reflection.GetLayerDefinition ( ILayerSource  layerSource)
static

Convert the ILayerSource to a layer instance by extracting the ILayerView.layer property if it is a ILayerView.

◆ GetRandomCurrency()

string AnalyzeReTesting.Utilities.Reflection.GetRandomCurrency ( )
inline

Random currency must be a 3-character currency. In most cases, it must also be a currency for which we have entries in the FX table, so we pull from a list of currencies in the sample FX table we upload.

Definition at line 2003 of file Reflection.cs.

◆ GetRandomExistingResource()

IReference< IAPIResource > AnalyzeReTesting.Utilities.Reflection.GetRandomExistingResource ( Type  T,
RecursionContext  currentRecursion 
)
inline

Tries to find an existing (POSTed) resource of the specified type without generating a new one.

Parameters
TThe type of resource to locate.
currentRecursionThe recursion context of the object generation routine that deferred to this helper method.
Returns
A random already posted resource, or null if none were found.

Definition at line 573 of file Reflection.cs.

◆ GetSampleOfType()

IReference< IAPIResource > AnalyzeReTesting.Utilities.Reflection.GetSampleOfType ( Type  T)
inline

Get a standard IInjectableResource<T> value matching the given type.

Parameters
TThe type of resource to return a reference to.

Definition at line 671 of file Reflection.cs.

◆ GetValidCurrenciesForAnalysisProfile()

HashSet< string > AnalyzeReTesting.Utilities.Reflection.GetValidCurrenciesForAnalysisProfile ( AnalysisProfile  analysis_profile)

◆ IsProperty< TPropertyOwner >()

static bool AnalyzeReTesting.Utilities.Reflection.IsProperty< TPropertyOwner > ( PropertyInfo  property,
Expression< Func< TPropertyOwner, object > >  propertyExpression 
)
static

Determines whether the object instance that returned the property is of the type TPropertyOwner and the property matches the specified propertyExpression.

◆ LayerSourceIsValidForAnalysisProfile()

bool AnalyzeReTesting.Utilities.Reflection.LayerSourceIsValidForAnalysisProfile ( ILayerSource  to_validate,
AnalysisProfile  analysis_profile 
)
inline

Validate that this ILayerSource is compatible with the current analysis profile, including recursive checks of inner layer and loss set definitions.

Parameters
to_validateThe ILayerSource to validate.
analysis_profileThe analysis profile to validate against.
Returns
True if the resource is valid.

Definition at line 2211 of file Reflection.cs.

◆ LimitAttempts< T >()

static T AnalyzeReTesting.Utilities.Reflection.LimitAttempts< T > ( Func< T generator,
Func< T, bool validation_test,
Func< string get_error,
PropertyInfo  property = null,
int  max_attempts = Max_ReRandomize_Attempts,
bool  exception_on_failure = true,
RecursionContext  recursionInfo = null 
)
inlinestatic

Attempt to generate a resource randomly, with some validation to see if the attempt was successful. Limit the number of attempts, then return null if we continue to fail.

Template Parameters
TThe type of object being generated.
Parameters
generatorGenerates an object of type T.
validation_testDetermines if the object is valid.
get_errorReturns an error message if object is invalid.
propertyThe property information for logs and error messages.
max_attemptsThe maximum number of attempts before failing.
exception_on_failureWhether failure sure raise a PropertyGenerationException or simply return default(T)
recursionInfoRecursion context used for logging.
Returns
The generated object.
Exceptions
PropertyGenerationExceptionGeneration fails and exception_on_failure is set to true.

Definition at line 2083 of file Reflection.cs.

◆ LossSetIsValidForAnalysisProfile()

bool AnalyzeReTesting.Utilities.Reflection.LossSetIsValidForAnalysisProfile ( LossSet  to_validate,
AnalysisProfile  analysis_profile 
)
inline

Validate that this LossSet is compatible with the current analysis profile, including recursive checks of inner layer and loss set definitions.

Parameters
to_validateThe LossSet to validate.
analysis_profileThe analysis profile to validate against.
Returns
True if the resource contains no invalid references.

Definition at line 2253 of file Reflection.cs.

◆ PickBackAllocatedLayerSourceId()

string AnalyzeReTesting.Utilities.Reflection.PickBackAllocatedLayerSourceId ( IReference< ILayerSource sinkReference)
inline

Finds a random source loss set or layer_view id within the specified sinkReference which can be used as a BackAllocatedLayer.source_id.

Exceptions
PropertyGenerationExceptionIf no valid source_id can be found within the sink structure.

Definition at line 2016 of file Reflection.cs.

◆ Resolve< T >()

T AnalyzeReTesting.Utilities.Reflection.Resolve< T > ( IReference< T reference)
inline

Resolves a reference in a way that benefits from caching, so that the same reference value never has to be retrieved more than once within an integration test run.

Type Constraints
T :class 
T :IAPIResource 

Definition at line 765 of file Reflection.cs.

◆ ResourceIsValidForAnalysisProfile()

bool AnalyzeReTesting.Utilities.Reflection.ResourceIsValidForAnalysisProfile ( IAPIResource  to_validate,
AnalysisProfile  rootAp 
)
inline

Validate that the specified resource is compatible with the current analysis profile (e.g. that it doesn't reference any other event catalogs or loss filters).

Parameters
to_validateThe resource to validate.
rootApThe analysis profile to validate against.
Returns
True if the resource contains no invalid references.

TODO: We frequently need to add conditions to this test method to ensure randomly generated resources aren't known to be invalid for POST. This usually arises when selecting random existing references from the server to reference. We should create a suite of tests to ensure this method is correctly detecting each of the known scenarios that have arisen in the past so that we don't need to rely on Randomized tests only sporadically producing those scenarios.

Definition at line 2161 of file Reflection.cs.

◆ TryGenerate< T >()

T AnalyzeReTesting.Utilities.Reflection.TryGenerate< T > ( Func< T generator,
Func< T, bool validation_test,
Func< string get_error,
PropertyInfo  property,
bool  exception_on_failure = true,
RecursionContext  recursionInfo = null 
)
inline

Attempt to generate a resource randomly, with some validation to see if the attempt was successful. Throws an exception if it fails.

Template Parameters
TThe type of object being generated.
Parameters
generatorGenerates an object of type T.
validation_testDetermines if the object is valid.
get_errorReturns an error message if object is invalid.
propertyThe property information for logs and error messages.
Returns
The generated object.
Parameters
exception_on_failureWhether failure sure raise a PropertyGenerationException or simply return default(T)
recursionInfoRecursion context used for logging.
Exceptions
PropertyGenerationExceptionGeneration fails.

Definition at line 2059 of file Reflection.cs.

◆ UploadResourceData< T >()

T AnalyzeReTesting.Utilities.Reflection.UploadResourceData< T > ( T  resource)
inline

If this resource has data endpoints, try uploading some data!

Parameters
resourceThe resource to try uploading data for.
Type Constraints
T :IAPIResource_WithDataEndpoint 

Definition at line 799 of file Reflection.cs.

Property Documentation

◆ Target_AnalysisProfile

AnalysisProfile AnalyzeReTesting.Utilities.Reflection.Target_AnalysisProfile
getset

The default AnalysisProfile to use for various validation assurances.

Definition at line 71 of file Reflection.cs.

◆ Validation_Enabled

bool AnalyzeReTesting.Utilities.Reflection.Validation_Enabled = true
getset

When true, randomly generated properties will be validated to verify that they can be POSTed successfully. If set to false, properties will be more random, validation will be ignored, and invalid resources may be generated.

Definition at line 83 of file Reflection.cs.


The documentation for this class was generated from the following file: