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