|
| 1 | +package uk.gov.justice.services.test.utils.core.matchers; |
| 2 | + |
| 3 | +import com.google.common.base.Strings; |
| 4 | +import com.google.common.io.Files; |
| 5 | +import com.google.common.io.Resources; |
| 6 | +import org.everit.json.schema.ArraySchema; |
| 7 | +import org.everit.json.schema.ObjectSchema; |
| 8 | +import org.everit.json.schema.ReferenceSchema; |
| 9 | +import org.everit.json.schema.Schema; |
| 10 | +import org.everit.json.schema.loader.SchemaLoader; |
| 11 | +import org.hamcrest.Description; |
| 12 | +import org.hamcrest.Matcher; |
| 13 | +import org.hamcrest.TypeSafeMatcher; |
| 14 | +import org.json.JSONObject; |
| 15 | +import org.json.JSONTokener; |
| 16 | + |
| 17 | +import java.io.File; |
| 18 | +import java.io.IOException; |
| 19 | +import java.nio.file.Paths; |
| 20 | +import java.util.Optional; |
| 21 | + |
| 22 | +import static com.google.common.base.Charsets.UTF_8; |
| 23 | +import static java.lang.String.format; |
| 24 | + |
| 25 | +public class JsonSchemaPropertyMatcher { |
| 26 | + |
| 27 | + private static final String JSON_PATH_SEPARATOR = "\\."; |
| 28 | + |
| 29 | + /** |
| 30 | + * Matcher to validate if a given property exists in the given json schema |
| 31 | + * |
| 32 | + * @param jsonpathToRequiredProperty json path of the property that should exist in the schema (i.e. case.offences.offenceId) |
| 33 | + * @return matcher |
| 34 | + */ |
| 35 | + public static Matcher<String> hasProperty(final String jsonpathToRequiredProperty) { |
| 36 | + |
| 37 | + return new TypeSafeMatcher<String>() { |
| 38 | + private String pathToJsonFile = null; |
| 39 | + |
| 40 | + @Override |
| 41 | + protected boolean matchesSafely(final String pathToJsonFile) { |
| 42 | + this.pathToJsonFile = pathToJsonFile; |
| 43 | + final JsonPath jsonPath = JsonPath.of(jsonpathToRequiredProperty); |
| 44 | + final ObjectSchema parentObjectSchema = getObjectSchemaFromFile(pathToJsonFile); |
| 45 | + return getObjectSchemaWithPath(parentObjectSchema, jsonPath).getPropertySchemas().containsKey(jsonPath.getProperty()); |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public void describeTo(final Description description) { |
| 50 | + description.appendText("property ").appendValue(jsonpathToRequiredProperty) |
| 51 | + .appendText(" should exist in JSON Schema ").appendValue(pathToJsonFile); |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + protected void describeMismatchSafely(final String pathToJsonFile, final Description mismatchDescription) { |
| 56 | + mismatchDescription.appendText("property ").appendValue(jsonpathToRequiredProperty).appendText(" doesn't exist in schema"); |
| 57 | + } |
| 58 | + }; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Matcher to validate if a given property is a required (non-optional) property in the given json schema |
| 63 | + * |
| 64 | + * @param jsonpathToRequiredProperty json path of the property that should be a required property in the schema (i.e. case.offences.offenceId) |
| 65 | + * @return matcher |
| 66 | + */ |
| 67 | + public static Matcher<String> hasRequiredProperty(final String jsonpathToRequiredProperty) { |
| 68 | + |
| 69 | + return new TypeSafeMatcher<String>() { |
| 70 | + private String pathToJsonFile = null; |
| 71 | + |
| 72 | + @Override |
| 73 | + protected boolean matchesSafely(final String pathToJsonFile) { |
| 74 | + this.pathToJsonFile = pathToJsonFile; |
| 75 | + final JsonPath jsonPath = JsonPath.of(jsonpathToRequiredProperty); |
| 76 | + final ObjectSchema parentObjectSchema = getObjectSchemaFromFile(pathToJsonFile); |
| 77 | + return getObjectSchemaWithPath(parentObjectSchema, jsonPath).getRequiredProperties().contains(jsonPath.getProperty()); |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public void describeTo(final Description description) { |
| 82 | + description.appendText("property ").appendValue(jsonpathToRequiredProperty) |
| 83 | + .appendText(" should be a required property in JSON Schema ").appendValue(pathToJsonFile); |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + protected void describeMismatchSafely(final String pathToJsonFile, final Description mismatchDescription) { |
| 88 | + mismatchDescription.appendText("property ").appendValue(jsonpathToRequiredProperty).appendText(" is not a required property"); |
| 89 | + } |
| 90 | + }; |
| 91 | + } |
| 92 | + |
| 93 | + private static ObjectSchema getObjectSchemaFromFile(final String pathToJsonSchema) { |
| 94 | + final String jsonSchema = getJsonContentFrom(pathToJsonSchema); |
| 95 | + final Schema rawSchema = SchemaLoader.load(new JSONObject(new JSONTokener(jsonSchema))); |
| 96 | + return Optional.of(rawSchema) |
| 97 | + .filter(ObjectSchema.class::isInstance) |
| 98 | + .map(ObjectSchema.class::cast) |
| 99 | + .orElseThrow(() -> new IllegalArgumentException(format("Schema found in file %s is invalid.", pathToJsonSchema))); |
| 100 | + } |
| 101 | + |
| 102 | + private static String getJsonContentFrom(final String pathToJsonSchema) { |
| 103 | + try { |
| 104 | + if (Paths.get(pathToJsonSchema).isAbsolute()) { |
| 105 | + return Files.toString(new File(pathToJsonSchema), UTF_8); |
| 106 | + } else { |
| 107 | + return Resources.toString(Resources.getResource(pathToJsonSchema), UTF_8); |
| 108 | + } |
| 109 | + } catch (IOException ex) { |
| 110 | + throw new IllegalArgumentException(format("Schema file %s not found.", pathToJsonSchema)); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + private static ObjectSchema getObjectSchemaWithPath(final ObjectSchema parentObjectSchema, final JsonPath jsonPath) { |
| 115 | + return jsonPath.getRemainder().map(s -> getObjectSchemaWithPathRecursive(parentObjectSchema, jsonPath)).orElse(parentObjectSchema); |
| 116 | + } |
| 117 | + |
| 118 | + private static ObjectSchema getObjectSchemaWithPathRecursive(final Schema rawSchema, final JsonPath jsonPath) { |
| 119 | + if (rawSchema == null) { |
| 120 | + throw new IllegalArgumentException("Invalid XPath to property."); |
| 121 | + } else if (rawSchema instanceof ObjectSchema) { |
| 122 | + final ObjectSchema objectSchema = (ObjectSchema) rawSchema; |
| 123 | + return jsonPath.getRemainder().map(remainder -> getObjectSchemaWithPathRecursive(objectSchema.getPropertySchemas().get(jsonPath.getParent()), JsonPath.of(remainder))).orElse(objectSchema); |
| 124 | + } else if (rawSchema instanceof ArraySchema) { |
| 125 | + final ArraySchema arraySchema = (ArraySchema) rawSchema; |
| 126 | + return getObjectSchemaWithPathRecursive(arraySchema.getAllItemSchema(), jsonPath); |
| 127 | + } else if (rawSchema instanceof ReferenceSchema) { |
| 128 | + final ReferenceSchema referenceSchema = (ReferenceSchema) rawSchema; |
| 129 | + return getObjectSchemaWithPathRecursive(referenceSchema.getReferredSchema(), jsonPath); |
| 130 | + } |
| 131 | + throw new IllegalArgumentException("Unsupported property type."); |
| 132 | + } |
| 133 | + |
| 134 | + private static class JsonPath { |
| 135 | + private final String property; |
| 136 | + private final String parent; |
| 137 | + private final String remainder; |
| 138 | + |
| 139 | + private static JsonPath of(final String jsonPath) { |
| 140 | + final String[] jsonPathSplit = jsonPath.split(JSON_PATH_SEPARATOR); |
| 141 | + return new JsonPath( |
| 142 | + jsonPathSplit[jsonPathSplit.length - 1], |
| 143 | + jsonPathSplit[0], |
| 144 | + jsonPathSplit.length >= 2 ? Strings.emptyToNull(jsonPath.split(JSON_PATH_SEPARATOR, 2)[1]) : null); |
| 145 | + } |
| 146 | + |
| 147 | + private JsonPath(final String property, final String parent, final String remainder) { |
| 148 | + this.parent = parent; |
| 149 | + this.remainder = remainder; |
| 150 | + this.property = property; |
| 151 | + } |
| 152 | + |
| 153 | + public String getProperty() { |
| 154 | + return property; |
| 155 | + } |
| 156 | + |
| 157 | + public String getParent() { |
| 158 | + return parent; |
| 159 | + } |
| 160 | + |
| 161 | + public Optional<String> getRemainder() { |
| 162 | + return Optional.ofNullable(remainder); |
| 163 | + } |
| 164 | + } |
| 165 | +} |
0 commit comments