/*- * ============LICENSE_START======================================================= * SDC * ================================================================================ * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * ============LICENSE_END========================================================= */ package org.openecomp.sdc.be.model.jsonjanusgraph.operations; import static org.assertj.core.api.Assertions.assertThat; import fj.data.Either; import java.util.ArrayList; import java.util.List; import javax.annotation.Resource; import org.apache.commons.lang3.tuple.ImmutablePair; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.openecomp.sdc.be.dao.api.ActionStatus; import org.openecomp.sdc.be.dao.janusgraph.JanusGraphDao; import org.openecomp.sdc.be.dao.jsongraph.utils.IdBuilderUtils; import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum; import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields; import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum; import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum; import org.openecomp.sdc.be.model.ComponentDependency; import org.openecomp.sdc.be.model.ComponentInstance; import org.openecomp.sdc.be.model.LifecycleStateEnum; import org.openecomp.sdc.be.model.ModelTestBase; import org.openecomp.sdc.be.model.User; import org.openecomp.sdc.be.model.category.CategoryDefinition; import org.openecomp.sdc.be.model.category.SubCategoryDefinition; import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.TopologyTemplate; import org.openecomp.sdc.be.model.jsonjanusgraph.utils.GraphTestUtils; import org.openecomp.sdc.be.model.operations.api.IElementOperation; import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; import org.openecomp.sdc.be.model.operations.impl.UserAdminOperation; import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; @SpringJUnitConfig(locations = "classpath:application-context-test.xml") public class UpgradeOperationTest extends ModelTestBase { private boolean isInitialized; @Resource private JanusGraphDao janusGraphDao; @Resource private UpgradeOperation upgradeOperation; @Resource private TopologyTemplateOperation topologyTemplateOperation; @Resource private NodeTemplateOperation nodeTemplateOperation; @Resource private UserAdminOperation userAdminOperation; @javax.annotation.Resource private IElementOperation elementOperation; private User user; private CategoryDefinition resourceCategory; private CategoryDefinition serviceCategory; @BeforeAll public static void initTest() { ModelTestBase.init(); } @BeforeEach public void beforeTest() { if (!isInitialized) { GraphTestUtils.clearGraph(janusGraphDao); initGraphForTest(); isInitialized = true; } } @Test public void testGetSimpleDependency() { TopologyTemplate vf = createVf("vf1"); TopologyTemplate service = createServiceWitnInstance("service1", vf); Either, StorageOperationStatus> result = upgradeOperation.getComponentDependencies(vf.getUniqueId()); assertThat(result.isLeft()).isTrue(); List dependencies = result.left().value(); assertThat(dependencies).hasSize(1); ComponentDependency dependency = dependencies.get(0); assertThat(dependency.getName()).isEqualTo(vf.getName()); assertThat(dependency.getVersion()).isEqualTo(vf.getVersion()); assertThat(dependency.getDependencies()).hasSize(1); ComponentDependency container = dependency.getDependencies().get(0); assertThat(container.getName()).isEqualTo(service.getName()); assertThat(container.getVersion()).isEqualTo(service.getVersion()); assertThat(container.getDependencies()).isNull(); } /******************************* * Preperation Methods *******************************/ private void initGraphForTest() { user = new User("Jim", "Tom", "jt123a", "1@mail.com", "DESIGNER", System.currentTimeMillis()); userAdminOperation.saveUserData(user); GraphTestUtils.createRootCatalogVertex(janusGraphDao); resourceCategory = createResourceCategories(); serviceCategory = createServiceCategory(); } private TopologyTemplate createServiceWitnInstance(String name, TopologyTemplate createdVf) { TopologyTemplate serviceNew = createTopologyTemplate(ComponentTypeEnum.SERVICE, name); List categoriesService = new ArrayList<>(); categoriesService.add(serviceCategory); serviceNew.setCategories(categoriesService); Either createService = topologyTemplateOperation.createTopologyTemplate(serviceNew); assertThat(createService.isLeft()).isTrue(); ComponentInstance vfInstance = new ComponentInstance(); vfInstance.setUniqueId(createdVf.getUniqueId() + createdVf.getName()); vfInstance.setComponentUid(createdVf.getUniqueId()); vfInstance.setName(createdVf.getName()); Either, StorageOperationStatus> addInstance = nodeTemplateOperation.addComponentInstanceToTopologyTemplate( serviceNew, createdVf, "0", vfInstance, false, user); assertThat(addInstance.isLeft()).isTrue(); return serviceNew; } private CategoryDefinition createServiceCategory() { CategoryDefinition categoryService = new CategoryDefinition(); categoryService.setName("servicecategory"); categoryService.setNormalizedName("servicecategory"); categoryService.setUniqueId("servicecategory"); Either createCategory = elementOperation.createCategory(categoryService, NodeTypeEnum.ServiceNewCategory); assertThat(createCategory.isLeft()).isTrue(); return categoryService; } private TopologyTemplate createVf(String name) { TopologyTemplate resource = createTopologyTemplate(ComponentTypeEnum.RESOURCE, name); resource.setResourceType(ResourceTypeEnum.VF); List categories = new ArrayList<>(); categories.add(resourceCategory); resource.setCategories(categories); Either createVf = topologyTemplateOperation.createTopologyTemplate(resource); assertThat(createVf.isLeft()).isTrue(); return resource; } private CategoryDefinition createResourceCategories() { CategoryDefinition category = new CategoryDefinition(); category.setName("category1"); category.setNormalizedName("category1"); category.setUniqueId("category1"); Either createCategory = elementOperation.createCategory(category, NodeTypeEnum.ResourceNewCategory); assertThat(createCategory.isLeft()).isTrue(); SubCategoryDefinition subCategory = new SubCategoryDefinition(); subCategory.setName("subcategory1"); subCategory.setNormalizedName("subcategory1"); subCategory.setUniqueId("subcategory1"); elementOperation.createSubCategory(createCategory.left().value().getUniqueId(), subCategory, NodeTypeEnum.ResourceSubcategory); category.addSubCategory(subCategory); return category; } private TopologyTemplate createTopologyTemplate(ComponentTypeEnum type, String name) { TopologyTemplate template = new TopologyTemplate(); template.setUniqueId(IdBuilderUtils.generateUniqueId()); template.setComponentType(type); template.setHighestVersion(true); template.setLifecycleState(LifecycleStateEnum.CERTIFIED); template.setMetadataValue(JsonPresentationFields.NAME, name); template.setMetadataValue(JsonPresentationFields.VERSION, "1.0"); template.setCreatorUserId(user.getUserId()); return template; } }