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