/
QCController.cs
283 lines (231 loc) · 8.4 KB
/
QCController.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
using System;
using System.Collections.Generic;
using System.Reflection;
using TDAPIOLELib;
using NLog;
namespace oneshore.QCIntegration
{
class QCController
{
public string qcHostname { get; set; }
public string qcDomain { get; set; }
public string qcProject { get; set; }
public string qcLoginName { get; set; }
public string qcPassword { get; set; }
public string qcUrl
{
get
{
if (qcUrl == null) { return "http://" + qcHostname + ":8080/qcbin"; }
else { return qcUrl; }
}
set { qcUrl = value; }
}
public static string DELIM = "\t";
public int testCount { get; set; }
private TDConnection tdConn;
private string message;
private static Logger log = NLog.LogManager.GetCurrentClassLogger();
/**
* Constructor creates the Connection object
*
*/
public QCController()
{
try
{
this.tdConn = createTDConnection();
}
catch (QCException e)
{
log.Error(e.Message);
throw;
}
}
/**
* create a new TDConnection object or throw QCException on failure
*
*/
public TDConnection createTDConnection()
{
log.Debug("Creating TD connection.");
TDConnection tdConn = new TDConnection();
if (tdConn == null)
{
message = "Can't create TDConnection object -- maybe you need to reference TDAPIOLELib?";
throw new QCException(message);
}
return tdConn;
}
/**
* Establish a connection to Quality Center
*
* @param string qcUrl
* @param string qcDomain
* @param string qcProject
* @param string qcLoginName
* @param string qcPassword
* @return TDConnection
*/
public TDConnection connectToQC(string qcUrl, string qcDomain, string qcProject, string qcLoginName, string qcPassword)
{
try
{
tdConn.InitConnectionEx(qcUrl);
tdConn.ConnectProjectEx(qcDomain, qcProject, qcLoginName, qcPassword);
}
catch (Exception e)
{
log.Warn("unable to connect to project in QC");
log.Warn(e.Message);
throw;
}
if (tdConn.Connected)
{
message = "connected";
if (tdConn.ProjectConnected)
{
message += " to QC project " + tdConn.ProjectName;
log.Info(message);
}
else
{
message += " to QC but unable to connect to QC Project " + qcProject;
log.Warn(message);
throw new QCException(message);
}
}
else
{
message = "failed to connect to QC";
log.Warn(message);
throw new QCException(message);
}
return tdConn;
}
/**
* find the test sets that will be updated in QC
*
* @param string tsPath
* @param string tsName
* @return List of TestSet
*/
public List retrieveTestSets(string tsPath, string tsName)
{
log.Debug("...in retriveTestSet() " + tsPath + " " + tsName);
TestSetFolder tsFolder = retrieveFolder(tsPath);
if (tsFolder == null)
{
throw new QCException("no TestSetFolder at " + tsPath);
}
List tsList = tsFolder.FindTestSets(tsName, false, null);
if (tsList == null)
{
throw new QCException("no TestSets matching " + tsName);
}
if (tsList.Count < 1)
{
throw new QCException("no TestSets found matching " + tsName);
}
return tsList;
}
/***
* get one or more tests that matches a specific test case name
*
* @param string tsFolderName
* @param string tsTestName
* @return List of TsTest
*/
public List retrieveTests(string tsFolderName, string tsTestName)
{
TestSetFolder tsFolder = retrieveFolder(tsFolderName);
log.Debug("tsFolder.Path: " + tsFolder.Path);
List tsTestList = tsFolder.FindTestInstances(tsTestName, false, null);
return tsTestList;
}
/**
* get a folder in QC
*
* @param string tsPath
* @return TestSetFolder
*/
public TestSetFolder retrieveFolder(string tsPath)
{
TestSetFactory tsFactory = (TestSetFactory)tdConn.TestSetFactory;
TestSetTreeManager tsTreeMgr = (TestSetTreeManager)tdConn.TestSetTreeManager;
TestSetFolder tsFolder = null;
try
{
tsFolder = (TestSetFolder)tsTreeMgr.get_NodeByPath(tsPath);
}
catch (Exception e)
{
log.Warn("couldn't get TestSetFolder with path " + tsPath);
log.Warn(e.Message);
}
return tsFolder;
}
/**
* set status for tests in a test set and update in QC
*
* @param TestSet testSet
* @param Dictionary<string, string> testResults - testCaseName, testResult (e.g. "EHR_REF_PAT_0001", "Passed")
*/
public void recordTestSetResults(TestSet testSet, Dictionary<string, string> testResults)
{
TestSetFolder tsFolder = (TestSetFolder)testSet.TestSetFolder;
log.Debug("tsFolder.Path: " + tsFolder.Path);
string testSetInfo = "testSet.ID: " + testSet.ID.ToString() + DELIM +
"testSet.Name: " + testSet.Name + DELIM +
"testSet.Status: " + testSet.Status + DELIM +
"";
log.Debug("testSetInfo: " + testSetInfo);
TSTestFactory tsTestFactory = (TSTestFactory)testSet.TSTestFactory;
List tsTestList = tsTestFactory.NewList("");
foreach (TSTest tsTest in tsTestList)
{
testCount++;
string testInfo = DELIM + DELIM + DELIM +
"TestId: " + tsTest.TestId + DELIM +
"TestName: " + tsTest.TestName + DELIM +
"";
Run lastRun = (Run)tsTest.LastRun;
if (lastRun != null)
{
testInfo += lastRun.Name + DELIM + lastRun.Status;
}
log.Debug("TestInfo: " + testInfo);
// look for a test in the results from this test set
if (testResults.ContainsKey(tsTest.TestName))
{
string status = testResults[tsTest.TestName];
recordTestResult(tsTest, status);
}
}
}
/**
* set status for a single test and update in QC
*
* @param TSTest test - the test to be updated
* @param string status - "Passed", "Failed", etc.
*/
public void recordTestResult(TSTest test, string status)
{
string testInfo = DELIM + DELIM + DELIM +
"TestId: " + test.TestId + DELIM +
"TestName: " + test.TestName + DELIM +
"";
Run lastRun = (Run)test.LastRun;
if (lastRun != null)
{
testInfo += lastRun.Name + DELIM + lastRun.Status;
}
log.Debug(testInfo);
RunFactory runFactory = (RunFactory)test.RunFactory;
String date = DateTime.Now.ToString("yyyyMMddhhmmss");
Run run = (Run)runFactory.AddItem("Run" + date);
run.Status = status;
run.Post();
}
}
}