Skip to content

Commit

Permalink
Merge pull request #60 from testit-tms/feature/add_export_iterations_…
Browse files Browse the repository at this point in the history
…from_zephyr_scale_server

Added export of iterations for ZephyrScaleServerExporter.
  • Loading branch information
PavelButuzov authored Jul 28, 2024
2 parents ca80781 + cf2cdf6 commit bae93a7
Show file tree
Hide file tree
Showing 11 changed files with 334 additions and 26 deletions.
57 changes: 57 additions & 0 deletions Migrators/ZephyrScaleServerExporter/Client/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,63 @@ public async Task<ZephyrArchivedTestCase> GetArchivedTestCase(string testCaseKey
return testCase;
}

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

var response = await _httpClient.GetAsync($"/rest/tests/1.0/testcase/{testCaseKey}?fields=testData,parameters(id,name,defaultValue,index),paramType");
if (!response.IsSuccessStatusCode)
{
_logger.LogError(
"Failed to get parameters by test case key {Key}. Status code: {StatusCode}. Response: {Response}",
testCaseKey, response.StatusCode, await response.Content.ReadAsStringAsync());

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

var content = await response.Content.ReadAsStringAsync();
var zephyrParametersData = JsonSerializer.Deserialize<ZephyrParametersData>(content);
var testData = new List<Dictionary<string, ZephyrDataParameter>>();

if (zephyrParametersData.TestData.Count != 0)
{
foreach (var zephyrIteration in zephyrParametersData.TestData)
{
var dataParameters = new Dictionary<string, ZephyrDataParameter>();

foreach (var parameterName in zephyrIteration.Keys)
{
try
{
var parameter = JsonSerializer.Deserialize<ZephyrDataParameter>(zephyrIteration[parameterName].ToString());

dataParameters[parameterName] = parameter;
}
catch (Exception)
{
continue;
}
}

if (dataParameters.Keys.Count != 0)
{
testData.Add(dataParameters);
}
}
}

var parametersData = new ParametersData
{
Type = zephyrParametersData.Type,
TestData = testData,
Parameters = zephyrParametersData.Parameters
};

_logger.LogDebug("Got parameters: {@Parameters}", parametersData);

return parametersData;
}

public async Task<List<JiraComponent>> GetComponents()
{
_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 @@ -9,6 +9,7 @@ public interface IClient
Task<List<ZephyrTestCase>> GetTestCases();
Task<ZephyrTestCase> GetTestCase(string testCaseKey);
Task<ZephyrArchivedTestCase> GetArchivedTestCase(string testCaseKey);
Task<ParametersData> GetParametersByTestCaseKey(string testCaseKey);
Task<List<JiraComponent>> GetComponents();
Task<JiraIssue> GetIssueById(string issueId);
Task<List<ZephyrAttachment>> GetAttachmentsForTestCase(string testCaseKey);
Expand Down
9 changes: 9 additions & 0 deletions Migrators/ZephyrScaleServerExporter/Models/StepsData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using Models;

namespace ZephyrScaleServerExporter.Models;

public class StepsData
{
public List<Step> Steps { get; set; }
public List<Iteration> Iterations { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace ZephyrScaleServerExporter.Models;

public class ZephyrParameterType
{
public const string TEST_DATA = "TEST_DATA";
public const string PARAMETER = "PARAMETER";
}
63 changes: 63 additions & 0 deletions Migrators/ZephyrScaleServerExporter/Models/ZephyrParametersData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using System.Text.Json.Serialization;

namespace ZephyrScaleServerExporter.Models;

public class ZephyrParametersData
{
[JsonPropertyName("paramType")]
public string Type { get; set; }

[JsonPropertyName("testData")]
public List<Dictionary<string, object>> TestData { get; set; }

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

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

[JsonPropertyName("parameters")]
public List<ZephyrParameter> Parameters { get; set; }
}

public class ParametersData
{
public string Type { get; set; }
public List<Dictionary<string, ZephyrDataParameter>> TestData { get; set; }
public List<ZephyrParameter> Parameters { get; set; }
}

public class ZephyrDataParameter
{
[JsonPropertyName("index")]
public int Index { get; set; }

[JsonPropertyName("value")]
public string Value { get; set; }

[JsonPropertyName("type")]
public string Type { get; set; }

[JsonPropertyName("dataSetItemId")]
public int DataSetItemId { get; set; }
}

public class DataParameter
{
List<ZephyrParameter> Parameters;
}

public class ZephyrParameter
{
[JsonPropertyName("defaultValue")]
public string Value { get; set; }

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

[JsonPropertyName("index")]
public int Index { get; set; }

[JsonPropertyName("id")]
public int Id { get; set; }
}
1 change: 1 addition & 0 deletions Migrators/ZephyrScaleServerExporter/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ static IHostBuilder CreateHostBuilder(string[] strings)
services.AddSingleton<ITestCaseService, TestCaseService>();
services.AddSingleton<IWriteService, WriteService>();
services.AddSingleton<IAttachmentService, AttachmentService>();
services.AddSingleton<IParameterService, ParameterService>();
});
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using Models;

namespace ZephyrScaleServerExporter.Services;

public interface IParameterService
{
Task<List<Iteration>> ConvertParameters(string testCaseKey);
List<Iteration> MergeIterations(List<Iteration> mainIterations, List<Iteration> subIterations);
}
3 changes: 1 addition & 2 deletions Migrators/ZephyrScaleServerExporter/Services/IStepService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@ namespace ZephyrScaleServerExporter.Services;

public interface IStepService
{
Task<List<Step>> ConvertSteps(Guid testCaseId, ZephyrTestScript testScript);
Task<List<Step>> ConvertSteps(Guid testCaseId, ZephyrArchivedTestScript testScript);
Task<StepsData> ConvertSteps(Guid testCaseId, ZephyrTestScript testScript, List<Iteration> iteranions);
}
113 changes: 113 additions & 0 deletions Migrators/ZephyrScaleServerExporter/Services/ParameterService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
using ZephyrScaleServerExporter.Client;
using ZephyrScaleServerExporter.Models;
using Microsoft.Extensions.Logging;
using Models;
using Microsoft.VisualBasic;

namespace ZephyrScaleServerExporter.Services;

public class ParameterService : IParameterService
{
private readonly ILogger<ParameterService> _logger;
private readonly IClient _client;

public ParameterService(ILogger<ParameterService> logger, IClient client)
{
_logger = logger;
_client = client;
}

public async Task<List<Iteration>> ConvertParameters(string testCaseKey)
{
_logger.LogInformation("Converting parameters");

var parametersData = await _client.GetParametersByTestCaseKey(testCaseKey);

switch (parametersData.Type)
{
case ZephyrParameterType.TEST_DATA:
return ConvertParametersWithTestDataType(parametersData.TestData);
case ZephyrParameterType.PARAMETER:
return ConvertParametersWithParameterType(parametersData.Parameters);
default:
return new List<Iteration>();
}
}

public List<Iteration> MergeIterations(List<Iteration> mainIterations, List<Iteration> subIterations)
{
_logger.LogInformation("Merging parameters:\nMain: {@MainParameters}\n Sub: {@SubParameters}",
mainIterations, subIterations);

foreach (var mainIteration in mainIterations)
{
foreach (var subIteration in subIterations)
{
var nonconflictingIterationParameters = subIteration.Parameters.Where(
subp => mainIteration.Parameters.FirstOrDefault(
mainp => subp.Name == mainp.Name) == null);

mainIteration.Parameters.AddRange(nonconflictingIterationParameters);
}
}

_logger.LogInformation("Merged parameters: {@Parameters}", mainIterations);

return mainIterations;
}

private List<Iteration> ConvertParametersWithTestDataType(List<Dictionary<string, ZephyrDataParameter>> ZephyrTestData)
{
var iterations = new List<Iteration>();

foreach (var zephyrDataParameters in ZephyrTestData)
{
var iteration = new Iteration
{
Parameters = new List<Parameter>()
};

foreach (var name in zephyrDataParameters.Keys)
{
iteration.Parameters.Add(
new Parameter
{
Name = name,
Value = zephyrDataParameters[name].Value
});
}

iterations.Add(iteration);
}

_logger.LogInformation("Converted parameters: {@Parameters}", iterations);

return iterations;
}

private List<Iteration> ConvertParametersWithParameterType(List<ZephyrParameter> zephyrParameters)
{
var parameters = new List<Parameter>();


foreach (var zephyrParameter in zephyrParameters)
{
parameters.Add(
new Parameter
{
Name = zephyrParameter.Name,
Value = zephyrParameter.Value
});
}

_logger.LogInformation("Converted parameters: {@Parameters}", parameters);

return new List<Iteration>
{
new Iteration
{
Parameters = parameters
}
};
}
}
Loading

0 comments on commit bae93a7

Please sign in to comment.