C# Client Library
A C# Client Library for the AnalyzeRe REST API
Loading...
Searching...
No Matches
BaseAbstractResourceTestsSuite.cs
Go to the documentation of this file.
1using System;
2using System.Linq;
3using System.Net;
4using AnalyzeRe;
7#if MSTEST
8using Microsoft.VisualStudio.TestTools.UnitTesting;
9#elif NUNIT
10using NUnit.Framework;
11using TestClass = NUnit.Framework.TestFixtureAttribute;
12using TestMethod = NUnit.Framework.TestAttribute;
13using TestCategory = NUnit.Framework.CategoryAttribute;
14#endif
15
17{
24 [TestClass]
27 {
28 private const string TypeName = "BaseAbstractStoredResourceTestSuite";
29
30 [TestMethod, TestCategory(TypeName), TestCategory("Randomized")]
32 {
34 Assert.Inconclusive("RANDOMIZED_TESTS_ENABLED = false");
35 // Get a collection with one of each type and verify that references were resolved.
36 GenericTest.GET_Collection_AllSubtypes(collection_source, Reflection,
38 additionalActions: list => list.items.ToList().ForEach(res => AssertApi.ReferencesResolved(res)),
39 // Resolving references can be much slower than a normal request, so...
40 timeout: EnvironmentSettings.COLLECTION_REQUEST_TIMEOUT * 10);
41 }
42 }
43
48 [TestClass]
51 {
52 #region Properties
53 private const string TypeName = "BaseAbstractResourceTest";
54
57 protected virtual bool PUT_Allowed => true;
58
61 protected abstract IResourceCollection<T> collection_source { get; }
62 #endregion Properties
63
64 #region GET
65 [TestMethod, TestCategory(TypeName)]
66 public virtual void Test_Resource_GET_Collection()
67 {
68 GenericTest.GET_Collection(collection_source);
69 }
70
71 [TestMethod, TestCategory(TypeName), TestCategory("Randomized")]
73 {
75 Assert.Inconclusive("RANDOMIZED_TESTS_ENABLED = false");
76 GenericTest.GET_Collection_AllSubtypes(collection_source, Reflection);
77 }
78
79
80 [TestMethod, TestCategory(TypeName), TestCategory("Randomized")]
82 {
84 Assert.Inconclusive("RANDOMIZED_TESTS_ENABLED = false");
85 // Request an offset of 1, and get 2 items in the page, and verify 2 or less were returned
86 GenericTest.GET_Collection_AllSubtypes(collection_source, Reflection, 1, 2,
88 {
89 // Look at the meta to determine how many items existed in the collection
90 // when this request was made.
91 long total_available = pagedList.meta.total_count;
92 int returned = pagedList.items.Count();
93 // If only one item was added, the pagedList should be empty (since we skipped one item)
94 if (total_available == 1)
95 Assert.AreEqual(0, returned, "Only one item existed in the collection, " +
96 "so getting offset of 1 should have yielded an empty list.");
97 if (total_available >= 2)
98 Assert.IsTrue(returned > 0, "More than one item existed in the collection, " +
99 "so getting offset of 1 should have yielded a non-empty list.");
100 if (total_available >= 3)
101 Assert.AreEqual(2, returned, "The pagedList limit was set to 2, " +
102 "so there should be exactly 2 results.");
103 });
104 }
105
108 [TestMethod, TestCategory(TypeName)]
109 public virtual void Test_Resource_GET_NonExistant()
110 {
112 Assert.Inconclusive("RUN_OFFLINE = true");
113 AssertApi.ExceptionThrown(
114 () => GenericTest.Mock<T>(Samples.Valid_NonExistant_UUID).Get(),
116 }
117
120 [TestMethod, TestCategory(TypeName)]
121 public virtual void Test_Resource_GET_BadlyFormed()
122 {
124 Assert.Inconclusive("RUN_OFFLINE = true");
125 AssertApi.ExceptionThrown(
126 () => GenericTest.Mock<T>("MALFORMED").Get(),
128 }
129
130 [TestMethod, TestCategory(TypeName)]
131 public virtual void Test_Resource_GET_EmptyID()
132 {
133 // TODO: Since the C# client throws this, assert the error message is nice.
134 AssertApi.ExceptionThrown<ArgumentException>(
135 () => GenericTest.Mock<T>("").Get());
136 }
137
138 [TestMethod, TestCategory(TypeName)]
139 public virtual void Test_Resource_GET_NullID()
140 {
141 // TODO: Since the C# client throws this, assert the error message is nice.
142 AssertApi.ExceptionThrown<ArgumentException>(
143 () => GenericTest.Mock<T>(null).Get());
144 }
145 #endregion GET
146
147 #region POST
148 [TestMethod, TestCategory(TypeName), TestCategory("Randomized")]
149 public virtual void Test_Resource_POST_AllTypes()
150 {
152 Assert.Inconclusive("RANDOMIZED_TESTS_ENABLED = false");
153 GenericTest.POST_AllSubtypes_Succeeds<T>(Reflection);
154 }
155 #endregion POST
156
157 #region PUT
160 [TestMethod, TestCategory(TypeName)]
161 public virtual void Test_Resource_PUT_NonExistant()
162 {
164 Assert.Inconclusive("RUN_OFFLINE = true");
166 PUT_Allowed ? HttpStatusCode.NotFound : HttpStatusCode.MethodNotAllowed;
167 AssertApi.ExceptionThrown(
168 () => GenericTest.Mock<T>(Samples.Valid_NonExistant_UUID).Put(),
170 }
171
174 [TestMethod, TestCategory(TypeName)]
175 public virtual void Test_Resource_PUT_BadlyFormed()
176 {
178 Assert.Inconclusive("RUN_OFFLINE = true");
180 PUT_Allowed ? HttpStatusCode.NotFound : HttpStatusCode.MethodNotAllowed;
181 AssertApi.ExceptionThrown(
182 () => GenericTest.Mock<T>("MALFORMED").Put(),
184 }
185
186 [TestMethod, TestCategory(TypeName)]
187 public virtual void Test_Resource_PUT_EmptyID()
188 {
189 // TODO: Since the C# client throws this, assert the error message is nice.
190 AssertApi.ExceptionThrown<ArgumentException>(
191 () => GenericTest.Mock<T>("").Put());
192 }
193
194 [TestMethod, TestCategory(TypeName)]
195 public virtual void Test_Resource_PUT_NullID()
196 {
197 // TODO: Since the C# client throws this, assert the error message is nice.
198 AssertApi.ExceptionThrown<ArgumentException>(
199 () => GenericTest.Mock<T>(null).Put());
200 }
201 #endregion PUT
202 }
203}
Tests for a collection (or base resource type) which do not require an instance of that resource to b...
virtual void Test_Resource_PUT_BadlyFormed()
Verify that doing a PUT for a resource with a malformed id returns a NotFound error.
virtual void Test_Resource_PUT_NonExistant()
Verify that doing a PUT for a resource that doesn't exist returns a NotFound error.
virtual void Test_Resource_GET_BadlyFormed()
Verify that doing a get for a resource with a malformed id returns a NotFound error.
virtual void Test_Resource_GET_NonExistant()
Verify that doing a get for a resource that doesn't exist returns a NotFound error.
virtual bool PUT_Allowed
Whether resources of this type support the PUT method. Deriving class should override the below if th...
IResourceCollection< T > collection_source
The ICollection object that can be used to retrieve resources of this type (as an alternative to the ...
Tests for a stored resource collection which do not require an instance of that resource to be define...
static Reflection Reflection
Shared instance of a class for generating random resources.
Create a test class that takes care of setting the server URL and cleaning up after each unit test.
Exposes sample resource objects, with built-in methods for injecting dependencies.
Definition Samples.cs:14
static string Valid_NonExistant_UUID
Definition Samples.cs:41
static void ReferencesResolved(object obj)
Definition AssertApi.cs:558
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...
static bool RANDOMIZED_TESTS_ENABLED
If false, tests that involve random generation of resources (which can be unstable) will be skipped.
Generic Unit test implementations that will test REST methods on arbitrary resources.
A collection of filthy hacks to populate some fields of APIResources objects of any type.
Definition Reflection.cs:41
Describes a collection of resources which can be listed.
Parameters that can be added to your REST requests to access additional API features.
static RequestParameters ExpandReferences()
Can be added to your GET requests to recursively resolve resource references in the response....
API methods / requests made available to the user.
Interface for Base class used by all stored resources.
Interface for Base class used by all resources.