+fun isOwnIdentity(id: String, nickname: String, requestUri: String, insertUri: String, contexts: Matcher<Iterable<String>>, properties: Matcher<Map<out String, String>>) =
+ AttributeMatcher<OwnIdentity>("own identity")
+ .addAttribute("id", id, OwnIdentity::getId)
+ .addAttribute("nickname", nickname, OwnIdentity::getNickname)
+ .addAttribute("request uri", requestUri, OwnIdentity::getRequestUri)
+ .addAttribute("insert uri", insertUri, OwnIdentity::getInsertUri)
+ .addAttribute("contexts", OwnIdentity::getContexts, contexts)
+ .addAttribute("properties", OwnIdentity::getProperties, properties)
+
+fun hasField(name: String, valueMatcher: Matcher<String>) = object : TypeSafeDiagnosingMatcher<SimpleFieldSet>() {
+ override fun matchesSafely(item: SimpleFieldSet, mismatchDescription: Description) =
+ valueMatcher.matches(item.get(name)).onFalse {
+ valueMatcher.describeMismatch(item, mismatchDescription)
+ }
+
+ override fun describeTo(description: Description) {
+ description
+ .appendText("simple field set with key ").appendValue(name)
+ .appendText(", value ").appendValue(valueMatcher)
+ }
+}
+
+/**
+ * [TypeSafeDiagnosingMatcher] implementation that aims to cut down boilerplate on verifying the attributes
+ * of typical container objects.
+ */
+class AttributeMatcher<T>(private val objectName: String) : TypeSafeDiagnosingMatcher<T>() {
+
+ private data class AttributeToMatch<T, V>(
+ val name: String,
+ val getter: (T) -> V,
+ val matcher: Matcher<out V>
+ )
+
+ private val attributesToMatch = mutableListOf<AttributeToMatch<T, *>>()
+
+ /**
+ * Adds an attribute to check for equality, returning `this`.
+ */
+ fun <V> addAttribute(name: String, expected: V, getter: (T) -> V): AttributeMatcher<T> = apply {
+ attributesToMatch.add(AttributeToMatch(name, getter, describedAs("$name %0", equalTo(expected), expected)))
+ }
+
+ /**
+ * Adds an attribute to check with the given [hamcrest matcher][Matcher].
+ */
+ fun <V> addAttribute(name: String, getter: (T) -> V, matcher: Matcher<out V>) = apply {
+ attributesToMatch.add(AttributeToMatch(name, getter, matcher))
+ }
+
+ override fun describeTo(description: Description) {
+ attributesToMatch.forEachIndexed { index, attributeToMatch ->
+ if (index == 0) {
+ description.appendText("$objectName with ")
+ } else {
+ description.appendText(", ")
+ }
+ attributeToMatch.matcher.describeTo(description)
+ }
+ }
+
+ override fun matchesSafely(item: T, mismatchDescription: Description): Boolean =
+ attributesToMatch.fold(true) { matches, attributeToMatch ->
+ if (!matches) {
+ false
+ } else {
+ if (!attributeToMatch.matcher.matches(attributeToMatch.getter(item))) {
+ mismatchDescription.appendText("but ${attributeToMatch.name} ")
+ attributeToMatch.matcher.describeMismatch(attributeToMatch.getter(item), mismatchDescription)
+ false
+ } else {
+ true
+ }
+ }
+ }
+
+}