Class Util

java.lang.Object
io.jenkins.plugins.casc.misc.Util

public class Util extends Object
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    assertLogContains(org.jvnet.hudson.test.LoggerRule logging, String expectedText)
    Checks whether LoggerRule has recorded the message.
    static void
    assertLogContains(org.jvnet.hudson.test.LogRecorder logging, String expectedText)
    Checks whether LogRecorder has recorded the message.
    static void
    assertNotInLog(org.jvnet.hudson.test.LoggerRule logging, String unexpectedText)
    Checks whether LoggerRule has not recorded the message.
    static void
    assertNotInLog(org.jvnet.hudson.test.LogRecorder logging, String unexpectedText)
    Checks whether LogRecorder has not recorded the message.
    static String
    convertToJson(String yamlString)
    Converts a given yamlString into a JsonString.
    static org.json.JSONObject
    convertYamlFileToJson(Object clazz, String yamlFileName)
    Converts a YAML file into a json object
    static io.jenkins.plugins.casc.core.JenkinsConfigurator
    Gets the Jenkins configurator.
    static io.jenkins.plugins.casc.model.Mapping
    getJenkinsRoot(io.jenkins.plugins.casc.ConfigurationContext context)
    Gets the "jenkins" root mapping.
    static io.jenkins.plugins.casc.model.Mapping
    getSecurityRoot(io.jenkins.plugins.casc.ConfigurationContext context)
    Gets the "security" root mapping.
    static io.jenkins.plugins.casc.model.Mapping
    getToolRoot(io.jenkins.plugins.casc.ConfigurationContext context)
    Gets the "tool" root mapping.
    static io.jenkins.plugins.casc.model.Mapping
    getUnclassifiedRoot(io.jenkins.plugins.casc.ConfigurationContext context)
    Gets the "unclassified" root mapping.
    static org.everit.json.schema.Schema
    Retrieves the JSON schema for the running jenkins instance.
    static String
    toStringFromYamlFile(Object clazz, String resourcePath)
    Reads a resource from the classpath to use in asserting expected export content.
    static String
    toYamlString(io.jenkins.plugins.casc.model.CNode rootNode)
    Converts a given CNode into a string.
    static List<String>
    validateSchema(org.json.JSONObject jsonSubject)
    Validates a given jsonObject against the schema generated for the current live jenkins instance.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • Util

      public Util()
  • Method Details

    • getJenkinsConfigurator

      public static io.jenkins.plugins.casc.core.JenkinsConfigurator getJenkinsConfigurator()
      Gets the Jenkins configurator.
      Returns:
      the Jenkins configurator
    • getJenkinsRoot

      public static io.jenkins.plugins.casc.model.Mapping getJenkinsRoot(io.jenkins.plugins.casc.ConfigurationContext context) throws Exception
      Gets the "jenkins" root mapping.

      Example usage:

      
       ConfiguratorRegistry registry = ConfiguratorRegistry.get();
       ConfigurationContext context = new ConfigurationContext(registry);
       CNode configNode = getJenkinsRoot(context).get("nodes");
      Parameters:
      context - the configuration context
      Returns:
      the "jenkins" root mapping
      Throws:
      Exception - something's not right...
    • getUnclassifiedRoot

      public static io.jenkins.plugins.casc.model.Mapping getUnclassifiedRoot(io.jenkins.plugins.casc.ConfigurationContext context) throws Exception
      Gets the "unclassified" root mapping.

      Example usage:

      
       ConfiguratorRegistry registry = ConfiguratorRegistry.get();
       ConfigurationContext context = new ConfigurationContext(registry);
       CNode configNode = getUnclassifiedRoot(context).get("my-plugin-attribute");
      Parameters:
      context - the configuration context
      Returns:
      the "unclassified" root mapping
      Throws:
      Exception - something's not right...
    • getSecurityRoot

      public static io.jenkins.plugins.casc.model.Mapping getSecurityRoot(io.jenkins.plugins.casc.ConfigurationContext context) throws Exception
      Gets the "security" root mapping.

      Example usage:

      
       ConfiguratorRegistry registry = ConfiguratorRegistry.get();
       ConfigurationContext context = new ConfigurationContext(registry);
       CNode configNode = getSecurityRoot(context).get("GlobalJobDslSecurityConfiguration");
      Parameters:
      context - the configuration context
      Returns:
      the "security" root mapping
      Throws:
      Exception - something's not right...
    • getToolRoot

      public static io.jenkins.plugins.casc.model.Mapping getToolRoot(io.jenkins.plugins.casc.ConfigurationContext context) throws Exception
      Gets the "tool" root mapping.

      Example usage:

      
       ConfiguratorRegistry registry = ConfiguratorRegistry.get();
       ConfigurationContext context = new ConfigurationContext(registry);
       CNode configNode = getToolRoot(context).get("git");
      Parameters:
      context - the configuration context
      Returns:
      the "tool" root mapping
      Throws:
      Exception - something's not right...
    • toYamlString

      public static String toYamlString(io.jenkins.plugins.casc.model.CNode rootNode) throws IOException
      Converts a given CNode into a string.

      Example usage:

      
       ConfiguratorRegistry registry = ConfiguratorRegistry.get();
       ConfigurationContext context = new ConfigurationContext(registry);
       CNode yourAttribute = getUnclassifiedRoot(context).get("<your-attribute>");
      
       String exported = toYamlString(yourAttribute);
      Parameters:
      rootNode - the CNode to convert to a string
      Returns:
      a YAML string
      Throws:
      IOException - if exporting to YAML fails
    • toStringFromYamlFile

      public static String toStringFromYamlFile(Object clazz, String resourcePath) throws URISyntaxException
      Reads a resource from the classpath to use in asserting expected export content.

      The resource is expected to be UTF-8 encoded.

      Example usage:

      
       toStringFromYamlFile(this, "expected-output.yml");
      Parameters:
      clazz - pass in this
      resourcePath - the file name to read, should be in the same package as your test class in resources
      Returns:
      the string content of the file
      Throws:
      URISyntaxException - if an invalid URI is passed.
    • assertNotInLog

      public static void assertNotInLog(org.jvnet.hudson.test.LoggerRule logging, String unexpectedText)
      Checks whether LoggerRule has not recorded the message.
      Parameters:
      logging - Logger rule
      unexpectedText - Text to check
      Since:
      1.25
    • assertNotInLog

      public static void assertNotInLog(org.jvnet.hudson.test.LogRecorder logging, String unexpectedText)
      Checks whether LogRecorder has not recorded the message.
      Parameters:
      logging - Logger rule
      unexpectedText - Text to check
    • assertLogContains

      public static void assertLogContains(org.jvnet.hudson.test.LoggerRule logging, String expectedText)
      Checks whether LoggerRule has recorded the message.
      Parameters:
      logging - Logger rule
      expectedText - Text to check
      Since:
      1.25
    • assertLogContains

      public static void assertLogContains(org.jvnet.hudson.test.LogRecorder logging, String expectedText)
      Checks whether LogRecorder has recorded the message.
      Parameters:
      logging - Logger rule
      expectedText - Text to check
    • convertToJson

      public static String convertToJson(String yamlString)

      Converts a given yamlString into a JsonString.

      Example Usage:

      
       String jsonString = convertToJson(yourYamlString);
       
      Parameters:
      yamlString - the yaml to convert
      Returns:
      the json conversion of the yaml string.
    • returnSchema

      public static org.everit.json.schema.Schema returnSchema()
      Retrieves the JSON schema for the running jenkins instance.

      Example Usage:

      
            Schema jsonSchema = returnSchema();
            
      Returns:
      the schema for the current jenkins instance
    • validateSchema

      public static List<String> validateSchema(org.json.JSONObject jsonSubject)
      Validates a given jsonObject against the schema generated for the current live jenkins instance.

      Example Usage:

      
         assertThat(validateSchema(convertYamlFileToJson(this, "invalidSchemaConfig.yml")),
                   contains("#/jenkins/numExecutors: expected type: Number, found: String"));
        
      
         assertThat(validateSchema(convertYamlFileToJson(this, "validConfig.yml")),
                   empty());
        
      Parameters:
      jsonSubject - the json object that needs to be validated
      Returns:
      a list of validation errors, empty list if no errors
    • convertYamlFileToJson

      public static org.json.JSONObject convertYamlFileToJson(Object clazz, String yamlFileName) throws URISyntaxException
      Converts a YAML file into a json object

      Example Usage:

      
        JSONObject jsonObject = convertYamlFileToJson(this, "filename");
       
      Parameters:
      clazz - the class used for loading resources, normally you want to pass 'this'
      yamlFileName - the name of the yaml file that needs to be converted
      Returns:
      JSONObject pertaining to that yaml file.
      Throws:
      URISyntaxException - if an invalid URI is passed.