diff --git a/obp-api/src/main/scala/code/dynamicEntity/DynamicEntityProvider.scala b/obp-api/src/main/scala/code/dynamicEntity/DynamicEntityProvider.scala index 2d53454017..9f7acaa662 100644 --- a/obp-api/src/main/scala/code/dynamicEntity/DynamicEntityProvider.scala +++ b/obp-api/src/main/scala/code/dynamicEntity/DynamicEntityProvider.scala @@ -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 @@ -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 diff --git a/obp-api/src/test/scala/code/api/v4_0_0/DynamicEntityTest.scala b/obp-api/src/test/scala/code/api/v4_0_0/DynamicEntityTest.scala index b0e32ac8e9..410f130ec8 100644 --- a/obp-api/src/test/scala/code/api/v4_0_0/DynamicEntityTest.scala +++ b/obp-api/src/test/scala/code/api/v4_0_0/DynamicEntityTest.scala @@ -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) @@ -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)