a7d573c6c5fbd322e640e73a8f40e5f7df6b43f5
[ccsdk/cds.git] /
1 /*
2  * Copyright © 2017-2018 AT&T Intellectual Property.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.onap.ccsdk.apps.blueprintsprocessor.services.resolution
18
19 import com.fasterxml.jackson.databind.node.ObjectNode
20 import org.apache.commons.collections.CollectionUtils
21 import org.apache.commons.io.FileUtils
22 import org.junit.Assert
23 import org.junit.Test
24 import org.junit.runner.RunWith
25 import org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.ResourceResolutionInput
26 import org.onap.ccsdk.apps.blueprintsprocessor.core.factory.ResourceAssignmentProcessorFactory
27 import org.onap.ccsdk.apps.blueprintsprocessor.services.resolution.processor.DataBaseResourceAssignmentProcessor
28 import org.onap.ccsdk.apps.blueprintsprocessor.services.resolution.processor.DefaultResourceAssignmentProcessor
29 import org.onap.ccsdk.apps.blueprintsprocessor.services.resolution.processor.InputResourceAssignmentProcessor
30 import org.onap.ccsdk.apps.blueprintsprocessor.services.resolution.processor.SimpleRestResourceAssignmentProcessor
31 import org.onap.ccsdk.apps.controllerblueprints.core.utils.JacksonUtils
32 import org.onap.ccsdk.apps.controllerblueprints.resource.dict.ResourceAssignment
33 import org.slf4j.LoggerFactory
34 import org.springframework.beans.factory.annotation.Autowired
35 import org.springframework.test.context.ContextConfiguration
36 import org.springframework.test.context.junit4.SpringRunner
37 import java.io.File
38 import java.nio.charset.Charset
39 import kotlin.test.assertNotNull
40 import kotlin.test.assertTrue
41
42 /**
43  * ResourceResolutionServiceTest
44  *
45  * @author Brinda Santh DATE : 8/15/2018
46  */
47 @RunWith(SpringRunner::class)
48 @ContextConfiguration(classes = [ResourceResolutionService::class, ResourceAssignmentProcessorFactory::class,
49     InputResourceAssignmentProcessor::class, DefaultResourceAssignmentProcessor::class,
50     DataBaseResourceAssignmentProcessor::class, SimpleRestResourceAssignmentProcessor::class])
51 class ResourceResolutionServiceTest {
52
53     private val log = LoggerFactory.getLogger(ResourceResolutionServiceTest::class.java)
54
55     @Autowired
56     lateinit var resourceResolutionService: ResourceResolutionService
57
58
59     @Test
60     fun testRegisteredSource() {
61         val sources = resourceResolutionService.registeredResourceSources()
62         assertNotNull(sources, "failed to get registered sources")
63         assertTrue(sources.containsAll(arrayListOf("input", "default", "db", "mdsal")), "failed to get registered sources")
64     }
65
66     @Test
67     @Throws(Exception::class)
68     fun testResolveResource() {
69
70         Assert.assertNotNull("failed to create ResourceResolutionService", resourceResolutionService)
71
72         val resourceResolutionInputContent = FileUtils.readFileToString(
73                 File("src/test/resources/payload/requests/sample-resourceresolution-request.json"), Charset.defaultCharset())
74
75         val resourceResolutionInput = JacksonUtils.readValue(resourceResolutionInputContent, ResourceResolutionInput::class.java)
76         Assert.assertNotNull("failed to populate resourceResolutionInput request ", resourceResolutionInput)
77
78         val resourceAssignmentContent = FileUtils.readFileToString(
79                 File("src/test/resources/mapping/db/resource-assignments-simple.json"), Charset.defaultCharset())
80         val batchResourceAssignment = JacksonUtils.getListFromJson(resourceAssignmentContent, ResourceAssignment::class.java)
81
82         Assert.assertTrue("failed to create ResourceAssignment from file", CollectionUtils.isNotEmpty(batchResourceAssignment))
83         resourceResolutionInput!!.resourceAssignments = batchResourceAssignment as MutableList<ResourceAssignment>
84
85         val inputContent = JacksonUtils.jsonNodeFromFile("src/test/resources/payload/inputs/input.json") as ObjectNode
86         Assert.assertNotNull("failed to populate input payload ", inputContent)
87         resourceResolutionInput.payload = inputContent
88         log.info("ResourceResolutionInput : {}", JacksonUtils.getJson(resourceResolutionInput, true))
89
90         val resourceResolutionOutput = resourceResolutionService.resolveResource(resourceResolutionInput)
91         Assert.assertNotNull("failed to populate output", resourceResolutionOutput)
92
93     }
94
95 }