Migrate "ms/controllerblueprints" from ccsdk/apps
[ccsdk/cds.git] / ms / controllerblueprints / modules / service / src / test / kotlin / org / onap / ccsdk / cds / controllerblueprints / service / controller / ModelTypeControllerTest.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.controllerblueprints.service.controller
18
19 import com.att.eelf.configuration.EELFManager
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.BluePrintConstants
27 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
28 import org.onap.ccsdk.cds.controllerblueprints.service.domain.ModelType
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 ModelTypeControllerTest {
40
41     private val log = EELFManager.getInstance().getLogger(ModelTypeControllerTest::class.java)!!
42
43     @Autowired
44     internal var modelTypeController: ModelTypeController? = null
45
46     private var modelName = "test-datatype"
47
48     @Test
49     @Commit
50     @Throws(Exception::class)
51     fun test01SaveModelType() {
52         log.info("**************** test01SaveModelType  ********************")
53
54         val content = JacksonUtils.getClassPathFileContent("model_type/data_type/datatype-property.json")
55         var modelType = ModelType()
56         modelType.definitionType = BluePrintConstants.MODEL_DEFINITION_TYPE_DATA_TYPE
57         modelType.derivedFrom = BluePrintConstants.MODEL_TYPE_DATATYPES_ROOT
58         modelType.description = "Definition for Sample Datatype "
59         modelType.definition = JacksonUtils.jsonNode(content)
60         modelType.modelName = modelName
61         modelType.version = "1.0.0"
62         modelType.tags = ("test-datatype ," + BluePrintConstants.MODEL_TYPE_DATATYPES_ROOT + ","
63                 + BluePrintConstants.MODEL_DEFINITION_TYPE_DATA_TYPE)
64         modelType.updatedBy = "xxxxxx@xxx.com"
65         modelType = modelTypeController!!.saveModelType(modelType)
66         log.info("Saved Mode {}", modelType.toString())
67         Assert.assertNotNull("Failed to get Saved ModelType", modelType)
68         Assert.assertNotNull("Failed to get Saved ModelType, Id", modelType.modelName)
69
70         val dbModelType = modelTypeController!!.getModelTypeByName(modelType.modelName)
71         Assert.assertNotNull("Failed to query ResourceMapping for ID (" + dbModelType!!.modelName + ")",
72                 dbModelType)
73
74         // Model Update
75         modelType.updatedBy = "bs2796@xxx.com"
76         modelType = modelTypeController!!.saveModelType(modelType)
77         Assert.assertNotNull("Failed to get Saved ModelType", modelType)
78         Assert.assertEquals("Failed to get Saved getUpdatedBy ", "bs2796@xxx.com", modelType.updatedBy)
79
80     }
81
82     @Test
83     @Throws(Exception::class)
84     fun test02SearchModelTypes() {
85         log.info("*********************** test02SearchModelTypes  ***************************")
86
87         val tags = "test-datatype"
88
89         val dbModelTypes = modelTypeController!!.searchModelTypes(tags)
90         Assert.assertNotNull("Failed to search ResourceMapping by tags", dbModelTypes)
91         Assert.assertTrue("Failed to search ResourceMapping by tags count", dbModelTypes.isNotEmpty())
92
93     }
94
95     @Test
96     @Throws(Exception::class)
97     fun test03GetModelType() {
98         log.info("************************* test03GetModelType  *********************************")
99         val dbModelType = modelTypeController!!.getModelTypeByName(modelName)
100         Assert.assertNotNull("Failed to get response for api call getModelByName $modelName", dbModelType)
101         Assert.assertNotNull("Failed to get Id for api call  getModelByName ", dbModelType!!.modelName)
102
103         val dbDatatypeModelTypes = modelTypeController!!.getModelTypeByDefinitionType(BluePrintConstants.MODEL_DEFINITION_TYPE_DATA_TYPE)
104         Assert.assertNotNull("Failed to find getModelTypeByDefinitionType by tags", dbDatatypeModelTypes)
105         Assert.assertTrue("Failed to find getModelTypeByDefinitionType by count", dbDatatypeModelTypes.isNotEmpty())
106     }
107
108     @Test
109     @Commit
110     @Throws(Exception::class)
111     fun test04DeleteModelType() {
112         log.info(
113                 "************************ test03DeleteModelType  ***********************")
114         val dbResourceMapping = modelTypeController!!.getModelTypeByName(modelName)
115         Assert.assertNotNull("Failed to get response for api call getModelByName ", dbResourceMapping)
116         Assert.assertNotNull("Failed to get Id for api call  getModelByName ", dbResourceMapping!!.modelName)
117
118         modelTypeController!!.deleteModelTypeByName(dbResourceMapping.modelName)
119     }
120 }