Resource resolution should return a string
[ccsdk/cds.git] / ms / blueprintsprocessor / functions / resource-resolution / src / test / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / functions / resource / resolution / ResourceResolutionServiceTest.kt
1 /*
2  * Copyright © 2017-2018 AT&T Intellectual Property.
3  *
4  * Modifications Copyright © 2018 - 2019 IBM, Bell Canada.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19 package org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution
20
21 import kotlinx.coroutines.runBlocking
22 import org.junit.Assert
23 import org.junit.Before
24 import org.junit.Test
25 import org.junit.runner.RunWith
26 import org.onap.ccsdk.cds.blueprintsprocessor.core.BluePrintProperties
27 import org.onap.ccsdk.cds.blueprintsprocessor.core.BlueprintPropertyConfiguration
28 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInput
29 import org.onap.ccsdk.cds.blueprintsprocessor.core.utils.PayloadUtils
30 import org.onap.ccsdk.cds.blueprintsprocessor.db.BluePrintDBLibConfiguration
31 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.processor.*
32 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.utils.ResourceAssignmentUtils
33 import org.onap.ccsdk.cds.controllerblueprints.core.config.BluePrintLoadConfiguration
34 import org.onap.ccsdk.cds.controllerblueprints.core.utils.BluePrintMetadataUtils
35 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
36 import org.slf4j.LoggerFactory
37 import org.springframework.beans.factory.annotation.Autowired
38 import org.springframework.boot.autoconfigure.EnableAutoConfiguration
39 import org.springframework.context.annotation.ComponentScan
40 import org.springframework.test.context.ContextConfiguration
41 import org.springframework.test.context.TestPropertySource
42 import org.springframework.test.context.junit4.SpringRunner
43 import kotlin.test.assertNotNull
44 import kotlin.test.assertTrue
45
46 /**
47  * ResourceResolutionServiceTest
48  *
49  * @author Brinda Santh DATE : 8/15/2018
50  */
51 @RunWith(SpringRunner::class)
52 @ContextConfiguration(classes = [ResourceResolutionServiceImpl::class,
53     InputResourceResolutionProcessor::class, DefaultResourceResolutionProcessor::class,
54     DatabaseResourceAssignmentProcessor::class, RestResourceResolutionProcessor::class,
55     CapabilityResourceResolutionProcessor::class,
56     BlueprintPropertyConfiguration::class, BluePrintProperties::class,
57     BluePrintDBLibConfiguration::class, BluePrintLoadConfiguration::class])
58 @TestPropertySource(locations = ["classpath:application-test.properties"])
59 @ComponentScan(basePackages = ["org.onap.ccsdk.cds.blueprintsprocessor", "org.onap.ccsdk.cds.controllerblueprints"])
60 @EnableAutoConfiguration
61 class ResourceResolutionServiceTest {
62
63     private val log = LoggerFactory.getLogger(ResourceResolutionServiceTest::class.java)
64
65     @Autowired
66     lateinit var resourceResolutionService: ResourceResolutionService
67
68     private val props = hashMapOf<String, Any>()
69     private val resolutionKey = "resolutionKey"
70     private val resourceId = "1"
71     private val resourceType = "ServiceInstance"
72     private val occurrence = 0
73
74     @Before
75     fun setup() {
76         props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_STORE_RESULT] = true
77         props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_KEY] = resolutionKey
78         props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_ID] = resourceId
79         props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_TYPE] = resourceType
80         props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_OCCURRENCE] = occurrence
81     }
82
83     @Test
84     fun testRegisteredSource() {
85         val sources = resourceResolutionService.registeredResourceSources()
86         assertNotNull(sources, "failed to get registered sources")
87         assertTrue(sources.containsAll(arrayListOf("source-input", "source-default", "source-processor-db",
88             "source-rest")), "failed to get registered sources : $sources")
89     }
90
91     @Test
92     @Throws(Exception::class)
93     fun testResolveResource() {
94         runBlocking {
95
96             Assert.assertNotNull("failed to create ResourceResolutionService", resourceResolutionService)
97
98             val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime("1234",
99                 "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration")
100
101             val executionServiceInput =
102                 JacksonUtils.readValueFromClassPathFile("payload/requests/sample-resourceresolution-request.json",
103                     ExecutionServiceInput::class.java)!!
104
105
106             val resourceAssignmentRuntimeService =
107                 ResourceAssignmentUtils.transformToRARuntimeService(bluePrintRuntimeService,
108                     "testResolveResource")
109
110
111             // Prepare Inputs
112             PayloadUtils.prepareInputsFromWorkflowPayload(bluePrintRuntimeService,
113                 executionServiceInput.payload,
114                 "resource-assignment")
115
116             resourceResolutionService.resolveResources(resourceAssignmentRuntimeService,
117                 "resource-assignment",
118                 "baseconfig",
119                 props)
120
121         }
122     }
123
124     @Test
125     @Throws(Exception::class)
126     fun testResolveResources() {
127         runBlocking {
128             Assert.assertNotNull("failed to create ResourceResolutionService", resourceResolutionService)
129
130             val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime("1234",
131                 "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration")
132
133             val executionServiceInput =
134                 JacksonUtils.readValueFromClassPathFile("payload/requests/sample-resourceresolution-request.json",
135                     ExecutionServiceInput::class.java)!!
136
137             val artefactNames = listOf("baseconfig", "another")
138
139             // Prepare Inputs
140             PayloadUtils.prepareInputsFromWorkflowPayload(bluePrintRuntimeService,
141                 executionServiceInput.payload,
142                 "resource-assignment")
143
144             resourceResolutionService.resolveResources(bluePrintRuntimeService,
145                 "resource-assignment",
146                 artefactNames,
147                 props)
148         }
149
150     }
151
152     @Test
153     @Throws(Exception::class)
154     fun testResolveResourcesWithMappingAndTemplate() {
155         runBlocking {
156             Assert.assertNotNull("failed to create ResourceResolutionService", resourceResolutionService)
157
158             val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime("1234",
159                 "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration")
160
161             val executionServiceInput =
162                 JacksonUtils.readValueFromClassPathFile("payload/requests/sample-resourceresolution-request.json",
163                     ExecutionServiceInput::class.java)!!
164
165             val resourceAssignmentRuntimeService =
166                 ResourceAssignmentUtils.transformToRARuntimeService(bluePrintRuntimeService,
167                     "testResolveResourcesWithMappingAndTemplate")
168
169             val artifactPrefix = "another"
170
171             // Prepare Inputs
172             PayloadUtils.prepareInputsFromWorkflowPayload(bluePrintRuntimeService,
173                 executionServiceInput.payload,
174                 "resource-assignment")
175
176             resourceResolutionService.resolveResources(resourceAssignmentRuntimeService,
177                 "resource-assignment",
178                 artifactPrefix,
179                 props)
180         }
181     }
182
183
184     @Test
185     fun testResolveResourcesWithResourceIdAndResourceType() {
186
187         props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_KEY] = ""
188
189         runBlocking {
190             Assert.assertNotNull("failed to create ResourceResolutionService", resourceResolutionService)
191
192             val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime("1234",
193                 "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration")
194
195             val executionServiceInput =
196                 JacksonUtils.readValueFromClassPathFile("payload/requests/sample-resourceresolution-request.json",
197                     ExecutionServiceInput::class.java)!!
198
199             val resourceAssignmentRuntimeService =
200                 ResourceAssignmentUtils.transformToRARuntimeService(bluePrintRuntimeService,
201                     "testResolveResourcesWithMappingAndTemplate")
202
203             val artifactPrefix = "another"
204
205             // Prepare Inputs
206             PayloadUtils.prepareInputsFromWorkflowPayload(bluePrintRuntimeService,
207                 executionServiceInput.payload,
208                 "resource-assignment")
209
210             resourceResolutionService.resolveResources(resourceAssignmentRuntimeService,
211                 "resource-assignment",
212                 artifactPrefix,
213                 props)
214         }
215     }
216 }