private Iterable<TestResult> pruneToDepth(List<TestResult> results, Integer depth) {
    // Prune the response to the requested depth
    // 0 - TestResult
    // 1 - TestCapability
    // 2 - TestSuite
    // 3 - TestCase
    // 4 - Entire response
    // null - Entire response
    if (depth == null || depth > 3) {
      return results;
    }
    for (TestResult result : results) {
      if (depth == 0) {
        result.getTestCapabilities().clear();
      } else {
        for (TestCapability testCapability : result.getTestCapabilities()) {
          if (depth == 1) {
            testCapability.getTestSuites().clear();
          } else {
            for (TestSuite testSuite : testCapability.getTestSuites()) {
              if (depth == 2) {
                testSuite.getTestCases().clear();
              } else { // depth == 3
                for (TestCase testCase : testSuite.getTestCases()) {
                  testCase.getTestSteps().clear();
                }
              }
            }
          }
        }
      }
    }

    return results;
  }
  private TestResult createTest(CollectorItem collectorItem, TestDataCreateRequest request) {
    TestResult testResult =
        testResultRepository.findByCollectorItemIdAndExecutionId(
            collectorItem.getId(), request.getExecutionId());
    if (testResult == null) {
      testResult = new TestResult();
    }

    testResult.setTargetAppName(request.getTargetAppName());
    testResult.setTargetEnvName(request.getTargetEnvName());
    testResult.setCollectorItemId(collectorItem.getId());
    testResult.setType(request.getType());
    testResult.setDescription(request.getDescription());
    testResult.setDuration(request.getDuration());
    testResult.setEndTime(request.getEndTime());
    testResult.setExecutionId(request.getExecutionId());
    testResult.setFailureCount(request.getFailureCount());
    testResult.setSkippedCount(request.getSkippedCount());
    testResult.setStartTime(request.getStartTime());
    testResult.setSuccessCount(request.getSuccessCount());
    testResult.setTimestamp(request.getTimestamp());
    testResult.setTotalCount(request.getTotalCount());
    testResult.setUnknownStatusCount(request.getUnknownStatusCount());
    testResult.setUrl(request.getTestJobUrl());
    testResult.getTestCapabilities().addAll(request.getTestCapabilities());
    testResult.setBuildId(new ObjectId(request.getTestJobId()));

    return testResultRepository.save(testResult);
  }
  @Test
  public void test_endToend() throws Exception {

    String artifacts = getJson("job-artifacts.json");
    String cucumberJson = getJson("two-features.json");

    URI lastBuildArtifactUri =
        URI.create(
            "http://server/job/job1/lastSuccessfulBuild/api/json?tree=timestamp,duration,number,fullDisplayName,building,artifacts[fileName,relativePath]");
    URI cucumberJsonUri =
        URI.create(
            "http://server/job/job1/lastSuccessfulBuild/artifact/job1/test1/report/web/cucumber.json");
    when(rest.exchange(
            eq(lastBuildArtifactUri),
            eq(HttpMethod.GET),
            Matchers.any(HttpEntity.class),
            eq(String.class)))
        .thenReturn(new ResponseEntity<String>(artifacts, HttpStatus.OK));
    when(rest.exchange(
            eq(cucumberJsonUri),
            eq(HttpMethod.GET),
            Matchers.any(HttpEntity.class),
            eq(String.class)))
        .thenReturn(new ResponseEntity<String>(cucumberJson, HttpStatus.OK));
    TestResult testResult = defaultJenkinsClient.getCucumberTestResult("http://server/job/job1/");
    Collection<TestCapability> capabilities = testResult.getTestCapabilities();
    assertThat(capabilities, notNullValue());

    Iterator<TestCapability> capabilityIterator = capabilities.iterator();

    TestCapability capability = capabilityIterator.next();
    List<TestSuite> suites = (List<TestSuite>) capability.getTestSuites();
    assertThat(suites, notNullValue());

    Iterator<TestSuite> suiteIt = suites.iterator();
    Iterator<TestCase> testCaseIt;
    TestSuite suite;

    suite = suiteIt.next();
    testCaseIt = suite.getTestCases().iterator();
    assertSuite(suite, "Feature:eCUKE Feature", 4, 0, 0, 4, 15019839l);

    assertTestCase(
        testCaseIt.next(),
        "ecuke-feature;i-say-hi",
        "Scenario:I say hi",
        4001555l,
        TestCaseStatus.Success);
    assertThat(testCaseIt.hasNext(), is(true));

    assertTestCase(
        testCaseIt.next(),
        "ecuke-feature;you-say-hi",
        "Scenario:You say hi",
        1001212l,
        TestCaseStatus.Success);
    assertThat(testCaseIt.hasNext(), is(true));

    assertTestCase(
        testCaseIt.next(),
        "ecuke-feature;eating-cucumbers",
        "Scenario Outline:Eating Cucumbers",
        2013197l,
        TestCaseStatus.Success);
    assertThat(testCaseIt.hasNext(), is(true));

    assertTestCase(
        testCaseIt.next(),
        "ecuke-feature;eating-cucumbers",
        "Scenario Outline:Eating Cucumbers",
        8003875l,
        TestCaseStatus.Success);
    assertThat(testCaseIt.hasNext(), is(false));
  }