2using System.Diagnostics;
11using Microsoft.VisualStudio.TestTools.UnitTesting;
14using TestClass = NUnit.Framework.TestFixtureAttribute;
15using AssemblyCleanup = NUnit.Framework.OneTimeTearDownAttribute;
25 #region Static Properties
28 public static string API_URL {
get;
set; }
34 private static bool? _lastTestCleanupSucceeded;
37 private static readonly
object SuiteInitializeLock =
new object();
40 private static volatile int _suiteInitializeCalled = 0;
41 #endregion Static Properties
43 #region Instance Properties
47 #endregion Instance Properties
49 #region Static Test Suite Constructor
68 ServicePointManager.ServerCertificateValidationCallback =
69 (sender, certificate, chain, errors) =>
true;
86 lock (SuiteInitializeLock)
88 if (_suiteInitializeCalled != 0)
90 Console.WriteLine(
"WARNING: Tried to initialize the test suite on thread " +
91 Thread.CurrentThread.ManagedThreadId +
" but another test thread (" +
92 _suiteInitializeCalled +
") has already invoked TestSuiteInitialize.");
95 _suiteInitializeCalled = Thread.CurrentThread.ManagedThreadId;
99 context.WriteLine(
"Test Settings:");
103 context.WriteLine(
"Tests will be run in parallel.");
113 context.WriteLine(
"Root DELETE is set to \"not supported\". Root DELETE will not be called.");
118 context.WriteLine(
"Root DELETE will be called " +
120 "before each test is run."));
122 context.WriteLine(
"Root DELETE will be called " +
124 "after each test is run."));
127 context.WriteLine(
"Certificate Verification has been Disabled.");
129 context.WriteLine(
"Randomized Tests have been Disabled.");
131 context.WriteLine(
"Skipping tests has been Disabled, skipped tests will be run.");
133 context.WriteLine(
"Any skipped tests past their expiry will raise an error.");
135 context.WriteLine(
"Any skipped tests past their expiry will be run.");
139 context.WriteLine(
"Debug logs for all API requests have been enabled.");
140 API.RequestLogging =
true;
143 API.RequestLogging =
false;
146 context.WriteLine(
"Debug logs for all API requests is unavailable in Release mode.");
154 ForceServerCleanupAction();
168 ForceServerCleanupAction();
171 #endregion Static Test Suite Constructor
180 bool? CleanAfterEveryTest =
null)
190 if (_lastTestCleanupSucceeded ==
false &&
191 TestContext.CurrentTestOutcome != UnitTestOutcome.Passed)
195 Assert.Fail(
"Warning: This test did not pass, but this may have been " +
196 "due to the fact that the previous test failed to successfully clean " +
197 "up after itself, so the server was not clean when this test started.");
209 (CleanBeforeEveryTest ?? EnvironmentSettings.ENFORCE_CLEANUP_BEFORE_RUN &&
210 EnvironmentSettings.ROOT_DELETE_SUPPORTED &&
221 #endregion Constructor
223 #region Setup/Teardown methods
229 API.ManagedSettingsEnabled =
false;
238 DateTime start = DateTime.UtcNow;
250 API.DefaultRequestTimeout = API.DefaultRequestTimeoutCollections =
251 Settings.Default.REQUEST_TIMEOUT = Settings.Default.COLLECTION_REQUEST_TIMEOUT =
252 Settings.Default.DATA_UPLOAD_TIMEOUT = Settings.Default.DELETE_TIMEOUT =
253 Settings.Default.OPTIMIZATION_TIMEOUTS = Settings.Default.POLLING_INTERVAL =
254 Settings.Default.SIMULATION_TIMEOUT = 1;
255 API.MaxRequestRetries = 0;
259 Assert.Fail(
"Could not communicate with server at " +
API_URL +
260 ". Test cannot run. (" + (DateTime.UtcNow - start).TotalSeconds +
261 " seconds)\nError Details: " + ex);
266 private void DeleteAllResources()
270 ForceServerCleanupAction();
274 private static void ForceServerCleanupAction()
276 Debug.WriteLine(
"LastTestCleanupSucceeded: " + (_lastTestCleanupSucceeded ?? (
object)
"(null)"));
282 _lastTestCleanupSucceeded ==
false,
288 #endregion Setup/Teardown methods
Create a test class that takes care of setting the server URL and cleaning up after each unit test.
bool DeletingResourcesBetweenTests
Determines based on the EnvironmentSettings whether injected resources (such as Samples) are being de...
static string API_URL
Location of the REST API being tested against. This can be set using the API_URL environment variable...
static void CheckConnection()
Set the Client API Server URL to the configured API_URL.
static void ApplyConfiguredCredentials()
Apply the current configured API_URL and API_AuthenticationToken credentials to the static API.
static IAccessToken API_AuthenticationToken
Set the AuthenticationToken to supply to the server before every test (if applicable).
static void TestSuiteInitialize(TestContext context)
Actions to run once when the C# Unit Tests initialize. Note: This method will not automatically be in...
static void TestSuiteCleanup()
Actions to run once when the C# Unit Tests finish. Note: This method will not automatically be inheri...
BaseServerTestSuite(bool? CleanBeforeEveryTest=null, bool? CleanAfterEveryTest=null)
BaseServerAPITest Constructor. MSTest runs this constructor for each test.
void AddCommonTestCleanupAction(Action action)
void AddCommonTestInitializeAction(Action action)
Exposes sample resource objects, with built-in methods for injecting dependencies.
void Reset()
Reset all cached injected resources.
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...
Retrieve settings from environment variables if they exist, or the project settings file otherwise.
static void Reload()
Read all settings and environment variables again and re-assign all properties.
static int DATA_UPLOAD_TIMEOUT
Amount of time to wait for data upload requests to complete (in milliseconds).
static string API_URL
The URL against which server tests are run.
static bool ENABLE_REQUEST_DEBUG_LOGGING
Controls whenther AnalyzeRe.Client.API.DebugLogging is enabled.
static int COLLECTION_REQUEST_TIMEOUT
Amount of time to wait for requests on collection endpoints to complete (in milliseconds).
static bool ENFORCE_CLEANUP_BEFORE_RUN
Indicates whether the server should be wiped clean before every unit test is run.
static int OPTIMIZATION_TIMEOUTS
Amount of time to wait for optimization engine requests to complete (in milliseconds).
static bool FAIL_ON_EXPIRED_SKIP
If true, expired SkipUntils will raise an exception saying they are expired. If false,...
static bool RUN_OFFLINE
Controls whether tests that normally require a connection to the server should be allowed to try to r...
static int REQUEST_TIMEOUT
Amount of time to wait for requests to complete (in milliseconds).
static int DELETE_TIMEOUT
Amount of time to wait for DELETE to complete (in milliseconds).
static bool ROOT_DELETE_SUPPORTED
Indicates whether the server currently supports the DELETE method on the server root to wipe out all ...
static bool RANDOMIZED_TESTS_ENABLED
If false, tests that involve random generation of resources (which can be unstable) will be skipped.
static int POLLING_INTERVAL
Frequency with which to poll the server for updates during tests (in milliseconds).
static int SIMULATION_TIMEOUT
Amount of time to wait for simulations to complete (in milliseconds).
static bool VALIDATE_SERVER_CERTIFICATE
Indicates whether the SSL/TLS certificate for the HTTPS server should be validated.
static bool SKIPS_ENABLED
If true, SkipUntils can be used to skip tests. If false, SkipUntils will be ignored and all tests wil...
static bool ENFORCE_CLEANUP_AFTER_RUN
Indicates whether the server should be wiped clean after every unit test is run.
static string HTTPS_USER
The username for the HTTPS server authentication.
static string HTTPS_PASSWORD
The password for the HTTPS server authentication.
static bool EXECUTING_IN_PARALLEL
If true, tests are being run in parallel, so certain assumptions about the state of the server after ...
API methods / requests made available to the user.
static OptionsResponse Options(IEnumerable< Parameter > requestParameters=null, int? timeout=null)
Perform an OPTIONS request on the API, which returns a list of features supported for the current aut...
An AccessToken storing basic authentication information for requests made to the server.
An AccessToken storing authentication information for requests made to the server.