Migrate "ms/controllerblueprints" from ccsdk/apps
[ccsdk/cds.git] / ms / controllerblueprints / modules / 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: String = ("./../../../../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("Failed to validate Workflow(resource-assignment)'s step(test)'s definition : resource-assignment/steps/test : could't get node template for the name(TestCaseFailNoNodeTemplate)", bluePrintRuntime.getBluePrintError().errors[0])
64     }
65
66     @Test
67     fun testValidateWorkflowFailNodeTemplateNotDgGeneric() {
68         val workflowName = "resource-assignment"
69         val nodeTemplateName = "resource-assignment-process"
70
71         val nodeTemplate = mockk<NodeTemplate>()
72         every { nodeTemplate.type } returns "TestNodeType"
73
74         val nodeType = mockk<NodeType>()
75         every { nodeType.derivedFrom } returns "tosca.nodes.TEST"
76
77         val blueprintContext = mockk<BluePrintContext>()
78         every { blueprintContext.nodeTemplateByName(nodeTemplateName) } returns nodeTemplate
79         every { blueprintContext.nodeTemplateNodeType(nodeTemplateName) } returns nodeType
80
81         val bluePrintRuntime = mockk<DefaultBluePrintRuntimeService>("1234")
82
83         every { bluePrintRuntime.getBluePrintError() } returns BluePrintError()
84         every { bluePrintRuntime.bluePrintContext() } returns blueprintContext
85
86         val step = Step()
87         step.target = nodeTemplateName
88         val workflow = Workflow()
89         workflow.steps = mutableMapOf("test" to step)
90         workflowValidator.validate(bluePrintRuntime, workflowName, workflow)
91
92         assertEquals(1, bluePrintRuntime.getBluePrintError().errors.size)
93         assertEquals("Failed to validate Workflow(resource-assignment)'s step(test)'s definition : " +
94                 "resource-assignment/steps/test : NodeType(TestNodeType) derived from is 'tosca.nodes.TEST', " +
95                 "Expected 'tosca.nodes.DG' or 'tosca.nodes.Component'", bluePrintRuntime.getBluePrintError().errors[0])
96     }
97
98     @Test
99     fun testValidateWorkflowSuccess() {
100         val workflowName = "resource-assignment"
101         workflowValidator.validate(bluePrintRuntime, workflowName, bluePrintRuntime.bluePrintContext().workflowByName(workflowName))
102     }
103
104 }
105