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