fe5cd4d3eb66dad4984132b99ce779581a137204
[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.blueprintsprocessor.designer.api.handler
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.blueprintsprocessor.core.BluePrintPropertiesService
26 import org.onap.ccsdk.cds.blueprintsprocessor.core.BluePrintPropertyConfiguration
27 import org.onap.ccsdk.cds.blueprintsprocessor.db.BluePrintDBLibConfiguration
28 import org.onap.ccsdk.cds.blueprintsprocessor.designer.api.DesignerApiTestConfiguration
29 import org.onap.ccsdk.cds.blueprintsprocessor.designer.api.domain.ModelType
30 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
31 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
32 import org.slf4j.LoggerFactory
33 import org.springframework.beans.factory.annotation.Autowired
34 import org.springframework.test.annotation.Commit
35 import org.springframework.test.context.ContextConfiguration
36 import org.springframework.test.context.TestPropertySource
37 import org.springframework.test.context.junit4.SpringRunner
38
39 @RunWith(SpringRunner::class)
40 @ContextConfiguration(
41     classes = [DesignerApiTestConfiguration::class,
42         BluePrintPropertyConfiguration::class, BluePrintPropertiesService::class, BluePrintDBLibConfiguration::class]
43 )
44 @TestPropertySource(locations = ["classpath:application-test.properties"])
45 @FixMethodOrder(MethodSorters.NAME_ASCENDING)
46 class ModelTypeServiceTest {
47
48     @Autowired
49     private val modelTypeHandler: ModelTypeHandler? = null
50
51     internal var modelName = "test-datatype"
52
53     private val log = LoggerFactory.getLogger(ModelTypeServiceTest::class.java)
54
55     @Test
56     @Commit
57     @Throws(Exception::class)
58     fun test01SaveModelType() {
59         runBlocking {
60             log.info("**************** test01SaveModelType  ********************")
61
62             val content = JacksonUtils.getClassPathFileContent("model_type/data_type/datatype-property.json")
63             var modelType = ModelType()
64             modelType.definitionType = BluePrintConstants.MODEL_DEFINITION_TYPE_DATA_TYPE
65             modelType.derivedFrom = BluePrintConstants.MODEL_TYPE_DATATYPES_ROOT
66             modelType.description = "Definition for Sample Datatype "
67             modelType.definition = JacksonUtils.jsonNode(content)
68             modelType.modelName = modelName
69             modelType.version = "1.0.0"
70             modelType.tags = ("test-datatype ," + BluePrintConstants.MODEL_TYPE_DATATYPES_ROOT + "," +
71                     BluePrintConstants.MODEL_DEFINITION_TYPE_DATA_TYPE)
72             modelType.updatedBy = "xxxxxx@xxx.com"
73             modelType = modelTypeHandler!!.saveModel(modelType)
74             log.info("Saved Mode {}", modelType.toString())
75             Assert.assertNotNull("Failed to get Saved ModelType", modelType)
76             Assert.assertNotNull("Failed to get Saved ModelType, Id", modelType.modelName)
77
78             val dbModelType = modelTypeHandler.getModelTypeByName(modelType.modelName)
79             Assert.assertNotNull(
80                 "Failed to query ResourceMapping for ID (" + dbModelType!!.modelName + ")",
81                 dbModelType
82             )
83
84             // Model Update
85             modelType.updatedBy = "bs2796@xxx.com"
86             modelType = modelTypeHandler.saveModel(modelType)
87             Assert.assertNotNull("Failed to get Saved ModelType", modelType)
88             Assert.assertEquals("Failed to get Saved getUpdatedBy ", "bs2796@xxx.com", modelType.updatedBy)
89         }
90     }
91
92     @Test
93     @Throws(Exception::class)
94     fun test02SearchModelTypes() {
95         runBlocking {
96             log.info("*********************** test02SearchModelTypes  ***************************")
97
98             val tags = "test-datatype"
99
100             val dbModelTypes = modelTypeHandler!!.searchModelTypes(tags)
101             Assert.assertNotNull("Failed to search ResourceMapping by tags", dbModelTypes)
102             Assert.assertTrue("Failed to search ResourceMapping by tags count", dbModelTypes.size > 0)
103         }
104     }
105
106     @Test
107     @Throws(Exception::class)
108     fun test03GetModelType() {
109         runBlocking {
110             log.info("************************* test03GetModelType  *********************************")
111             val dbModelType = modelTypeHandler!!.getModelTypeByName(modelName)
112             Assert.assertNotNull("Failed to get response for api call getModelByName ", dbModelType)
113             Assert.assertNotNull("Failed to get Id for api call  getModelByName ", dbModelType!!.modelName)
114
115             val dbDatatypeModelTypes = modelTypeHandler.getModelTypeByDefinitionType(BluePrintConstants.MODEL_DEFINITION_TYPE_DATA_TYPE)
116             Assert.assertNotNull("Failed to find getModelTypeByDefinitionType by tags", dbDatatypeModelTypes)
117             Assert.assertTrue("Failed to find getModelTypeByDefinitionType by count", dbDatatypeModelTypes.size > 0)
118
119             val dbModelTypeByDerivedFroms = modelTypeHandler.getModelTypeByDerivedFrom(BluePrintConstants.MODEL_TYPE_DATATYPES_ROOT)
120             Assert.assertNotNull("Failed to find getModelTypeByDerivedFrom by tags", dbModelTypeByDerivedFroms)
121             Assert.assertTrue("Failed to find getModelTypeByDerivedFrom by count", dbModelTypeByDerivedFroms.size > 0)
122         }
123     }
124
125     @Test
126     @Throws(Exception::class)
127     fun test04DeleteModelType() {
128         runBlocking {
129             log.info(
130                 "************************ test03DeleteModelType  ***********************"
131             )
132             val dbResourceMapping = modelTypeHandler!!.getModelTypeByName(modelName)
133             Assert.assertNotNull("Failed to get response for api call getModelByName ", dbResourceMapping)
134             Assert.assertNotNull("Failed to get Id for api call  getModelByName ", dbResourceMapping!!.modelName)
135
136             modelTypeHandler.deleteByModelName(dbResourceMapping.modelName)
137         }
138     }
139 }