Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added test Framework Utility Functions For the json Schema #1078

Merged
merged 24 commits into from
Nov 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions docs/PLUGINS.md
Original file line number Diff line number Diff line change
Expand Up @@ -311,3 +311,32 @@ public class ConfigurationAsCodeTest {
}
}
```

### JSON Schema Test (Beta)
We generate a JSON schema that users can use to validate their changes and provide IDE assistance,
you can test that your plugin's example yaml file validates correctly by implementing the below test:
`SchemaGenerationTest` provides a abstraction layer to test out the plugins YAML file against the generated schema.

You can test if your YAML file validates against the schema.

Step 1

Create a YAML file for the configurators corresponding to the developed plugin.
For eg: `validJenkinsConfigurator.yml`
```yaml
jenkins:
systemMessage: "Configured by Configuration as Code plugin"
numExecutors: "Hello"

```

Step 2

Add a test for the YAML file

```java
@Test
public void validSchemaShouldSucceed() throws Exception {
assertTrue(validateSchema(convertYamlFileToJson(this, "validJenkinsConfigurator.yml")));
}
```
Original file line number Diff line number Diff line change
Expand Up @@ -209,4 +209,3 @@ public static String removeHtmlTags(String htmlDocString) {
return htmlDocString.replaceAll("\\<.*?\\>", "").trim();
}
}

Original file line number Diff line number Diff line change
@@ -1,57 +1,30 @@
package io.jenkins.plugins.casc;

import io.jenkins.plugins.casc.misc.JenkinsConfiguredWithCodeRule;
import io.jenkins.plugins.casc.misc.Util;
import org.everit.json.schema.Schema;
import org.everit.json.schema.ValidationException;
import org.everit.json.schema.loader.SchemaLoader;
import org.json.JSONObject;
import org.json.JSONTokener;
import org.junit.Rule;
import org.junit.Test;

import static io.jenkins.plugins.casc.SchemaGeneration.generateSchema;
import static io.jenkins.plugins.casc.SchemaGeneration.removeHtmlTags;
import static io.jenkins.plugins.casc.SchemaGeneration.retrieveDocStringFromAttribute;
import static io.jenkins.plugins.casc.misc.Util.toStringFromYamlFile;
import static io.jenkins.plugins.casc.misc.Util.convertYamlFileToJson;
import static io.jenkins.plugins.casc.misc.Util.validateSchema;
import static junit.framework.TestCase.assertTrue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import static org.junit.Assert.assertFalse;

public class SchemaGenerationTest {

@Rule
public JenkinsConfiguredWithCodeRule j = new JenkinsConfiguredWithCodeRule();

@Test
public void validSchemaShouldSucceed() throws Exception {
JSONObject schemaObject = generateSchema();
JSONObject jsonSchema = new JSONObject(
new JSONTokener(schemaObject.toString()));
String yamlStringContents = toStringFromYamlFile(this, "validSchemaConfig.yml");
JSONObject jsonSubject = new JSONObject(
new JSONTokener(Util.convertToJson(yamlStringContents)));
Schema schema = SchemaLoader.load(jsonSchema);
try {
schema.validate(jsonSubject);
} catch (Exception e) {
fail(e.getMessage());
}
assertTrue(validateSchema(convertYamlFileToJson(this, "validSchemaConfig.yml")));
}

@Test
public void invalidSchemaShouldNotSucceed() throws Exception {
JSONObject schemaObject = generateSchema();
JSONObject jsonSchema = new JSONObject(
new JSONTokener(schemaObject.toString()));
String yamlStringContents = toStringFromYamlFile(this, "invalidSchemaConfig.yml");
JSONObject jsonSubject = new JSONObject(
new JSONTokener(Util.convertToJson(yamlStringContents)));
Schema schema = SchemaLoader.load(jsonSchema);
try {
schema.validate(jsonSubject);
fail();
} catch (ValidationException ve) {
ve.printStackTrace();
}
assertFalse(validateSchema(convertYamlFileToJson(this,"invalidSchemaConfig.yml")));
}

@Test
Expand Down
57 changes: 57 additions & 0 deletions plugin/src/test/java/io/jenkins/plugins/casc/misc/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,26 @@
import java.nio.file.Paths;
import java.util.Map;
import java.util.Objects;
import java.util.logging.Level;
import java.util.logging.Logger;
import jenkins.model.GlobalConfigurationCategory;
import jenkins.tools.ToolConfigurationCategory;
import org.everit.json.schema.Schema;
import org.everit.json.schema.loader.SchemaLoader;
import org.json.JSONObject;
import org.json.JSONTokener;
import org.jvnet.hudson.test.LoggerRule;

import static io.jenkins.plugins.casc.ConfigurationAsCode.serializeYamlNode;
import static io.jenkins.plugins.casc.SchemaGeneration.generateSchema;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

public class Util {

private static final Logger LOGGER = Logger.getLogger(Util.class.getName());
private static final String failureMessage = "The YAML file provided for this schema is invalid";

/**
* Gets the Jenkins configurator.
*
Expand Down Expand Up @@ -203,4 +212,52 @@ public static String convertToJson(String yamlString) {
JSONObject jsonObject=new JSONObject(map);
return jsonObject.toString();
}

/**
* Retrieves the JSON schema for the running jenkins instance.
* Example Usage:
* * <pre>{@code
* * Schema jsonSchema = returnSchema();}
* * </pre>
sladyn98 marked this conversation as resolved.
Show resolved Hide resolved
* @return Schema the schema for the current jenkins instance
*/
public static Schema returnSchema() throws Exception{
JSONObject schemaObject = generateSchema();
JSONObject jsonSchema = new JSONObject(
new JSONTokener(schemaObject.toString()));
return SchemaLoader.load(jsonSchema);
}

/**
* Validates a given jsonObject against the schema generated for the current live jenkins instance
* * Example Usage:
* * * <pre>{@code
* * * assertTrue(validateSchema(jsonSubject));}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a nicer usage could be something like:

Suggested change
* * * assertTrue(validateSchema(jsonSubject));}
* * * assertThat(yaml, isValidJsonSchema()));}

* * * </pre>
* @param jsonSubject The json Object that needs to be validated
* @return true if it's valid else returns false
*/
public static boolean validateSchema(JSONObject jsonSubject) {
try {
returnSchema().validate(jsonSubject);
} catch (Exception ie) {
LOGGER.log(Level.WARNING, failureMessage, ie);
return false;
}
return true;
}

/**
* Converts a YAML file into a json object
* Example Usage:
* * <pre>{@code
* * JSONObject jsonObject = convertYamlFileToJson("filename");}
* * </pre>
* @param yamlFileName the name of the yaml file that needs to be converted
* @return JSONObject pertaining to that yaml file.
*/
public static JSONObject convertYamlFileToJson(Object clazz, String yamlFileName) throws Exception {
String yamlStringContents = toStringFromYamlFile(clazz, yamlFileName);
return new JSONObject(new JSONTokener(convertToJson(yamlStringContents)));
}
}