Add @transactional to delete data-dictionary
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / inbounds / designer-api / src / test / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / designer / api / repository / ResourceDictionaryReactRepositoryTest.kt
1 /*
2  *  Copyright © 2019 IBM.
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.repository
18
19 import org.junit.Assert
20 import org.junit.FixMethodOrder
21 import org.junit.Test
22 import org.junit.runner.RunWith
23 import org.junit.runners.MethodSorters
24 import org.onap.ccsdk.cds.blueprintsprocessor.designer.api.DesignerApiTestConfiguration
25 import org.onap.ccsdk.cds.blueprintsprocessor.designer.api.domain.ResourceDictionary
26 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
27 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceDefinition
28 import org.springframework.beans.factory.annotation.Autowired
29 import org.springframework.test.annotation.Commit
30 import org.springframework.test.context.ContextConfiguration
31 import org.springframework.test.context.TestPropertySource
32 import org.springframework.test.context.junit4.SpringRunner
33
34 @RunWith(SpringRunner::class)
35 @ContextConfiguration(
36     classes = [DesignerApiTestConfiguration::class]
37 )
38 @TestPropertySource(locations = ["classpath:application-test.properties"])
39 @FixMethodOrder(MethodSorters.NAME_ASCENDING)
40 class ResourceDictionaryReactRepositoryTest {
41
42     private val sourceName = "test-source"
43
44     @Autowired
45     lateinit var resourceDictionaryRepository: ResourceDictionaryRepository
46
47     @Test
48     @Commit
49     fun test01Save() {
50         val resourceDefinition = JacksonUtils
51             .readValueFromFile(
52                 "./../../../../../components/model-catalog/resource-dictionary/starter-dictionary/sample-db-source.json",
53                 ResourceDefinition::class.java
54             )
55         Assert.assertNotNull("Failed to get resourceDefinition from content", resourceDefinition)
56         resourceDefinition!!.name = sourceName
57
58         val resourceDictionary = transformResourceDictionary(resourceDefinition)
59         val dbResourceDictionary = resourceDictionaryRepository.save(resourceDictionary)
60         Assert.assertNotNull("Failed to save ResourceDictionary", dbResourceDictionary)
61     }
62
63     @Test
64     fun test02FindByNameReact() {
65         val dbResourceDictionary = resourceDictionaryRepository.findByName(sourceName)
66         Assert.assertNotNull("Failed to query React Resource Dictionary by Name", dbResourceDictionary)
67     }
68
69     @Test
70     fun test03FindByNameInReact() {
71         val dbResourceDictionaries = resourceDictionaryRepository.findByNameIn(arrayListOf(sourceName))
72         Assert.assertNotNull("Failed to query React Resource Dictionary by Names", dbResourceDictionaries)
73     }
74
75     @Test
76     fun test04FindByTagsContainingIgnoreCaseReact() {
77         val dbTagsResourceDictionaries = resourceDictionaryRepository.findByTagsContainingIgnoreCase(sourceName)
78         Assert.assertNotNull("Failed to query React Resource Dictionary by Tags", dbTagsResourceDictionaries)
79     }
80
81     @Test
82     @Commit
83     fun test05Delete() {
84         resourceDictionaryRepository.deleteByName(sourceName)
85     }
86
87     private fun transformResourceDictionary(resourceDefinition: ResourceDefinition): ResourceDictionary {
88         val resourceDictionary = ResourceDictionary()
89         resourceDictionary.name = resourceDefinition.name
90         resourceDictionary.dataType = resourceDefinition.property.type
91         resourceDictionary.description = resourceDefinition.property.description!!
92         resourceDictionary.tags = resourceDefinition.tags!!
93         resourceDictionary.updatedBy = resourceDefinition.updatedBy
94         resourceDictionary.definition = resourceDefinition
95         return resourceDictionary
96     }
97 }