Merge "Controller Blueprints Microservice"
[ccsdk/apps.git] / ms / controllerblueprints / modules / service / src / test / java / org / onap / ccsdk / apps / controllerblueprints / service / rs / ModelTypeRestTest.java
1 /*\r
2  * Copyright © 2017-2018 AT&T Intellectual Property.\r
3  *\r
4  * Licensed under the Apache License, Version 2.0 (the "License");\r
5  * you may not use this file except in compliance with the License.\r
6  * You may obtain a copy of the License at\r
7  *\r
8  *     http://www.apache.org/licenses/LICENSE-2.0\r
9  *\r
10  * Unless required by applicable law or agreed to in writing, software\r
11  * distributed under the License is distributed on an "AS IS" BASIS,\r
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13  * See the License for the specific language governing permissions and\r
14  * limitations under the License.\r
15  */\r
16 \r
17 package org.onap.ccsdk.apps.controllerblueprints.service.rs;\r
18 \r
19 import org.apache.commons.io.FileUtils;\r
20 import org.junit.*;\r
21 import org.junit.runner.RunWith;\r
22 import org.junit.runners.MethodSorters;\r
23 import org.onap.ccsdk.apps.controllerblueprints.TestApplication;\r
24 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants;\r
25 import org.onap.ccsdk.apps.controllerblueprints.service.domain.ModelType;\r
26 import org.slf4j.Logger;\r
27 import org.slf4j.LoggerFactory;\r
28 import org.springframework.beans.factory.annotation.Autowired;\r
29 import org.springframework.boot.test.context.SpringBootTest;\r
30 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;\r
31 import org.springframework.test.context.ContextConfiguration;\r
32 import org.springframework.test.context.junit4.SpringRunner;\r
33 \r
34 import java.io.File;\r
35 import java.nio.charset.Charset;\r
36 import java.util.List;\r
37 \r
38 @RunWith(SpringRunner.class)\r
39 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)\r
40 @ContextConfiguration(classes = {TestApplication.class})\r
41 @FixMethodOrder(MethodSorters.NAME_ASCENDING)\r
42 public class ModelTypeRestTest {\r
43     private static Logger log = LoggerFactory.getLogger(ModelTypeRestTest.class);\r
44     @Autowired\r
45     ModelTypeRest modelTypeRest;\r
46 \r
47     String modelName = "test-datatype";\r
48 \r
49     @Before\r
50     public void setUp() {\r
51 \r
52     }\r
53 \r
54 \r
55     @After\r
56     public void tearDown() {}\r
57 \r
58     @Test\r
59     public void test01SaveModelType() throws Exception {\r
60         log.info( "**************** test01SaveModelType  ********************");\r
61 \r
62         String content = FileUtils.readFileToString(new File("load/model_type/data_type/datatype-property.json"), Charset.defaultCharset());\r
63         ModelType modelType = new ModelType();\r
64         modelType.setDefinitionType(BluePrintConstants.MODEL_DEFINITION_TYPE_DATA_TYPE);\r
65         modelType.setDerivedFrom(BluePrintConstants.MODEL_TYPE_DATATYPES_ROOT);\r
66         modelType.setDescription("Definition for Sample Datatype ");\r
67         modelType.setDefinition(content);\r
68         modelType.setModelName(modelName);\r
69         modelType.setVersion("1.0.0");\r
70         modelType.setTags("test-datatype ," + BluePrintConstants.MODEL_TYPE_DATATYPES_ROOT + ","\r
71                 + BluePrintConstants.MODEL_DEFINITION_TYPE_DATA_TYPE);\r
72         modelType.setUpdatedBy("xxxxxx@xxx.com");\r
73         modelType = modelTypeRest.saveModelType(modelType);\r
74         log.info( "Saved Mode {}", modelType.toString());\r
75         Assert.assertNotNull("Failed to get Saved ModelType", modelType);\r
76         Assert.assertNotNull("Failed to get Saved ModelType, Id", modelType.getModelName());\r
77 \r
78         ModelType dbModelType = modelTypeRest.getModelTypeByName(modelType.getModelName());\r
79         Assert.assertNotNull("Failed to query ResourceMapping for ID (" + dbModelType.getModelName() + ")",\r
80                 dbModelType);\r
81 \r
82         // Model Update\r
83         modelType.setUpdatedBy("bs2796@xxx.com");\r
84         modelType = modelTypeRest.saveModelType(modelType);\r
85         Assert.assertNotNull("Failed to get Saved ModelType", modelType);\r
86         Assert.assertEquals("Failed to get Saved getUpdatedBy ", "bs2796@xxx.com", modelType.getUpdatedBy());\r
87 \r
88     }\r
89 \r
90     @Test\r
91     public void test02SearchModelTypes() throws Exception {\r
92         log.info( "*********************** test02SearchModelTypes  ***************************");\r
93 \r
94         String tags = "test-datatype";\r
95 \r
96         List<ModelType> dbModelTypes = modelTypeRest.searchModelTypes(tags);\r
97         Assert.assertNotNull("Failed to search ResourceMapping by tags", dbModelTypes);\r
98         Assert.assertTrue("Failed to search ResourceMapping by tags count", dbModelTypes.size() > 0);\r
99 \r
100     }\r
101 \r
102     @Test\r
103     public void test03GetModelType() throws Exception {\r
104         log.info( "************************* test03GetModelType  *********************************");\r
105         ModelType dbModelType = modelTypeRest.getModelTypeByName(modelName);\r
106         Assert.assertNotNull("Failed to get response for api call getModelByName ", dbModelType);\r
107         Assert.assertNotNull("Failed to get Id for api call  getModelByName ", dbModelType.getModelName());\r
108 \r
109         List<ModelType> dbDatatypeModelTypes =\r
110                 modelTypeRest.getModelTypeByDefinitionType(BluePrintConstants.MODEL_DEFINITION_TYPE_DATA_TYPE);\r
111         Assert.assertNotNull("Failed to find getModelTypeByDefinitionType by tags", dbDatatypeModelTypes);\r
112         Assert.assertTrue("Failed to find getModelTypeByDefinitionType by count", dbDatatypeModelTypes.size() > 0);\r
113     }\r
114 \r
115     @Test\r
116     public void test04DeleteModelType() throws Exception {\r
117         log.info(\r
118                 "************************ test03DeleteModelType  ***********************");\r
119         ModelType dbResourceMapping = modelTypeRest.getModelTypeByName(modelName);\r
120         Assert.assertNotNull("Failed to get response for api call getModelByName ", dbResourceMapping);\r
121         Assert.assertNotNull("Failed to get Id for api call  getModelByName ", dbResourceMapping.getModelName());\r
122 \r
123         modelTypeRest.deleteModelTypeByName(dbResourceMapping.getModelName());\r
124     }\r
125 \r
126 \r
127 \r
128 }\r