forked from griddynamics/OpenGenesis
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request griddynamics#874 from rsvato/databag-templates
[RJA-1953][feature] - Modify domain objects to support template links
- Loading branch information
Showing
17 changed files
with
205 additions
and
46 deletions.
There are no files selected for viewing
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
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
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
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
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
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
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
42 changes: 42 additions & 0 deletions
42
backend/src/main/scala/com/griddynamics/genesis/service/impl/TemplateValidator.scala
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,42 @@ | ||
package com.griddynamics.genesis.service.impl | ||
|
||
import com.griddynamics.genesis.model.{DatabagTemplate, TemplateProperty} | ||
import com.griddynamics.genesis.api.{Failure, TemplateBased, Success, ExtendedResult} | ||
import com.griddynamics.genesis.validation.ConfigValueValidator | ||
import com.griddynamics.genesis.repository.DatabagTemplateRepository | ||
|
||
trait TemplateValidator { | ||
def validations: Map[String, ConfigValueValidator] | ||
def templates: DatabagTemplateRepository | ||
|
||
private def validateValue(propName: String, value: String, defaults: Map[String, TemplateProperty]): ExtendedResult[Any] = { | ||
val results: Seq[ExtendedResult[Any]] = for {prop <- defaults.get(propName).toSeq | ||
(msg, validatorName) <- prop.getValidation | ||
validator = validations.get(validatorName)} yield | ||
validator.map(v => v.validate(propName, value, msg, Map("name" -> validatorName))).getOrElse(Success(propName)) | ||
if (results.isEmpty) | ||
Success(propName) | ||
else | ||
results.reduce(_ ++ _) | ||
} | ||
|
||
def validAccordingToTemplate(bag: TemplateBased): ExtendedResult[TemplateBased] = { | ||
bag.templateId.flatMap(templateId => { | ||
templates.get(templateId).map(template => { | ||
val requiredValidation = validateRequiredKeys(template, bag) | ||
val rulesValidation = bag.itemsMap.foldLeft(Success(bag).asInstanceOf[ExtendedResult[TemplateBased]])((acc, item) => acc ++ | ||
validateValue(item._1, item._2, template.values).flatMap(v => Success(bag))) | ||
requiredValidation ++ rulesValidation | ||
}) | ||
}).getOrElse(Success(bag)) | ||
} | ||
|
||
def validateRequiredKeys(template: DatabagTemplate, bag: TemplateBased): ExtendedResult[TemplateBased] = { | ||
val requiredKeys: Iterable[String] = template.values.filter({ | ||
case (_, value) => value.required | ||
}).keys.filterNot(name => bag.itemsMap.find({case (k,v) => k == name}).isDefined) | ||
val left = requiredKeys.foldLeft(Success(bag).asInstanceOf[ExtendedResult[TemplateBased]])((acc, item) => acc ++ | ||
Failure(serviceErrors = Map(item -> s"Required key $item not found in databag"))) | ||
left | ||
} | ||
} |
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
58 changes: 58 additions & 0 deletions
58
...st/scala/com/griddynamics/genesis/service/impl/DatabagValidationAgainstTemplateTest.scala
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,58 @@ | ||
package com.griddynamics.genesis.service.impl | ||
|
||
import org.scalatest.junit.AssertionsForJUnit | ||
import org.scalatest.mock.MockitoSugar | ||
import org.scalatest.matchers.ShouldMatchers | ||
import org.scalatest.FunSpec | ||
import com.griddynamics.genesis.repository.impl.DatabagTemplateRepositoryImpl | ||
import com.griddynamics.genesis.api.{Failure, DataItem, ExtendedResult, DataBag} | ||
import com.griddynamics.genesis.validation.IntValidator | ||
|
||
class DatabagValidationAgainstTemplateTest extends AssertionsForJUnit with MockitoSugar with ShouldMatchers with FunSpec with DSLTestUniverse { | ||
val repository = new DatabagTemplateRepositoryImpl("../resources/databags", "*.dbtemplate") | ||
val service = new DataBagServiceImpl(null, repository, Map("int_nonnegative" -> new IntValidator(0, Int.MaxValue))) | ||
val emptyDatabag = new DataBag(None, "", Seq(), None, Some("1")) | ||
|
||
val databagWithRequiredKey = emptyDatabag.copy(items = Seq( | ||
new DataItem(None, "other-long-key", "value", None) | ||
)) | ||
|
||
val databagWithErrorInValues = emptyDatabag.copy(items = Seq( | ||
new DataItem(None, "other-long-key", "value", None), | ||
new DataItem(None, "some-long-key", "aaa", None) | ||
)) | ||
|
||
val validDatabag = emptyDatabag.copy(items = Seq( | ||
new DataItem(None, "other-long-key", "value", None), | ||
new DataItem(None, "some-long-key", "123", None) | ||
)) | ||
|
||
describe("Databag validation against template") { | ||
it ("Should report missing keys") { | ||
val missingErrors = service.validateRequiredKeys(repository.get("1").get, emptyDatabag) | ||
missingErrors.isSuccess should equal(false) | ||
missingErrors.asInstanceOf[Failure].serviceErrors.get("other-long-key") should not be None | ||
} | ||
|
||
it ("Should not report missing keys when no required key is specified in template") { | ||
val missingErrors = service.validateRequiredKeys(repository.get("2").get, emptyDatabag) | ||
missingErrors.isSuccess should equal(true) | ||
} | ||
|
||
it ("Should not report missing keys when databag contains them") { | ||
val missingErrors = service.validateRequiredKeys(repository.get("1").get, databagWithRequiredKey) | ||
missingErrors.isSuccess should equal(true) | ||
} | ||
|
||
it ("Should report validation errors when databag value is invalid") { | ||
val missingErrors = service.validAccordingToTemplate(databagWithErrorInValues) | ||
missingErrors.isSuccess should equal(false) | ||
} | ||
|
||
it ("Should not report validation errors when databag value is valid") { | ||
val missingErrors = service.validAccordingToTemplate(databagWithErrorInValues) | ||
missingErrors.isSuccess should equal(false) | ||
} | ||
|
||
} | ||
} |
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
Oops, something went wrong.