62adde73b7eb5ea0be3ec782ec8284301934f431
[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.services.resolution.processor.DataBaseResourceAssignmentProcessor
27 import org.onap.ccsdk.apps.blueprintsprocessor.services.resolution.processor.DefaultResourceAssignmentProcessor
28 import org.onap.ccsdk.apps.blueprintsprocessor.services.resolution.processor.InputResourceAssignmentProcessor
29 import org.onap.ccsdk.apps.blueprintsprocessor.services.resolution.processor.SimpleRestResourceAssignmentProcessor
30 import org.onap.ccsdk.apps.controllerblueprints.core.utils.JacksonUtils
31 import org.onap.ccsdk.apps.controllerblueprints.resource.dict.ResourceAssignment
32 import org.slf4j.LoggerFactory
33 import org.springframework.beans.factory.annotation.Autowired
34 import org.springframework.test.context.ContextConfiguration
35 import org.springframework.test.context.junit4.SpringRunner
36 import java.io.File
37 import java.nio.charset.Charset
38 import kotlin.test.assertNotNull
39 import kotlin.test.assertTrue
40
41 /**
42  * ResourceResolutionServiceTest
43  *
44  * @author Brinda Santh DATE : 8/15/2018
45  */
46 @RunWith(SpringRunner::class)
47 @ContextConfiguration(classes = [ResourceResolutionService::class,
48     InputResourceAssignmentProcessor::class, DefaultResourceAssignmentProcessor::class,
49     DataBaseResourceAssignmentProcessor::class, SimpleRestResourceAssignmentProcessor::class])
50 class ResourceResolutionServiceTest {
51
52     private val log = LoggerFactory.getLogger(ResourceResolutionServiceTest::class.java)
53
54     @Autowired
55     lateinit var resourceResolutionService: ResourceResolutionService
56
57
58     @Test
59     fun testRegisteredSource() {
60         val sources = resourceResolutionService.registeredResourceSources()
61         assertNotNull(sources, "failed to get registered sources")
62         assertTrue(sources.containsAll(arrayListOf("input", "default", "db", "mdsal")), "failed to get registered sources")
63     }
64
65     @Test
66     @Throws(Exception::class)
67     fun testResolveResource() {
68
69         Assert.assertNotNull("failed to create ResourceResolutionService", resourceResolutionService)
70
71         val resourceResolutionInputContent = FileUtils.readFileToString(
72                 File("src/test/resources/payload/requests/sample-resourceresolution-request.json"), Charset.defaultCharset())
73
74         val resourceResolutionInput = JacksonUtils.readValue(resourceResolutionInputContent, ResourceResolutionInput::class.java)
75         Assert.assertNotNull("failed to populate resourceResolutionInput request ", resourceResolutionInput)
76
77         val resourceAssignmentContent = FileUtils.readFileToString(
78                 File("src/test/resources/mapping/db/resource-assignments-simple.json"), Charset.defaultCharset())
79         val batchResourceAssignment = JacksonUtils.getListFromJson(resourceAssignmentContent, ResourceAssignment::class.java)
80
81         Assert.assertTrue("failed to create ResourceAssignment from file", CollectionUtils.isNotEmpty(batchResourceAssignment))
82         resourceResolutionInput!!.resourceAssignments = batchResourceAssignment as MutableList<ResourceAssignment>
83
84         val inputContent = JacksonUtils.jsonNodeFromFile("src/test/resources/payload/inputs/input.json") as ObjectNode
85         Assert.assertNotNull("failed to populate input payload ", inputContent)
86         resourceResolutionInput.payload = inputContent
87         log.info("ResourceResolutionInput : {}", JacksonUtils.getJson(resourceResolutionInput, true))
88
89         val resourceResolutionOutput = resourceResolutionService.resolveResource(resourceResolutionInput)
90         Assert.assertNotNull("failed to populate output", resourceResolutionOutput)
91
92     }
93
94 }