2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.be.model.jsonjanusgraph.operations;
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.janusgraph.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;
47 import javax.annotation.Resource;
48 import java.util.ArrayList;
49 import java.util.List;
51 import static org.assertj.core.api.Assertions.assertThat;
53 @RunWith(SpringJUnit4ClassRunner.class)
54 @ContextConfiguration("classpath:application-context-test.xml")
55 public class UpgradeOperationTest extends ModelTestBase {
57 private boolean isInitialized;
59 private JanusGraphDao janusGraphDao;
61 private UpgradeOperation upgradeOperation;
63 private TopologyTemplateOperation topologyTemplateOperation;
65 private NodeTemplateOperation nodeTemplateOperation;
67 private UserAdminOperation userAdminOperation;
68 @javax.annotation.Resource
69 private IElementOperation elementOperation;
73 private CategoryDefinition resourceCategory;
74 private CategoryDefinition serviceCategory;
78 public static void initTest() {
83 public void beforeTest() {
85 GraphTestUtils.clearGraph(janusGraphDao);
92 public void testGetSimpleDependency() {
94 TopologyTemplate vf = createVf("vf1");
96 TopologyTemplate service = createServiceWitnInstance("service1", vf);
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);
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);
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();
116 /*******************************
117 * Preperation Methods
118 *******************************/
119 private void initGraphForTest() {
121 user = new User("Jim", "Tom", "jt123a", "1@mail.com", "DESIGNER", System.currentTimeMillis());
122 userAdminOperation.saveUserData(user);
124 GraphTestUtils.createRootCatalogVertex(janusGraphDao);
125 resourceCategory = createResourceCategories();
126 serviceCategory = createServiceCategory();
130 private TopologyTemplate createServiceWitnInstance(String name, TopologyTemplate createdVf) {
132 TopologyTemplate serviceNew = createTopologyTemplate(ComponentTypeEnum.SERVICE, name);
133 List<CategoryDefinition> categoriesService = new ArrayList<>();
134 categoriesService.add(serviceCategory);
135 serviceNew.setCategories(categoriesService);
137 Either<TopologyTemplate, StorageOperationStatus> createService = topologyTemplateOperation.createTopologyTemplate(serviceNew);
138 assertThat(createService.isLeft()).isTrue();
140 ComponentInstance vfInstance = new ComponentInstance();
141 vfInstance.setUniqueId(createdVf.getUniqueId() + createdVf.getName());
142 vfInstance.setComponentUid(createdVf.getUniqueId());
143 vfInstance.setName(createdVf.getName());
144 Either<ImmutablePair<TopologyTemplate, String>, StorageOperationStatus> addInstance = nodeTemplateOperation.addComponentInstanceToTopologyTemplate(serviceNew, createdVf, "0", vfInstance, false, user);
145 assertThat(addInstance.isLeft()).isTrue();
149 private CategoryDefinition createServiceCategory() {
150 CategoryDefinition categoryService = new CategoryDefinition();
151 categoryService.setName("servicecategory");
152 categoryService.setNormalizedName("servicecategory");
153 categoryService.setUniqueId("servicecategory");
154 Either<CategoryDefinition, ActionStatus> createCategory = elementOperation.createCategory(categoryService , NodeTypeEnum.ServiceNewCategory);
156 assertThat(createCategory.isLeft()).isTrue();
157 return categoryService;
160 private TopologyTemplate createVf(String name) {
162 TopologyTemplate resource = createTopologyTemplate(ComponentTypeEnum.RESOURCE, name);
164 resource.setResourceType(ResourceTypeEnum.VF);
165 List<CategoryDefinition> categories = new ArrayList<>();
166 categories.add(resourceCategory);
167 resource.setCategories(categories);
168 Either<TopologyTemplate, StorageOperationStatus> createVf = topologyTemplateOperation.createTopologyTemplate(resource);
169 assertThat( createVf.isLeft()).isTrue();
173 private CategoryDefinition createResourceCategories() {
174 CategoryDefinition category = new CategoryDefinition();
175 category.setName("category1");
176 category.setNormalizedName("category1");
177 category.setUniqueId("category1");
178 Either<CategoryDefinition, ActionStatus> createCategory = elementOperation.createCategory(category , NodeTypeEnum.ResourceNewCategory);
179 assertThat(createCategory.isLeft()).isTrue();
181 SubCategoryDefinition subCategory = new SubCategoryDefinition();
183 subCategory.setName("subcategory1");
184 subCategory.setNormalizedName("subcategory1");
185 subCategory.setUniqueId("subcategory1");
186 elementOperation.createSubCategory(createCategory.left().value().getUniqueId(), subCategory, NodeTypeEnum.ResourceSubcategory);
187 category.addSubCategory(subCategory);
191 private TopologyTemplate createTopologyTemplate(ComponentTypeEnum type, String name) {
192 TopologyTemplate template = new TopologyTemplate();
193 template.setUniqueId(IdBuilderUtils.generateUniqueId());
194 template.setComponentType(type);
195 template.setHighestVersion(true);
196 template.setLifecycleState(LifecycleStateEnum.CERTIFIED);
197 template.setMetadataValue(JsonPresentationFields.NAME, name);
198 template.setMetadataValue(JsonPresentationFields.VERSION, "1.0");
199 template.setCreatorUserId(user.getUserId());