Enabling Code Formatter
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / inbounds / designer-api / src / test / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / designer / api / handler / ResourceDictionaryHandlerTest.kt
1 /*
2  * Copyright © 2019 Bell Canada.
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.cds.blueprintsprocessor.designer.api.handler
18
19 import kotlinx.coroutines.runBlocking
20 import org.hamcrest.Matchers.samePropertyValuesAs
21 import org.junit.Assert
22 import org.junit.Test
23 import org.junit.runner.RunWith
24 import org.mockito.ArgumentCaptor
25 import org.mockito.ArgumentMatchers.any
26 import org.mockito.Mockito
27 import org.onap.ccsdk.cds.blueprintsprocessor.designer.api.domain.ResourceDictionary
28 import org.onap.ccsdk.cds.blueprintsprocessor.designer.api.repository.ResourceDictionaryRepository
29 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
30 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceDefinition
31 import org.springframework.test.context.junit4.SpringRunner
32
33 @RunWith(SpringRunner::class)
34 class ResourceDictionaryHandlerTest {
35
36     private val mockRepository = Mockito.mock(ResourceDictionaryRepository::class.java)
37     private val resourceDictionaryHandler = ResourceDictionaryHandler(mockRepository)
38
39     @Test
40     fun testSaveResourceDictionary() {
41         val resourceDefinition: ResourceDefinition = JacksonUtils
42             .readValueFromFile(
43                 "./../../../../../components/model-catalog/resource-dictionary/starter-dictionary/sample-db-source.json",
44                 ResourceDefinition::class.java
45             )!!
46
47         val expectedResourceDictionary = ResourceDictionary()
48         expectedResourceDictionary.name = resourceDefinition.name
49         expectedResourceDictionary.updatedBy = resourceDefinition.updatedBy
50         expectedResourceDictionary.resourceDictionaryGroup = resourceDefinition.group
51         expectedResourceDictionary.tags = resourceDefinition.tags!!
52         expectedResourceDictionary.description = resourceDefinition.property.description!!
53         expectedResourceDictionary.dataType = resourceDefinition.property.type
54         expectedResourceDictionary.definition = resourceDefinition
55
56         // Mock save success
57         val mockReturnValue = ResourceDictionary()
58         mockReturnValue.definition = ResourceDefinition()
59         Mockito.`when`(mockRepository.save(any(ResourceDictionary::class.java)))
60             .thenReturn(mockReturnValue)
61
62         runBlocking {
63             resourceDictionaryHandler.saveResourceDefinition(resourceDefinition)
64         }
65
66         val argumentCaptor = ArgumentCaptor.forClass(ResourceDictionary::class.java)
67         Mockito.verify(mockRepository).save(argumentCaptor.capture())
68
69         Assert.assertThat(argumentCaptor.value, samePropertyValuesAs(expectedResourceDictionary))
70     }
71 }