2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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=========================================================
19 * Modifications copyright (c) 2019 Nokia
20 * ================================================================================
22 package org.openecomp.sdc.be.components;
24 import static org.mockito.ArgumentMatchers.any;
25 import static org.mockito.Mockito.when;
27 import com.google.common.collect.Sets;
28 import fj.data.Either;
29 import java.util.ArrayList;
30 import java.util.HashMap;
31 import java.util.List;
33 import javax.servlet.ServletContext;
34 import org.junit.Before;
35 import org.mockito.Mockito;
36 import org.openecomp.sdc.ElementOperationMock;
37 import org.openecomp.sdc.be.auditing.impl.AuditingManager;
38 import org.openecomp.sdc.be.components.distribution.engine.IDistributionEngine;
39 import org.openecomp.sdc.be.components.impl.ArtifactsBusinessLogic;
40 import org.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic;
41 import org.openecomp.sdc.be.components.impl.ResponseFormatManager;
42 import org.openecomp.sdc.be.components.impl.ServiceBusinessLogic;
43 import org.openecomp.sdc.be.components.impl.generic.GenericTypeBusinessLogic;
44 import org.openecomp.sdc.be.components.path.ForwardingPathValidator;
45 import org.openecomp.sdc.be.components.utils.ComponentBusinessLogicMock;
46 import org.openecomp.sdc.be.components.validation.ServiceDistributionValidation;
47 import org.openecomp.sdc.be.dao.api.ActionStatus;
48 import org.openecomp.sdc.be.dao.cassandra.AuditCassandraDao;
49 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphDao;
50 import org.openecomp.sdc.be.datamodel.utils.UiComponentDataConverter;
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.ComponentTypeEnum;
54 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
55 import org.openecomp.sdc.be.impl.ComponentsUtils;
56 import org.openecomp.sdc.be.impl.WebAppContextWrapper;
57 import org.openecomp.sdc.be.model.ArtifactDefinition;
58 import org.openecomp.sdc.be.model.Component;
59 import org.openecomp.sdc.be.model.ComponentInstance;
60 import org.openecomp.sdc.be.model.GroupInstance;
61 import org.openecomp.sdc.be.model.Resource;
62 import org.openecomp.sdc.be.model.Service;
63 import org.openecomp.sdc.be.model.User;
64 import org.openecomp.sdc.be.model.category.CategoryDefinition;
65 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ForwardingPathOperation;
66 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
67 import org.openecomp.sdc.be.model.operations.api.IElementOperation;
68 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
69 import org.openecomp.sdc.be.model.operations.impl.GraphLockOperation;
70 import org.openecomp.sdc.be.resources.data.auditing.ResourceAdminEvent;
71 import org.openecomp.sdc.be.user.Role;
72 import org.openecomp.sdc.be.user.UserBusinessLogic;
73 import org.openecomp.sdc.common.api.ArtifactGroupTypeEnum;
74 import org.openecomp.sdc.common.api.Constants;
75 import org.openecomp.sdc.common.datastructure.AuditingFieldsKey;
76 import org.openecomp.sdc.common.impl.ExternalConfiguration;
77 import org.springframework.web.context.WebApplicationContext;
79 public abstract class BaseServiceBusinessLogicTest extends ComponentBusinessLogicMock {
80 private static final String SERVICE_CATEGORY = "Mobility";
81 private final ServletContext servletContext = Mockito.mock(ServletContext.class);
82 private UserBusinessLogic mockUserAdmin = Mockito.mock(UserBusinessLogic.class);
83 private WebAppContextWrapper webAppContextWrapper = Mockito.mock(WebAppContextWrapper.class);
84 private WebApplicationContext webAppContext = Mockito.mock(WebApplicationContext.class);
85 protected final IDistributionEngine distributionEngine = Mockito.mock(IDistributionEngine.class);
86 protected final ComponentInstanceBusinessLogic componentInstanceBusinessLogic = Mockito.mock(ComponentInstanceBusinessLogic.class);
87 protected final ServiceDistributionValidation serviceDistributionValidation = Mockito.mock(ServiceDistributionValidation.class);
88 protected final ForwardingPathValidator forwardingPathValidator = Mockito.mock(ForwardingPathValidator.class);
89 protected final UiComponentDataConverter uiComponentDataConverter = Mockito.mock(UiComponentDataConverter.class);
90 private ServiceBusinessLogic bl;
91 private ResponseFormatManager responseManager = null;
92 private IElementOperation mockElementDao;
93 private ComponentsUtils componentsUtils;
94 private AuditCassandraDao auditingDao = Mockito.mock(AuditCassandraDao.class);
95 private ArtifactsBusinessLogic artifactBl = Mockito.mock(ArtifactsBusinessLogic.class);
96 private GraphLockOperation graphLockOperation = Mockito.mock(GraphLockOperation.class);
97 private JanusGraphDao mockJanusGraphDao = Mockito.mock(JanusGraphDao.class);
98 private ToscaOperationFacade toscaOperationFacade = Mockito.mock(ToscaOperationFacade.class);
99 private GenericTypeBusinessLogic genericTypeBusinessLogic = Mockito.mock(GenericTypeBusinessLogic.class);
100 private ForwardingPathOperation forwardingPathOperation = Mockito.mock(ForwardingPathOperation.class);
103 Service serviceResponse = null;
104 Resource genericService = null;
106 protected static final String CERTIFIED_VERSION = "1.0";
107 protected static final String UNCERTIFIED_VERSION = "0.2";
108 protected static final String COMPONNET_ID = "myUniqueId";
109 protected static final String GENERIC_SERVICE_NAME = "org.openecomp.resource.abstract.nodes.service";
110 protected static Map<AuditingFieldsKey, Object> FILTER_MAP_CERTIFIED_VERSION = new HashMap<>();
111 protected static Map<AuditingFieldsKey, Object> FILTER_MAP_UNCERTIFIED_VERSION_CURR = new HashMap<>();
112 protected static Map<AuditingFieldsKey, Object> FILTER_MAP_UNCERTIFIED_VERSION_PREV = new HashMap<>();
114 public void setup() {
116 ExternalConfiguration.setAppName("catalog-be");
117 componentsUtils = new ComponentsUtils(Mockito.mock(AuditingManager.class));
120 mockElementDao = new ElementOperationMock();
122 // User data and management
124 user.setUserId("jh0003");
125 user.setFirstName("Jimmi");
126 user.setLastName("Hendrix");
127 user.setRole(Role.ADMIN.name());
129 when(mockUserAdmin.getUser("jh0003", false)).thenReturn(user);
131 // Servlet Context attributes
132 when(servletContext.getAttribute(Constants.CONFIGURATION_MANAGER_ATTR)).thenReturn(configurationManager);
133 // when(servletContext.getAttribute(Constants.SERVICE_OPERATION_MANAGER)).thenReturn(new ServiceOperation());
134 when(servletContext.getAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR)).thenReturn(webAppContextWrapper);
135 when(webAppContextWrapper.getWebAppContext(servletContext)).thenReturn(webAppContext);
136 when(webAppContext.getBean(IElementOperation.class)).thenReturn(mockElementDao);
137 when(graphLockOperation.lockComponent(Mockito.anyString(), Mockito.eq(NodeTypeEnum.Service))).thenReturn(StorageOperationStatus.OK);
138 when(graphLockOperation.lockComponentByName(Mockito.anyString(), Mockito.eq(NodeTypeEnum.Service))).thenReturn(StorageOperationStatus.OK);
140 // artifact bussinesslogic
141 ArtifactDefinition artifactDef = new ArtifactDefinition();
142 when(artifactBl.createArtifactPlaceHolderInfo(Mockito.anyString(), Mockito.anyString(), Mockito.anyMap(), Mockito.any(User.class), Mockito.any(ArtifactGroupTypeEnum.class))).thenReturn(artifactDef);
145 serviceResponse = createServiceObject(true);
146 Either<Component, StorageOperationStatus> eitherCreate = Either.left(serviceResponse);
147 when(toscaOperationFacade.createToscaComponent(Mockito.any(Component.class))).thenReturn(eitherCreate);
148 Either<Boolean, StorageOperationStatus> eitherCount = Either.left(false);
149 when(toscaOperationFacade.validateComponentNameExists("Service", null, ComponentTypeEnum.SERVICE)).thenReturn(eitherCount);
150 Either<Boolean, StorageOperationStatus> eitherCountExist = Either.left(true);
151 when(toscaOperationFacade.validateComponentNameExists("alreadyExist", null, ComponentTypeEnum.SERVICE)).thenReturn(eitherCountExist);
153 genericService = setupGenericServiceMock();
154 Either<Resource, StorageOperationStatus> findLatestGeneric = Either.left(genericService);
155 when(toscaOperationFacade.getLatestCertifiedNodeTypeByToscaResourceName(GENERIC_SERVICE_NAME)).thenReturn(findLatestGeneric);
158 when(forwardingPathOperation.addForwardingPath(any(),any())).thenReturn(Either.left(createMockPath()));
159 when(forwardingPathOperation.updateForwardingPath(any(),any())).thenReturn(Either.left(createMockPath()));
160 when(forwardingPathOperation.deleteForwardingPath(any(),any())).thenReturn(Either.left(Sets.newHashSet("Wow-It-Works")));
161 when(toscaOperationFacade.getToscaElement("delete_forward_test")).thenReturn(Either.left(createServiceObject(true)));
163 bl = new ServiceBusinessLogic(elementDao, groupOperation, groupInstanceOperation,
164 groupTypeOperation, groupBusinessLogic, interfaceOperation, interfaceLifecycleTypeOperation,
165 artifactsBusinessLogic, distributionEngine, componentInstanceBusinessLogic,
166 serviceDistributionValidation, forwardingPathValidator, uiComponentDataConverter,
167 artifactToscaOperation, componentContactIdValidator, componentNameValidator,
168 componentTagsValidator, componentValidator, componentIconValidator, componentProjectCodeValidator, componentDescriptionValidator);
169 bl.setUserAdmin(mockUserAdmin);
170 bl.setGraphLockOperation(graphLockOperation);
171 bl.setJanusGraphDao(mockJanusGraphDao);
172 bl.setToscaOperationFacade(toscaOperationFacade);
173 bl.setGenericTypeBusinessLogic(genericTypeBusinessLogic);
174 bl.setComponentsUtils(componentsUtils);
175 bl.setCassandraAuditingDao(auditingDao);
176 bl.setForwardingPathOperation(forwardingPathOperation);
177 bl.setToscaOperationFacade(toscaOperationFacade);
178 mockAuditingDaoLogic();
180 responseManager = ResponseFormatManager.getInstance();
184 protected Component createNewService() {
186 Service service = new Service();
188 service.setName("serviceName");
189 service.setUniqueId("serviceUniqueId");
190 List<ComponentInstance> componentInstances = new ArrayList<>();
191 ComponentInstance ci;
192 for(int i= 0; i<listSize; ++i){
193 ci = new ComponentInstance();
194 ci.setName("ciName" + i);
195 ci.setUniqueId("ciId" + i);
196 List<GroupInstance> groupInstances= new ArrayList<>();
198 for(int j = 0; j<listSize; ++j){
199 gi = new GroupInstance();
200 gi.setName(ci.getName( )+ "giName" + j);
201 gi.setUniqueId(ci.getName() + "giId" + j);
202 groupInstances.add(gi);
204 ci.setGroupInstances(groupInstances);
205 componentInstances.add(ci);
207 service.setComponentInstances(componentInstances);
211 private void mockAuditingDaoLogic() {
212 FILTER_MAP_CERTIFIED_VERSION.put(AuditingFieldsKey.AUDIT_SERVICE_INSTANCE_ID, COMPONNET_ID);
213 FILTER_MAP_UNCERTIFIED_VERSION_CURR.put(AuditingFieldsKey.AUDIT_SERVICE_INSTANCE_ID, COMPONNET_ID);
214 FILTER_MAP_UNCERTIFIED_VERSION_PREV.put(AuditingFieldsKey.AUDIT_SERVICE_INSTANCE_ID, COMPONNET_ID);
216 FILTER_MAP_UNCERTIFIED_VERSION_CURR.put(AuditingFieldsKey.AUDIT_RESOURCE_CURR_VERSION, UNCERTIFIED_VERSION);
217 FILTER_MAP_UNCERTIFIED_VERSION_PREV.put(AuditingFieldsKey.AUDIT_RESOURCE_PREV_VERSION, UNCERTIFIED_VERSION);
219 final ResourceAdminEvent createResourceAudit = new ResourceAdminEvent();
220 createResourceAudit.setModifier("Carlos Santana(cs0008)");
221 createResourceAudit.setCurrState("NOT_CERTIFIED_CHECKOUT");
222 createResourceAudit.setCurrVersion("0.1");
223 createResourceAudit.setServiceInstanceId("82eddd99-0bd9-4742-ab0a-1bdb5e262a05");
224 createResourceAudit.setRequestId("3e65cea1-7403-4bc7-b461-e2544d83799f");
225 createResourceAudit.setDesc("OK");
226 createResourceAudit.setResourceType("Resource");
227 createResourceAudit.setStatus("201");
228 createResourceAudit.setPrevVersion("");
229 createResourceAudit.setAction("Create");
230 // fields.put("TIMESTAMP", "2015-11-22 09:19:12.977");
231 createResourceAudit.setPrevState("");
232 createResourceAudit.setResourceName("MyTestResource");
233 // createResourceAudit.setFields(fields);
235 final ResourceAdminEvent checkInResourceAudit = new ResourceAdminEvent();
236 checkInResourceAudit.setModifier("Carlos Santana(cs0008)");
237 checkInResourceAudit.setCurrState("NOT_CERTIFIED_CHECKIN");
238 checkInResourceAudit.setCurrVersion("0.1");
239 checkInResourceAudit.setServiceInstanceId("82eddd99-0bd9-4742-ab0a-1bdb5e262a05");
240 checkInResourceAudit.setRequestId("ffacbf5d-eeb1-43c6-a310-37fe7e1cc091");
241 checkInResourceAudit.setDesc("OK");
242 checkInResourceAudit.setComment("Stam");
243 checkInResourceAudit.setResourceType("Resource");
244 checkInResourceAudit.setStatus("200");
245 checkInResourceAudit.setPrevVersion("0.1");
246 checkInResourceAudit.setAction("Checkin");
247 // fields.put("TIMESTAMP", "2015-11-22 09:25:03.797");
248 checkInResourceAudit.setPrevState("NOT_CERTIFIED_CHECKOUT");
249 checkInResourceAudit.setResourceName("MyTestResource");
251 final ResourceAdminEvent checkOutResourceAudit = new ResourceAdminEvent();
252 checkOutResourceAudit.setModifier("Carlos Santana(cs0008)");
253 checkOutResourceAudit.setCurrState("NOT_CERTIFIED_CHECKOUT");
254 checkOutResourceAudit.setCurrVersion("0.2");
255 checkOutResourceAudit.setServiceInstanceId("82eddd99-0bd9-4742-ab0a-1bdb5e262a05");
256 checkOutResourceAudit.setRequestId("7add5078-4c16-4d74-9691-cc150e3c96b8");
257 checkOutResourceAudit.setDesc("OK");
258 checkOutResourceAudit.setComment("");
259 checkOutResourceAudit.setResourceType("Resource");
260 checkOutResourceAudit.setStatus("200");
261 checkOutResourceAudit.setPrevVersion("0.1");
262 checkOutResourceAudit.setAction("Checkout");
263 // fields.put("TIMESTAMP", "2015-11-22 09:39:41.024");
264 checkOutResourceAudit.setPrevState("NOT_CERTIFIED_CHECKIN");
265 checkOutResourceAudit.setResourceName("MyTestResource");
266 // checkOutResourceAudit.setFields(fields);
268 List<ResourceAdminEvent> list = new ArrayList<ResourceAdminEvent>() {
270 add(createResourceAudit);
271 add(checkInResourceAudit);
272 add(checkOutResourceAudit);
275 Either<List<ResourceAdminEvent>, ActionStatus> result = Either.left(list);
276 Mockito.when(auditingDao.getByServiceInstanceId(Mockito.anyString())).thenReturn(result);
278 List<ResourceAdminEvent> listPrev = new ArrayList<>();
279 Either<List<ResourceAdminEvent>, ActionStatus> resultPrev = Either.left(listPrev);
280 Mockito.when(auditingDao.getAuditByServiceIdAndPrevVersion(Mockito.anyString(), Mockito.anyString())).thenReturn(resultPrev);
282 List<ResourceAdminEvent> listCurr = new ArrayList<ResourceAdminEvent>() {
284 add(checkOutResourceAudit);
287 Either<List<ResourceAdminEvent>, ActionStatus> resultCurr = Either.left(listCurr);
288 Mockito.when(auditingDao.getAuditByServiceIdAndCurrVersion(Mockito.anyString(), Mockito.anyString())).thenReturn(resultCurr);
292 protected Service createServiceObject(boolean afterCreate) {
293 Service service = new Service();
294 service.setName("Service");
295 CategoryDefinition category = new CategoryDefinition();
296 category.setName(SERVICE_CATEGORY);
297 List<CategoryDefinition> categories = new ArrayList<>();
298 categories.add(category);
299 service.setCategories(categories);
301 service.setDescription("description");
302 List<String> tgs = new ArrayList<>();
303 tgs.add(service.getName());
304 service.setTags(tgs);
305 // service.setVendorName("Motorola");
306 // service.setVendorRelease("1.0.0");
307 service.setIcon("MyIcon");
308 // service.setState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
309 service.setContactId("aa1234");
310 service.setProjectCode("12345");
313 service.setVersion("0.1");
314 service.setUniqueId(service.getName() + ":" + service.getVersion());
315 service.setCreatorUserId(user.getUserId());
316 service.setCreatorFullName(user.getFirstName() + " " + user.getLastName());
321 protected Resource setupGenericServiceMock(){
322 Resource genericService = new Resource();
323 genericService.setVersion("1.0");
324 genericService.setToscaResourceName(GENERIC_SERVICE_NAME);
325 return genericService;
328 protected static ForwardingPathDataDefinition forwardingPathDataDefinition;
330 protected static ForwardingPathDataDefinition createMockPath() {
331 if (forwardingPathDataDefinition != null){
332 return forwardingPathDataDefinition ;
334 forwardingPathDataDefinition = new ForwardingPathDataDefinition("Yoyo");
335 forwardingPathDataDefinition.setUniqueId(java.util.UUID.randomUUID().toString());
336 forwardingPathDataDefinition.setDestinationPortNumber("414155");
337 forwardingPathDataDefinition.setProtocol("http");
338 org.openecomp.sdc.be.datatypes.elements.ListDataDefinition<org.openecomp.sdc.be.datatypes.elements.ForwardingPathElementDataDefinition> forwardingPathElementDataDefinitionListDataDefinition = new org.openecomp.sdc.be.datatypes.elements.ListDataDefinition<>();
339 forwardingPathElementDataDefinitionListDataDefinition.add(new ForwardingPathElementDataDefinition("fromNode","toNode", "333","444","2222","5555"));
340 forwardingPathElementDataDefinitionListDataDefinition.add(new ForwardingPathElementDataDefinition("toNode","toNode2", "4444","44444","4","44"));
341 forwardingPathDataDefinition.setPathElements(forwardingPathElementDataDefinitionListDataDefinition);
342 return forwardingPathDataDefinition;