cfcbd9b2f7d7515b83ab176b7b35b243d820d0b9
[ccsdk/cds.git] /
1 /*
2  * Copyright © 2017-2018 AT&T Intellectual Property.
3  * Modifications Copyright © 2018 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.apps.controllerblueprints.core.validation
19
20 import io.mockk.every
21 import io.mockk.mockk
22 import org.junit.Test
23 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintError
24 import org.onap.ccsdk.apps.controllerblueprints.core.data.NodeTemplate
25 import org.onap.ccsdk.apps.controllerblueprints.core.data.NodeType
26 import org.onap.ccsdk.apps.controllerblueprints.core.data.Step
27 import org.onap.ccsdk.apps.controllerblueprints.core.data.Workflow
28 import org.onap.ccsdk.apps.controllerblueprints.core.mock.MockBluePrintTypeValidatorService
29 import org.onap.ccsdk.apps.controllerblueprints.core.service.BluePrintContext
30 import org.onap.ccsdk.apps.controllerblueprints.core.service.DefaultBluePrintRuntimeService
31 import org.onap.ccsdk.apps.controllerblueprints.core.utils.BluePrintMetadataUtils
32 import kotlin.test.assertEquals
33 import kotlin.test.assertTrue
34
35 class BluePrintValidatorServiceImplTest {
36
37     private val blueprintBasePath: String = ("./../../../../components/model-catalog/blueprint-model/starter-blueprint/baseconfiguration")
38     private val bluePrintRuntime = BluePrintMetadataUtils.getBluePrintRuntime("1234", blueprintBasePath)
39     private val mockBluePrintTypeValidatorService = MockBluePrintTypeValidatorService()
40     private val defaultBluePrintValidatorService = BluePrintValidatorServiceImpl(mockBluePrintTypeValidatorService)
41     private val workflowValidator = BluePrintWorkflowValidatorImpl(mockBluePrintTypeValidatorService)
42
43     @Test
44     fun testValidateOfType() {
45         val valid = defaultBluePrintValidatorService.validateBluePrints(bluePrintRuntime)
46         assertTrue(valid, "failed in blueprint Validation")
47     }
48
49     @Test
50     fun testValidateWorkflowFailToFoundNodeTemplate() {
51         val workflowName = "resource-assignment"
52
53         val step = Step()
54         step.target = "TestCaseFailNoNodeTemplate"
55         val workflow = Workflow()
56         workflow.steps = mutableMapOf("test" to step)
57         workflowValidator.validate(bluePrintRuntime, workflowName, workflow)
58
59         assertEquals(1, bluePrintRuntime.getBluePrintError().errors.size)
60         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])
61     }
62
63     @Test
64     fun testValidateWorkflowFailNodeTemplateNotDgGeneric() {
65         val workflowName = "resource-assignment"
66         val nodeTemplateName = "resource-assignment-process"
67
68         val nodeTemplate = mockk<NodeTemplate>()
69         every { nodeTemplate.type } returns "TestNodeType"
70
71         val nodeType = mockk<NodeType>()
72         every { nodeType.derivedFrom } returns "tosca.nodes.TEST"
73
74         val blueprintContext = mockk<BluePrintContext>()
75         every { blueprintContext.nodeTemplateByName(nodeTemplateName) } returns nodeTemplate
76         every { blueprintContext.nodeTemplateNodeType(nodeTemplateName) } returns nodeType
77
78         val bluePrintRuntime = mockk<DefaultBluePrintRuntimeService>("1234")
79
80         every { bluePrintRuntime.getBluePrintError() } returns BluePrintError()
81         every { bluePrintRuntime.bluePrintContext() } returns blueprintContext
82
83         val step = Step()
84         step.target = nodeTemplateName
85         val workflow = Workflow()
86         workflow.steps = mutableMapOf("test" to step)
87         workflowValidator.validate(bluePrintRuntime, workflowName, workflow)
88
89         assertEquals(1, bluePrintRuntime.getBluePrintError().errors.size)
90         assertEquals("Failed to validate Workflow(resource-assignment)'s step(test)'s definition : resource-assignment/steps/test : NodeType(TestNodeType) derived from is 'tosca.nodes.TEST', Expected is 'tosca.nodes.DG'", bluePrintRuntime.getBluePrintError().errors[0])
91     }
92
93     @Test
94     fun testValidateWorkflowSuccess() {
95         val workflowName = "resource-assignment"
96         workflowValidator.validate(bluePrintRuntime, workflowName, bluePrintRuntime.bluePrintContext().workflowByName(workflowName))
97     }
98
99 }
100