2using System.Collections;
3using System.Collections.Generic;
4using System.Diagnostics;
7using System.Reflection;
17using Microsoft.VisualStudio.TestTools.UnitTesting;
41 ex =
ex.InnerException;
55 Console.WriteLine(
"Error response from the URL: " +
57 $"\n{response.Request.PrettyPrint()}\n{response.PrettyPrint()}");
59 Console.WriteLine(
$"Unexpected exception: \"{ex.Message}\". Details:\n{ex}");
77 Console.WriteLine(
$"Assert ExceptionThrown<{typeof(T).NiceTypeName()}> " +
78 $"Successfully caught an exception of the expected type. Details:\n{e}");
85 Assert.Fail(
"Exception of the wrong type was thrown. " +
86 $"Expected type {typeof(T).NiceTypeName()}, " +
87 $"but received {e.GetType().NiceTypeName()}.");
89 Assert.Fail(
"The action ran without raising an exception. " +
90 $"Expected: {typeof(T).NiceTypeName()} to be thrown.");
109 $"{methodName} method is not allowed.");
111 $"but it should be for this test to pass.\nDetails:\n{e}");
117 " for an unexpected reason. Expected 405 - MethodNotAllowed") +
118 $", Received status: {e.RestResponse?.StatusCode.ToString() ?? "(
No Response)
"}, " +
119 $"and error message: {e.Message}\nDetails:\n{e}");
122 "but should not be for this test to pass. (Expected 405 - MethodNotAllowed)");
138 Console.WriteLine(
"\nOne or more sub-properties of these two resources were not equal:" +
139 $"\nExpected: {objExpected.Serialize()}" +
140 $"\n Actual: {objActual.Serialize()}");
159 Console.WriteLine(
"\nOne or more sub-properties of the POST response did not match:" +
160 $"\nPOST Body: {objPosted.Serialize()}" +
161 $"\n Response: {objReturned.Serialize()}");
181 Console.WriteLine(
"\nOne or more sub-properties of the PUT response did not match expectations:" +
182 $"\nOriginal: {original.Serialize()}" +
183 $"\nPUT Body: {objPut.Serialize()}" +
184 $"\n Result: {objReturned.Serialize()}");
190 private static void AllPropertiesEqual_Helper(Type
runtimeType,
208 Assert.IsNotNull(
fromClient,
$"The expected \"{propertyName}\" attribute value is null, " +
209 $"but the result was not null, it was: {Output.AutoFormat(fromServer)}");
210 Assert.IsNotNull(
fromServer,
$"The resulting \"{propertyName}\" attribute value was null, " +
211 $"but it was expected to have the following value: {Output.AutoFormat(fromClient)}");
228 $"The value {fromServer} could not be converted to the type {clientType}.");
233 Assert.Fail(
"The objects being compared are of fundamentally different types: " +
234 $"objPosted is a {clientType.NiceTypeName()}, " +
235 $"objReturned is a {fromServer.GetType().NiceTypeName()}. " +
236 $"Expected type is {sharedType.NiceTypeName()}");
264 Assert.IsTrue(
fromServer.GetType().GetGenericArguments()[0].IsAssignableFrom(
266 $"{propertyName} reference types do not match. " +
267 $"Expected: \"{clientType.NiceTypeName()}\", " +
268 $"Actual: \"{fromServer.GetType().NiceTypeName()}\".");
282 $"{propertyName} reference objects do not match: " +
283 $"Expected: {(IReference)fromClient}, " +
284 $"Actual: {(IReference)fromServer}");
294 $"{propertyName} collections do not contain the same number of elements. " +
295 $"Expected: \"{expectedCollection.Count}\", " +
296 $"Actual: \"{returnedCollection.Count}\".");
315 $"\nSpecifically - collection element number {expectedIndex} had no matching " +
316 "element in the resulting collection. Results from each comparison attempt:\n " +
317 String.Join(
"\n ",
errors.Select(
ex =>
ex.Message.Replace(
"\n",
"\n "))));
320 Debug.WriteLine(
$"All \"{propertyName}\" collection elements were found, " +
321 "but they appeared in a different order than what was originally posted.");
341 Console.WriteLine(
"Fatal error encountered while getting the property from " +
342 $"one or both {sharedType.NiceTypeName()} resources being compared. " +
343 $"Property was: {property.PropertyType.NiceTypeName()} " +
344 $"{property.DeclaringType.NiceTypeName()}.{property.Name} \nError was: {ex}");
391 Console.WriteLine(
"Warning: The server is known to provide a default value " +
392 $"when \"{property.Name}\" is set to null, but the defaulted value returned " +
393 $"({propActual ?? "null"}) could not be verified. Assuming it is valid.");
404 reference.DeepCopy().GetValue().Change(r => r.id,
null).ToReference(
true);
440 AllPropertiesEqual_Helper(
sharedType.GetGenericArguments()[0],
443 AllPropertiesEqual_Helper(
sharedType.GetGenericArguments()[1],
451 Assert.Fail(
"This routine doesn't know how to compare the following two objects:\n" +
452 $"Expected: {fromClient.GetType().NiceTypeName()} \"{fromClient}\"\n" +
453 $"Actual: {fromServer.GetType().NiceTypeName()} \"{fromServer}\"");
467 using (
new NUnit.Framework.Internal.TestExecutionContext.IsolatedContext())
516 return $"{propertyName} values do not match. " +
517 $"Type: \"{expected?.GetType().NiceTypeName() ?? "(
null)
"}\"." +
518 $"\nExpected: {Output.AutoFormat(expected)}," +
519 $"\nActual: {Output.AutoFormat(actual)}.";
532 string mod_actual = String.Join(
"\n",
actual.Split(
'\n').Select(
s =>
s.Trim())).Trim();
543 Assert.IsNotNull(
ex,
"Expected some APIRequestException, got null.");
544 string message =
"Expected an APIRequestException to contain a RestResponse " +
546 string messageEnd =
"\nReceived Exception Details:\n" +
ex;
547 Assert.IsNotNull(
ex.RestResponse, message +
548 ", but the exception contained no RestResponse." +
messageEnd);
549 Assert.IsNotNull(
ex.ServerError, message +
550 ", but the exception contained no ServerError." +
messageEnd);
551 message +=
". Received Status: " + ex.RestResponse.StatusCode +
" and Message: \"" +
552 (String.IsNullOrWhiteSpace(
ex.ServerError.message) ?
"(null)" :
ex.ServerError.message) +
"\"";
584 Assert.IsTrue(
reference.resolved,
$"A {reference.GetType().NiceTypeName()} reference " +
585 "that has not been resolved was encountered.");
597 bool isTrue = relative_tolerance.HasValue ?
612 $"Actual: {Output.AutoFormat(actual)} do not match within a tolerance of: " +
613 $"{relative_tolerance ?? DEFAULT_DOUBLE_EQUIVALENCE_TOLERANCE:G17}. " +
637 if (!
dt1.HasValue && !
dt2.HasValue)
return;
638 if (!
dt1.HasValue || !
dt2.HasValue || !
dt1.Value.AlmostEquals(
dt2.Value))
646 Assert.IsNotNull(
toCheck,
"Expected a UUID but the result was empty.");
648 "The Id \"" +
toCheck +
"\" was not a valid UUID.");
static void PostResponseMatches< T >(T objPosted, T objReturned)
Check that all properties of POST response matches expectations of how the API responds to requests....
static bool AlmostEquals(double d1, double d2, double tolerance=DEFAULT_DOUBLE_EQUIVALENCE_TOLERANCE)
Determines if two doubles are equivalent within the specified tolerance.
static bool TryFindException< T >(Exception ex, out T foundException)
Convert the current exception to the specified Exception type T , or check if any InnerExceptions mat...
static void ReferencesResolved(object obj)
static void LogExceptionDetails(Exception ex)
Creates a useful log of the exception details, and in request/response resulting in the error if any ...
const double DEFAULT_DOUBLE_EQUIVALENCE_TOLERANCE
static void MethodIsAllowed(Action request, string methodName, bool methodAllowed=true)
Wrap a request in a tryGet with some formatting for testing purposes.
static void ExceptionThrown< T >(Action action, Action< T > exceptionTest=null)
Asserts that an exception is thrown while invoking an action, and optionally performs an additional t...
static bool FileContentsAreEqual(string expected, string actual, StringComparison comparisonType=StringComparison.InvariantCulture)
Determines whether the two strings are equal, ignoring differences in line ending styles and trailing...
static void DoublesAreEqual(double expected, double actual, string message=null, double? relative_tolerance=null)
Determines if two doubles are equivalent within the accepted tolerance.
static void DatesAreEqual(DateTime? dt1, DateTime? dt2, string propertyName="DateTime")
Asserts that two dates are equal within the supported precision. Analyze Re only supports a precision...
static void IsValidUUID(string toCheck)
Assert that the string is a valid UUID.
static Action< APIRequestException > ApiExceptionTest(HttpStatusCode expectedStatusCode)
Generate a function that will test a REST request exception in a standard way.
static void DoublesAreEqual(double expected, double actual, Func< string > message, double? relative_tolerance=null)
Determines if two doubles are equivalent within the accepted tolerance.
static void PutResponseMatches< T >(T original, T objPut, T objReturned)
Check that all properties of a PUT response matches expectations of how the API responds to requests....
static void AllPropertiesEqual< T >(T objExpected, T objActual)
Check that all properties of the two objects match. A deep object value equivalency test.
A custom exception class that includes the RestSharp.IRestResponse that generated the exception,...
Describes a collection of resources which can be listed.
Describes an additional sub-resource available off a resource which is not actually a property of tha...
Indicates that the property, if left null, will have a default value generated and filled in by the s...
A wrapper for a List<T> of IReference<ILayerSource> references that simplifies the construction and u...
Allows one or more source layers or layer_views to be attached as loss sources to some other layer de...
Represents the Analysis of a Portfolio.
Utility for resolving types given only the types name (Useful for parsing ambiguous JSON objects).
static Type GetEnumerableType(Type type)
If the specified type implements IEnumerable<T>, returns the generic type argument....
Describes an endpoint on the server that can be interacted with. Inheriting from this interface means...
PortfolioView and LayerView interface.
Interface for Base class used by all resources.
Interface shared by all object types and resources returned by the Analyze Re server.
Base interface for all reference entities.
@ Failed
Data has already been uploaded but failed processing.