C# Client Library
A C# Client Library for the AnalyzeRe REST API
Loading...
Searching...
No Matches
InjectableResource[T].cs
Go to the documentation of this file.
1using System;
2using AnalyzeRe;
5
6#if MSTEST
7using Microsoft.VisualStudio.TestTools.UnitTesting;
8#elif NUNIT
9using NUnit.Framework;
10#endif
11
12namespace AnalyzeReTesting
13{
24 {
27 bool Mocked { get; set; }
28
30 T Posted { get; }
31
33 T Unposted { get; }
34
37
41
43 Type UnderlyingType { get; }
44 }
45
49 public sealed class InjectableResource<T> : IInjectableResource<T>
51 {
52 #region Public Properties
55 public bool Mocked { get; set; }
56
58 public T Unposted => _unPOSTedResource.DeepCopy();
59
61 public T Posted => (_posted ? _postedResource : _postedResource = Post()).DeepCopy();
62
64 public IReference<T> AsReference => Posted.ToReference(true);
65
68 public IReference<T> AsInlinedReference => Unposted.ToReference(true);
69
71 public Type UnderlyingType => _unPOSTedResource.GetType();
72 #endregion Public Properties
73
74 #region Private Fields
75 // The resource before it was posted.
76 private readonly T _unPOSTedResource;
77
78 // The data to upload at Post time.
79 private readonly string _postData;
80
81 // A lock taken when posting the resource for the first time.
82 private readonly object _postLock = new object();
83
84 // Set to true once the resource has been posted.
85 private bool _posted;
86
87 // The resource after it was posted.
88 private T _postedResource;
89 #endregion Private Fields
90
91 #region Constructors
95 {
96 if (unPOSTedResource.id != null)
97 throw new ArgumentException("An attempt was made to create a disposable " +
98 "resource using a resource that was already posted elsewhere.");
99 _unPOSTedResource = unPOSTedResource;
100 }
101
107 {
109 throw new ArgumentException("data cannot be used on resources without a data endpoint.");
110 _postData = data;
111 }
112 #endregion Constructors
113
114 #region Public Methods
117 private T Post()
118 {
119 try
120 {
121 // Immediately return a copy of this resource if it's already been posted.
122 if (_posted) return _postedResource;
123 // Ensure multiple threads don't concurrently post this resource
124 lock (_postLock)
125 {
126 // If the resource has been posted by another thread since taking the lock, return
127 if (_posted) return _postedResource;
128 // if this is a mocked resource, simply fake assign it an id.
129 if (Mocked)
130 {
131 _postedResource = Unposted;
132 _postedResource.id = Samples.Valid_NonExistant_UUID;
134 asWithStatus.status = TaskStatus.Success;
135 _posted = true;
136 return _postedResource;
137 }
138
139 // Resources with data endpoints need to have their data uploaded
140 if (_postData != null && _unPOSTedResource is IAPIResource_WithDataEndpoint)
141 {
142 _postedResource = (T)((IAPIResource_WithDataEndpoint)_unPOSTedResource)
143 .Post(_postData, upload_parameters: Samples.UploadParams);
144 }
145 else
146 _postedResource = _unPOSTedResource.Post();
147
148 // HACK: Some resources must be 'polled' until they are ready to be considered
149 // successfully posted (Analysis Profiles)
150 // HACK: Exclude OptimizationViews because they aren't "ready" until both
151 // validation has completed (which we care about) and the optimization_view
152 // has completed running (which we don't care about at post time).
153 // There's no way to distinguish, so don't bother polling these
154 // (tests using these resources should accordingly ensure they're "ready"
155 // before using them i.e. to retrieve results)
156 // TODO: Revise if this is necessary.
157 if (_postedResource is IStoredAPIResource_WithStatus &&
158 !(_postedResource is OptimizationView))
159 {
160 _postedResource = (T)(_postedResource as IStoredAPIResource_WithStatus)
161 .PollUntilReady(Samples.DataPollingOptions);
162 }
163 _posted = true;
164 }
165 return _postedResource;
166 }
167 catch (Exception e)
168 {
169 Assert.Fail("An error occurred while posting one of the supporting " +
170 "resources required to run this test:\n" + e);
171 throw;
172 }
173 }
174 #endregion Public Methods
175 }
176
179 public static class InjectableResource
180 {
188 public static IInjectableResource<T> Create<T>(T resource, bool mocked = false)
190 {
192 }
193
202 public static IInjectableResource<T> Create<T>(T resource, string data, bool mocked = false)
204 {
205 return new InjectableResource<T>(resource, data) { Mocked = mocked };
206 }
207 }
208}
A class containing a resource that can be Posted with dependencies.
Type UnderlyingType
Get the runtime type of the resource that an instance will inject.
T Unposted
The unPOSTed resource definition.
static IInjectableResource< T > Create< T >(T resource, bool mocked=false)
Static method for creating wrapping a resource in a DisposableResource.Create container (where the ty...
InjectableResource(T unPOSTedResource, string data)
Create a new injectable resource with some data to upload at injection time.
IReference< T > AsInlinedReference
A reference to the unPOSTed resource (i.e. an inlined reference).
bool Mocked
If true, the resource is mocked (doesn't actually exist on a server) and so attempts to do server-sid...
T Posted
The posted resource, ready to be referenced.
InjectableResource(T unPOSTedResource)
Create a new injectable resource.
IReference< T > AsReference
A reference to the posted resource.
Exposes sample resource objects, with built-in methods for injecting dependencies.
Definition Samples.cs:14
static string Valid_NonExistant_UUID
Definition Samples.cs:41
Describes a collection of resources which can be listed.
Representation of a set of Optimization Parameters.
Interface for a class containing a resource that can be Posted with dependencies.
bool Mocked
If true, the resource is mocked (doesn't actually exist on a server) and so attempts to do server-sid...
IReference< T > AsInlinedReference
A reference to the unPOSTed resource (i.e. an inlined reference).
Type UnderlyingType
The runtime type of the resource that an instance will inject.
IReference< T > AsReference
A reference to the posted resource.
T Unposted
The unPOSTed resource definition.
T Posted
The posted resource, ready to be referenced.
Describes an APIResource class that adds a "/data" sub-resource, since this functionality is common t...
Describes an APIResource class that has a "status" property and corresponding "status_message" which ...
Interface for Base class used by all resources.
TaskStatus
The status of a data upload which may be in progress.
Definition TaskStatus.cs:9