Merge "Bug 559403: Slow integration with Zephyr"
diff --git a/modules/zephyr/plugins/org.eclipse.rcptt.zephyr/src/org/eclipse/rcptt/internal/zephyr/ZephyrTestCase.java b/modules/zephyr/plugins/org.eclipse.rcptt.zephyr/src/org/eclipse/rcptt/internal/zephyr/ZephyrTestCase.java
index d207201..d322f98 100644
--- a/modules/zephyr/plugins/org.eclipse.rcptt.zephyr/src/org/eclipse/rcptt/internal/zephyr/ZephyrTestCase.java
+++ b/modules/zephyr/plugins/org.eclipse.rcptt.zephyr/src/org/eclipse/rcptt/internal/zephyr/ZephyrTestCase.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2019 Xored Software Inc and others.
+ * Copyright (c) 2019, 2020 Xored Software Inc and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v2.0
  * which accompanies this distribution, and is available at
@@ -30,25 +30,12 @@
 
 	public static final int PASS = 1;
 	public static final int FAIL = 2;
-	private static final int MAX_ISSUE_SEARCH_COUNT = 250;
 
-	private static final String URL_GET_ALL_TESTS = "%s/rest/api/2/search?%sjql=project=%s&issuetype=%s&maxResults="
-			+ MAX_ISSUE_SEARCH_COUNT + "&startAt=%s";
+	private static final String URL_GET_TEST_BY_NAME = "%s/rest/api/2/issue/%s";
 	private static final String URL_CREATE_EXECUTIONS_URL = "%s/rest/zapi/latest/execution?projectId=%s&versionId=%s&cycleId=%s";
 	private static final String URL_EXECUTE_TEST = "%s/rest/zapi/latest/execution/%s/execute";
 
-	public static Long getTestCaseId(ZephyrConfigModel zephyrData) {
-		final String issueName = zephyrData.getIssueName();
-		if (issueName == null) {
-			return null;
-		}
-
-		final Map<String, Long> searchedTests = searchIssues(zephyrData);
-		return searchedTests.get(issueName);
-	}
-
 	private static Map<Long, Long> fetchExecutionIds(ZephyrConfigModel zephyrData) {
-
 		final Map<Long, Long> issueKeyExecutionIdMap = new HashMap<Long, Long>();
 		final String url = String.format(URL_CREATE_EXECUTIONS_URL, zephyrData.getRestClient().getUrl(),
 				zephyrData.getProjectId(), zephyrData.getVersionId(), zephyrData.getCycleId());
@@ -91,7 +78,7 @@
 	}
 
 	public static void processTestCaseDetails(ZephyrConfigModel zephyrData) {
-		final Long testCaseId = getTestCaseId(zephyrData);
+		final Long testCaseId = searchTestCaseId(zephyrData);
 
 		if (testCaseId == null) {
 			ZephyrPlugin.log("Invalid Issue: " + zephyrData.getIssueName());
@@ -103,7 +90,6 @@
 	}
 
 	public static void executeTest(ZephyrConfigModel zephyrData, Long testId) {
-
 		final String url = String.format(URL_EXECUTE_TEST, zephyrData.getRestClient().getUrl(), testId);
 
 		final Header[] headers = new Header[1];
@@ -134,58 +120,39 @@
 		}
 	}
 
-	private static Map<String, Long> searchIssues(ZephyrConfigModel zephyrData) {
+	private static Long searchTestCaseId(ZephyrConfigModel zephyrData) {
+		final String issueName = zephyrData.getIssueName();
+		if (issueName == null) {
+			return null;
+		}
+		
+		final String url = String.format(URL_GET_TEST_BY_NAME, zephyrData.getRestClient().getUrl(),
+				zephyrData.getIssueName());
 
-		long searchIssueStartcount = -1L;
-		long totalIssueCount = 0L;
-		long searchedIsssuesCount = 0L;
+		try (final CloseableHttpResponse response = zephyrData.getRestClient().get(url)) {
 
-		final Map<String, Long> searchedTests = new HashMap<String, Long>();
+			final int statusCode = response.getStatusLine().getStatusCode();
 
-		do {
-			final String url = String.format(URL_GET_ALL_TESTS, zephyrData.getRestClient().getUrl(),
-					zephyrData.getProjectId(), zephyrData.getTestIssueTypeId(), searchIssueStartcount,
-					searchIssueStartcount);
-			JsonArray searchedIssues = null;
-
-			try (final CloseableHttpResponse response = zephyrData.getRestClient().get(url)) {
-
-				final int statusCode = response.getStatusLine().getStatusCode();
-
-				if (statusCode >= 200 && statusCode < 300) {
-					final HttpEntity entity = response.getEntity();
-					String string = null;
-					try {
-						string = EntityUtils.toString(entity);
-					} catch (IOException e) {
-						throw new IllegalArgumentException(
-								String.format("Bad response entity upon receipt of issues <%s>", url), e);
-					}
-
-					final JsonObject testCaseIssues = new JsonParser().parse(string).getAsJsonObject();
-					totalIssueCount = testCaseIssues.get("total").getAsLong();
-					searchedIssues = testCaseIssues.get("issues").getAsJsonArray();
-
-					searchedIsssuesCount += searchedIssues.size();
-				} else {
-					throw new IllegalArgumentException(String
-							.format("Unexpected response status <%s> upon receipt of issues <%s>", statusCode, url));
+			if (statusCode >= 200 && statusCode < 300) {
+				final HttpEntity entity = response.getEntity();
+				String string = null;
+				try {
+					string = EntityUtils.toString(entity);
+				} catch (IOException e) {
+					throw new IllegalArgumentException(
+							String.format("Bad response entity upon receipt of issues <%s>", url), e);
 				}
 
-				if (searchedIssues != null && searchedIssues.size() > 0) {
-					for (int i = 0; i < searchedIssues.size(); i++) {
-						final JsonObject jsonObject = searchedIssues.get(i).getAsJsonObject();
-						final long testId = jsonObject.get("id").getAsLong();
-						final String testKey = jsonObject.get("key").getAsString().trim();
-
-						searchedTests.put(testKey, testId);
-					}
-				}
-			} catch (IOException e) {
-				throw new IllegalArgumentException(e);
+				final JsonObject testCaseIssue = new JsonParser().parse(string).getAsJsonObject();
+				final long testId = testCaseIssue.get("id").getAsLong();
+				return testId;
+			} else {
+				throw new IllegalArgumentException(
+						String.format("Unexpected response status <%s> upon receipt of issues <%s>", statusCode, url));
 			}
-		} while (searchedIsssuesCount < totalIssueCount);
 
-		return searchedTests;
+		} catch (IOException e) {
+			throw new IllegalArgumentException(e);
+		}
 	}
 }
\ No newline at end of file
diff --git a/modules/zephyr/plugins/org.eclipse.rcptt.zephyr/src/org/eclipse/rcptt/zephyr/ZephyrService.java b/modules/zephyr/plugins/org.eclipse.rcptt.zephyr/src/org/eclipse/rcptt/zephyr/ZephyrService.java
index 2d94474..950ab51 100644
--- a/modules/zephyr/plugins/org.eclipse.rcptt.zephyr/src/org/eclipse/rcptt/zephyr/ZephyrService.java
+++ b/modules/zephyr/plugins/org.eclipse.rcptt.zephyr/src/org/eclipse/rcptt/zephyr/ZephyrService.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2019 Xored Software Inc and others.
+ * Copyright (c) 2019, 2020 Xored Software Inc and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v2.0
  * which accompanies this distribution, and is available at
@@ -59,7 +59,6 @@
 
 	@Override
 	public void testRunCompleted() {
-		this.config.getRestClient().destroy();
 	}
 
 	@Override
@@ -68,7 +67,6 @@
 
 	@Override
 	public void sessionCompleted(ExecutionSession session) {
-
 	}
 
 	@Override
@@ -87,7 +85,6 @@
 		if (!zephyrEnabled) {
 			return;
 		}
-
 		final Boolean passed = getZephyrStatus(scenario);
 		if (passed == null)
 			return;
@@ -96,7 +93,7 @@
 
 		this.config.setTestcase(testcase);
 		ZephyrTestCase.processTestCaseDetails(this.config);
-
+		this.config.getRestClient().destroy();
 	}
 
 	private void applyConfig(final EclScenarioExecutable scenario, final Map<String, String> config) {