private <T> RequestMatcher existMatcher(RequestExtractor<T> extractor, TextContainer container) {
    if ("true".equals(container.getText())) {
      return exist(extractor);
    }

    if ("false".equals(container.getText())) {
      return not(exist(extractor));
    }

    throw new RuntimeException(String.format("Unknown exist parameter: [%s]", container.getText()));
  }
  private RequestMatcher createSingleTextMatcher(String name, TextContainer container) {
    if (container.isRawText()) {
      return createSingleMatcher(name, container.getText());
    }

    if ("exist".equals(container.getOperation())) {
      return existMatcher(Extractors.extractor(name), container);
    }

    return createRequestMatcherWithResource(
        container.getOperation(), createResource(name, container.getText()));
  }
예제 #3
0
  private ResponseHandler createResponseHandler(
      Map.Entry<String, TextContainer> pair, String targetMethodName) {
    TextContainer container = pair.getValue();
    try {
      if (container.isForTemplate()) {
        Method method = Moco.class.getMethod(targetMethodName, String.class, Resource.class);
        return (ResponseHandler) method.invoke(null, pair.getKey(), template(container.getText()));
      }

      Method method = Moco.class.getMethod(targetMethodName, String.class, String.class);
      return (ResponseHandler) method.invoke(null, pair.getKey(), container.getText());
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }
  private <T> RequestMatcher getRequestMatcher(
      RequestExtractor<T> extractor, TextContainer container) {
    if (container.isRawText()) {
      return eq(extractor, container.getText());
    }

    if ("exist".equals(container.getOperation())) {
      return existMatcher(extractor, container);
    }

    try {
      Method operationMethod =
          Moco.class.getMethod(container.getOperation(), RequestExtractor.class, String.class);
      Object result = operationMethod.invoke(null, extractor, container.getText());
      return RequestMatcher.class.cast(result);
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }
예제 #5
0
  private Resource resourceFrom(String name, TextContainer container) {
    if (container.isRawText()) {
      return invokeTarget(name, container.getText(), Resource.class);
    }

    if (container.isForTemplate()) {
      if ("version".equalsIgnoreCase(name)) {
        return version(template(container.getText()));
      }

      if (container.hasProperties()) {
        return template(
            invokeTarget(name, container.getText(), ContentResource.class), container.getProps());
      }

      return template(invokeTarget(name, container.getText(), ContentResource.class));
    }

    throw new IllegalArgumentException(format("unknown operation [%s]", container.getOperation()));
  }