Ejemplo n.º 1
0
  @Override
  public void doExecute(TestContext context) {
    try {
      ClassLoader parent = getClass().getClassLoader();
      GroovyClassLoader loader = new GroovyClassLoader(parent);

      assertScriptProvided();

      String rawCode =
          StringUtils.hasText(script)
              ? script.trim()
              : FileUtils.readToString(FileUtils.getFileResource(scriptResourcePath, context));
      String code = context.replaceDynamicContentInString(rawCode.trim());

      // load groovy code
      Class<?> groovyClass = loader.parseClass(code);
      // Instantiate an object from groovy code
      GroovyObject groovyObject = (GroovyObject) groovyClass.newInstance();

      // only apply default script template in case we have feature enabled and code is not a class,
      // too
      if (useScriptTemplate && groovyObject.getClass().getSimpleName().startsWith("script")) {
        // build new script with surrounding template
        code =
            TemplateBasedScriptBuilder.fromTemplateResource(
                    FileUtils.getFileResource(scriptTemplatePath, context))
                .withCode(code)
                .build();

        groovyClass = loader.parseClass(code);
        groovyObject = (GroovyObject) groovyClass.newInstance();
      }

      if (log.isDebugEnabled()) {
        log.debug("Executing Groovy script:\n" + code);
      }

      // execute the Groovy script
      if (groovyObject instanceof ScriptExecutor) {
        ((ScriptExecutor) groovyObject).execute(context);
      } else {
        groovyObject.invokeMethod("run", new Object[] {});
      }

      log.info("Groovy script execution successfully");
    } catch (CitrusRuntimeException e) {
      throw e;
    } catch (Exception e) {
      throw new CitrusRuntimeException(e);
    }
  }
Ejemplo n.º 2
0
  /**
   * @see
   *     com.consol.citrus.actions.ReceiveMessageAction#validateMessage(org.springframework.integration.Message,
   *     com.consol.citrus.context.TestContext)
   */
  @Override
  protected void validateMessage(Message<?> receivedMessage, TestContext context) {
    try {
      super.validateMessage(receivedMessage, context);

      if (attachmentData != null) {
        controlAttachment.setContent(context.replaceDynamicContentInString(attachmentData));
      } else if (attachmentResourcePath != null) {
        controlAttachment.setContent(
            context.replaceDynamicContentInString(
                FileUtils.readToString(
                    FileUtils.getFileResource(attachmentResourcePath, context))));
      } else {
        return; // no attachment expected, no validation
      }

      // handle variables in content id
      if (controlAttachment.getContentId() != null) {
        controlAttachment.setContentId(
            context.replaceDynamicContentInString(controlAttachment.getContentId()));
      }

      // handle variables in content type
      if (controlAttachment.getContentType() != null) {
        controlAttachment.setContentType(
            context.replaceDynamicContentInString(controlAttachment.getContentType()));
      }

      attachmentValidator.validateAttachment(receivedMessage, controlAttachment);
    } catch (IOException e) {
      throw new CitrusRuntimeException(e);
    }
  }