19d1ef0a45b1de79ee200cf213947c8168f6284f
[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 org.junit.Test
23 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintError
24 import org.onap.ccsdk.cds.controllerblueprints.core.data.NodeTemplate
25 import org.onap.ccsdk.cds.controllerblueprints.core.data.NodeType
26 import org.onap.ccsdk.cds.controllerblueprints.core.data.Step
27 import org.onap.ccsdk.cds.controllerblueprints.core.data.Workflow
28 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintContext
29 import org.onap.ccsdk.cds.controllerblueprints.core.service.DefaultBluePrintRuntimeService
30 import org.onap.ccsdk.cds.controllerblueprints.core.utils.BluePrintMetadataUtils
31 import org.onap.ccsdk.cds.controllerblueprints.validation.extension.ResourceDefinitionValidator
32 import kotlin.test.assertEquals
33 import kotlin.test.assertTrue
34
35 class BluePrintDesignTimeValidatorServiceTest {
36
37     private val blueprintBasePath = "./../../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
38     private val bluePrintRuntime = BluePrintMetadataUtils.getBluePrintRuntime("1234", blueprintBasePath)
39     private val mockBluePrintTypeValidatorService = MockBluePrintTypeValidatorService()
40     private val resourceDefinitionValidator = mockk<ResourceDefinitionValidator>()
41     private val defaultBluePrintValidatorService = BluePrintDesignTimeValidatorService(mockBluePrintTypeValidatorService, resourceDefinitionValidator)
42     private val workflowValidator = BluePrintWorkflowValidatorImpl(mockBluePrintTypeValidatorService)
43
44     @Test
45     fun testValidateOfType() {
46         every { resourceDefinitionValidator.validate(bluePrintRuntime, any(), any()) } returns Unit
47
48         val valid = defaultBluePrintValidatorService.validateBluePrints(bluePrintRuntime)
49         assertTrue(valid, "failed in blueprint Validation")
50     }
51
52     @Test
53     fun testValidateWorkflowFailToFoundNodeTemplate() {
54         val workflowName = "resource-assignment"
55
56         val step = Step()
57         step.target = "TestCaseFailNoNodeTemplate"
58         val workflow = Workflow()
59         workflow.steps = mutableMapOf("test" to step)
60         workflowValidator.validate(bluePrintRuntime, workflowName, workflow)
61
62         assertEquals(1, bluePrintRuntime.getBluePrintError().errors.size)
63         assertEquals(
64             "Failed to validate Workflow(resource-assignment)'s step(test)'s definition : resource-assignment/steps/test : could't get node template for the name(TestCaseFailNoNodeTemplate)",
65             bluePrintRuntime.getBluePrintError().errors[0]
66         )
67     }
68
69     @Test
70     fun testValidateWorkflowFailNodeTemplateNotDgGeneric() {
71         val workflowName = "resource-assignment"
72         val nodeTemplateName = "resource-assignment-process"
73
74         val nodeTemplate = mockk<NodeTemplate>()
75         every { nodeTemplate.type } returns "TestNodeType"
76
77         val nodeType = mockk<NodeType>()
78         every { nodeType.derivedFrom } returns "tosca.nodes.TEST"
79
80         val blueprintContext = mockk<BluePrintContext>()
81         every { blueprintContext.nodeTemplateByName(nodeTemplateName) } returns nodeTemplate
82         every { blueprintContext.nodeTemplateNodeType(nodeTemplateName) } returns nodeType
83
84         val bluePrintRuntime = mockk<DefaultBluePrintRuntimeService>("1234")
85
86         every { bluePrintRuntime.getBluePrintError() } returns BluePrintError()
87         every { bluePrintRuntime.bluePrintContext() } returns blueprintContext
88
89         val step = Step()
90         step.target = nodeTemplateName
91         val workflow = Workflow()
92         workflow.steps = mutableMapOf("test" to step)
93         workflowValidator.validate(bluePrintRuntime, workflowName, workflow)
94
95         assertEquals(1, bluePrintRuntime.getBluePrintError().errors.size)
96         assertEquals(
97             "Failed to validate Workflow(resource-assignment)'s step(test)'s definition : " +
98                     "resource-assignment/steps/test : NodeType(TestNodeType) derived from is 'tosca.nodes.TEST', " +
99                     "Expected 'tosca.nodes.Workflow' or 'tosca.nodes.Component'", bluePrintRuntime.getBluePrintError().errors[0]
100         )
101     }
102
103     @Test
104     fun testValidateWorkflowSuccess() {
105         val workflowName = "resource-assignment"
106         workflowValidator.validate(bluePrintRuntime, workflowName, bluePrintRuntime.bluePrintContext().workflowByName(workflowName))
107     }
108 }