diff options
author | Brinda Santh <brindasanth@in.ibm.com> | 2019-05-17 10:07:14 -0400 |
---|---|---|
committer | Steve Siani <alphonse.steve.siani.djissitchi@ibm.com> | 2019-05-23 00:15:54 -0400 |
commit | bcc6c4dfe9db06e0dab22472b11a4939255091e5 (patch) | |
tree | 62a4bbcf1ec53d9cbc165bcf7dfc9a04211f67de /ms/controllerblueprints/modules/blueprint-core | |
parent | 145662ff6a41e0e46a23e8eedf53f30581f8ab64 (diff) |
Add cli test blueprints
Change-Id: Ieab385f5e4ae60cca3d86f22c4304e4867e6fa96
Issue-ID: CCSDK-1335
Signed-off-by: Brinda Santh <brindasanth@in.ibm.com>
Diffstat (limited to 'ms/controllerblueprints/modules/blueprint-core')
7 files changed, 98 insertions, 54 deletions
diff --git a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/CustomFunctions.kt b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/CustomFunctions.kt index 0e45232f2..583fc9d4d 100644 --- a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/CustomFunctions.kt +++ b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/CustomFunctions.kt @@ -122,6 +122,18 @@ fun JsonNode.rootFieldsToMap(): MutableMap<String, JsonNode> { } } +fun JsonNode.removeNullNode() { + val it = this.iterator() + while (it.hasNext()) { + val child = it.next() + if (child.isNull) { + it.remove() + } else { + child.removeNullNode() + } + } +} + fun MutableMap<String, JsonNode>.putJsonElement(key: String, value: Any) { val convertedValue = value.asJsonType() diff --git a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/interfaces/BlueprintTemplateService.kt b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/interfaces/BlueprintTemplateService.kt index ec1542cd3..86bf3ff56 100644 --- a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/interfaces/BlueprintTemplateService.kt +++ b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/interfaces/BlueprintTemplateService.kt @@ -18,21 +18,43 @@ package org.onap.ccsdk.cds.controllerblueprints.core.interfaces import com.fasterxml.jackson.core.io.CharTypes import com.fasterxml.jackson.databind.node.JsonNodeFactory import com.fasterxml.jackson.databind.node.TextNode +import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintRuntimeService interface BlueprintTemplateService { /** * Generate dynamique content using Velocity Template or Jinja template * + * @param bluePrintRuntimeService blueprint runtime + * @param nodeTemplateName node template + * @param artifactName Artifact Name + * @param jsonData json string data content to mash + * @param ignoreJsonNull Ignore Null value in the JSON content + * @param additionalContext (Key, value) mutable map for additional variables + * @return Content result + * + **/ + suspend fun generateContent(bluePrintRuntimeService: BluePrintRuntimeService<*>, + nodeTemplateName: String, + artifactName: String, + jsonData: String = "", + ignoreJsonNull: Boolean = false, + additionalContext: MutableMap<String, Any> = mutableMapOf()): String + + + /** + * Generate dynamique content using Velocity Template or Jinja template + * * @param template template string content - * @param json json string content + * @param templateType template type + * @param jsonData json string data content to mash * @param ignoreJsonNull Ignore Null value in the JSON content * @param additionalContext (Key, value) mutable map for additional variables * @return Content result * **/ - suspend fun generateContent(template: String, json: String = "", ignoreJsonNull: Boolean = false, - additionalContext: MutableMap<String, Any> = mutableMapOf()): String + suspend fun generateContent(template: String, templateType: String, jsonData: String = "", ignoreJsonNull: Boolean = false, + additionalContext: MutableMap<String, Any> = mutableMapOf()): String } /** diff --git a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/service/BluePrintJinjaTemplateService.kt b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/service/BluePrintJinjaTemplateService.kt index f835cbe52..912667e0a 100644 --- a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/service/BluePrintJinjaTemplateService.kt +++ b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/service/BluePrintJinjaTemplateService.kt @@ -21,14 +21,13 @@ import com.fasterxml.jackson.databind.ObjectMapper import com.hubspot.jinjava.Jinjava import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintJsonNodeFactory -import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BlueprintTemplateService -import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils +import org.onap.ccsdk.cds.controllerblueprints.core.removeNullNode -object BluePrintJinjaTemplateService: BlueprintTemplateService { +object BluePrintJinjaTemplateService { - override suspend fun generateContent(template: String, json: String, ignoreJsonNull: Boolean, - additionalContext: MutableMap<String, Any>): String { + fun generateContent(template: String, json: String, ignoreJsonNull: Boolean, + additionalContext: MutableMap<String, Any>): String { // Load template val jinJava = Jinjava() val mapper = ObjectMapper() @@ -40,7 +39,7 @@ object BluePrintJinjaTemplateService: BlueprintTemplateService { val jsonNode = mapper.readValue<JsonNode>(json, JsonNode::class.java) ?: throw BluePrintProcessorException("couldn't get json node from json") if (ignoreJsonNull) - JacksonUtils.removeJsonNullNode(jsonNode) + jsonNode.removeNullNode() jsonNode.fields().forEach { entry -> additionalContext[entry.key] = entry.value } diff --git a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/service/BluePrintVelocityTemplateService.kt b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/service/BluePrintVelocityTemplateService.kt index 4b6905bff..496182ee7 100644 --- a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/service/BluePrintVelocityTemplateService.kt +++ b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/service/BluePrintVelocityTemplateService.kt @@ -26,35 +26,50 @@ import org.apache.velocity.VelocityContext import org.apache.velocity.app.Velocity import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintJsonNodeFactory -import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BlueprintTemplateService -import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils +import org.onap.ccsdk.cds.controllerblueprints.core.removeNullNode import java.io.StringWriter -object BluePrintVelocityTemplateService: BlueprintTemplateService { +object BluePrintVelocityTemplateService { /** - * Generate Content from Velocity Template and JSON Content or property map. + * Generate Content from Velocity Template and JSON Content with injected API */ - override suspend fun generateContent(template: String, json: String, ignoreJsonNull: Boolean, additionalContext: - MutableMap<String, Any>): String { - Velocity.init() + fun generateContent(template: String, json: String, ignoreJsonNull: Boolean = false, + additionalContext: MutableMap<String, Any> = mutableMapOf()): String { + + // Customized Object Mapper to remove String double quotes val mapper = ObjectMapper() val nodeFactory = BluePrintJsonNodeFactory() mapper.nodeFactory = nodeFactory + val jsonNode: JsonNode? = if (json.isNotEmpty()) { + mapper.readValue(json, JsonNode::class.java) + ?: throw BluePrintProcessorException("couldn't get json node from json") + } else { + null + } + return generateContent(template, jsonNode, ignoreJsonNull, additionalContext) + } + + /** + * Generate Content from Velocity Template and JSON Node with injected API + */ + fun generateContent(template: String, jsonNode: JsonNode?, ignoreJsonNull: Boolean = false, + additionalContext: MutableMap<String, Any> = mutableMapOf()): String { + + Velocity.init() + val velocityContext = VelocityContext() velocityContext.put("StringUtils", StringUtils::class.java) velocityContext.put("BooleanUtils", BooleanUtils::class.java) // Add the Custom Velocity Context API - additionalContext.forEach { name, value -> velocityContext.put(name, value) } + additionalContext.forEach { (name, value) -> velocityContext.put(name, value) } // Add the JSON Data to the context - if (json.isNotEmpty()) { - val jsonNode = mapper.readValue<JsonNode>(json, JsonNode::class.java) - ?: throw BluePrintProcessorException("couldn't get json node from json") + if (jsonNode != null) { if (ignoreJsonNull) - JacksonUtils.removeJsonNullNode(jsonNode) + jsonNode.removeNullNode() jsonNode.fields().forEach { entry -> velocityContext.put(entry.key, entry.value) } @@ -64,6 +79,7 @@ object BluePrintVelocityTemplateService: BlueprintTemplateService { Velocity.evaluate(velocityContext, stringWriter, "TemplateData", template) stringWriter.flush() return stringWriter.toString() + } } diff --git a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/service/BlueprintTemplateService.kt b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/service/BlueprintTemplateService.kt index 73457b986..45e2678ed 100644 --- a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/service/BlueprintTemplateService.kt +++ b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/service/BlueprintTemplateService.kt @@ -20,19 +20,26 @@ import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BlueprintTemplateService import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils -class BluePrintTemplateService(val bluePrintRuntimeService: BluePrintRuntimeService<*>, - val nodeTemplateName: String, val artifactName: String): - BlueprintTemplateService { +class BluePrintTemplateService : BlueprintTemplateService { + + override suspend fun generateContent(bluePrintRuntimeService: BluePrintRuntimeService<*>, + nodeTemplateName: String, artifactName: String, jsonData: String, + ignoreJsonNull: Boolean, additionalContext: MutableMap<String, Any>): String { - override suspend fun generateContent(template: String, json: String, ignoreJsonNull: Boolean, additionalContext: MutableMap<String, Any>): String { val artifactDefinition = bluePrintRuntimeService.resolveNodeTemplateArtifactDefinition(nodeTemplateName, artifactName) val templateType = artifactDefinition.type + val template = bluePrintRuntimeService.resolveNodeTemplateArtifact(nodeTemplateName, artifactName) + return generateContent(template, templateType, jsonData, ignoreJsonNull, additionalContext) + } + + override suspend fun generateContent(template: String, templateType: String, jsonData: String, ignoreJsonNull: Boolean, + additionalContext: MutableMap<String, Any>): String { return when (templateType) { BluePrintConstants.ARTIFACT_JINJA_TYPE_NAME -> { - BluePrintJinjaTemplateService.generateContent(template, json, ignoreJsonNull, additionalContext) + BluePrintJinjaTemplateService.generateContent(template, jsonData, ignoreJsonNull, additionalContext) } BluePrintConstants.ARTIFACT_VELOCITY_TYPE_NAME -> { - BluePrintVelocityTemplateService.generateContent(template, json, ignoreJsonNull, additionalContext) + BluePrintVelocityTemplateService.generateContent(template, jsonData, ignoreJsonNull, additionalContext) } else -> { throw BluePrintProcessorException("Unknown Artifact type, expecting ${BluePrintConstants.ARTIFACT_JINJA_TYPE_NAME}" + @@ -41,9 +48,11 @@ class BluePrintTemplateService(val bluePrintRuntimeService: BluePrintRuntimeServ } } - suspend fun generateContentFromFiles(templatePath: String, jsonPath: String, ignoreJsonNull: Boolean, additionalContext: MutableMap<String, Any>): String { + suspend fun generateContentFromFiles(templatePath: String, templateType: String, jsonPath: String, + ignoreJsonNull: Boolean, + additionalContext: MutableMap<String, Any>): String { val json = JacksonUtils.getClassPathFileContent(jsonPath) val template = JacksonUtils.getClassPathFileContent(templatePath) - return generateContent(template, json, ignoreJsonNull, additionalContext) + return generateContent(template, templateType, json, ignoreJsonNull, additionalContext) } }
\ No newline at end of file diff --git a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/utils/JacksonUtils.kt b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/utils/JacksonUtils.kt index 7b5f181da..5e9fd6207 100644 --- a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/utils/JacksonUtils.kt +++ b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/utils/JacksonUtils.kt @@ -58,19 +58,6 @@ class JacksonUtils { return objectMapper.treeToValue(node, valueType) } - fun removeJsonNullNode(node: JsonNode) { - val it = node.iterator() - while (it.hasNext()) { - val child = it.next() - if (child.isNull) { - it.remove() - } else { - removeJsonNullNode(child) - } - } - } - - fun getContent(fileName: String): String = runBlocking { try { normalizedFile(fileName).readNBText() diff --git a/ms/controllerblueprints/modules/blueprint-core/src/test/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/service/BluePrintTemplateServiceTest.kt b/ms/controllerblueprints/modules/blueprint-core/src/test/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/service/BluePrintTemplateServiceTest.kt index e4227180b..02505acad 100644 --- a/ms/controllerblueprints/modules/blueprint-core/src/test/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/service/BluePrintTemplateServiceTest.kt +++ b/ms/controllerblueprints/modules/blueprint-core/src/test/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/service/BluePrintTemplateServiceTest.kt @@ -21,6 +21,7 @@ package org.onap.ccsdk.cds.controllerblueprints.core.service import kotlinx.coroutines.runBlocking import org.junit.Test import org.junit.runner.RunWith +import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants import org.onap.ccsdk.cds.controllerblueprints.core.utils.BluePrintMetadataUtils import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils import org.springframework.test.context.junit4.SpringRunner @@ -40,13 +41,13 @@ class BluePrintTemplateServiceTest { @Test fun testVelocityGeneratedContent() { - runBlocking { - val template = JacksonUtils.getClassPathFileContent("templates/base-config-velocity-template.vtl") - val json = JacksonUtils.getClassPathFileContent("templates/base-config-data-velocity.json") + runBlocking { + val template = JacksonUtils.getClassPathFileContent("templates/base-config-velocity-template.vtl") + val json = JacksonUtils.getClassPathFileContent("templates/base-config-data-velocity.json") - val content = BluePrintVelocityTemplateService.generateContent(template, json) - assertNotNull(content, "failed to generate content for velocity template") - } + val content = BluePrintVelocityTemplateService.generateContent(template, json) + assertNotNull(content, "failed to generate content for velocity template") + } } @@ -68,13 +69,12 @@ class BluePrintTemplateServiceTest { @Test fun testVelocityGeneratedContentFromFiles() { runBlocking { - val bluePrintTemplateService = BluePrintTemplateService(blueprintRuntime, - "resource-assignment", "baseconfig-template") + val bluePrintTemplateService = BluePrintTemplateService() val templateFile = "templates/base-config-velocity-template.vtl" val jsonFile = "templates/base-config-data-velocity.json" val content = bluePrintTemplateService.generateContentFromFiles( - templateFile, jsonFile, false, mutableMapOf()) + templateFile, BluePrintConstants.ARTIFACT_VELOCITY_TYPE_NAME, jsonFile, false, mutableMapOf()) assertNotNull(content, "failed to generate content for velocity template") } @@ -86,14 +86,13 @@ class BluePrintTemplateServiceTest { var element: MutableMap<String, Any> = mutableMapOf() element["additional_array"] = arrayListOf(hashMapOf("name" to "Element1", "location" to "Region0"), hashMapOf("name" to "Element2", "location" to "Region1")) - val bluePrintTemplateService = BluePrintTemplateService(blueprintRuntime, - "resource-assignment", "another-template") + val bluePrintTemplateService = BluePrintTemplateService() val templateFile = "templates/base-config-jinja-template.jinja" val jsonFile = "templates/base-config-data-jinja.json" val content = bluePrintTemplateService.generateContentFromFiles( - templateFile, + templateFile, BluePrintConstants.ARTIFACT_JINJA_TYPE_NAME, jsonFile, false, element) assertNotNull(content, "failed to generate content for velocity template") } |