Skip to content

Commit

Permalink
Merge pull request #54 from testit-tms/hotfix/fix_getting_archived_sh…
Browse files Browse the repository at this point in the history
…ared_steps_for_zephyr_scale_server

Fixed getting archived shared steps for ZephyrScaleServer.
  • Loading branch information
PavelButuzov authored Jul 16, 2024
2 parents e1200ae + 167a0f4 commit 928764f
Show file tree
Hide file tree
Showing 10 changed files with 217 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Migrators/ZephyrScaleExporter/Models/ZephyrStep.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace ZephyrScaleExporter.Models;
public class ZephyrStep
{
[JsonPropertyName("inline")]
public Inline Inline { get; set; }
public Inline? Inline { get; set; }
}

public class Inline
Expand Down
5 changes: 5 additions & 0 deletions Migrators/ZephyrScaleExporter/Services/StepService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ public async Task<List<Step>> ConvertSteps(Guid testCaseId, string testCaseName,

foreach (var step in steps)
{
if (step.Inline == null)
{
continue;
}

var action = Utils.ExtractAttachments(step.Inline.Description);
var expected = Utils.ExtractAttachments(step.Inline.ExpectedResult);
var testData = Utils.ExtractAttachments(step.Inline.TestData);
Expand Down
22 changes: 22 additions & 0 deletions Migrators/ZephyrScaleServerExporter/Client/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,28 @@ public async Task<ZephyrTestCase> GetTestCase(string testCaseKey)
return testCase;
}

public async Task<ZephyrArchivedTestCase> GetArchivedTestCase(string testCaseKey)
{
_logger.LogInformation("Getting test case by key {Key}", testCaseKey);

var response = await _httpClient.GetAsync($"/rest/tests/1.0/testcase/{testCaseKey}?fields=key,name,testScript(id,text,steps(index,reflectRef,description,text,expectedResult,testData,attachments,customFieldValues,id,stepParameters(id,testCaseParameterId,value),testCase(id,key,name,archived,majorVersion,latestVersion,parameters(id,name,defaultValue,index)))),testData,parameters(id,name,defaultValue,index),paramType");
if (!response.IsSuccessStatusCode)
{
_logger.LogError(
"Failed to get test case by key {Key}. Status code: {StatusCode}. Response: {Response}",
testCaseKey, response.StatusCode, await response.Content.ReadAsStringAsync());

throw new Exception($"Failed to get test case by key {testCaseKey}. Status code: {response.StatusCode}");
}

var content = await response.Content.ReadAsStringAsync();
var testCase = JsonSerializer.Deserialize<ZephyrArchivedTestCase>(content);

_logger.LogDebug("Got test case {@TestCase}", testCase);

return testCase;
}

public async Task<List<JiraComponent>> GetComponents(string projectKey)
{
_logger.LogInformation("Getting components by project key {Key}", projectKey);
Expand Down
1 change: 1 addition & 0 deletions Migrators/ZephyrScaleServerExporter/Client/IClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public interface IClient
Task<ZephyrProject> GetProject();
Task<List<ZephyrTestCase>> GetTestCases();
Task<ZephyrTestCase> GetTestCase(string testCaseKey);
Task<ZephyrArchivedTestCase> GetArchivedTestCase(string testCaseKey);
Task<List<JiraComponent>> GetComponents(string projectKey);
Task<JiraIssue> GetIssueById(string issueId);
Task<List<ZephyrAttachment>> GetAttachmentsForTestCase(string testCaseKey);
Expand Down
3 changes: 3 additions & 0 deletions Migrators/ZephyrScaleServerExporter/Models/ZephyrStep.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,7 @@ public class ZephyrStep

[JsonPropertyName("testCaseKey")]
public string? TestCaseKey { get; set; }

[JsonPropertyName("index")]
public int Index { get; set; }
}
13 changes: 13 additions & 0 deletions Migrators/ZephyrScaleServerExporter/Models/ZephyrTestCase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,19 @@ public class ZephyrTestCase
public string? Component { get; set; }
}

public class ZephyrArchivedTestCase
{

[JsonPropertyName("key")]
public string Key { get; set; }

[JsonPropertyName("name")]
public string Name { get; set; }

[JsonPropertyName("testScript")]
public ZephyrArchivedTestScript TestScript { get; set; }
}

public class Links
{
[JsonPropertyName("issues")]
Expand Down
27 changes: 27 additions & 0 deletions Migrators/ZephyrScaleServerExporter/Models/ZephyrTestScript.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,30 @@ public class ZephyrTestScript
[JsonPropertyName("text")]
public string? Text { get; set; }
}

public class ZephyrArchivedTestScript
{
[JsonPropertyName("stepByStepScript")]
public ZephyrStepByStepScript? StepScript { get; set; }

[JsonPropertyName("plainTextScript")]
public ZephyrTextScript? TextScript { get; set; }
}

public class ZephyrStepByStepScript
{
[JsonPropertyName("id")]
public int Id { get; set; }

[JsonPropertyName("steps")]
public List<ZephyrStep>? Steps { get; set; }
}

public class ZephyrTextScript
{
[JsonPropertyName("id")]
public int Id { get; set; }

[JsonPropertyName("text")]
public string? Text { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ namespace ZephyrScaleServerExporter.Services;
public interface IStepService
{
Task<List<Step>> ConvertSteps(Guid testCaseId, ZephyrTestScript testScript);
Task<List<Step>> ConvertSteps(Guid testCaseId, ZephyrArchivedTestScript testScript);
}
147 changes: 142 additions & 5 deletions Migrators/ZephyrScaleServerExporter/Services/StepService.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.Extensions.Logging;
using Models;
using System.Text.RegularExpressions;
using ZephyrScaleServerExporter.Client;
using ZephyrScaleServerExporter.Models;

Expand All @@ -24,7 +25,7 @@ public async Task<List<Step>> ConvertSteps(Guid testCaseId, ZephyrTestScript tes

if (testScript.Steps != null)
{
var steps = testScript.Steps;
var steps = testScript.Steps.OrderBy(s => s.Index).ToList();

var stepList = new List<Step>();

Expand Down Expand Up @@ -68,6 +69,56 @@ public async Task<List<Step>> ConvertSteps(Guid testCaseId, ZephyrTestScript tes
return new List<Step>();
}

public async Task<List<Step>> ConvertSteps(Guid testCaseId, ZephyrArchivedTestScript testScript)
{
_logger.LogInformation("Converting steps from test script {@TestScript}", testScript);

if (testScript.StepScript != null && testScript.StepScript.Steps != null)
{
var steps = testScript.StepScript.Steps.OrderBy(s => s.Index).ToList();

var stepList = new List<Step>();

foreach (var step in steps)
{
if (string.IsNullOrEmpty(step.TestCaseKey))
{
var newStep = await ConvertStep(testCaseId, step);

stepList.Add(newStep);
}
else
{
var sharedSteps = await ConvertSharedSteps(testCaseId, step.TestCaseKey);

stepList.AddRange(sharedSteps);
}
}

_logger.LogDebug("Steps: {@StepList}", stepList);

return stepList;
}

if (testScript.TextScript != null && testScript.TextScript.Text != null)
{
return new List<Step>
{
new()
{
Action = testScript.TextScript.Text,
Expected = string.Empty,
TestData = string.Empty,
ActionAttachments = new List<string>(),
ExpectedAttachments = new List<string>(),
TestDataAttachments = new List<string>()
}
};
}

return new List<Step>();
}

private async Task<Step> ConvertStep(Guid testCaseId, ZephyrStep step)
{
var action = Utils.ExtractAttachments(step.Description);
Expand All @@ -78,12 +129,21 @@ private async Task<Step> ConvertStep(Guid testCaseId, ZephyrStep step)
{
Action = action.Description,
Expected = expected.Description,
TestData = testData.Description + $"<br><p>{step.CustomFields}</p>",
TestData = testData.Description,
ActionAttachments = new List<string>(),
ExpectedAttachments = new List<string>(),
TestDataAttachments = new List<string>()
};

if (step.CustomFields != null)
{
foreach (var customField in step.CustomFields)
{

newStep.TestData += ConvertCustomField(customField);
}
}

if (action.Attachments.Count > 0)
{
foreach (var attachment in action.Attachments)
Expand Down Expand Up @@ -114,13 +174,90 @@ private async Task<Step> ConvertStep(Guid testCaseId, ZephyrStep step)
return newStep;
}

private string ConvertCustomField(ZephyrCustomField customField)
{
_logger.LogInformation("Converting custom field \"{Name}\" for step", customField.CustomField.Name);

if (customField.CustomField.Options != null)
{
return ConvertOptionsFromCustomField(customField);
}

if (customField.IntValue != null)
{
return $"<br><p>{customField.CustomField.Name}: {customField.IntValue}</p>";
}

if (customField.StringValue != null)
{
return $"<br><p>{customField.CustomField.Name}: {customField.StringValue}</p>";
}

_logger.LogInformation("Failed to convert empty custom field \"{Name}\" for step", customField.CustomField.Name);

return "";
}

private string ConvertOptionsFromCustomField(ZephyrCustomField customField)
{
_logger.LogInformation("Converting custom field with options \"{Name}\" for step", customField.CustomField.Name);

if (customField.IntValue != null && customField.CustomField.Options != null)
{
return $"<br><p>{customField.CustomField.Name}: {customField.CustomField.Options.Find(o => o.Id == customField.IntValue.GetValueOrDefault()).Name}</p>";
}

if (customField.StringValue != null && customField.CustomField.Options != null)
{
var ids = ConvertStringToIds(customField.StringValue);
var options = "";

foreach (var id in ids)
{
options += customField.CustomField.Options.Find(o => o.Id == id).Name + ", ";
}

return $"<br><p>{customField.CustomField.Name}: {options}</p>";
}

_logger.LogInformation("Failed to convert empty custom field with options \"{Name}\" for step", customField.CustomField.Name);

return "";
}

private List<int> ConvertStringToIds(string strWithIds)
{

var ids = new List<int>();
string pattern = @"\d+";
Regex reg = new Regex(pattern);
MatchCollection m = reg.Matches(strWithIds);

for (int i = 0; i < m.Count; i++)
{
ids.Add(int.Parse(m[i].Value));
}

return ids;
}

private async Task<List<Step>> ConvertSharedSteps(Guid testCaseId, string testCaseKey)
{
_logger.LogInformation("Converting shared steps from test case key {testCaseKey}", testCaseKey);

var zephyrTestCase = await _client.GetTestCase(testCaseKey);
try
{
var zephyrTestCase = await _client.GetTestCase(testCaseKey);

return zephyrTestCase.TestScript != null ?
await ConvertSteps(testCaseId, zephyrTestCase.TestScript) : new List<Step>();
return zephyrTestCase.TestScript != null ?
await ConvertSteps(testCaseId, zephyrTestCase.TestScript) : new List<Step>();
}
catch (Exception ex)
{
var zephyrArchivedTestCase = await _client.GetArchivedTestCase(testCaseKey);

return zephyrArchivedTestCase.TestScript != null ?
await ConvertSteps(testCaseId, zephyrArchivedTestCase.TestScript) : new List<Step>();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,9 @@ public async Task<TestCaseData> ConvertTestCases(SectionData sectionData, Dictio
};

_attributeMap.Add(keyValuePair.Key, attribute);
testCase.Attributes.AddRange(ConvertAttributes(zephyrTestCase.CustomFields));
}

testCase.Attributes.AddRange(ConvertAttributes(zephyrTestCase.CustomFields));
}
testCases.Add(testCase);
}
Expand Down

0 comments on commit 928764f

Please sign in to comment.