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