C# Client Library
A C# Client Library for the AnalyzeRe REST API
Loading...
Searching...
No Matches
Test_API_Serializers.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using AnalyzeRe;
8
9#if MSTEST
10using Microsoft.VisualStudio.TestTools.UnitTesting;
11#elif NUNIT
12using NUnit.Framework;
13using TestClass = NUnit.Framework.TestFixtureAttribute;
14using TestMethod = NUnit.Framework.TestAttribute;
15using TestCategory = NUnit.Framework.CategoryAttribute;
16#endif
17
19{
20 [TestClass]
22 {
23 private const string TestCategory = "Serializers";
24
27 [TestMethod, TestCategory(TestCategory)]
29 {
32 Samples.GetInjectableLayerView(new Nested
33 {
34 description = "Sample Cascaded Layer",
36 sources = new Nested.Sources(catXL.AsInlinedReference)
37 });
38
39 GenericTest.POST_ValidResource(layerView.Unposted);
40 }
41
44 [TestMethod, TestCategory(TestCategory)]
46 {
49 Samples.GetInjectableLayerView(new Nested
50 {
51 description = "Sample Cascaded Layer",
53 sources = new Nested.Sources(catXL.AsReference)
54 });
55 GenericTest.POST_ValidResource(layerView.Unposted);
56 }
57
60 [TestMethod, TestCategory(TestCategory)]
62 {
64 CatXL modified = testLayer.Unposted;
65 modified.meta_data = new Dictionary<string, object>
66 {
67 { "One", 1 },
68 { "Two", 2.22 },
69 { "Three", "Three" },
70 //Skip.Indefinitely("APIV1 currently only supports flat metadata.");
71 //{ "Four",
72 // new Dictionary<string, object>
73 // {
74 // { "A", "a" },
75 // { "B", 2 },
76 // { "C", 3.33 }
77 // }
78 //},
79 //{ "Five", new List<object> { 1, 2, 3, 4, 5 } },
80 //{ "Six", new List<object> { "1", "2", "3", "4", "5" } },
81 //{ "Seven", new List<object> { 1, "2", 3.33, "4" } }
82 };
84 //Skip.Indefinitely("APIV1 currently only supports flat metadata.");
85 //Assert.AreEqual(typeof(Dictionary<string, object>), result.meta_data["Four"].GetType());
86 //Assert.AreEqual(typeof(List<object>), result.meta_data["Seven"].GetType());
87 //Assert.AreEqual(typeof(List<object>), result.meta_data["Five"].GetType());
88 //Assert.AreEqual(typeof(List<object>), result.meta_data["Six"].GetType());
89 AssertApi.PostResponseMatches(modified, result);
90 }
91
95 [TestMethod, TestCategory(TestCategory)]
97 {
99
101 string json = weakReference.Serialize();
102 ILayerView<ILayer> restored = deserializer.Deserialize<ILayerView<ILayer>>(json);
103 AssertApi.AllPropertiesEqual(weakReference, restored);
104
105 // Wrap that weak reference in a Nested sources list.
106 Nested nested = new Nested();
107 nested.sources.Add(weakReference.layer.ToReference());
108 json = nested.Serialize();
109 Nested restored2 = deserializer.Deserialize<Nested>(json);
110 AssertApi.AllPropertiesEqual(nested, restored2);
111
112 // Wrap that in a nested layer
113 weakReference = new LayerView<Nested>(nested, null, null);
114 json = weakReference.Serialize();
115 restored = deserializer.Deserialize<ILayerView<ILayer>>(json);
116 AssertApi.AllPropertiesEqual(weakReference, restored);
117 }
118
121 [TestMethod, TestCategory(TestCategory)]
123 {
125 // Give the sub-layer property an id (to ensure nested server-generated attributes are maintained)
126 catXLView.layer.id = Samples.Valid_NonExistant_UUID;
127 // Invoke the serialize helper method
128 string serializedResult = catXLView.Serialize(pretty: true);
129 Console.WriteLine("Serialized (default):\n" + serializedResult);
130
131 // Test that all properties were returned by deserializing the result and comparing all attributes.
133 ILayerView<CatXL> deserialized = deserializer.Deserialize<ILayerView<CatXL>>(serializedResult);
134 AssertApi.AllPropertiesEqual(catXLView, deserialized);
135 // The above only tests user-editable properties.
136 // Also assert that server-generated properties were maintained.
137 Assert.AreEqual(catXLView.id, deserialized.id);
138 Assert.AreEqual(catXLView.ylt_id, deserialized.ylt_id);
139 Assert.AreEqual(catXLView.layer.id, deserialized.layer.id);
140
141 // Now assert that if we explicitly exclude server-generated properties, they are lost.
142 serializedResult = catXLView.Serialize(true, false);
143 Console.WriteLine("\nSerialized (exclude server-generated):\n" + serializedResult);
144 deserialized = deserializer.Deserialize<ILayerView<CatXL>>(serializedResult);
145 Assert.IsNull(deserialized.id);
146 Assert.IsNull(deserialized.ylt_id);
147 Assert.IsNull(deserialized.layer.id);
148 }
149 }
150}
Create a test class that takes care of setting the server URL and cleaning up after each unit test.
A class containing a resource that can be Posted with dependencies.
T Posted
The posted resource, ready to be referenced.
Exposes sample resource objects, with built-in methods for injecting dependencies.
Definition Samples.cs:14
IInjectableResource< Generic > Layer_Generic
static string Valid_NonExistant_UUID
Definition Samples.cs:41
IInjectableResource< CatXL > Layer_CatXL
IInjectableResource< ILayerView< CatXL > > LayerView_CatXL
void Test_API_POST_NestedLayer_Referenced()
Test that the serializer produces a valid Cascaded Loss Set containing a reference to a layer when th...
void Test_Serialize_Extension()
Test that the serialize extension method returns all properties of the resource, including server-gen...
void Test_API_Serialize_WeaklyTypedReference()
Serializers and comparers are sensitive to dealing with weakly typed generic types,...
void Test_API_POST_NestedLayer_Nested()
Test that the serializer produces a valid Cascaded Loss Set containing a fully resolved layer when th...
void Test_API_POST_Metadata_Nested_Dictionary_and_List()
Test that deserializing a layer whose metadata contains a nested dictionary correctly returns a dicti...
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...
Generic Unit test implementations that will test REST methods on arbitrary resources.
A pre-configured RestSharp serializer / deserializer which is made to support parsing of AnalyzeRe AP...
static readonly AnalyzeReJsonConverter Default
Returns the default AnalyzeReJsonConverter instance.
object Deserialize(string json, Type deserializeType)
Deserializes the rest response to a run-time type as per this converter's configuration.
Represents the Analysis of a Layer.
Representation of a Catastrophe Excess of Loss (CatXL) layer.
Definition CatXL.cs:9
A wrapper for a List<T> of IReference<ILayerSource> references that simplifies the construction and u...
Definition Nested.cs:67
void Add(IReference reference, bool automaticTyping=true)
Allows a weakly-typed reference to be added to this collection, with the possibility of runtime check...
Allows one or more source layers or layer_views to be attached as loss sources to some other layer de...
Definition Nested.cs:22
Sources sources
A list of the source layers forwarding losses to this layer.
Definition Nested.cs:43
Interface for a class containing a resource that can be Posted with dependencies.
IReference< T > AsInlinedReference
A reference to the unPOSTed resource (i.e. an inlined reference).
IReference< T > AsReference
A reference to the posted resource.
T Unposted
The unPOSTed resource definition.
T Posted
The posted resource, ready to be referenced.
string ylt_id
The hash identifier for the simulation results of this layer.
string id
The resource's unique identifier. It will be used in the request URL when requesting the resource fro...
Represents the Analysis of a Layer.
ILayer layer
The layer contained in this LayerView.
Definition ILayerView.cs:12