Migrate ccsdk/apps to ccsdk/cds
[ccsdk/cds.git] / ms / controllerblueprints / modules / service / src / test / java / org / onap / ccsdk / cds / controllerblueprints / service / ModelTypeServiceTest.java
1 /*
2  *  Copyright © 2018 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;
18
19 import com.att.eelf.configuration.EELFLogger;
20 import com.att.eelf.configuration.EELFManager;
21 import org.junit.Assert;
22 import org.junit.FixMethodOrder;
23 import org.junit.Test;
24 import org.junit.runner.RunWith;
25 import org.junit.runners.MethodSorters;
26 import org.onap.ccsdk.cds.controllerblueprints.TestApplication;
27 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants;
28 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils;
29 import org.onap.ccsdk.cds.controllerblueprints.service.domain.ModelType;
30 import org.onap.ccsdk.cds.controllerblueprints.service.handler.ModelTypeHandler;
31 import org.springframework.beans.factory.annotation.Autowired;
32 import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
33 import org.springframework.test.annotation.Commit;
34 import org.springframework.test.context.ContextConfiguration;
35 import org.springframework.test.context.junit4.SpringRunner;
36 import org.springframework.transaction.annotation.Propagation;
37 import org.springframework.transaction.annotation.Transactional;
38
39 import java.util.List;
40
41 @RunWith(SpringRunner.class)
42 @DataJpaTest
43 @Transactional(propagation = Propagation.NOT_SUPPORTED)
44 @ContextConfiguration(classes = {TestApplication.class})
45 @FixMethodOrder(MethodSorters.NAME_ASCENDING)
46 public class ModelTypeServiceTest {
47     private static EELFLogger log = EELFManager.getInstance().getLogger(ModelTypeServiceTest.class);
48     @Autowired
49     private ModelTypeHandler modelTypeHandler;
50
51     String modelName = "test-datatype";
52
53     @Test
54     @Commit
55     public void test01SaveModelType() throws Exception {
56         log.info("**************** test01SaveModelType  ********************");
57
58         String content = JacksonUtils.Companion.getClassPathFileContent("model_type/data_type/datatype-property.json");
59         ModelType modelType = new ModelType();
60         modelType.setDefinitionType(BluePrintConstants.MODEL_DEFINITION_TYPE_DATA_TYPE);
61         modelType.setDerivedFrom(BluePrintConstants.MODEL_TYPE_DATATYPES_ROOT);
62         modelType.setDescription("Definition for Sample Datatype ");
63         modelType.setDefinition(JacksonUtils.Companion.jsonNode(content));
64         modelType.setModelName(modelName);
65         modelType.setVersion("1.0.0");
66         modelType.setTags("test-datatype ," + BluePrintConstants.MODEL_TYPE_DATATYPES_ROOT + ","
67                 + BluePrintConstants.MODEL_DEFINITION_TYPE_DATA_TYPE);
68         modelType.setUpdatedBy("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.getModelName());
73
74         ModelType dbModelType = modelTypeHandler.getModelTypeByName(modelType.getModelName());
75         Assert.assertNotNull("Failed to query ResourceMapping for ID (" + dbModelType.getModelName() + ")",
76                 dbModelType);
77
78         // Model Update
79         modelType.setUpdatedBy("bs2796@xxx.com");
80         modelType = modelTypeHandler.saveModel(modelType);
81         Assert.assertNotNull("Failed to get Saved ModelType", modelType);
82         Assert.assertEquals("Failed to get Saved getUpdatedBy ", "bs2796@xxx.com", modelType.getUpdatedBy());
83
84     }
85
86     @Test
87     public void test02SearchModelTypes() throws Exception {
88         log.info("*********************** test02SearchModelTypes  ***************************");
89
90         String tags = "test-datatype";
91
92         List<ModelType> dbModelTypes = modelTypeHandler.searchModelTypes(tags);
93         Assert.assertNotNull("Failed to search ResourceMapping by tags", dbModelTypes);
94         Assert.assertTrue("Failed to search ResourceMapping by tags count", dbModelTypes.size() > 0);
95
96     }
97
98     @Test
99     public void test03GetModelType() throws Exception {
100         log.info("************************* test03GetModelType  *********************************");
101         ModelType dbModelType = modelTypeHandler.getModelTypeByName(modelName);
102         Assert.assertNotNull("Failed to get response for api call getModelByName ", dbModelType);
103         Assert.assertNotNull("Failed to get Id for api call  getModelByName ", dbModelType.getModelName());
104
105         List<ModelType> dbDatatypeModelTypes =
106                 modelTypeHandler.getModelTypeByDefinitionType(BluePrintConstants.MODEL_DEFINITION_TYPE_DATA_TYPE);
107         Assert.assertNotNull("Failed to find getModelTypeByDefinitionType by tags", dbDatatypeModelTypes);
108         Assert.assertTrue("Failed to find getModelTypeByDefinitionType by count", dbDatatypeModelTypes.size() > 0);
109
110         List<ModelType> dbModelTypeByDerivedFroms =
111                 modelTypeHandler.getModelTypeByDerivedFrom(BluePrintConstants.MODEL_TYPE_DATATYPES_ROOT);
112         Assert.assertNotNull("Failed to find getModelTypeByDerivedFrom by tags", dbModelTypeByDerivedFroms);
113         Assert.assertTrue("Failed to find getModelTypeByDerivedFrom by count", dbModelTypeByDerivedFroms.size() > 0);
114
115     }
116
117     @Test
118     public void test04DeleteModelType() throws Exception {
119         log.info(
120                 "************************ test03DeleteModelType  ***********************");
121         ModelType dbResourceMapping = modelTypeHandler.getModelTypeByName(modelName);
122         Assert.assertNotNull("Failed to get response for api call getModelByName ", dbResourceMapping);
123         Assert.assertNotNull("Failed to get Id for api call  getModelByName ", dbResourceMapping.getModelName());
124
125         modelTypeHandler.deleteByModelName(dbResourceMapping.getModelName());
126     }
127 }