Metadata for name, version, tags and type
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / blueprints / blueprint-validation / src / test / kotlin / org / onap / ccsdk / cds / controllerblueprints / validation / BluePrintDesignTimeValidatorServiceTest.kt
1 /*
2  * Copyright © 2017-2018 AT&T Intellectual Property.
3  * Modifications Copyright © 2019 IBM.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 package org.onap.ccsdk.cds.controllerblueprints.validation
19
20 import io.mockk.every
21 import io.mockk.mockk
22 import kotlinx.coroutines.runBlocking
23 import org.junit.Test
24 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintError
25 import org.onap.ccsdk.cds.controllerblueprints.core.data.NodeTemplate
26 import org.onap.ccsdk.cds.controllerblueprints.core.data.NodeType
27 import org.onap.ccsdk.cds.controllerblueprints.core.data.Step
28 import org.onap.ccsdk.cds.controllerblueprints.core.data.Workflow
29 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintContext
30 import org.onap.ccsdk.cds.controllerblueprints.core.service.DefaultBluePrintRuntimeService
31 import org.onap.ccsdk.cds.controllerblueprints.core.utils.BluePrintMetadataUtils
32 import org.onap.ccsdk.cds.controllerblueprints.validation.extension.ResourceDefinitionValidator
33 import kotlin.test.assertEquals
34 import kotlin.test.assertTrue
35
36 class BluePrintDesignTimeValidatorServiceTest {
37
38     private val blueprintBasePath = "./../../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
39     private val bluePrintRuntime = BluePrintMetadataUtils.bluePrintRuntime("1234", blueprintBasePath)
40     private val mockBluePrintTypeValidatorService = MockBluePrintTypeValidatorService()
41     private val resourceDefinitionValidator = mockk<ResourceDefinitionValidator>()
42     private val defaultBluePrintValidatorService =
43         BluePrintDesignTimeValidatorService(mockBluePrintTypeValidatorService, resourceDefinitionValidator)
44     private val workflowValidator = BluePrintWorkflowValidatorImpl(mockBluePrintTypeValidatorService)
45
46     @Test
47     fun testValidateOfType() {
48         runBlocking {
49             every { resourceDefinitionValidator.validate(bluePrintRuntime, any(), any()) } returns Unit
50
51             val valid = defaultBluePrintValidatorService.validateBluePrints(bluePrintRuntime)
52             assertTrue(valid, "failed in blueprint Validation")
53         }
54     }
55
56     @Test
57     fun testValidateWorkflowFailToFoundNodeTemplate() {
58         val workflowName = "resource-assignment"
59
60         val step = Step()
61         step.target = "TestCaseFailNoNodeTemplate"
62         val workflow = Workflow()
63         workflow.steps = mutableMapOf("test" to step)
64         workflowValidator.validate(bluePrintRuntime, workflowName, workflow)
65
66         assertEquals(1, bluePrintRuntime.getBluePrintError().errors.size)
67         assertEquals(
68             "Failed to validate Workflow(resource-assignment)'s step(test)'s definition : resource-assignment/steps/test : could't get node template for the name(TestCaseFailNoNodeTemplate)",
69             bluePrintRuntime.getBluePrintError().errors[0]
70         )
71     }
72
73     @Test
74     fun testValidateWorkflowFailNodeTemplateNotDgGeneric() {
75         val workflowName = "resource-assignment"
76         val nodeTemplateName = "resource-assignment-process"
77
78         val nodeTemplate = mockk<NodeTemplate>()
79         every { nodeTemplate.type } returns "TestNodeType"
80
81         val nodeType = mockk<NodeType>()
82         every { nodeType.derivedFrom } returns "tosca.nodes.TEST"
83
84         val blueprintContext = mockk<BluePrintContext>()
85         every { blueprintContext.nodeTemplateByName(nodeTemplateName) } returns nodeTemplate
86         every { blueprintContext.nodeTemplateNodeType(nodeTemplateName) } returns nodeType
87
88         val bluePrintRuntime = mockk<DefaultBluePrintRuntimeService>("1234")
89
90         every { bluePrintRuntime.getBluePrintError() } returns BluePrintError()
91         every { bluePrintRuntime.bluePrintContext() } returns blueprintContext
92
93         val step = Step()
94         step.target = nodeTemplateName
95         val workflow = Workflow()
96         workflow.steps = mutableMapOf("test" to step)
97         workflowValidator.validate(bluePrintRuntime, workflowName, workflow)
98
99         assertEquals(1, bluePrintRuntime.getBluePrintError().errors.size)
100         assertEquals(
101             "Failed to validate Workflow(resource-assignment)'s step(test)'s definition : " +
102                 "resource-assignment/steps/test : NodeType(TestNodeType) derived from is 'tosca.nodes.TEST', " +
103                 "Expected 'tosca.nodes.Workflow' or 'tosca.nodes.Component'",
104             bluePrintRuntime.getBluePrintError().errors[0]
105         )
106     }
107
108     @Test
109     fun testValidateWorkflowSuccess() {
110         val workflowName = "resource-assignment"
111         workflowValidator.validate(
112             bluePrintRuntime,
113             workflowName,
114             bluePrintRuntime.bluePrintContext().workflowByName(workflowName)
115         )
116     }
117 }