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