From f6c1718fe8fb24d53108885cfd1095b54d248dd8 Mon Sep 17 00:00:00 2001 From: Dmitry Mayer Date: Wed, 5 Aug 2020 14:45:24 +0200 Subject: [PATCH] Reworked how Test Cycles are handled Re-used Cycles to support Test Plans and for better reporting --- .../letsrokk/tm4j/client/TM4JAPIClient.java | 5 +++++ .../letsrokk/tm4j/client/TM4JClient.java | 20 +++++++++++++++++-- .../tm4j/testng/TM4JTestResultListerner.java | 8 +++++--- 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/tm4j-api/src/main/java/com/github/letsrokk/tm4j/client/TM4JAPIClient.java b/tm4j-api/src/main/java/com/github/letsrokk/tm4j/client/TM4JAPIClient.java index 1a6ed13..9bd86e8 100644 --- a/tm4j-api/src/main/java/com/github/letsrokk/tm4j/client/TM4JAPIClient.java +++ b/tm4j-api/src/main/java/com/github/letsrokk/tm4j/client/TM4JAPIClient.java @@ -11,6 +11,7 @@ import retrofit2.http.POST; import retrofit2.http.Part; import retrofit2.http.Path; +import retrofit2.http.Query; import java.util.List; @@ -20,6 +21,10 @@ interface TM4JAPIClient { @POST("testrun") Call createTestRun(@Body TestRun body); + @Headers("Content-Type: application/json") + @GET("testrun/search") + Call> searchTestRun(@Query("query") String query); + @Headers("Content-Type: application/json") @GET("testrun/{testRunKey}") Call getTestRun(@Path("testRunKey") String testRunKey); diff --git a/tm4j-api/src/main/java/com/github/letsrokk/tm4j/client/TM4JClient.java b/tm4j-api/src/main/java/com/github/letsrokk/tm4j/client/TM4JClient.java index 5ca27f2..feda2b7 100644 --- a/tm4j-api/src/main/java/com/github/letsrokk/tm4j/client/TM4JClient.java +++ b/tm4j-api/src/main/java/com/github/letsrokk/tm4j/client/TM4JClient.java @@ -6,6 +6,8 @@ import java.io.IOException; import java.util.List; +import java.util.Objects; +import java.util.Optional; @Log4j2 public class TM4JClient { @@ -25,7 +27,7 @@ public TestRun createTestRun(String projectKey, String name) { String testRunKey = apiClient.createTestRun(testRun).execute().body().getKey(); testRun = - getTestRun(testRunKey); + getTestRunByKey(testRunKey); return testRun; } catch (IOException e) { log.throwing(e); @@ -33,7 +35,7 @@ public TestRun createTestRun(String projectKey, String name) { } } - public TestRun getTestRun(String testRunKey) { + public TestRun getTestRunByKey(String testRunKey) { try { return apiClient.getTestRun(testRunKey).execute().body(); } catch (IOException e) { @@ -42,6 +44,20 @@ public TestRun getTestRun(String testRunKey) { } } + public Optional getTestRunByProjectKeyAndName(String projectKey, String testRunName) { + try { + String searchByProjectKeyQuery = String.format("projectKey = \"%s\"", projectKey); + List testRuns = apiClient.searchTestRun(searchByProjectKeyQuery).execute().body(); + return Objects.requireNonNull(testRuns).stream() + .filter(t -> t.getProjectKey().equalsIgnoreCase(projectKey) + && t.getName().equalsIgnoreCase(testRunName)) + .findFirst(); + } catch (IOException e) { + log.throwing(e); + return Optional.empty(); + } + } + public void postResult(Execution execution) { try { apiClient.postExecution(execution).execute(); diff --git a/tm4j-testng/src/main/java/com/github/letsrokk/tm4j/testng/TM4JTestResultListerner.java b/tm4j-testng/src/main/java/com/github/letsrokk/tm4j/testng/TM4JTestResultListerner.java index 34b1fc5..8c54535 100644 --- a/tm4j-testng/src/main/java/com/github/letsrokk/tm4j/testng/TM4JTestResultListerner.java +++ b/tm4j-testng/src/main/java/com/github/letsrokk/tm4j/testng/TM4JTestResultListerner.java @@ -8,7 +8,6 @@ import com.github.letsrokk.tm4j.client.TM4JClientFactory; import com.github.letsrokk.tm4j.client.model.Execution; import com.github.letsrokk.tm4j.client.model.ExecutionStatus; -import com.github.letsrokk.tm4j.client.model.TestRun; import com.github.letsrokk.tm4j.testng.container.CustomExecutionContainer; import com.github.letsrokk.tm4j.testng.container.CustomExecutionException; import com.github.letsrokk.tm4j.testng.container.CustomSuiteContainer; @@ -78,8 +77,11 @@ private void initSuiteContainer(String projectKey, String suiteName) { .name(suiteName) .build(); - TestRun testRun = tm4jClient.createTestRun(suiteContainer.getProjectKey(), suiteContainer.getName()); - suiteContainer.setTestRunKey(testRun.getKey()); + String testRunKey = tm4jClient.getTestRunByProjectKeyAndName(projectKey, suiteName) + .orElseGet(() -> tm4jClient.createTestRun(suiteContainer.getProjectKey(), suiteContainer.getName())) + .getKey(); + + suiteContainer.setTestRunKey(testRunKey); } else { log.error("TM4J Project Key is not set"); suiteContainer = CustomSuiteContainer.builder()