Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pivotal ID # 176052254: JSON Submission Attributes Validation #310

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ inline fun <reified T : JsonNode?> JsonNode.ensureType(node: JsonNode, property:
require(node is T) {
val type = node::class.java.simpleName
val expectedType = T::class.java.simpleName
"Expecting node: '$this', property: '$property' to be of type '$expectedType' but '$type' find instead"
"Expecting node: '$this', property: '$property' to be of type '$expectedType' but '$type' was found instead"
}

return node
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import ac.uk.ebi.biostd.common.NAME_ATTRIBUTES
import ac.uk.ebi.biostd.common.REFERENCE
import ac.uk.ebi.biostd.common.VALUE
import ac.uk.ebi.biostd.common.VAL_ATTRIBUTES
import ac.uk.ebi.biostd.json.exception.NoAttributeValueException
import com.fasterxml.jackson.core.JsonParser
import com.fasterxml.jackson.databind.DeserializationContext
import com.fasterxml.jackson.databind.JsonNode
Expand All @@ -20,10 +21,13 @@ internal class AttributeJsonDeserializer : StdDeserializer<Attribute>(Attribute:
override fun deserialize(jp: JsonParser, ctxt: DeserializationContext): Attribute {
val mapper = jp.codec as ObjectMapper
val node: JsonNode = mapper.readTree(jp)
val name = node.getNode<TextNode>(NAME).textValue()
val value = node.getNode<TextNode>(VALUE).textValue()
require(value.isNotBlank()) { throw NoAttributeValueException(name) }

return Attribute(
name = node.getNode<TextNode>(NAME).textValue(),
value = node.getNode<TextNode>(VALUE).textValue(),
name = name,
value = value,
reference = node.get(REFERENCE)?.asBoolean().orFalse(),
valueAttrs = mapper.convertList(node.get(VAL_ATTRIBUTES)),
nameAttrs = mapper.convertList(node.get(NAME_ATTRIBUTES)))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package ac.uk.ebi.biostd.json.exception

class NoAttributeValueException(name: String) : RuntimeException("The value for the attribute '$name' can't be empty")
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ac.uk.ebi.biostd.json.deserialization

import ac.uk.ebi.biostd.json.JsonSerializer
import ac.uk.ebi.biostd.json.exception.NoAttributeValueException
import ebi.ac.uk.dsl.json.jsonArray
import ebi.ac.uk.dsl.json.jsonObj
import ebi.ac.uk.model.Attribute
Expand All @@ -14,21 +15,33 @@ class AttributeDeserializerTest {
private val testInstance = JsonSerializer.mapper

@Test
fun `deserialize no value`() {
fun `deserialize empty`() {
val exception = assertThrows<IllegalStateException> { testInstance.deserialize<Attribute>("{}") }

assertThat(exception.message).isEqualTo("Expecting to find property with 'name' in node '{}'")
}

@Test
fun `deserialize no value`() {
val exception = assertThrows<NoAttributeValueException> { testInstance.deserialize<Attribute>("""{
|"name": "attr name",
|"value": ""
|}""".trimMargin()) }

assertThat(exception.message).isEqualTo("The value for the attribute 'attr name' can't be empty")
}

@Test
fun `deserialize with wrong type`() {
val invalidJson = jsonObj {
"name" to jsonArray(1, 2, 3)
}.toString()

val node = "{\"name\":[1,2,3]}"
val exception = assertThrows<IllegalArgumentException> { testInstance.deserialize<Attribute>(invalidJson) }

assertThat(exception.message).isEqualTo(
"Expecting node: '{\"name\":[1,2,3]}', property: 'name' to be of type 'TextNode' but 'ArrayNode' find instead")
"Expecting node: '$node', property: 'name' to be of type 'TextNode' but 'ArrayNode' was found instead")
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ class FileDeserializerTest {
"path" to jsonArray(1, 2, 3)
}.toString()

val node = "{\"path\":[1,2,3]}"
val exception = assertThrows<IllegalArgumentException> { testInstance.deserialize<File>(invalidJson) }
assertThat(exception.message).isEqualTo(
"Expecting node: '{\"path\":[1,2,3]}', property: 'path' to be of type 'TextNode' but 'ArrayNode' find instead")
"Expecting node: '$node', property: 'path' to be of type 'TextNode' but 'ArrayNode' was found instead")
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ class LinkDeserializerTest {
"url" to jsonArray(1, 2, 3)
}.toString()

val node = "{\"url\":[1,2,3]}"
val exception = assertThrows<IllegalArgumentException> { testInstance.deserialize<Link>(invalidJson) }
assertThat(exception.message).isEqualTo(
"Expecting node: '{\"url\":[1,2,3]}', property: 'url' to be of type 'TextNode' but 'ArrayNode' find instead")
"Expecting node: '$node', property: 'url' to be of type 'TextNode' but 'ArrayNode' was found instead")
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ class SectionDeserializerTest {
"accno" to jsonArray(1, 2, 3)
}.toString()

val node = "{\"accno\":[1,2,3]}"
val exception = assertThrows<IllegalArgumentException> { testInstance.deserialize<Section>(invalidJson) }
assertThat(exception.message).isEqualTo(
"Expecting node: '{\"accno\":[1,2,3]}', property: 'accno' to be of type 'TextNode' but 'ArrayNode' find instead")
"Expecting node: '$node', property: 'accno' to be of type 'TextNode' but 'ArrayNode' was found instead")
}

@Test
Expand Down