Skip to content
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 @@ -409,11 +409,20 @@ object DynamicEntityCommons extends Converter[DynamicEntityT, DynamicEntityCommo

val fields = jsonObject.obj

// validate whether json is object and have a single field, currently support one entity definition
checkFormat(fields.nonEmpty, s"$DynamicEntityInstanceValidateFail The Json root object should have a single entity, but current have none.")
checkFormat(fields.size <= 2, s"$DynamicEntityInstanceValidateFail The Json root object should at most two fields: entity and hasPersonalEntity, but current entityNames: ${fields.map(_.name).mkString(", ")}")
// validate root object fields: allowed sizes are 1 or 2, order agnostic
val fieldsSize = fields.size
// Check whether the hasPersonalEntity field exists in the root object (does not check its value)
val hasPersonalEntityField = fields.exists(_.name == "hasPersonalEntity")
// Determine the value of hasPersonalEntity; use the field's boolean value if provided, otherwise default to true
val hasPersonalEntityValue: Boolean = fields.filter(_.name == "hasPersonalEntity").map(_.value.asInstanceOf[JBool].values).headOption.getOrElse(true)

val hasPersonalEntity: Boolean = fields.filter(_.name=="hasPersonalEntity").map(_.value.asInstanceOf[JBool].values).headOption.getOrElse(true)
checkFormat(fields.nonEmpty, s"$DynamicEntityInstanceValidateFail The Json root object should have a single entity, but current have none.")
checkFormat(fieldsSize <= 2, s"$DynamicEntityInstanceValidateFail The Json root object should have at most two fields: entity and hasPersonalEntity, but current root objects: ${fields.map(_.name).mkString(", ")}")
checkFormat(
(fieldsSize == 1 && fields(0).name != "hasPersonalEntity") ||
(fieldsSize == 2 && hasPersonalEntityField),
s"$DynamicEntityInstanceValidateFail The Json root object should contain one entity or two fields: the entity and hasPersonalEntity, in any order. Current root objects: ${fields.map(_.name).mkString(", ")}"
)

val JField(entityName, metadataJson) = fields.filter(_.name!="hasPersonalEntity").head

Expand Down Expand Up @@ -520,7 +529,7 @@ object DynamicEntityCommons extends Converter[DynamicEntityT, DynamicEntityCommo
}
})

DynamicEntityCommons(entityName, compactRender(jsonObject), dynamicEntityId, userId, bankId, hasPersonalEntity)
DynamicEntityCommons(entityName, compactRender(jsonObject), dynamicEntityId, userId, bankId, hasPersonalEntityValue)
}

private def allowedFieldType: List[String] = DynamicEntityFieldType.values.map(_.toString) ++: ReferenceType.referenceTypeNames
Expand Down
79 changes: 79 additions & 0 deletions obp-api/src/test/scala/code/api/v4_0_0/DynamicEntityTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,59 @@ class DynamicEntityTest extends V400ServerSetup {
|}
|""".stripMargin)

val wrongRootEntity = parse(
"""
|{
|"EntityName": "Dog",
| "FooBar": {
| "description": "description of this entity, can be markdown text.",
| "required": [
| "name"
| ],
| "properties": {
| "name": {
| "type": "string",
| "maxLength": 20,
| "minLength": 3,
| "example": "James Brown",
| "description":"description of **name** field, can be markdown text."
| },
| "number": {
| "type": "integer",
| "example": 69876172
| }
| }
| }
|
|}
|""".stripMargin)

val wrongRootEntity2 = parse(
"""
|{
| "FooBar": {
| "description": "description of this entity, can be markdown text.",
| "required": [
| "name"
| ],
| "properties": {
| "name": {
| "type": "string",
| "maxLength": 20,
| "minLength": 3,
| "example": "James Brown",
| "description":"description of **name** field, can be markdown text."
| },
| "number": {
| "type": "integer",
| "example": 69876172
| }
| }
| },
| "EntityName": "Dog",
|}
|""".stripMargin)

val foobarObject = parse("""{ "name":"James Brown", "number":698761728}""".stripMargin)

val foobarUpdateObject = parse("""{ "name":"James Brown123", "number":698761728}""".stripMargin)
Expand Down Expand Up @@ -264,6 +317,32 @@ class DynamicEntityTest extends V400ServerSetup {
val errorMessage = response400User2.body.extract[ErrorMessage].message
errorMessage contains DynamicEntityNameAlreadyExists should be (true)
}

scenario("Create Dynamic - the request json root can only contains two objects: entity and hasPersonalEntity ", ApiEndpoint1, VersionOfApi) {
When("We make a request v4.0.0")
Entitlement.entitlement.vend.addEntitlement("", resourceUser2.userId, CanCreateSystemLevelDynamicEntity.toString)

val request400User2 = (v4_0_0_Request / "management" / "system-dynamic-entities").POST <@(user2)
val response400User2 = makePostRequest(request400User2, write(wrongRootEntity))
Then("We should get a 400")
response400User2.code should equal(400)
val errorMessage = response400User2.body.extract[ErrorMessage].message
errorMessage contains DynamicEntityInstanceValidateFail should be (true)
errorMessage contains "The Json root object should contain one entity or two fields: the entity and hasPersonalEntity, in any order. Current root objects:" should be (true)
}

scenario("Create Dynamic - the request json root can only contains two objects: entity and hasPersonalEntity, test2 ", ApiEndpoint1, VersionOfApi) {
When("We make a request v4.0.0")
Entitlement.entitlement.vend.addEntitlement("", resourceUser2.userId, CanCreateSystemLevelDynamicEntity.toString)

val request400User2 = (v4_0_0_Request / "management" / "system-dynamic-entities").POST <@(user2)
val response400User2 = makePostRequest(request400User2, write(wrongRootEntity2))
Then("We should get a 400")
response400User2.code should equal(400)
val errorMessage = response400User2.body.extract[ErrorMessage].message
errorMessage contains DynamicEntityInstanceValidateFail should be (true)
errorMessage contains "The Json root object should contain one entity or two fields: the entity and hasPersonalEntity, in any order. Current root objects:" should be (true)
}

scenario("We will test the successful cases " , ApiEndpoint1, ApiEndpoint2, ApiEndpoint3, ApiEndpoint4, VersionOfApi) {
Entitlement.entitlement.vend.addEntitlement("", resourceUser1.userId, CanCreateSystemLevelDynamicEntity.toString)
Expand Down