Skip to content

Commit

Permalink
Merge pull request #1 from TatianaTochko/add-ability-to-update-test-c…
Browse files Browse the repository at this point in the history
…ases-in-zephyr

Add update zephyr test case functionality
  • Loading branch information
PSharuba authored Jan 11, 2022
2 parents ffc0ff8 + e1b92d6 commit fb1037d
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 3 deletions.
10 changes: 10 additions & 0 deletions docs/modules/integrations/pages/zephyr-exporter.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Zephyr Exporter is a tool used for exporting test execution results into Jira Ze

Features:

* Create test cases
* Update test cases
* Create test executions
* Set test execution statuses
Expand Down Expand Up @@ -34,6 +36,14 @@ include::partial$jira-configuration.adoc[]
|false
|Property for update existing executions statuses only.

|`zephyr.exporter.update-cases-on-export`
|false
|Property to update existing test cases. User with update status permissions required in order to work.

|`zephyr.exporter.status-for-updated-test-cases`
|false
|Property defines status which will be set for all updated tests ("Backlog" by default).

|`zephyr.exporter.statuses-of-test-cases-to-add-to-execution`
|false
|List of test case statuses for adding to execution.
Expand Down
17 changes: 17 additions & 0 deletions vividus-facade-jira/src/main/java/org/vividus/jira/JiraFacade.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ public class JiraFacade

private static final String REST_API_ENDPOINT = "/rest/api/latest/";
private static final String ISSUE = "issue/";
private static final String TRANSITIONS = ISSUE + "%s/transitions/";
private static final String ISSUE_ENDPOINT = REST_API_ENDPOINT + ISSUE;
private static final String TRANSITIONS_ENDPOINT = REST_API_ENDPOINT + TRANSITIONS;
private static final String UPDATE_TRANSITION_BODY = "{'transition':{'id':%s}}";

private final JiraClientProvider jiraClientProvider;

Expand Down Expand Up @@ -72,6 +75,20 @@ public String getIssueStatus(String issueKey) throws IOException, JiraConfigurat
return JsonPathUtils.getData(issue, "$.fields.status.name");
}

public String setIssueStatus(String issueKey, String status) throws JiraConfigurationException, IOException
{
String statusId = getStatusIdByName(issueKey, status);
return jiraClientProvider.getByIssueKey(issueKey).executePost(String.format(TRANSITIONS_ENDPOINT, issueKey),
String.format(UPDATE_TRANSITION_BODY, statusId));
}

private String getStatusIdByName(String issueKey, String name) throws JiraConfigurationException, IOException
{
String statuses = jiraClientProvider.getByIssueKey(issueKey)
.executeGet(String.format(TRANSITIONS_ENDPOINT, issueKey));
return JsonPathUtils.getData(statuses, String.format("$.transitions[?(@.to.name=='%s')].id", name));
}

public Project getProject(String projectKey) throws IOException, JiraConfigurationException
{
return getJiraEntity("project/", projectKey, () -> jiraClientProvider.getByProjectKey(projectKey),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ public class ZephyrExporterProperties

private TestLevel level;

private boolean updateCasesOnExport;

private String statusForUpdatedTestCases = "Backlog";

public String getJiraInstanceKey()
{
return jiraInstanceKey;
Expand Down Expand Up @@ -88,4 +92,24 @@ public void setLevel(TestLevel level)
{
this.level = level;
}

public boolean isUpdateCasesOnExport()
{
return updateCasesOnExport;
}

public void setUpdateCasesOnExport(boolean updateCasesOnExport)
{
this.updateCasesOnExport = updateCasesOnExport;
}

public String getStatusForUpdatedTestCases()
{
return statusForUpdatedTestCases;
}

public void setStatusForUpdatedTestCases(String statusForUpdatedTestCases)
{
this.statusForUpdatedTestCases = statusForUpdatedTestCases;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ private void exportStory(Story story)
parameters.setCucumberTestSteps(CucumberStoryScenarioConverter.convert(story));
fillTestCase(parameters, zephyrTest);

if (testCaseId != null)
if (testCaseId != null && zephyrExporterProperties.isUpdateCasesOnExport())
{
zephyrFacade.updateTestCase(testCaseId, zephyrTest);
}
Expand Down Expand Up @@ -194,7 +194,7 @@ private void exportScenario(String storyTitle, Scenario scenario)
.convert(scenario.getTitle(), scenario.collectSteps()));
fillTestCase(parameters, zephyrTest);

if (testCaseId != null)
if (testCaseId != null && zephyrExporterProperties.isUpdateCasesOnExport())
{
zephyrFacade.updateTestCase(testCaseId, zephyrTest);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,16 @@ public Integer createExecution(String execution) throws IOException, JiraConfigu

@Override
public void updateTestCase(String testCaseId, ZephyrTestCase zephyrTest)
throws IOException, JiraConfigurationException
{
LOGGER.atInfo().addArgument(testCaseId).log("Updating Test Case: {}");
String updateTestRequest = objectMapper.writeValueAsString(zephyrTest);
LOGGER.atInfo().addArgument(testCaseId)
.addArgument(updateTestRequest)
.log("Updating Test Case with ID {}: {}");
jiraFacade.updateIssue(testCaseId, updateTestRequest);
jiraFacade.setIssueStatus(testCaseId, zephyrExporterProperties.getStatusForUpdatedTestCases());
LOGGER.atInfo().addArgument(testCaseId)
.log("Test with key {} has been updated");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
class ZephyrExporterTests
{
public static final String CREATEREPORTS = "createreports";
public static final String UPDATEREPORTS = "updatereports";
private static final String TEST_CASE_KEY1 = "TEST-1";
private static final String TEST_CASE_KEY2 = "TEST-2";
private static final String ISSUE_ID1 = "1";
Expand Down Expand Up @@ -149,8 +150,10 @@ void shouldExportNewTestWithStoryLevel() throws URISyntaxException, IOException,
URI jsonResultsUri = getJsonResultsUri(CREATEREPORTS);
zephyrExporterProperties.setLevel(TestLevel.STORY);
zephyrExporterProperties.setSourceDirectory(Paths.get(jsonResultsUri));

zephyrExporter.exportResults();

verify(zephyrFacade).createTestCase(any());
assertThat(logger.getLoggingEvents(), is(Collections.singletonList(info("Exporting {} story", STORY_TITLE))));
}

Expand All @@ -163,10 +166,40 @@ void shouldExportNewTestWithScenarioLevel() throws URISyntaxException, IOExcepti

zephyrExporter.exportResults();

verify(zephyrFacade).createTestCase(any());
assertThat(logger.getLoggingEvents(), is(List.of(info(EXPORTING_SCENARIO_FROM_STORY, STORY_TITLE),
info(EXPORTING_SCENARIO, SCENARIO_TITLE))));
}

@Test
void shouldUpdateTestWithStoryLevel() throws URISyntaxException, IOException, JiraConfigurationException
{
URI jsonResultsUri = getJsonResultsUri(UPDATEREPORTS);
zephyrExporterProperties.setLevel(TestLevel.STORY);
zephyrExporterProperties.setSourceDirectory(Paths.get(jsonResultsUri));
zephyrExporterProperties.setUpdateCasesOnExport(true);

zephyrExporter.exportResults();

verify(zephyrFacade).updateTestCase(any(),any());
assertThat(logger.getLoggingEvents(), is(List.of(info("Exporting {} story", STORY_TITLE))));
}

@Test
void shouldUpdateTestWithScenarioLevel() throws URISyntaxException, IOException, JiraConfigurationException
{
URI jsonResultsUri = getJsonResultsUri(UPDATEREPORTS);
zephyrExporterProperties.setLevel(TestLevel.SCENARIO);
zephyrExporterProperties.setSourceDirectory(Paths.get(jsonResultsUri));
zephyrExporterProperties.setUpdateCasesOnExport(true);

zephyrExporter.exportResults();

verify(zephyrFacade).updateTestCase(any(),any());
assertThat(logger.getLoggingEvents(), is(List.of(info(EXPORTING_SCENARIO_FROM_STORY, STORY_TITLE),
info(EXPORTING_SCENARIO, SCENARIO_TITLE))));
}

private ZephyrConfiguration prepareTestConfiguration()
{
ZephyrConfiguration configuration = new ZephyrConfiguration();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,20 @@ void testCreateNewTestCase() throws IOException, JiraConfigurationException
info("Test with key {} has been created", ISSUE_ID))));
}

@Test
void testUpdateTestCase() throws IOException, JiraConfigurationException
{
ZephyrTestCase test = createZephyrTestCase();
mockSerialization(test);
when(jiraFacade.updateIssue(ISSUE_ID, BODY)).thenReturn(BODY);

zephyrFacade.updateTestCase(ISSUE_ID, test);

assertThat(logger.getLoggingEvents(), is(List.of(
info("Updating Test Case with ID {}: {}", ISSUE_ID, BODY),
info("Test with key {} has been updated", ISSUE_ID))));
}

private void mockJiraProjectRetrieve() throws IOException, JiraConfigurationException
{
Version version = new Version();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@
"lifecycle": {
"keyword": "Lifecycle:"
},
"meta": [
{
"keyword": "@",
"name": "testCaseId",
"value": "STUB-1"
}
],
"beforeStorySteps": [],
"scenarios": [
{
Expand Down

0 comments on commit fb1037d

Please sign in to comment.