2using System.Collections.Generic;
4using System.Reflection;
6using System.Threading.Tasks;
18using Microsoft.VisualStudio.TestTools.UnitTesting;
23using TestCategory =
NUnit.Framework.CategoryAttribute;
31 private const string Category =
"Test Utilities";
38 Assert.Inconclusive(
"DELETE is disabled.");
40 Assert.Inconclusive(
"Cannot DELETE while tests running in parallel.");
57 Tuple.Create(
true, 1.00000000000001, 1.0),
58 Tuple.Create(
false, 1.0000000000001, 1.0),
59 Tuple.Create(
true, 10000000000000.1, 10000000000000.0),
60 Tuple.Create(
false, 1000000000000.1, 1000000000000.0),
61 Tuple.Create(
true, 0.123456789012345, 0.123456789012346),
62 Tuple.Create(
false, 0.12345678912345, 0.12345678912346),
64 Tuple.Create(
true, 0.123456789012345*1
E100, 0.123456789012346*1
E100),
65 Tuple.Create(
false, 0.12345678912345*1
E100, 0.12345678912346*1
E100),
67 Tuple.Create(
true, 0.123456789012345/1
E100, 0.123456789012346/1
E100),
68 Tuple.Create(
false, 0.12345678912345/1
E100, 0.12345678912346/1
E100),
71 Tuple.Create(
true, 817393214.3568030, 817393214.35680345),
72 Tuple.Create(
true, 0.408297614850242462, 0.408297614850242018)
90 #region Test TypeResolver
94 string testURL =
"http://localhost:8000/layer_views/ae53378c-9a94-39f4-898a-4d888d43ae73";
96 testURL =
"https://dev-master-api.analyzere.net/portfolios/ae53378c-9a94-39f4-898a-4d888d43ae73/";
98 testURL =
"https://host/api/loss_sets/dead-beef";
143 Enumerable.Range(0, layers.Length).ToList().ForEach(
i =>
152 Type
t = layers[
i % layers.Length].GetType();
162 Type
t = layers[
i % layers.Length].GetType();
169 "than reflecting each time, so it should be removed.");
171 #endregion Test TypeResolver
173 #region Test GetCollectionName
247 string expectedError =
$"Invalid type {runtimeType.NiceTypeName()}. " +
248 "Type can be linked to more than one resource";
275 "IAPIType does not derive from IAPIResource.",
ex.Message));
277 #endregion Test GetCollectionName
279 #region Test API.PollUntilReady
280 private const int DefaultRetryTime = 10;
281 private const int DefaultQueuePosition = 1;
303 "Function was not retried as many times as expected.");
317 Assert.AreEqual(
$"Simulation is queued at position {DefaultQueuePosition}. " +
318 $"Server suggests retrying after {DefaultRetryTime / 1000d} " +
319 $"seconds. Please try again later.",
ex.Message);
331 Assert.AreEqual(
$"Simulation is queued at position {DefaultQueuePosition}. " +
332 $"Server suggests retrying after {DefaultRetryTime / 1000d} seconds. " +
333 $"The maximum wait time of {DefaultRetryTime / 10000d} seconds has been exceeded. " +
334 "Please try again later.",
ex.Message);
354 Assert.Fail(
"Expected a NotWaitingException to be thrown.");
359 Console.WriteLine(
"PollUntilReady reports time waited was: " +
ex.TimeWaited);
360 Console.WriteLine(
"Time until control was returned to caller: " +
elapsed);
367 Console.WriteLine(
"Warning: PollUntilReady took " + (
elapsed -
ex.TimeWaited) *
368 1000 +
" ms longer than desired to stop polling.");
375 Console.WriteLine(
"Warning: Waited too long (" +
ex.TimeWaited +
"s >> " +
383 Console.WriteLine(
"Warning: Didn't wait long enough before giving up (" +
399 Assert.Fail(
"Expected a NotWaitingException to be thrown.");
404 Assert.AreEqual(
$"Simulation is queued at position {mockQueuePosition}. " +
405 "Please try again later.",
ex.Message);
414 canceler.CancelAfter(DefaultRetryTime * 10);
420 #endregion Test API.PollUntilReady
422 #region Test AsyncRequestMonitor
435 Assert.AreEqual(
returned, result,
"Function was not retried as many times as expected.");
451 Assert.IsNotNull(
ex.InnerException);
471 Assert.IsNotNull(
ex.InnerException);
488 Assert.IsNotNull(
ex.InnerException);
495 #endregion Test AsyncRequestMonitor
497 #region Test Reflection
498 [
TestMethod, TestCategory(Category), TestCategory(
"Randomized")]
512 Assert.Fail(
"CreateRandomizedInstance did not generate a valid " +
513 $"{subtype.NiceTypeName()} instance. Result was \"{test ?? "(
null)
"}\"");
517 Console.WriteLine(
$"{DateTime.UtcNow:mm:ss.ff} Generated a random " +
518 $"{subtype.NiceTypeName()} with runtime type {test.GetType().NiceTypeName()}");
522 .GetUserFacingProperties(
true,
true).ToList();
528 Console.WriteLine(
$"{DateTime.UtcNow:mm:ss.ff} Verified that " +
529 $"the reference property {prop.DeclaringType.NiceTypeName()}.{prop.Name} " +
535 Console.WriteLine(
$"{DateTime.UtcNow:mm:ss.ff} Randomizing the " +
536 $"{test.GetType().NiceTypeName()} property {prop.PropertyType.NiceTypeName()} " +
537 $"{prop.DeclaringType.NiceTypeName()}.{prop.Name}");
546 Assert.IsTrue(
elapsed.TotalSeconds < 10,
"Expected this test to take significantly " +
547 "less than 10 seconds to run since it should be making no server requests. " +
548 $"Actual time taken was {elapsed.TotalMilliseconds}ms.");
550 #endregion Test Reflection
static Reflection Reflection
Shared instance of a class for generating random resources.
Create a test class that takes care of setting the server URL and cleaning up after each unit test.
static void ApplyConfiguredCredentials()
Apply the current configured API_URL and API_AuthenticationToken credentials to the static API.
Exposes sample resource objects, with built-in methods for injecting dependencies.
static List< Type > GetAPIResourceTypes()
A list of base "production" resource types on the server, (e.g. each with their own collection) used ...
IInjectableResource< BinomialDistribution > Distribution_BinomialDistribution
static bool AlmostEquals(double d1, double d2, double tolerance=DEFAULT_DOUBLE_EQUIVALENCE_TOLERANCE)
Determines if two doubles are equivalent within the specified tolerance.
static void DoublesAreEqual(double expected, double actual, Func< string > message, double? relative_tolerance=null)
Determines if two doubles are equivalent within the accepted tolerance.
Utilities for cleaning up the server.
static bool ForceCleanServer(bool ignoreExceptions=false, int? timeout=null)
Invoke the DELETE method on the root of the server. Will wait for the DELETE to complete successfully...
static bool CheckServerIsClean(bool ignoreExceptions=false)
Check all known server endpoints and determine whether there are any resources on the server.
Retrieve settings from environment variables if they exist, or the project settings file otherwise.
static bool ROOT_DELETE_SUPPORTED
Indicates whether the server currently supports the DELETE method on the server root to wipe out all ...
static bool RESOURCE_DELETE_SUPPORTED
Indicates whether the server currently supports the DELETE method on resources.
static bool EXECUTING_IN_PARALLEL
If true, tests are being run in parallel, so certain assumptions about the state of the server after ...
static MockResponse RetryAfter(double? retry_time, int? queue_position)
Throw a server-like retry-after response.
A series of generated messages and formatted strings for use in unit tests.
static string AutoFormat(object obj)
Format a value as a string for output in a message.
A collection of filthy hacks to populate some fields of APIResources objects of any type.
static IEnumerable< Type > GetAllInstantiableSubtypes(Type type)
Creates a list of types that can be derived from a given type.
A custom exception class that includes the RestSharp.IRestResponse that generated the exception,...
Implements the basic reference entity interface, but has no support for resolving references.
Describes a collection of resources which can be listed.
API methods / requests made available to the user.
static string GetCollectionName(Type requestType)
Gets the collection name for the given APIResource type, whether it's instantiable or not.
Abstract representation of a distribution, used for parametric loss sets.
Abstract representation of a layer. This resource type cannot be instantiated instead derived resourc...
Representation of an Aggregate Catastrophe Excess of Loss layer.
Representation of a Quota Share contract that is limited in annual payouts.
Representation of a Catastrophe Excess of Loss (CatXL) layer.
Filter is like a layer whose 'terms' are to filter events out of the loss sources....
Representation of an Aggregate Catastrophe Excess of Loss layer with reinstatements.
Representation of an Industry Loss Warranty, which is a layer that triggers a payout (currently expre...
Allows one or more source layers or layer_views to be attached as loss sources to some other layer de...
Representation of a Quota Share contract.
Representation of a Surplus Share contract.
Base class for all LossSet sub-types. A LossSet is a resource that generates sample (trial) losses wh...
Representation of a single loss set with an associated event loss table.
Thrown when a request requires additional time to complete, but it exceeds the time we are willing to...
Determines the behaviour of the API when automatically retrying a request whose result is not yet rea...
static PollingOptions Default
The default PollingArguments used when none are specified.
Representation of a portfolio.
Representation of a portfolio.
Utility for resolving types given only the types name (Useful for parsing ambiguous JSON objects).
static Type ResolveBaseTypeByURL(string href)
Get the base type of object referenced by the supplied server URL.
static Type GetGenericCovariantBase(Type T)
Returns the most strongly typed covariant base type which is assignable from the specified type T.
T Posted
The posted resource, ready to be referenced.
Interface for Base class used by all resources.
Interface shared by all object types and resources returned by the Analyze Re server.
Abstract representation of a layer.
Base interface for all reference entities.
Abstract representation of a layer with terms.