C# Client Library
A C# Client Library for the AnalyzeRe REST API
Loading...
Searching...
No Matches
BaseResourceWithDataTestSuite.cs
Go to the documentation of this file.
1using System;
2using System.Net;
3using System.Text;
4
5using AnalyzeRe;
8#if MSTEST
9using Microsoft.VisualStudio.TestTools.UnitTesting;
10#elif NUNIT
11using NUnit.Framework;
12using TestClass = NUnit.Framework.TestFixtureAttribute;
13using TestMethod = NUnit.Framework.TestAttribute;
14using TestCategory = NUnit.Framework.CategoryAttribute;
15#endif
16
18{
19 [TestClass]
22 {
23 #region Properties
24 private const string TypeName = "BaseResourceWithLargeDataTest";
25
26 #region Overrideable Protected Properties
27 // Deriving class should override the below if the default behaviour isn't desired.
28 protected virtual bool DELETE_Allowed_BeforeData => DELETE_Allowed;
29 protected abstract string POSTData { get; }
30
31 // Since we now perform additional validation of resources after PUT,
32 // we must first ensure they are finalized.
33 public sealed override void AdditionalValidResourceTests(T posted)
34 {
35 if (posted.status != TaskStatus.Success)
36 {
37 posted.Get();
38 if (posted.data_file == null)
39 GenericTest.UploadData(posted, POSTData);
40 posted.PollUntilReady();
41 }
43 }
44
46 #endregion Overrideable Protected Properties
47 #endregion Properties
48
49 #region Test Methods
50 #region POST
52 [TestMethod, TestCategory(TypeName)]
53 public virtual void Test_Resource_GET_Data()
54 {
56 Assert.Inconclusive("RUN_OFFLINE = true");
57 string dataFromGet = null;
58
61 "GET on the /data endpoint appears to have succeeded, " +
62 "but didn't match the data uploaded.");
63 }
64
67 public sealed override void POST_ThenDoAction(T validToPost, Action<T> toExecute)
68 {
69 // If the current test resource has no data_file reference,
70 // upload data following the POST to finalize the resource.
71 if (validToPost.data_file == null)
72 GenericTest.POST_WithData_ThenDoAction(validToPost, POSTData, toExecute);
73 else
74 GenericTest.POST_ThenDoAction(validToPost, toExecute);
75 }
76
78 [TestMethod, TestCategory(TypeName), TestCategory("Post Valid")]
79 public sealed override void Test_Resource_POST_Valid()
80 {
82 {
83 Assert.IsNotNull(obj.data_file, "Missing the data file for this resource.");
84 Assert.IsNotNull(obj.data_file.ref_id, "Missing a data file ref_id");
86 });
87 }
88
89 #region CreatedAndModified
94 [TestMethod, TestCategory(TypeName)]
96 {
98 {
99 Assert.IsNotNull(posted.created, "Expected a 'created' date to be assigned.");
100 Assert.IsNotNull(posted.modified, "Expected a 'modified' date to be assigned.");
101 Assert.AreEqual(posted.created, posted.modified,
102 "Expected the created and modified date to be equal on a newly posted resource.");
103 });
104 }
105 #endregion CreatedAndModified
106 #endregion POST
107
108 #region PUT
110 [TestMethod, TestCategory(TypeName)]
112 {
113 // The server doesn't like empty files (at this time).
114 // This will ensure that the DataUpload library doesn't blow up on an empty string,
115 // and that the resulting error comes from the server.
116 GenericTest.POST_InvalidResourceWithData_Fails(TestResource, "");
117 }
118
119 [TestMethod, TestCategory(TypeName)]
120 public override void Test_Resource_PUT_NoChanges()
121 {
122 // PUT is allowed before data upload (before resource is finalized)
124 // PUT is forbidden on a finalized resource, even if there are no changes
126 AssertApi.ApiExceptionTest(HttpStatusCode.MethodNotAllowed));
127 }
128 #endregion PUT
129
134 [TestMethod, TestCategory(TypeName)]
142
145 [TestMethod, TestCategory(TypeName)]
147 {
149 {
150 LargeDataSubResource data = posted.data;
151
152 // Otherwise, Initiate the large file upload
153 AssertApi.MethodIsAllowed(() => data.Post(Encoding.UTF8.GetByteCount(POSTData)),
154 "/data POST", true);
155 // Upload the file in several parts
156 int chunkLength = POSTData.Length / 4 + 1;
157 int bytesOffset = 0;
158 for (int startIndex = 0; startIndex < POSTData.Length; startIndex += chunkLength)
159 {
160 int nextChunkSize = Math.Min(chunkLength, POSTData.Length - startIndex);
161 string chunk = POSTData.Substring(startIndex, nextChunkSize);
162 AssertApi.MethodIsAllowed(() => data.Patch(chunk, bytesOffset), "/data PATCH", true);
163 bytesOffset += Encoding.UTF8.GetByteCount(chunk);
164 }
165
166 // Commit the large file upload
167 AssertApi.MethodIsAllowed(() => data.commit.POST(), "/data/commit POST", true);
168 // Poll the /status endpoint with GET until the upload is complete.
170
171 // Assert that the data is uploaded by doing a get.
172 Assert.AreEqual(POSTData, data.Get(), "Downloaded data did not match uploaded data.");
173 });
174 }
175
178 [TestMethod, TestCategory(TypeName)]
180 {
182 {
183 LargeDataSubResource data = posted.data;
184
185 // Otherwise, Initiate the large file upload
186 data.LargeStreamUpload(
189
190 // Assert that the data is uploaded by doing a get.
191 Assert.AreEqual(POSTData, data.Get(), "Downloaded data did not match uploaded data.");
192 });
193 }
194 #endregion Test Methods
195 }
196}
virtual T PUT_WithChanges_Succeeds(Func< T, T > modifyTestResource)
POST a valid resource, then test doing a PUT on the resource after modifying it in some way,...
virtual T POST_ForPutTest(T validToPost, Action< T > putTest)
Used for all PUT tests and PUT helper methods. Similar to the base helper method BaseResourceTestSuit...
virtual T TestResource_Existing
The resource used for all unit tests that require a prepared and already posted resource....
virtual T TestResource
The resource used for all unit tests that require a valid prepared but unPOSTed resource.
void TestResourceDeleteHelper(T toDelete, bool isDeleteAllowed)
virtual void Test_Resource_GET_Data()
Verify that posting the test resource along with it's data succeeds.
override void Test_Resource_POST_Valid()
Verify that posting the test resource along with it's data succeeds.
override void POST_ThenDoAction(T validToPost, Action< T > toExecute)
We need to override this universally used POST test helper method to ensure test data is uploaded as ...
virtual void Test_Resource_DELETE_Before_Uploading_Data()
Verify that the delete method is either supported or not supported before data has been uploaded....
virtual void Test_Resource_PUT_Data_EmptyString()
Verify that doing a PUT with empty data fails with a BadRequest.
override void Test_Resource_POST_CreatedAndModified()
Overridden because the base test doesn't account for the fact that a full POST (with data) involves m...
virtual void Test_Resource_Data_SegmentedUpload()
Verify that posting the test resource along with it's data succeeds using segmented uploads.
override void AdditionalValidResourceTests(T posted)
Deriving classes can optionally override this function to perform additional validation on every succ...
virtual void Test_Resource_Data_LargeStreamUpload()
Verify that posting the test resource along with it's data succeeds using the LargeStreamUpload exten...
Exposes sample resource objects, with built-in methods for injecting dependencies.
Definition Samples.cs:14
static Parameters UploadParams
The LargeDataUpload parameters to use when uploading data for test fixtures.
static void MethodIsAllowed(Action request, string methodName, bool methodAllowed=true)
Wrap a request in a tryGet with some formatting for testing purposes.
Definition AssertApi.cs:98
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...
Definition AssertApi.cs:528
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...
Generic Unit test implementations that will test REST methods on arbitrary resources.
IRestResponse POST(int? timeout=null, bool throwOnError=true, IEnumerable< Parameter > requestParameters=null)
Performs an empty POST at this endpoint (executing the command). Throws an APIRequestException if the...
Describes an endpoint off of some types of resources from which an associated "large data file" can b...
CommandSubResource commit
The endpoint at which a large file upload is finalized with a POST.
void Patch(string data, long bytesOffset, int? timeout=null, IEnumerable< Parameter > requestParameters=null)
Performs a PATCH of part of this object's data at this endpoint.
void Post(long? totalSizeBytes, int? timeout=null)
Performs a POST that initiates a chunked large data upload.
Describes a collection of resources which can be listed.
T Get(IEnumerable< Parameter > parameters=null, int? timeout=null)
Performs a GET request a at this endpoint.
Large Data Upload Utilities.
Definition Utilities.cs:11
static StatusResponse PollUntilDataProcessed(LargeDataSubResource dataEndpoint, Parameters parameters=null)
Polls the dataEndpoint until the state is no longer Processing.
Definition Utilities.cs:50
static MemoryStream GetStringStream(string input)
Generate a byte stream from a string which can be used in the LargeStreamUpload methods.
Definition Utilities.cs:40
Describes an APIResource class that adds a "/data" sub-resource, since this functionality is common t...
TaskStatus
The status of a data upload which may be in progress.
Definition TaskStatus.cs:9