-
Notifications
You must be signed in to change notification settings - Fork 5
/
BaseSDKTester.cs
329 lines (277 loc) · 11 KB
/
BaseSDKTester.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using NUnit.Framework;
using VersionOne.SDK.APIClient;
using VersionOne.SDK.ObjectModel.Filters;
using VersionOne.SDK.Utility;
namespace VersionOne.SDK.ObjectModel.Tests {
public abstract class BaseSDKTester {
private Oid defaultSchemeOid;
private EntityFactory entityFactory;
private V1Instance instance;
protected virtual string ApplicationPath
{
get
{
const string settingName = "TEST_URL";
var url = System.Environment.GetEnvironmentVariable(settingName);
if (string.IsNullOrWhiteSpace(url))
{
url = System.Configuration.ConfigurationManager.AppSettings[settingName];
if (string.IsNullOrWhiteSpace(url))
{
url = "http://localhost/V1SDKTests/";
}
}
url = url.Trim();
return url;
}
}
protected virtual string Username {
get { return System.Environment.GetEnvironmentVariable("TEST_USER") ?? "admin"; }
}
protected virtual string Password {
get { return System.Environment.GetEnvironmentVariable("TEST_PASSWORD") ?? "admin"; }
}
protected Oid DefaultSchemeOid {
get { return defaultSchemeOid ?? (defaultSchemeOid = GetFirstAvailableScheme().Oid); }
}
internal EntityFactory EntityFactory {
get { return entityFactory ?? (entityFactory = new EntityFactory(Instance)); }
}
protected V1Instance Instance {
get {
if (instance == null)
{
//var oauth2dir = System.Environment.GetEnvironmentVariable("OAUTH2_DIR");
string oauth2dir = null;
if (oauth2dir != null)
{
instance = new V1Instance(ApplicationPath,
new OAuth2Client.Storage.JsonFileStorage(oauth2dir + @"\client_secrets.json",
oauth2dir + @"\stored_credentials.json"));
}
else
{
instance = new V1Instance(ApplicationPath, Username, Password);
}
instance.Validate();
}
return instance;
}
}
#region Sandbox
/// <summary>
/// The name to be used when creating your sandbox projects and teams. Override to specify a special name. I like to call mine Fred.
/// </summary>
protected virtual string SandboxName {
get { return GetType().Name; }
}
#region Schedule
private AssetID sandboxScheduleID;
/// <summary>
/// The ID of your sandbox Schedule, so you can get it again yourself, Elvis.
/// </summary>
protected AssetID SandboxScheduleID {
get {
if (sandboxScheduleID == null) {
var sandbox = CreateSandboxSchedule();
sandboxScheduleID = sandbox.ID;
}
return sandboxScheduleID;
}
}
/// <summary>
/// A sandbox for you to play in. The Entity is retrieved from the Instance on every call (so ResetInstance will force a re-query). You don't need to do anything to initialize it. Just use it, Mort.
/// </summary>
protected Schedule SandboxSchedule {
get { return Instance.Get.ScheduleByID(SandboxScheduleID); }
}
protected void NewSandboxSchedule() {
sandboxScheduleID = null;
}
/// <summary>
/// Override to create your sandbox with properties other than the defaults (today as the start date, child of Scope:0, no schedule). You go, Einstein.
/// </summary>
/// <returns></returns>
protected virtual Schedule CreateSandboxSchedule() {
return Instance.Create.Schedule(SandboxName, TimeSpan.FromDays(14), TimeSpan.FromDays(0) );
}
#endregion
#region Project
private AssetID sandboxProjectID;
/// <summary>
/// The ID of your sandbox project, so you can get it again yourself, Elvis.
/// </summary>
protected AssetID SandboxProjectID {
get {
if (sandboxProjectID == null) {
var rootProject = Instance.Get.ProjectByID("Scope:0");
var sandbox = CreateSandboxProject(rootProject);
sandboxProjectID = sandbox.ID;
}
return sandboxProjectID;
}
}
/// <summary>
/// A sandbox for you to play in. The Entity is retrieved from the Instance on every call (so ResetInstance will force a re-query). You don't need to do anything to initialize it. Just use it, Mort.
/// </summary>
protected Project SandboxProject {
get { return Instance.Get.ProjectByID(SandboxProjectID); }
}
protected void NewSandboxProject() {
sandboxProjectID = null;
}
/// <summary>
/// Override to create your sandbox with properties other than the defaults (today as the start date, child of Scope:0, no schedule). You go, Einstein.
/// </summary>
/// <param name="rootProject"></param>
/// <returns></returns>
protected virtual Project CreateSandboxProject(Project rootProject) {
var mandatoryAttributes = new Dictionary<string, object>(1) {{"Scheme", DefaultSchemeOid}};
return Instance.Create.Project(SandboxName, rootProject, DateTime.Now, null, mandatoryAttributes);
}
#endregion
#region Iteration
private AssetID sandboxIterationID;
/// <summary>
/// The ID of your sandbox iteration, so you can get it again yourself, Elvis.
/// </summary>
protected AssetID SandboxIterationID {
get { return sandboxIterationID ?? (sandboxIterationID = SandboxProject.CreateIteration().ID); }
}
/// <summary>
/// A sandbox for you to play in. The Entity is retrieved from the Instance on every call (so ResetInstance will force a re-query). You don't need to do anything to initialize it. Just use it, Mort.
/// </summary>
protected Iteration SandboxIteration {
get { return Instance.Get.IterationByID(SandboxIterationID); }
}
protected void NewSandboxIteration() {
sandboxIterationID = null;
}
#endregion
#region Team
private AssetID sandboxTeamID;
/// <summary>
/// The ID of your sandbox team, so you can get it again yourself, Elvis.
/// </summary>
protected AssetID SandboxTeamID {
get {
if (sandboxTeamID == null) {
var sanboxTeam = EntityFactory.Create(() => Instance.Create.Team(SandboxName));
sandboxTeamID = sanboxTeam.ID;
}
return sandboxTeamID;
}
}
/// <summary>
/// A sandbox for you to play in. The Entity is retrieved from the Instance on every call (so ResetInstance will force a re-query). You don't need to do anything to initialize it. Just use it, Mort.
/// </summary>
protected Team SandboxTeam {
get { return Instance.Get.TeamByID(SandboxTeamID); }
}
protected void NewSandboxTeam() {
sandboxTeamID = null;
}
#endregion
#region Member
private AssetID sandboxMemberID;
/// <summary>
/// The ID of your sandbox member, so you can get it again yourself, Elvis.
/// </summary>
protected AssetID SandboxMemberID {
get { return sandboxMemberID ?? (sandboxMemberID = Instance.Create.Member(SandboxName, SandboxName).ID); }
}
/// <summary>
/// A sandbox for you to play in. The Entity is retrieved from the Instance on every call (so ResetInstance will force a re-query). You don't need to do anything to initialize it. Just use it, Mort.
/// </summary>
protected Member SandboxMember {
get { return Instance.Get.MemberByID(SandboxMemberID); }
}
protected void NewSandboxMember() {
sandboxMemberID = null;
}
#endregion
protected Story CreateStory(string name, Project project, Iteration iteration) {
var story = EntityFactory.CreateStory(name, project);
story.Iteration = iteration;
story.Save();
return story;
}
protected Defect CreateDefect(string name, Project project, Iteration iteration) {
var defect = EntityFactory.CreateDefect(name, project);
defect.Iteration = iteration;
defect.Save();
return defect;
}
#endregion
protected Environment CreateEnvironment(string name, IDictionary<string, object> attributes) {
//return Instance.Create.Environment(name, SandboxProject, attributes);
return EntityFactory.CreateEnvironment(name, SandboxProject, attributes);
}
protected void ResetInstance() {
instance = null;
}
protected static bool FindRelated<T>(T needle, IEnumerable<T> haystack) {
return haystack.Contains(needle);
}
internal static T First<T>(IEnumerable<T> list) {
var enumerator = list.GetEnumerator();
return enumerator.MoveNext() ? enumerator.Current : default(T);
}
private Asset GetFirstAvailableScheme() {
var schemaType = Instance.ApiClient.MetaModel.GetAssetType("Scheme");
var nameDefinition = schemaType.GetAttributeDefinition("Name");
var schemaQuery = new Query(schemaType);
schemaQuery.Selection.Add(nameDefinition);
var result = Instance.ApiClient.Services.Retrieve(schemaQuery);
return result.Assets[0];
}
internal Environment GetEnvironment() {
const string name = "Testing env abv123";
var filter = new EnvironmentFilter();
filter.Name.Add(name);
var env = Instance.Get.Environments(filter);
if (env.Count == 0) {
return CreateEnvironment(name, null);
}
var environments = new List<Environment>(env);
return environments[0];
}
[TearDown]
public void TearDown() {
EntityFactory.Dispose();
}
protected class EntityToAssetIDTransformer<T> : ITransformer where T : Entity {
public object Transform(object input) {
return ((T)input).ID.Token;
}
}
protected class EntityToNameTransformer<T> : ITransformer where T : BaseAsset {
public object Transform(object input) {
return ((T)input).Name;
}
}
public IEnumerable DeriveListOfNamesFromAssets(IEnumerable baseAssets)
{
var names = new List<string> {};
foreach (BaseAsset asset in baseAssets)
{
names.Add(asset.Name);
}
return names;
}
public IEnumerable DeriveListOfIdsFromAssets(IEnumerable assets)
{
var ids = new List<string> {};
foreach (BaseAsset asset in assets)
{
ids.Add(asset.ID.Token);
}
return ids;
}
}
}