Package io.jenkins.plugins.casc.misc
Class Util
java.lang.Object
io.jenkins.plugins.casc.misc.Util
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidassertLogContains(org.jvnet.hudson.test.LoggerRule logging, String expectedText) Checks whetherLoggerRulehas recorded the message.static voidassertLogContains(org.jvnet.hudson.test.LogRecorder logging, String expectedText) Checks whetherLogRecorderhas recorded the message.static voidassertNotInLog(org.jvnet.hudson.test.LoggerRule logging, String unexpectedText) Checks whetherLoggerRulehas not recorded the message.static voidassertNotInLog(org.jvnet.hudson.test.LogRecorder logging, String unexpectedText) Checks whetherLogRecorderhas not recorded the message.static StringconvertToJson(String yamlString) Converts a given yamlString into a JsonString.static org.json.JSONObjectconvertYamlFileToJson(Object clazz, String yamlFileName) Converts a YAML file into a json objectstatic io.jenkins.plugins.casc.core.JenkinsConfiguratorGets the Jenkins configurator.static io.jenkins.plugins.casc.model.MappinggetJenkinsRoot(io.jenkins.plugins.casc.ConfigurationContext context) Gets the "jenkins" root mapping.static io.jenkins.plugins.casc.model.MappinggetSecurityRoot(io.jenkins.plugins.casc.ConfigurationContext context) Gets the "security" root mapping.static io.jenkins.plugins.casc.model.MappinggetToolRoot(io.jenkins.plugins.casc.ConfigurationContext context) Gets the "tool" root mapping.static io.jenkins.plugins.casc.model.MappinggetUnclassifiedRoot(io.jenkins.plugins.casc.ConfigurationContext context) Gets the "unclassified" root mapping.static org.everit.json.schema.SchemaRetrieves the JSON schema for the running jenkins instance.static StringtoStringFromYamlFile(Object clazz, String resourcePath) Reads a resource from the classpath to use in asserting expected export content.static StringtoYamlString(io.jenkins.plugins.casc.model.CNode rootNode) Converts a givenCNodeinto a string.validateSchema(org.json.JSONObject jsonSubject) Validates a given jsonObject against the schema generated for the current live jenkins instance.
-
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
Converts a givenCNodeinto 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- theCNodeto 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 inthisresourcePath- 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
Checks whetherLoggerRulehas not recorded the message.- Parameters:
logging- Logger ruleunexpectedText- Text to check- Since:
- 1.25
-
assertNotInLog
Checks whetherLogRecorderhas not recorded the message.- Parameters:
logging- Logger ruleunexpectedText- Text to check
-
assertLogContains
Checks whetherLoggerRulehas recorded the message.- Parameters:
logging- Logger ruleexpectedText- Text to check- Since:
- 1.25
-
assertLogContains
public static void assertLogContains(org.jvnet.hudson.test.LogRecorder logging, String expectedText) Checks whetherLogRecorderhas recorded the message.- Parameters:
logging- Logger ruleexpectedText- Text to check
-
convertToJson
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
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 objectExample 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.
-