C# Client Library
A C# Client Library for the AnalyzeRe REST API
Loading...
Searching...
No Matches
CleanUp.cs
Go to the documentation of this file.
1using System;
2using System.Diagnostics;
3using System.Linq;
4
5using AnalyzeRe;
7
8using RestSharp;
9#if MSTEST
10using Microsoft.VisualStudio.TestTools.UnitTesting;
11#elif NUNIT
12using NUnit.Framework;
13#endif
14
16{
18 public static class CleanUp
19 {
23 public static bool CheckServerIsClean(bool ignoreExceptions = false)
24 {
26 Assert.Inconclusive("RUN_OFFLINE = true");
27 bool serverIsClean = true;
28 try
29 {
30 foreach (Type T in Samples.GetAPIResourceTypes())
31 {
32 int count = API.GetResourceList(T).items.Count();
33 if (count == 0) continue;
34 string alert = "Warning: The server has not been properly cleaned up. " +
35 "There are " + count + " items in the " + API.GetCollectionName(T) +
36 " resource collection.";
37 if (ignoreExceptions)
38 {
39 Debug.WriteLine(alert);
40 serverIsClean = false;
41 }
42 else
43 Assert.AreEqual(0, count, alert);
44 }
45 }
46 catch (Exception e)
47 {
48 Debug.WriteLine("Error: Exception during CheckServerIsClean.\n" +
49 " Exception Details: " + e);
50 if (!ignoreExceptions)
51 throw;
52 }
53 return serverIsClean;
54 }
55
63 public static bool ForceCleanServer(bool ignoreExceptions = false, int? timeout = null)
64 {
66 Assert.Inconclusive("RUN_OFFLINE = true");
67 DateTime start = DateTime.UtcNow;
68 try
69 {
70 // TODO: DELETE_TIMEOUT Currently so long because the OE can get hung up (Story #1631)
71 API.PollUntilReady(
72 () => API.RequestAndParse<string>("", Method.DELETE, null, null),
73 new PollingOptions(maxPollTotalTime: timeout ??
75 return true;
76 }
77 catch (Exception e)
78 {
79 string alert = "Cleanup of the server failed after " +
80 (DateTime.UtcNow - start).TotalSeconds + " seconds.\n" +
81 " Exception Details: " + e;
82 Debug.WriteLine("Warning: " + alert);
83 if (!ignoreExceptions)
84 Assert.Fail(alert);
85 return false;
86 }
87 }
88
93 public static bool TryDelete_Silent(IAPIResource item)
94 {
97 Assert.Inconclusive("RUN_OFFLINE = true");
98 if (item == null) return true;
99 try
100 {
101 item.Delete();
102 return true;
103 }
104 catch (Exception e)
105 {
106 Debug.WriteLine("Warning: Cannot delete the " + item.GetType().NiceTypeName() +
107 " with id: " + item.id + ".\n Exception Details: " + e);
108 return false;
109 }
110 }
111 }
112}
Exposes sample resource objects, with built-in methods for injecting dependencies.
Definition Samples.cs:14
static List< Type > GetAPIResourceTypes()
A list of base "production" resource types on the server, (e.g. each with their own collection) used ...
Utilities for cleaning up the server.
Definition CleanUp.cs:19
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...
Definition CleanUp.cs:63
static bool CheckServerIsClean(bool ignoreExceptions=false)
Check all known server endpoints and determine whether there are any resources on the server.
Definition CleanUp.cs:23
static bool TryDelete_Silent(IAPIResource item)
Attempt to delete the resource, but throw no exceptions if it cannot be deleted.
Definition CleanUp.cs:93
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...
static int DELETE_TIMEOUT
Amount of time to wait for DELETE to complete (in milliseconds).
static bool RESOURCE_DELETE_SUPPORTED
Indicates whether the server currently supports the DELETE method on resources.
API methods / requests made available to the user.
static ICollectionResponse< IAPIResource > GetResourceList(Type resourceType, IEnumerable< Parameter > requestParameters=null, string collectionNameOverride=null, int? timeout=null)
Get a collection of resources from the server.
static object RequestAndParse(Type deserializeType, string resource, Method method, IEnumerable< Parameter > requestParameters=null, int? timeout=null)
Perform a REST request on the server and serializes the response to the desired run-time type.
static string GetCollectionName(Type requestType)
Gets the collection name for the given APIResource type, whether it's instantiable or not.
Determines the behaviour of the API when automatically retrying a request whose result is not yet rea...
Interface for Base class used by all resources.
string id
The resource's unique identifier. It will be used in the request URL when requesting the resource fro...