C# Client Library
A C# Client Library for the AnalyzeRe REST API
Loading...
Searching...
No Matches
Test_API_BugReports.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.Net;
4
5using AnalyzeRe;
11
12#if MSTEST
13using Microsoft.VisualStudio.TestTools.UnitTesting;
14#elif NUNIT
15using NUnit.Framework;
16using TestClass = NUnit.Framework.TestFixtureAttribute;
17using TestMethod = NUnit.Framework.TestAttribute;
18using TestCategory = NUnit.Framework.CategoryAttribute;
19#endif
20
22{
23 [TestClass]
25 {
26 private const string Category = "Test API BugReports";
27
29 {
31 {
32 // All tests in this class require a connection.
34 Assert.Inconclusive("RUN_OFFLINE = true");
35 });
36 }
37
38 [TestMethod, TestCategory(Category)]
40 {
41 // In this issue, each API thread maintained its own cache instance which was not being
42 // cleared between requests - so to increase the odds of hitting a "Dirty" worker thread,
43 // we submit the same request multiple times.
44 const int api_workers = 4;
45
46 // Get the analysis profile that will be used for validation.
48 // Post a layer that uses a loss set with the same catalog as the above analysis profile
49 CatXL goodLayer = Samples.Layer_CatXL.Posted;
50
51 // Post Several QCLS Loss Sets with one event catalog.
52 EventCatalog differentCatalog = Samples.EventCatalog.Posted.Post()
53 .PollUntilReady(Samples.DataPollingOptions);
54 Simulation differentSimulation = Samples.Simulation_StaticSimulation.Posted
55 .Change(s => s.event_catalogs,
56 new List<IReference<EventCatalog>> { differentCatalog.ToReference() })
57 .Post().PollUntilReady(Samples.DataPollingOptions);
58 QCLSLossSet[] qclsLossSets = new QCLSLossSet[api_workers];
59
60 // Post QCLS Loss Sets that reference the wrong event catalog.
61 for (int i = 0; i < api_workers; i++)
62 {
63 qclsLossSets[i] = new QCLSLossSet
64 {
65 description = "QCLS" + i,
66 simulation = differentSimulation.ToReference(),
67 correlation_coefficient = 1d,
69 currency = "USD"
70 }.Post();
71
72 // Post nested layer_view(s) that intentionally reference the invalid loss sets.
73 try
74 {
75 IReference<Filter> CreateInlinedFilter() => new Filter
76 {
77 loss_sets = new List<IReference<LossSet>> { qclsLossSets[i].ToReference() }
78 }.ToReference();
80 {
81 analysis_profile = ap.ToReference(),
82 layer = new Nested
83 {
84 sink = CreateInlinedFilter(),
85 sources = new Nested.Sources(CreateInlinedFilter())
86 }
87 }.Post();
88 }
89 // Ignore the error that is raised.
90 catch (Exception ex)
91 {
92 Console.Write("Expected error:" + ex.Message);
93 }
94 }
95
96 for (int i = 0; i < api_workers; i++)
97 {
98 // Post a layer_view that references only good things. This post should succeed,
99 // but in the issue scenario, it fails due to old information from the previous POST
101 {
102 analysis_profile = ap.ToReference(),
103 layer = new Nested
104 {
105 sink = goodLayer.ToReference(),
106 sources = new Nested.Sources(goodLayer.ToReference())
107 }
108 }.Post();
109 }
110 }
111
112 [TestMethod, TestCategory(Category)]
114 {
115 // Get the analysis profile that will be used for validation.
117
118 // Post Several QCLS Loss Sets with one event catalog.
119 EventCatalog differentCatalog = Samples.EventCatalog.Posted.Post()
120 .PollUntilReady(Samples.DataPollingOptions);
121 Simulation differentSimulation = Samples.Simulation_StaticSimulation.Posted
122 .Change(s => s.event_catalogs,
123 new List<IReference<EventCatalog>> { differentCatalog.ToReference() })
124 .Post().PollUntilReady(Samples.DataPollingOptions);
125 QCLSLossSet badQCLSLossSets = new QCLSLossSet
126 {
127 description = "Bad QCLS",
128 simulation = differentSimulation.ToReference(),
129 correlation_coefficient = 1d,
131 currency = "USD"
132 }.Post();
133
134 QuotaShare quotaShareWithBadLossSet = new QuotaShare
135 {
136 description = "QS with bad QCLS",
137 event_limit = new MonetaryUnit(Double.MaxValue, "USD"),
138 participation = 1.0,
139 loss_sets = new List<IReference<LossSet>> { badQCLSLossSets.ToReference() }
140 }.Post();
141
142 // Post nested layer_view(s) that intentionally reference the invalid loss sets.
143 ILayerView badLayerView = new LayerView<Nested>
144 {
145 analysis_profile = ap.ToReference(),
146 layer = new Nested
147 {
148 sink = quotaShareWithBadLossSet.ToReference(),
149 sources = new Nested.Sources(quotaShareWithBadLossSet.ToReference())
150 }
151 };
152 AssertApi.ExceptionThrown(() => badLayerView.Post(),
153 AssertApi.ApiExceptionTest(HttpStatusCode.BadRequest));
154 }
155
156 [TestMethod, TestCategory(Category), TestCategory("Randomized")]
158 {
159 // Test against random failures observed when generating references for base types
160 // Test at multiple recursion depths in case this is responsible for the behaviour
161 Type testType = typeof(ILayerSource);
162 for (int i = 0; i < 10; i++)
163 {
164 object result = Reflection.GetRandomExistingResource(testType,
165 new Reflection.RecursionContext(testType, i));
166 if(result is IReference<ILayerSource> randomReference)
167 Console.WriteLine($"Test {i + 1} generated reference: {randomReference.ref_id} " +
168 $"with type {result.GetType().NiceTypeName()}");
169 else
170 Assert.Fail($"Test {i + 1} generated a {result?.GetType().NiceTypeName() ?? "null"}");
171 }
172 }
173 }
174}
Create a test class that takes care of setting the server URL and cleaning up after each unit test.
void AddCommonTestInitializeAction(Action action)
Exposes sample resource objects, with built-in methods for injecting dependencies.
Definition Samples.cs:14
static readonly PollingOptions DataPollingOptions
Polling options to use when uploading a data file.
IInjectableResource< CustomSeverityDistribution > Distribution_CustomSeverityDistribution
IInjectableResource< AnalysisProfile > AnalysisProfile
An analysis profile with one of each possible type of filter.
IInjectableResource< CatXL > Layer_CatXL
IInjectableResource< EventCatalog > EventCatalog
static Action< APIRequestException > ApiExceptionTest(HttpStatusCode expectedStatusCode)
Generate a function that will test a REST request exception in a standard way.
Definition AssertApi.cs:539
Retrieve settings from environment variables if they exist, or the project settings file otherwise.
static bool RUN_OFFLINE
Controls whether tests that normally require a connection to the server should be allowed to try to r...
Used to keep track of the recursive random generation process for objects and their attributes....
A collection of filthy hacks to populate some fields of APIResources objects of any type.
Definition Reflection.cs:41
IReference< IAPIResource > GetRandomExistingResource(Type T, RecursionContext currentRecursion)
Tries to find an existing (POSTed) resource of the specified type without generating a new one.
A configuration of resources used to simulate a layer or portfolio.
Representation of an event catalog. The event catalog may cover multiple region/perils,...
Represents the Analysis of a Layer.
Representation of a Catastrophe Excess of Loss (CatXL) layer.
Definition CatXL.cs:9
Filter is like a layer whose 'terms' are to filter events out of the loss sources....
Definition Filter.cs:13
A wrapper for a List<T> of IReference<ILayerSource> references that simplifies the construction and u...
Definition Nested.cs:67
Allows one or more source layers or layer_views to be attached as loss sources to some other layer de...
Definition Nested.cs:22
Representation of a Quota Share contract.
Definition QuotaShare.cs:9
The Quantile-Based Correlated Loss Sampling (QCLS) loss set is a simulated parametric loss set which ...
Representation of a monetary value with a currency.
Representation of a simulation of a set of trials each representing an event occurrence sequence.
Definition Simulation.cs:14
IReference< T > AsReference
A reference to the posted resource.
T Posted
The posted resource, ready to be referenced.
An interface for an object that provides access to some layer instance.
Represents the Analysis of a Layer.
Base interface for all reference entities.
@ currency
Reinstatement values represent a fixed dollar amount.