This repository has been archived by the owner on May 16, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
139 additions
and
0 deletions.
There are no files selected for viewing
69 changes: 69 additions & 0 deletions
69
src/test/kotlin/com/github/pgutkowski/kgraphql/merge/MapMergeTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package com.github.pgutkowski.kgraphql.merge | ||
|
||
import com.fasterxml.jackson.databind.JsonNode | ||
import com.fasterxml.jackson.databind.node.JsonNodeFactory | ||
import com.github.pgutkowski.kgraphql.expect | ||
import com.github.pgutkowski.kgraphql.schema.execution.merge | ||
import org.hamcrest.CoreMatchers.equalTo | ||
import org.hamcrest.MatcherAssert.assertThat | ||
import org.junit.Test | ||
|
||
class MapMergeTest { | ||
private val jsonNodeFactory = JsonNodeFactory.instance | ||
|
||
@Test | ||
fun `merge should add property`() { | ||
val existing = createMap("param1" to jsonNodeFactory.textNode("value1")) | ||
val update: JsonNode? = jsonNodeFactory.textNode("value2") | ||
|
||
existing.merge("param2", update) | ||
|
||
assertThat(existing.get("param2"), equalTo(update)) | ||
} | ||
|
||
@Test | ||
fun `merge should add nested property`() { | ||
val existing = createMap("param1" to jsonNodeFactory.textNode("value1")) | ||
val update: JsonNode? = jsonNodeFactory.objectNode().put("param2", "value2") | ||
|
||
existing.merge("sub", update) | ||
|
||
assertThat(existing.get("sub"), equalTo(update)) | ||
} | ||
|
||
@Test | ||
fun `merge should not change simple node`() { | ||
val existingValue: JsonNode? = jsonNodeFactory.textNode("value1") | ||
val existing = createMap("param" to existingValue) | ||
val update = jsonNodeFactory.textNode("value2") | ||
|
||
expect<IllegalStateException>("different simple nodes") { existing.merge("param", update) } | ||
|
||
assertThat(existing.get("param"), equalTo(existingValue)) | ||
} | ||
|
||
@Test | ||
fun `merge should not merge simple node with object node`() { | ||
val existingValue: JsonNode? = jsonNodeFactory.textNode("value1") | ||
val existing = createMap("param" to existingValue) | ||
val update = jsonNodeFactory.objectNode() | ||
|
||
expect<IllegalStateException>("merge object with simple node") { existing.merge("param", update) } | ||
|
||
val expected: JsonNode? = jsonNodeFactory.textNode("value1") | ||
assertThat(existing.get("param"), equalTo(expected)) | ||
} | ||
|
||
@Test | ||
fun `merge should not merge object node with simple node`() { | ||
val existingObj: JsonNode? = jsonNodeFactory.objectNode().put("other", "value1") | ||
val existing = createMap("param" to existingObj) | ||
val update = jsonNodeFactory.textNode("value2") | ||
|
||
expect<IllegalStateException>("merge simple node with object node") { existing.merge("param", update) } | ||
|
||
assertThat(existing.get("param"), equalTo(existingObj)) | ||
} | ||
|
||
private fun createMap(vararg pairs: Pair<String, JsonNode?>) = mutableMapOf(*pairs) | ||
} |
70 changes: 70 additions & 0 deletions
70
src/test/kotlin/com/github/pgutkowski/kgraphql/merge/ObjectNodeMergeTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package com.github.pgutkowski.kgraphql.merge | ||
|
||
import com.fasterxml.jackson.databind.JsonNode | ||
import com.fasterxml.jackson.databind.node.JsonNodeFactory | ||
import com.github.pgutkowski.kgraphql.expect | ||
import com.github.pgutkowski.kgraphql.schema.execution.merge | ||
import org.hamcrest.CoreMatchers.equalTo | ||
import org.hamcrest.MatcherAssert.assertThat | ||
import org.junit.Test | ||
|
||
class ObjectNodeMergeTest { | ||
private val jsonNodeFactory = JsonNodeFactory.instance | ||
|
||
@Test | ||
fun `merge should add property`() { | ||
val existing = jsonNodeFactory.objectNode().put("param1", "value1") | ||
val update = jsonNodeFactory.objectNode().put("param2", "value2") | ||
|
||
existing.merge(update) | ||
|
||
val expected: JsonNode? = jsonNodeFactory.textNode("value2") | ||
assertThat(existing.get("param2"), equalTo(expected)) | ||
} | ||
|
||
@Test | ||
fun `merge should add nested property`() { | ||
val existing = jsonNodeFactory.objectNode().put("param1", "value1") | ||
val update = jsonNodeFactory.objectNode() | ||
update.putObject("sub").put("param2", "value2") | ||
|
||
existing.merge(update) | ||
|
||
val expected: JsonNode? = jsonNodeFactory.objectNode().put("param2", "value2") | ||
assertThat(existing.get("sub"), equalTo(expected)) | ||
} | ||
|
||
@Test | ||
fun `merge should not change simple node`() { | ||
val existing = jsonNodeFactory.objectNode().put("param", "value1") | ||
val update = jsonNodeFactory.objectNode().put("param", "value2") | ||
|
||
expect<IllegalStateException>("different simple nodes") { existing.merge(update) } | ||
|
||
val expected: JsonNode? = jsonNodeFactory.textNode("value1") | ||
assertThat(existing.get("param"), equalTo(expected)) | ||
} | ||
|
||
@Test | ||
fun `merge should not merge simple node with object node`() { | ||
val existing = jsonNodeFactory.objectNode().put("param", "value1") | ||
val update = jsonNodeFactory.objectNode() | ||
update.putObject("param") | ||
|
||
expect<IllegalStateException>("merge object with simple node") { existing.merge(update) } | ||
|
||
val expected: JsonNode? = jsonNodeFactory.textNode("value1") | ||
assertThat(existing.get("param"), equalTo(expected)) | ||
} | ||
|
||
@Test | ||
fun `merge should not merge object node with simple node`() { | ||
val existing = jsonNodeFactory.objectNode() | ||
val existingObj: JsonNode? = existing.putObject("param").put("other", "value1") | ||
val update = jsonNodeFactory.objectNode().put("param", "value2") | ||
|
||
expect<IllegalStateException>("merge simple node with object node") { existing.merge(update) } | ||
|
||
assertThat(existing.get("param"), equalTo(existingObj)) | ||
} | ||
} |