Added oparent to sdc main
[sdc.git] / catalog-model / src / test / java / org / openecomp / sdc / be / model / jsonjanusgraph / operations / UpgradeOperationTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.be.model.jsonjanusgraph.operations;
22
23 import fj.data.Either;
24 import org.apache.commons.lang3.tuple.ImmutablePair;
25 import org.junit.Before;
26 import org.junit.BeforeClass;
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29 import org.openecomp.sdc.be.dao.api.ActionStatus;
30 import org.openecomp.sdc.be.dao.jsongraph.JanusGraphDao;
31 import org.openecomp.sdc.be.dao.jsongraph.utils.IdBuilderUtils;
32 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
33 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
34 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
35 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
36 import org.openecomp.sdc.be.model.*;
37 import org.openecomp.sdc.be.model.category.CategoryDefinition;
38 import org.openecomp.sdc.be.model.category.SubCategoryDefinition;
39 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.TopologyTemplate;
40 import org.openecomp.sdc.be.model.jsonjanusgraph.utils.GraphTestUtils;
41 import org.openecomp.sdc.be.model.operations.api.IElementOperation;
42 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
43 import org.openecomp.sdc.be.model.operations.impl.UserAdminOperation;
44 import org.springframework.test.context.ContextConfiguration;
45 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
46
47 import javax.annotation.Resource;
48 import java.util.ArrayList;
49 import java.util.List;
50
51 import static org.assertj.core.api.Assertions.assertThat;
52
53 @RunWith(SpringJUnit4ClassRunner.class)
54 @ContextConfiguration("classpath:application-context-test.xml")
55 public class UpgradeOperationTest extends ModelTestBase {
56
57     private boolean isInitialized;
58     @Resource
59     private JanusGraphDao janusGraphDao;
60     @Resource
61     private UpgradeOperation upgradeOperation;
62     @Resource
63     private TopologyTemplateOperation topologyTemplateOperation;
64     @Resource
65     private NodeTemplateOperation nodeTemplateOperation;
66     @Resource
67     private UserAdminOperation userAdminOperation; 
68     @javax.annotation.Resource
69     private IElementOperation elementOperation;
70
71     private User user;
72
73     private CategoryDefinition resourceCategory;
74     private CategoryDefinition serviceCategory;
75     
76
77     @BeforeClass
78     public static void initTest() {
79         ModelTestBase.init();
80     }
81
82     @Before
83     public void beforeTest() {
84         if (!isInitialized) {
85             GraphTestUtils.clearGraph(janusGraphDao);
86             initGraphForTest();
87             isInitialized = true;
88         }
89     }
90
91     @Test
92     public void testGetSimpleDependency() {
93         
94         TopologyTemplate vf = createVf("vf1");
95         
96         TopologyTemplate service = createServiceWitnInstance("service1", vf);
97
98         
99         Either<List<ComponentDependency>, StorageOperationStatus> result = upgradeOperation.getComponentDependencies(vf.getUniqueId());
100         assertThat(result.isLeft()).isTrue();
101         List<ComponentDependency> dependencies = result.left().value();
102         assertThat(dependencies).hasSize(1);
103         
104         ComponentDependency dependency = dependencies.get(0);
105         assertThat(dependency.getName()).isEqualTo(vf.getName());
106         assertThat(dependency.getVersion()).isEqualTo(vf.getVersion());
107         assertThat(dependency.getDependencies()).hasSize(1);
108         
109         ComponentDependency container = dependency.getDependencies().get(0);
110         assertThat(container.getName()).isEqualTo(service.getName());
111         assertThat(container.getVersion()).isEqualTo(service.getVersion());
112         assertThat(container.getDependencies()).isNull();
113
114     }
115
116     /*******************************
117      * Preperation Methods
118      *******************************/
119     private void initGraphForTest() {
120
121         user = new User("Jim", "Tom", "jt123a", "1@mail.com", "DESIGNER", System.currentTimeMillis());
122         Either<User, StorageOperationStatus> saveUserData = userAdminOperation.saveUserData(user);
123         assertThat(saveUserData.isLeft()).isTrue();
124         
125         GraphTestUtils.createRootCatalogVertex(janusGraphDao);
126         resourceCategory = createResourceCategories();
127         serviceCategory = createServiceCategory(); 
128         
129     }
130
131     private TopologyTemplate createServiceWitnInstance(String name, TopologyTemplate createdVf) {
132         
133         TopologyTemplate serviceNew = createTopologyTemplate(ComponentTypeEnum.SERVICE, name);
134         List<CategoryDefinition> categoriesService = new ArrayList<>();
135         categoriesService.add(serviceCategory);
136         serviceNew.setCategories(categoriesService);
137
138         Either<TopologyTemplate, StorageOperationStatus> createService = topologyTemplateOperation.createTopologyTemplate(serviceNew);
139         assertThat(createService.isLeft()).isTrue();
140         
141         ComponentInstance vfInstance = new ComponentInstance();
142         vfInstance.setUniqueId(createdVf.getUniqueId() + createdVf.getName());
143         vfInstance.setComponentUid(createdVf.getUniqueId());
144         vfInstance.setName(createdVf.getName());
145         Either<ImmutablePair<TopologyTemplate, String>, StorageOperationStatus> addInstance = nodeTemplateOperation.addComponentInstanceToTopologyTemplate(serviceNew, createdVf, "0", vfInstance, false, user);
146         assertThat(addInstance.isLeft()).isTrue();
147         return serviceNew;
148     }
149
150     private CategoryDefinition createServiceCategory() {
151         CategoryDefinition categoryService = new CategoryDefinition();
152         categoryService.setName("servicecategory");
153         categoryService.setNormalizedName("servicecategory");
154         categoryService.setUniqueId("servicecategory");
155         Either<CategoryDefinition, ActionStatus> createCategory = elementOperation.createCategory(categoryService , NodeTypeEnum.ServiceNewCategory);
156         
157         assertThat(createCategory.isLeft()).isTrue();
158         return categoryService;
159     }
160
161     private TopologyTemplate createVf(String name) {
162         
163         TopologyTemplate resource = createTopologyTemplate(ComponentTypeEnum.RESOURCE, name);
164
165         resource.setResourceType(ResourceTypeEnum.VF);
166         List<CategoryDefinition> categories = new ArrayList<>();
167         categories.add(resourceCategory);
168         resource.setCategories(categories);
169         Either<TopologyTemplate, StorageOperationStatus> createVf = topologyTemplateOperation.createTopologyTemplate(resource);
170         assertThat( createVf.isLeft()).isTrue();
171         return resource;
172     }
173
174     private CategoryDefinition createResourceCategories() {
175         CategoryDefinition category = new CategoryDefinition();
176         category.setName("category1");
177         category.setNormalizedName("category1");
178         category.setUniqueId("category1");
179         Either<CategoryDefinition, ActionStatus> createCategory = elementOperation.createCategory(category , NodeTypeEnum.ResourceNewCategory);
180         assertThat(createCategory.isLeft()).isTrue();
181         
182         SubCategoryDefinition subCategory = new SubCategoryDefinition();
183         
184         subCategory.setName("subcategory1");
185         subCategory.setNormalizedName("subcategory1");
186         subCategory.setUniqueId("subcategory1");
187         elementOperation.createSubCategory(createCategory.left().value().getUniqueId(), subCategory, NodeTypeEnum.ResourceSubcategory);
188         category.addSubCategory(subCategory);
189         return category;
190     }
191
192     private TopologyTemplate createTopologyTemplate(ComponentTypeEnum type, String name) {
193         TopologyTemplate template = new TopologyTemplate();
194         template.setUniqueId(IdBuilderUtils.generateUniqueId());
195         template.setComponentType(type);
196         template.setHighestVersion(true);
197         template.setLifecycleState(LifecycleStateEnum.CERTIFIED);
198         template.setMetadataValue(JsonPresentationFields.NAME, name);
199         template.setMetadataValue(JsonPresentationFields.VERSION, "1.0");
200         template.setCreatorUserId(user.getUserId());
201         return template;
202     }
203 }