--- /dev/null
+package org.onap.ccsdk.cds.blueprintsprocessor.uat.utils
+
+import org.mockito.ArgumentMatcher
+import org.skyscreamer.jsonassert.JSONAssert
+import org.skyscreamer.jsonassert.JSONCompareMode
+
+class JsonMatcher(val expectedJson: String?) : ArgumentMatcher<String> {
+
+ override fun matches(actualJson: String?): Boolean {
+ if (expectedJson == null) {
+ return actualJson == null
+ } else if (actualJson.isNullOrEmpty() && (expectedJson.isEmpty() || expectedJson.equals("null"))) {
+ // null, "" and "null" means the same here
+ return true
+ } else if (!actualJson.isNullOrEmpty() && expectedJson.isNotEmpty()) {
+ return try {
+ JSONAssert.assertEquals("", expectedJson, actualJson, JSONCompareMode.LENIENT)
+ true
+ } catch (e: AssertionError) {
+ false
+ }
+ } else {
+ return false
+ }
+ }
+}
verify(mockClient, evalVerificationMode(expectation.times)).exchangeResource(
eq(request.method),
eq(request.path),
- argThat { assertJsonEquals(request.body, this) },
+ any(),
argThat(RequiredMapEntriesMatcher(request.headers))
)
}
restClient.exchangeResource(
eq(expectation.request.method),
eq(expectation.request.path),
- any(),
+ argThat(JsonMatcher(expectation.request.body.toString())),
any()
)
)
override fun getInstance(selector: String, service: BlueprintWebClientService): BlueprintWebClientService {
var spiedService = spies[selector]
- if (spiedService != null)
+ if (spiedService != null) {
+ // inject the service here as realService: needed for "stateful services" (e.g. holding a session token)
+ spiedService.realService = service
return spiedService
+ }
spiedService = SpyService(mapper, selector, service)
spies[selector] = spiedService
open class SpyService(
private val mapper: ObjectMapper,
val selector: String,
- private val realService: BlueprintWebClientService
+ var realService: BlueprintWebClientService
) :
BlueprintWebClientService by realService {
--- /dev/null
+package org.onap.ccsdk.cds.blueprintsprocessor.uat.utils
+
+import org.junit.Assert.assertFalse
+import org.junit.Assert.assertTrue
+import org.junit.Test
+
+class JsonMatcherTest {
+ @Test
+ fun `matches easy case`() {
+ val expected = """
+ {
+ "a": "b"
+ }
+ """.trimIndent()
+ val actual = """
+ {
+ "a": "b"
+ }
+ """.trimIndent()
+ assertTrue(JsonMatcher(expected).matches(actual))
+ }
+
+ @Test
+ fun `matches fails easy case`() {
+ val expected = """
+ {
+ "a": "b"
+ }
+ """.trimIndent()
+ val actual = """
+ {
+ "a": "c"
+ }
+ """.trimIndent()
+ assertFalse(JsonMatcher(expected).matches(actual))
+ }
+
+ @Test
+ fun `matches easy case (actual is lenient aka Extensible)`() {
+ val expected = """
+ {
+ "a": "b"
+ }
+ """.trimIndent()
+ val actual = """
+ {
+ "a": "b",
+ "c": "d"
+ }
+ """.trimIndent()
+ assertTrue(JsonMatcher(expected).matches(actual))
+
+ assertFalse(JsonMatcher(actual).matches(expected))
+ }
+
+ @Test
+ fun `matches null`() {
+ assertTrue(JsonMatcher(null).matches(null))
+ }
+
+ @Test
+ fun `matches null and "null"`() {
+ val expected: String? = null
+ val actual: String? = null
+ assertTrue(JsonMatcher("null").matches(null))
+ }
+
+ @Test
+ fun `matches ""`() {
+ val expected: String? = null
+ val actual: String? = null
+ assertTrue(JsonMatcher("").matches(""))
+ }
+}