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