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