######################################################################
#
# File editing edit_xml - example for inserting a host into a Tomcat configuration file
#
######################################################################
body common control
{
  inputs => { "../../default.sub.cf" };
  bundlesequence => { default("$(this.promise_filename)") };
  version => "1.0";
}

#######################################################
bundle agent init
{
  vars:
    "states" slist => { "actual", "expected" };

    "actual"
      string => "<Server port=\"8005\" shutdown=\"SHUTDOWN\" debug=\"0\">

  <!-- Define the Tomcat Stand-Alone Service -->
  <Service name=\"Catalina\">

        <!-- Define a non-SSL Coyote HTTP/1.1 Connector on port 8080 -->
    <Connector port=\"80\"
               maxThreads=\"150\" minSpareThreads=\"25\" maxSpareThreads=\"75\"
               enableLookups=\"false\" redirectPort=\"8443\" acceptCount=\"100\"
               debug=\"0\" connectionTimeout=\"20000\" 
               disableUploadTimeout=\"true\" />

                <Engine name=\"Catalina\" defaultHost=\"localhost\" debug=\"0\">

      <!-- Define the default virtual host
           Note: XML Schema validation will not work with Xerces 2.2.
       -->

      <Host name=\"localhost\" debug=\"0\" appBase=\"webapps/localhost\"
       unpackWARs=\"true\" autoDeploy=\"true\"
       xmlValidation=\"false\" xmlNamespaceAware=\"false\">
        <Logger className=\"org.apache.catalina.logger.FileLogger\"
                 directory=\"logs\"  prefix=\"localhost_log.\" suffix=\".txt\"
            timestamp=\"true\"/>
      </Host>

         <!-- VIRTUAL HOST INJECTION POINT -->

    </Engine>

  </Service>

</Server>";

    "expected"
      string => "<?xml version=\"1.0\"?>
<Server
    port=\"8005\"
    shutdown=\"SHUTDOWN\"
    debug=\"0\">

  <!-- Define the Tomcat Stand-Alone Service -->
  <Service name=\"Catalina\">

        <!-- Define a non-SSL Coyote HTTP/1.1 Connector on port 8080 -->
    <Connector port=\"80\" maxThreads=\"150\" minSpareThreads=\"25\" maxSpareThreads=\"75\" enableLookups=\"false\" redirectPort=\"8443\" acceptCount=\"100\" debug=\"0\" connectionTimeout=\"20000\" disableUploadTimeout=\"true\"/>

                <Engine name=\"Catalina\" defaultHost=\"localhost\" debug=\"0\">

      <!-- Define the default virtual host
           Note: XML Schema validation will not work with Xerces 2.2.
       -->

      <Host name=\"localhost\" debug=\"0\" appBase=\"webapps/localhost\" unpackWARs=\"true\" autoDeploy=\"true\" xmlValidation=\"false\" xmlNamespaceAware=\"false\">
        <Logger className=\"org.apache.catalina.logger.FileLogger\" directory=\"logs\" prefix=\"localhost_log.\" suffix=\".txt\" timestamp=\"true\"/>
      </Host>

         <!-- VIRTUAL HOST INJECTION POINT -->

    <Host name=\"cfe_host\" debug=\"0\" appBase=\"webapps/cfe_host\" unpackWARs=\"true\" autoDeploy=\"true\" xmlValidation=\"false\" xmlNamespaceAware=\"false\">

<Logger className=\"org.apache.catalina.logger.FileLogger\" directory=\"logs\" prefix=\"cfe_host\_log.\" suffix=\".txt\" timestamp=\"true\"/>

<Alias>cfe_alias</Alias>
</Host></Engine>

  </Service>

</Server>";

  files:
    "$(G.testfile).$(states)"
      create => "true",
      edit_line => init_insert("$(init.$(states))"),
      edit_defaults => init_empty;
}

bundle edit_line init_insert(str)
{
  insert_lines:
    "$(str)";
}

body edit_defaults init_empty
{
  empty_file_before_editing => "true";
}

#######################################################
bundle agent test
{
  meta:
    "test_soft_fail"
      string => "windows",
      meta => { "ENT-10215" };

  vars:
    "host"
      string => "<Host name=\"cfe_host\" debug=\"0\" appBase=\"webapps/cfe_host\"
        unpackWARs=\"true\" autoDeploy=\"true\"
    xmlValidation=\"false\" xmlNamespaceAware=\"false\">

<Logger className=\"org.apache.catalina.logger.FileLogger\"
directory=\"logs\"  prefix=\"cfe_host\_log.\" suffix=\".txt\" timestamp=\"true\"/>

<Alias>cfe_alias</Alias>
</Host>";

  files:
    "$(G.testfile).actual"
      create => "true",
      edit_xml => test_insert("$(test.host)");
}

bundle edit_xml test_insert(str)
{
  insert_tree:
    "$(str)" select_xpath => "/Server/Service/Engine";
}

#######################################################
bundle agent check
{
  methods:
    "any"
      usebundle => xml_check_diff(
        "$(G.testfile).actual",
        "$(G.testfile).expected",
        "$(this.promise_filename)",
        "no"
      );
}

### PROJECT_ID: core
### CATEGORY_ID: 27
