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.components.path;
23 import static org.junit.jupiter.api.Assertions.assertNull;
24 import static org.junit.jupiter.api.Assertions.assertTrue;
25 import static org.mockito.ArgumentMatchers.any;
26 import static org.mockito.Mockito.mock;
27 import static org.mockito.Mockito.when;
29 import com.google.common.collect.Lists;
30 import fj.data.Either;
31 import java.util.ArrayList;
32 import java.util.HashMap;
33 import java.util.HashSet;
36 import org.junit.jupiter.api.BeforeAll;
37 import org.junit.jupiter.api.BeforeEach;
38 import org.openecomp.sdc.be.components.BeConfDependentTest;
39 import org.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic;
40 import org.openecomp.sdc.be.components.impl.ServiceBusinessLogic;
41 import org.openecomp.sdc.be.components.path.beans.JanusGraphTestSetup;
42 import org.openecomp.sdc.be.components.path.utils.GraphTestUtils;
43 import org.openecomp.sdc.be.components.validation.service.ServiceValidator;
44 import org.openecomp.sdc.be.config.ConfigurationManager;
45 import org.openecomp.sdc.be.dao.api.ActionStatus;
46 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphClient;
47 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphDao;
48 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
49 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
50 import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum;
51 import org.openecomp.sdc.be.datatypes.elements.ForwardingPathDataDefinition;
52 import org.openecomp.sdc.be.datatypes.elements.ForwardingPathElementDataDefinition;
53 import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
54 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
55 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
56 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
57 import org.openecomp.sdc.be.facade.operations.CatalogOperation;
58 import org.openecomp.sdc.be.model.LifecycleStateEnum;
59 import org.openecomp.sdc.be.model.Service;
60 import org.openecomp.sdc.be.model.User;
61 import org.openecomp.sdc.be.model.category.CategoryDefinition;
62 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
63 import org.openecomp.sdc.be.model.operations.api.IElementOperation;
64 import org.openecomp.sdc.be.model.operations.impl.UniqueIdBuilder;
65 import org.openecomp.sdc.be.tosca.CapabilityRequirementConverter;
66 import org.openecomp.sdc.be.user.Role;
67 import org.openecomp.sdc.common.datastructure.UserContext;
68 import org.openecomp.sdc.common.impl.ExternalConfiguration;
69 import org.openecomp.sdc.common.impl.FSConfigurationSource;
70 import org.openecomp.sdc.common.util.ThreadLocalsHolder;
71 import org.openecomp.sdc.common.util.ValidationUtils;
72 import org.openecomp.sdc.exception.ResponseFormat;
73 import org.springframework.beans.factory.annotation.Autowired;
75 public abstract class BaseForwardingPathTest extends BeConfDependentTest implements ForwardingPathTestUtils {
77 static final String FORWARDING_PATH_ID = "forwarding_pathId";
78 static final String HTTP_PROTOCOL = "http";
79 private static final String CATEGORY_NAME = "cat_name";
80 private static final String INSTANTIATION_TYPE = "A-la-carte";
81 private static final String CAPABILITY_NAME_1 = "CP1";
82 private static final String CAPABILITY_NAME_2 = "CP2";
83 private static final String CAPABILITY_NAME_3 = "CP3";
84 private static final String CI_NAME_1 = "CI1";
85 private static final String CI_NAME_2 = "CI2";
86 private static final String CI_NAME_3 = "CI3";
89 protected JanusGraphClient janusGraphClient;
91 protected CapabilityRequirementConverter capabiltyRequirementConvertor;
93 protected ToscaOperationFacade toscaOperationFacade;
95 protected ServiceBusinessLogic bl;
97 protected IElementOperation elementDao;
99 protected ComponentInstanceBusinessLogic componentInstanceBusinessLogic;
100 @javax.annotation.Resource
101 protected JanusGraphDao janusGraphDao;
102 private ForwardingPathDataDefinition forwardingPathDataDefinition;
103 private CatalogOperation catalogOperation = mock(CatalogOperation.class);
104 private ServiceValidator serviceValidator = mock(ServiceValidator.class);
105 private CategoryDefinition categoryDefinition;
108 public void initJanusGraph() {
109 JanusGraphTestSetup.createGraph(janusGraphClient.getGraph().left().value());
110 categoryDefinition = new CategoryDefinition();
111 categoryDefinition.setName(CATEGORY_NAME);
115 public void initUser() {
116 // User data and management
118 user.setUserId("jh0003");
119 user.setFirstName("Jimmi");
120 user.setLastName("Hendrix");
121 user.setRole(Role.ADMIN.name());
122 Set<String> userRole = new HashSet<>();
123 userRole.add(user.getRole());
124 UserContext userContext = new UserContext(user.getUserId(), userRole, user.getFirstName(), user.getLastName());
125 ThreadLocalsHolder.setUserContext(userContext);
126 bl.setServiceValidator(serviceValidator);
129 private void initGraph() {
130 Map<GraphPropertyEnum, Object> props = new HashMap<>();
131 props.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
132 props.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
133 props.put(GraphPropertyEnum.TOSCA_RESOURCE_NAME, "org.openecomp.resource.abstract.nodes.service");
135 GraphTestUtils.createServiceVertex(janusGraphDao, props);
137 GraphVertex resourceVertex = GraphTestUtils.createResourceVertex(janusGraphDao, props, ResourceTypeEnum.PNF);
138 resourceVertex.setJsonMetadataField(JsonPresentationFields.VERSION, "0.1");
139 Either<GraphVertex, JanusGraphOperationStatus> vertexJanusGraphOperationStatusEither = janusGraphDao
140 .updateVertex(resourceVertex);
141 assertTrue(vertexJanusGraphOperationStatusEither.isLeft());
144 private Service createTestService() {
145 when(catalogOperation.updateCatalog(any(), any())).thenReturn(ActionStatus.OK);
146 bl.setCatalogOperations(catalogOperation);
147 bl.setServiceValidator(serviceValidator);
149 createServiceCategory(CATEGORY_NAME);
151 Service service = new Service();
152 service.setName("ForwardingPathTestingService");
153 service.setDescription("Just a comment.");
154 service.setTags(Lists.newArrayList(service.getName(), service.getComponentType().getValue() + service.getName() + "2"));
155 service.setContactId("as123y");
156 service.setIcon("MyIcon");
157 service.setProjectCode("414155");
158 service.setInstantiationType(INSTANTIATION_TYPE);
159 ArrayList<CategoryDefinition> categories = new ArrayList<>();
160 CategoryDefinition cd = new CategoryDefinition();
161 cd.setName(CATEGORY_NAME);
162 cd.setNormalizedName("abcde");
164 service.setCategories(categories);
165 service.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
169 private void createCategory() {
170 Either<CategoryDefinition, ActionStatus> category = elementDao.createCategory(categoryDefinition, NodeTypeEnum.ServiceNewCategory);
171 assertTrue(category.isLeft(), "Failed to create category");
174 private void createServiceCategory(String categoryName) {
175 GraphVertex cat = new GraphVertex(VertexTypeEnum.SERVICE_CATEGORY);
176 Map<GraphPropertyEnum, Object> metadataProperties = new HashMap<>();
177 String catId = UniqueIdBuilder.buildComponentCategoryUid(categoryName, VertexTypeEnum.SERVICE_CATEGORY);
178 cat.setUniqueId(catId);
179 metadataProperties.put(GraphPropertyEnum.UNIQUE_ID, catId);
180 metadataProperties.put(GraphPropertyEnum.LABEL, VertexTypeEnum.SERVICE_CATEGORY.getName());
181 metadataProperties.put(GraphPropertyEnum.NAME, categoryName);
182 metadataProperties.put(GraphPropertyEnum.NORMALIZED_NAME, ValidationUtils.normalizeCategoryName4Uniqueness(categoryName));
183 cat.setMetadataProperties(metadataProperties);
184 cat.updateMetadataJsonWithCurrentMetadataProperties();
186 Either<GraphVertex, JanusGraphOperationStatus> catRes = janusGraphDao.createVertex(cat);
188 assertTrue(catRes.isLeft());
191 public Service initForwardPath() {
192 ForwardingPathDataDefinition forwardingPathDataDefinition = createMockPath();
193 Service service = new Service();
194 service.setUniqueId(FORWARDING_PATH_ID);
195 assertNull(service.addForwardingPath(forwardingPathDataDefinition));
199 private ForwardingPathDataDefinition createMockPath() {
200 if (forwardingPathDataDefinition != null) {
201 return forwardingPathDataDefinition;
203 forwardingPathDataDefinition = new ForwardingPathDataDefinition("Yoyo");
204 forwardingPathDataDefinition.setUniqueId(java.util.UUID.randomUUID().toString());
205 forwardingPathDataDefinition.setDestinationPortNumber("414155");
206 forwardingPathDataDefinition.setProtocol(HTTP_PROTOCOL);
207 org.openecomp.sdc.be.datatypes.elements.ListDataDefinition<org.openecomp.sdc.be.datatypes.elements.ForwardingPathElementDataDefinition> forwardingPathElementDataDefinitionListDataDefinition = new org.openecomp.sdc.be.datatypes.elements.ListDataDefinition<>();
208 forwardingPathElementDataDefinitionListDataDefinition.add(
209 new ForwardingPathElementDataDefinition(CI_NAME_1, CI_NAME_2, CAPABILITY_NAME_1, CAPABILITY_NAME_2, "2222", "5555"));
210 forwardingPathElementDataDefinitionListDataDefinition.add(
211 new ForwardingPathElementDataDefinition(CI_NAME_2, CI_NAME_3, CAPABILITY_NAME_2, CAPABILITY_NAME_3, "4", "44"));
212 forwardingPathDataDefinition.setPathElements(forwardingPathElementDataDefinitionListDataDefinition);
213 return forwardingPathDataDefinition;
216 Service createService() {
217 Either<Service, ResponseFormat> serviceCreateResult;
218 serviceCreateResult = bl.createService(createTestService(), user);
219 assertTrue(serviceCreateResult.isLeft(), "Failed to create service");
220 return serviceCreateResult.left().value();