@Override
  public DataResponse<Iterable<TestResult>> search(TestResultRequest request) {
    Component component = componentRepository.findOne(request.getComponentId());
    if (!component.getCollectorItems().containsKey(CollectorType.Test)) {
      return new DataResponse<>(null, 0L);
    }
    List<TestResult> result = new ArrayList<>();

    for (CollectorItem item : component.getCollectorItems().get(CollectorType.Test)) {

      QTestResult testResult = new QTestResult("testResult");
      BooleanBuilder builder = new BooleanBuilder();

      builder.and(testResult.collectorItemId.eq(item.getId()));

      if (request.validStartDateRange()) {
        builder.and(
            testResult.startTime.between(request.getStartDateBegins(), request.getStartDateEnds()));
      }
      if (request.validEndDateRange()) {
        builder.and(
            testResult.endTime.between(request.getEndDateBegins(), request.getEndDateEnds()));
      }

      if (request.validDurationRange()) {
        builder.and(
            testResult.duration.between(
                request.getDurationGreaterThan(), request.getDurationLessThan()));
      }

      if (!request.getTypes().isEmpty()) {
        builder.and(testResult.testCapabilities.any().type.in(request.getTypes()));
      }

      if (request.getMax() == null) {
        result.addAll(
            Lists.newArrayList(
                testResultRepository.findAll(builder.getValue(), testResult.timestamp.desc())));
      } else {
        PageRequest pageRequest =
            new PageRequest(0, request.getMax(), Sort.Direction.DESC, "timestamp");
        result.addAll(
            Lists.newArrayList(
                testResultRepository.findAll(builder.getValue(), pageRequest).getContent()));
      }
    }
    // One collector per Type. get(0) is hardcoded.
    if (!CollectionUtils.isEmpty(component.getCollectorItems().get(CollectorType.Test))
        && (component.getCollectorItems().get(CollectorType.Test).get(0) != null)) {
      Collector collector =
          collectorRepository.findOne(
              component.getCollectorItems().get(CollectorType.Test).get(0).getCollectorId());
      if (collector != null) {
        return new DataResponse<>(
            pruneToDepth(result, request.getDepth()), collector.getLastExecuted());
      }
    }

    return new DataResponse<>(null, 0L);
  }
  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);
  }
  @Override
  public DataResponse<Iterable<TestResult>> historySearch(String componentId, String jobName) {
    ObjectId cId = null;
    if (componentId != null && !componentId.isEmpty()) {
      cId = new ObjectId(componentId);
    }
    Component component = componentRepository.findOne(cId);
    if (!component.getCollectorItems().containsKey(CollectorType.Test)) {

      return new DataResponse<>(null, 0L);
    }
    List<TestResult> result = new ArrayList<>();

    for (CollectorItem item : component.getCollectorItems().get(CollectorType.Test)) {

      QTestResult testResult = new QTestResult("testResult");
      BooleanBuilder builder = new BooleanBuilder();

      builder.and(testResult.collectorItemId.eq(item.getId()));

      if (jobName != null && !jobName.isEmpty()) {
        builder.and(testResult.jobName.eq(jobName));
      }

      result.addAll(
          Lists.newArrayList(
              testResultRepository.findAll(builder.getValue(), testResult.timestamp.desc())));
    }

    return new DataResponse<>(result, 0L);
  }
 private CollectorItem createCollectorItem(Collector collector, TestDataCreateRequest request)
     throws HygieiaException {
   CollectorItem tempCi = new CollectorItem();
   tempCi.setCollectorId(collector.getId());
   tempCi.setDescription(request.getDescription());
   tempCi.setPushed(true);
   tempCi.setLastUpdated(System.currentTimeMillis());
   Map<String, Object> option = new HashMap<>();
   option.put("jobName", request.getTestJobName());
   option.put("jobUrl", request.getTestJobUrl());
   option.put("instanceUrl", request.getServerUrl());
   tempCi.getOptions().putAll(option);
   tempCi.setNiceName(request.getNiceName());
   if (StringUtils.isEmpty(tempCi.getNiceName())) {
     return collectorService.createCollectorItem(tempCi);
   }
   return collectorService.createCollectorItemByNiceNameAndJobName(
       tempCi, request.getTestJobName());
 }