public ServiceResults executeServiceRequest(
      UriInfo ui, ApiResponse response, ServiceAction action, ServicePayload payload)
      throws Exception {

    logger.debug("ServiceResource.executeServiceRequest");

    boolean tree = "true".equalsIgnoreCase(ui.getQueryParameters().getFirst("tree"));
    boolean collectionGet = false;
    if (action == ServiceAction.GET) {
      collectionGet =
          (getServiceParameters().size() == 1
                  && InflectionUtils.isPlural(getServiceParameters().get(0)))
              ? true
              : false;
    }
    addQueryParams(getServiceParameters(), ui);
    ServiceRequest r = services.newRequest(action, tree, getServiceParameters(), payload);
    response.setServiceRequest(r);
    ServiceResults results = r.execute();
    if (results != null) {
      if (results.hasData()) {
        response.setData(results.getData());
      }
      if (results.getServiceMetadata() != null) {
        response.setMetadata(results.getServiceMetadata());
      }
      Query query = r.getLastQuery();
      if (query != null) {
        query = new Query(query);
        query.setIdsOnly(false);
        if (query.hasSelectSubjects()) {
          response.setList(query.getSelectionResults(results));
          response.setCount(response.getList().size());
          response.setNext(results.getNextResult());
          response.setPath(results.getPath());
          return results;
        }
      }
      if (collectionGet) {
        response.setCount(results.size());
      }

      response.setResults(results);
    }

    httpServletRequest.setAttribute("applicationId", services.getApplicationId());

    return results;
  }
  public ServiceResults testRequest(
      ServiceManager sm,
      ServiceAction action,
      int expectedCount,
      Map<String, Object> properties,
      Object... params)
      throws Exception {
    ServiceRequest request = sm.newRequest(action, parameters(params), payload(properties));

    logger.info("Request: " + action + " " + request.toString());

    ServiceResults results = request.execute();
    assertNotNull(results);
    assertEquals(expectedCount, results.getEntities().size());

    return results;
  }