com.github.codeframes.hal.tooling.test.json.JsonObjectMatchers.groovy Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hal-tooling-test Show documentation
Show all versions of hal-tooling-test Show documentation
A collection of Groovy utilities and Spock Framework extensions used for testing RESTful API's.
/*
* Copyright © 2016 Richard Burrow (https://github.com/codeframes)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.codeframes.hal.tooling.test.json
import org.hamcrest.BaseMatcher
import org.hamcrest.Description
import org.hamcrest.Matcher
import org.hamcrest.StringDescription
/**
* A collection of Hamcrest Matcher's for JSON assertions.
*/
class JsonObjectMatchers {
/**
* Returns a Matcher for asserting if a provided JSON Array matches that of jsonArray, ignoring the ordering
* elements.
*
* @param jsonArray the expected JSON Array to match against ignoring the ordering of the elements
* @return a Matcher to assert if a provided JSON Array matches that of jsonArray, ignoring the ordering elements
*/
static Matcher inAnyOrder(String jsonArray) {
return new UnorderedListMatcher(JsonUtils.toJsonObj(jsonArray) as List)
}
/**
* ul (Unordered List), an alias of {@link #inAnyOrder(java.lang.String)} with a minimalist name for convenient
* embedding in {@link JSON#JSON(GString)}.
*/
static Matcher ul(String jsonArray) {
return inAnyOrder(jsonArray)
}
/**
* Returns a Matcher for asserting if a provided JSON Array matches that of jsonArray, ignoring the ordering elements.
* In addition supports templated values as described by {@link JsonUtils#toJsonObj(GString json)}.
*
* @param jsonArray the expected JSON Array to match against ignoring the ordering of the elements
* @return a Matcher to assert if a provided JSON Array matches that of jsonArray, ignoring the ordering elements
*/
static Matcher inAnyOrder(GString jsonArray) {
return new UnorderedListMatcher(JsonUtils.toJsonObj(jsonArray) as List)
}
/**
* ul (Unordered List), an alias of {@link #inAnyOrder(groovy.lang.GString)} with a minimalist name for convenient
* embedding in {@link JSON#JSON(GString)}.
*/
static Matcher ul(GString jsonArray) {
return inAnyOrder(jsonArray)
}
static class UnorderedListMatcher extends BaseMatcher {
private final List expected
UnorderedListMatcher(List expected) {
this.expected = expected
}
@Override
boolean matches(Object actual) {
List actualList = (List) actual
for (Object expectedItem : expected) {
boolean found = false
for (Object actualItem : actualList) {
if (actualItem == expectedItem) {
found = true
break
} else if (expectedItem instanceof Map) {
if (equalTo(expectedItem).matches(actualItem)) {
found = true
break
}
} else if (expectedItem instanceof List) {
if (equalTo(expectedItem).matches(actualItem)) {
found = true
break
}
} else {
return false
}
}
if (!found) {
return false
}
}
return true
}
@Override
void describeTo(Description description) {
description.appendText("to contain in no particular order ").appendValue(expected)
}
@Override
void describeMismatch(Object item, Description description) {
description.appendText("was ").appendValue(item)
}
@Override
public String toString() {
return expected.toString()
}
}
/**
* Returns a Matcher for asserting if a provided JSON Object matches that of jsonObj.
*
* @param jsonObj the expected JSON Object to match against
* @return a Matcher to assert if a provided JSON Object matches that of jsonObj
*/
static Matcher
© 2015 - 2024 Weber Informatics LLC | Privacy Policy