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