Revert "Delete workflow artifact
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / components / impl / InterfaceOperationBusinessLogicTest.java
1 /*
2  * Copyright © 2016-2018 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.openecomp.sdc.be.components.impl;
18
19 import fj.data.Either;
20 import org.junit.Assert;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.mockito.InjectMocks;
24 import org.mockito.Mockito;
25 import org.mockito.MockitoAnnotations;
26 import org.openecomp.sdc.ElementOperationMock;
27 import org.openecomp.sdc.be.auditing.impl.AuditingManager;
28 import org.openecomp.sdc.be.components.InterfaceOperationTestUtils;
29 import org.openecomp.sdc.be.components.impl.generic.GenericTypeBusinessLogic;
30 import org.openecomp.sdc.be.components.validation.InterfaceOperationValidation;
31 import org.openecomp.sdc.be.components.validation.UserValidations;
32 import org.openecomp.sdc.be.config.ConfigurationManager;
33 import org.openecomp.sdc.be.dao.api.ActionStatus;
34 import org.openecomp.sdc.be.dao.jsongraph.TitanDao;
35 import org.openecomp.sdc.be.dao.titan.TitanOperationStatus;
36 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
37 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
38 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
39 import org.openecomp.sdc.be.impl.ComponentsUtils;
40 import org.openecomp.sdc.be.impl.WebAppContextWrapper;
41 import org.openecomp.sdc.be.model.Component;
42 import org.openecomp.sdc.be.model.DataTypeDefinition;
43 import org.openecomp.sdc.be.model.InputDefinition;
44 import org.openecomp.sdc.be.model.InterfaceDefinition;
45 import org.openecomp.sdc.be.model.LifecycleStateEnum;
46 import org.openecomp.sdc.be.model.Operation;
47 import org.openecomp.sdc.be.model.Resource;
48 import org.openecomp.sdc.be.model.User;
49 import org.openecomp.sdc.be.model.cache.ApplicationDataTypeCache;
50 import org.openecomp.sdc.be.model.jsontitan.operations.InterfaceOperation;
51 import org.openecomp.sdc.be.model.jsontitan.operations.NodeTemplateOperation;
52 import org.openecomp.sdc.be.model.jsontitan.operations.NodeTypeOperation;
53 import org.openecomp.sdc.be.model.jsontitan.operations.TopologyTemplateOperation;
54 import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade;
55 import org.openecomp.sdc.be.model.operations.api.IElementOperation;
56 import org.openecomp.sdc.be.model.operations.api.IPropertyOperation;
57 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
58 import org.openecomp.sdc.be.model.operations.impl.GraphLockOperation;
59 import org.openecomp.sdc.be.user.Role;
60 import org.openecomp.sdc.be.user.UserBusinessLogic;
61 import org.openecomp.sdc.common.api.ConfigurationSource;
62 import org.openecomp.sdc.common.api.Constants;
63 import org.openecomp.sdc.common.impl.ExternalConfiguration;
64 import org.openecomp.sdc.common.impl.FSConfigurationSource;
65 import org.openecomp.sdc.exception.ResponseFormat;
66 import org.springframework.web.context.WebApplicationContext;
67
68 import javax.servlet.ServletContext;
69 import java.util.ArrayList;
70 import java.util.HashMap;
71 import java.util.HashSet;
72 import java.util.List;
73 import java.util.Map;
74 import java.util.Set;
75 import java.util.stream.Collectors;
76 import java.util.stream.Stream;
77
78 import static org.mockito.ArgumentMatchers.anyCollection;
79 import static org.mockito.ArgumentMatchers.anyBoolean;
80 import static org.mockito.ArgumentMatchers.anyObject;
81 import static org.mockito.ArgumentMatchers.any;
82 import static org.mockito.Matchers.anyString;
83 import static org.mockito.Matchers.eq;
84 import static org.mockito.Mockito.when;
85
86 public class InterfaceOperationBusinessLogicTest implements InterfaceOperationTestUtils{
87
88     public static final String RESOURCE_CATEGORY1 = "Network Layer 2-3";
89     public static final String RESOURCE_SUBCATEGORY = "Router";
90
91
92     private String resourceId = "resourceId1";
93     private String operationId = "uniqueId1";
94     Resource resourceUpdate;
95
96     public static final String RESOURCE_NAME = "My-Resource_Name with   space";
97
98     final ServletContext servletContext = Mockito.mock(ServletContext.class);
99     IElementOperation mockElementDao;
100     TitanDao mockTitanDao = Mockito.mock(TitanDao.class);
101     UserBusinessLogic mockUserAdmin = Mockito.mock(UserBusinessLogic.class);
102     ToscaOperationFacade toscaOperationFacade = Mockito.mock(ToscaOperationFacade.class);
103     NodeTypeOperation nodeTypeOperation = Mockito.mock(NodeTypeOperation.class);
104     NodeTemplateOperation nodeTemplateOperation = Mockito.mock(NodeTemplateOperation.class);
105     TopologyTemplateOperation topologyTemplateOperation = Mockito.mock(TopologyTemplateOperation.class);
106     final IPropertyOperation propertyOperation = Mockito.mock(IPropertyOperation.class);
107     final ApplicationDataTypeCache applicationDataTypeCache = Mockito.mock(ApplicationDataTypeCache.class);
108     WebAppContextWrapper webAppContextWrapper = Mockito.mock(WebAppContextWrapper.class);
109     UserValidations userValidations = Mockito.mock(UserValidations.class);
110     WebApplicationContext webAppContext = Mockito.mock(WebApplicationContext.class);
111
112     InterfaceOperation interfaceOperation = Mockito.mock(InterfaceOperation.class);
113     InterfaceOperationValidation operationValidator = Mockito.mock(InterfaceOperationValidation.class);
114
115     ResponseFormatManager responseManager = null;
116     GraphLockOperation graphLockOperation = Mockito.mock(GraphLockOperation.class);
117     User user = null;
118     Resource resourceResponse = null;
119     ComponentsUtils componentsUtils;
120     ArtifactsBusinessLogic artifactManager = new ArtifactsBusinessLogic();
121     private GenericTypeBusinessLogic genericTypeBusinessLogic = Mockito.mock(GenericTypeBusinessLogic.class);
122
123
124     @InjectMocks
125     InterfaceOperationBusinessLogic bl = new InterfaceOperationBusinessLogic();
126
127     @Before
128     public void setup() {
129         MockitoAnnotations.initMocks(this);
130         Mockito.reset(propertyOperation);
131
132         ExternalConfiguration.setAppName("catalog-be");
133
134         // init Configuration
135         String appConfigDir = "src/test/resources/config/catalog-be";
136         ConfigurationSource configurationSource = new FSConfigurationSource(ExternalConfiguration.getChangeListener(), appConfigDir);
137         ConfigurationManager configurationManager = new ConfigurationManager(configurationSource);
138         componentsUtils = new ComponentsUtils(Mockito.mock(AuditingManager.class));
139
140         // Elements
141         mockElementDao = new ElementOperationMock();
142
143         // User data and management
144         user = new User();
145         user.setUserId("jh0003");
146         user.setFirstName("Jimmi");
147         user.setLastName("Hendrix");
148         user.setRole(Role.ADMIN.name());
149
150         Either<User, ActionStatus> eitherGetUser = Either.left(user);
151         when(mockUserAdmin.getUser("jh0003", false)).thenReturn(eitherGetUser);
152         when(userValidations.validateUserExists(eq(user.getUserId()), anyString(), eq(false))).thenReturn(Either.left(user));
153         when(userValidations.validateUserNotEmpty(eq(user), anyString())).thenReturn(Either.left(user));
154         // Servlet Context attributes
155         when(servletContext.getAttribute(Constants.CONFIGURATION_MANAGER_ATTR)).thenReturn(configurationManager);
156         when(servletContext.getAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR)).thenReturn(webAppContextWrapper);
157         when(webAppContextWrapper.getWebAppContext(servletContext)).thenReturn(webAppContext);
158         when(webAppContext.getBean(IElementOperation.class)).thenReturn(mockElementDao);
159
160         Either<Boolean, StorageOperationStatus> eitherFalse = Either.left(true);
161         when(toscaOperationFacade.validateComponentNameExists("Root", ResourceTypeEnum.VFC, ComponentTypeEnum.RESOURCE)).thenReturn(eitherFalse);
162
163         Either<Boolean, StorageOperationStatus> eitherCountExist = Either.left(true);
164         when(toscaOperationFacade.validateComponentNameExists("alreadyExists", ResourceTypeEnum.VFC, ComponentTypeEnum.RESOURCE)).thenReturn(eitherCountExist);
165
166         Either<Boolean, StorageOperationStatus> eitherCount = Either.left(false);
167         when(toscaOperationFacade.validateComponentNameExists(eq(RESOURCE_NAME), any(ResourceTypeEnum.class), eq(ComponentTypeEnum.RESOURCE))).thenReturn(eitherCount);
168
169         Either<Boolean, StorageOperationStatus> validateDerivedExists = Either.left(true);
170         when(toscaOperationFacade.validateToscaResourceNameExists("Root")).thenReturn(validateDerivedExists);
171
172         Either<Boolean, StorageOperationStatus> validateDerivedNotExists = Either.left(false);
173         when(toscaOperationFacade.validateToscaResourceNameExists("kuku")).thenReturn(validateDerivedNotExists);
174         when(graphLockOperation.lockComponent(Mockito.anyString(), eq(NodeTypeEnum.Resource))).thenReturn(StorageOperationStatus.OK);
175         when(graphLockOperation.lockComponentByName(Mockito.anyString(), eq(NodeTypeEnum.Resource))).thenReturn(StorageOperationStatus.OK);
176
177         // createResource
178         resourceResponse = createResourceObject(true);
179         Either<Resource, StorageOperationStatus> eitherCreate = Either.left(resourceResponse);
180         Either<Integer, StorageOperationStatus> eitherValidate = Either.left(null);
181         when(toscaOperationFacade.createToscaComponent(any(Resource.class))).thenReturn(eitherCreate);
182         when(toscaOperationFacade.validateCsarUuidUniqueness(Mockito.anyString())).thenReturn(eitherValidate);
183         Map<String, DataTypeDefinition> emptyDataTypes = new HashMap<String, DataTypeDefinition>();
184         when(applicationDataTypeCache.getAll()).thenReturn(Either.left(emptyDataTypes));
185
186         //InterfaceOperation
187
188         when(operationValidator.validateInterfaceOperations(anyCollection(), anyString(), anyBoolean())).thenReturn(Either.left(true));
189         when(interfaceOperation.addInterface(anyString(), anyObject())).thenReturn(Either.left(mockInterfaceDefinitionToReturn(RESOURCE_NAME)));
190         when(interfaceOperation.updateInterface(anyString(), anyObject())).thenReturn(Either.left(mockInterfaceDefinitionToReturn(RESOURCE_NAME)));
191         when(interfaceOperation.deleteInterface(anyObject(), anyObject())).thenReturn(Either.left(new HashSet<>()));
192         when(interfaceOperation.deleteInterface(any(),any())).thenReturn(Either.left(new HashSet<>()));
193         when(interfaceOperation.updateInterface(any(),any())).thenReturn(Either.left(mockInterfaceDefinitionToReturn(RESOURCE_NAME)));
194         when(mockTitanDao.commit()).thenReturn(TitanOperationStatus.OK);
195
196         // BL object
197         artifactManager.setNodeTemplateOperation(nodeTemplateOperation);
198         bl = new InterfaceOperationBusinessLogic();
199
200         bl.setUserAdmin(mockUserAdmin);
201         bl.setComponentsUtils(componentsUtils);
202         bl.setGraphLockOperation(graphLockOperation);
203         bl.setTitanGenericDao(mockTitanDao);
204         bl.setGenericTypeBusinessLogic(genericTypeBusinessLogic);
205         toscaOperationFacade.setNodeTypeOperation(nodeTypeOperation);
206         toscaOperationFacade.setTopologyTemplateOperation(topologyTemplateOperation);
207         bl.setToscaOperationFacade(toscaOperationFacade);
208         bl.setUserValidations(userValidations);
209         bl.setInterfaceOperation(interfaceOperation);
210         bl.setInterfaceOperationValidation(operationValidator);
211         Resource resourceCsar = createResourceObjectCsar(true);
212         setCanWorkOnResource(resourceCsar);
213         Either<Component, StorageOperationStatus> oldResourceRes = Either.left(resourceCsar);
214         when(toscaOperationFacade.getToscaFullElement(resourceCsar.getUniqueId())).thenReturn(oldResourceRes);
215         responseManager = ResponseFormatManager.getInstance();
216
217     }
218
219     @Test
220     public void createInterfaceOperationTest() {
221         validateUserRoles(Role.ADMIN, Role.DESIGNER);
222         when(toscaOperationFacade.getToscaElement(resourceId)).thenReturn(Either.left(createMockResourceForAddInterface()));
223         resourceUpdate = setUpResourceMock();
224         Either<Resource, ResponseFormat> interfaceOperation = bl.createInterfaceOperation(resourceId, resourceUpdate, user, true);
225         Assert.assertTrue(interfaceOperation.isLeft());
226     }
227
228
229     @Test
230     public void updateInterfaceOperationTest() {
231         validateUserRoles(Role.ADMIN, Role.DESIGNER);
232         resourceUpdate = setUpResourceMock();
233         when(toscaOperationFacade.getToscaElement(resourceId)).thenReturn(Either.left(createResourceForInterfaceOperation()));
234         Either<Resource, ResponseFormat> interfaceOperation = bl.updateInterfaceOperation(resourceId, resourceUpdate, user, true);
235         Assert.assertTrue(interfaceOperation.isLeft());
236     }
237
238
239     @Test
240     public void deleteInterfaceOperationTest() {
241         validateUserRoles(Role.ADMIN, Role.DESIGNER);
242         when(toscaOperationFacade.getToscaElement(resourceId)).thenReturn(Either.left(createResourceForInterfaceOperation()));
243         Set<String> idsToDelete = new HashSet<>();
244         idsToDelete.add(operationId);
245
246         Either<Resource, ResponseFormat> deleteResourceResponseFormatEither = bl.deleteInterfaceOperation(resourceId, idsToDelete, user, true);
247         Assert.assertTrue(deleteResourceResponseFormatEither.isLeft());
248     }
249
250     @Test
251     public void deleteInterfaceOperationTestShouldFailWrongId() {
252         validateUserRoles(Role.ADMIN, Role.DESIGNER);
253         Set<String> idsToDelete = new HashSet<>();
254         when(toscaOperationFacade.getToscaElement(resourceId)).thenReturn(Either.left(createResourceForInterfaceOperation()));
255         idsToDelete.add(resourceId);
256         Either<Resource, ResponseFormat> deleteResourceResponseFormatEither = bl.deleteInterfaceOperation(resourceId, idsToDelete, user, true);
257         Assert.assertFalse(deleteResourceResponseFormatEither.isLeft());
258     }
259
260     @Test
261     public void interfaceOperationFailedScenarioTest() {
262         validateUserRoles(Role.ADMIN, Role.DESIGNER);
263         Resource resourceWithoutInterface = new Resource();
264         resourceWithoutInterface.setUniqueId(resourceId);
265         when(toscaOperationFacade.getToscaElement(resourceId)).thenReturn(Either.left(resourceWithoutInterface));
266         Either<Resource, ResponseFormat> interfaceOperation = bl.updateInterfaceOperation(resourceId, resourceWithoutInterface, user, true);
267         Assert.assertTrue(interfaceOperation.isRight());
268     }
269
270     private void validateUserRoles(Role... roles) {
271         List<Role> listOfRoles = Stream.of(roles).collect(Collectors.toList());
272         when(userValidations.validateUserRole(user, listOfRoles)).thenReturn(Either.left(true));
273     }
274     private Resource createMockResourceForAddInterface () {
275         Resource resource = new Resource();
276         resource.setUniqueId(resourceId);
277         resource.setName(RESOURCE_NAME);
278         resource.addCategory(RESOURCE_CATEGORY1, RESOURCE_SUBCATEGORY);
279         resource.setDescription("My short description");
280
281         Map<String, Operation> operationMap = new HashMap<>();
282         Map<String, InterfaceDefinition> interfaceDefinitionMap = new HashMap<>();
283         interfaceDefinitionMap.put("int1", createInterface("int1", "Interface 1",
284                 "lifecycle", "org.openecomp.interfaces.node.lifecycle." + RESOURCE_NAME, operationMap));
285         resource.setInterfaces(interfaceDefinitionMap);
286         List<InputDefinition> inputDefinitionList = new ArrayList<>();
287         inputDefinitionList.add(createInputDefinition("uniqueId1"));
288         resource.setInputs(inputDefinitionList);
289
290         return  resource;
291     }
292
293     private Resource setCanWorkOnResource(Resource resource) {
294         resource.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
295         resource.setLastUpdaterUserId(user.getUserId());
296         return resource;
297     }
298
299     private Resource setUpResourceMock(){
300         Resource resource = new Resource();
301         resource.setUniqueId(resourceId);
302         resource.setName(RESOURCE_NAME);
303         resource.addCategory(RESOURCE_CATEGORY1, RESOURCE_SUBCATEGORY);
304         resource.setDescription("My short description");
305         resource.setInterfaces(createMockInterfaceDefinition(RESOURCE_NAME));
306
307         List<InputDefinition> inputDefinitionList = new ArrayList<>();
308         inputDefinitionList.add(createInputDefinition("uniqueId1"));
309         resource.setInputs(inputDefinitionList);
310
311         return  resource;
312     }
313
314     private InputDefinition createInputDefinition(String inputId) {
315         InputDefinition inputDefinition = new InputDefinition();
316         inputDefinition.setInputId(inputId);
317         inputDefinition.setDescription("Input Description");
318
319         return  inputDefinition;
320
321     }
322     private Resource createResourceForInterfaceOperation() {
323         Resource resource = new Resource();
324         resource.setUniqueId(resourceId);
325         resource.setName(RESOURCE_NAME);
326         resource.addCategory(RESOURCE_CATEGORY1, RESOURCE_SUBCATEGORY);
327         resource.setDescription("Resource name for response");
328         resource.setInterfaces(createMockInterfaceDefinition(RESOURCE_NAME));
329         return resource;
330     }
331
332     private Resource createResourceObjectCsar(boolean afterCreate) {
333         Resource resource = new Resource();
334         resource.setName(RESOURCE_NAME);
335         resource.addCategory(RESOURCE_CATEGORY1, RESOURCE_SUBCATEGORY);
336         resource.setDescription("My short description");
337         List<String> tgs = new ArrayList<String>();
338         tgs.add("test");
339         tgs.add(resource.getName());
340         resource.setTags(tgs);
341         List<String> template = new ArrayList<String>();
342         template.add("Root");
343         resource.setDerivedFrom(template);
344         resource.setVendorName("Motorola");
345         resource.setVendorRelease("1.0.0");
346         resource.setResourceVendorModelNumber("");
347         resource.setContactId("ya5467");
348         resource.setIcon("MyIcon");
349         resource.setCsarUUID("valid_vf.csar");
350         resource.setCsarVersion("1");
351
352         if (afterCreate) {
353             resource.setName(resource.getName());
354             resource.setVersion("0.1");
355
356             resource.setUniqueId(resource.getName().toLowerCase() + ":" + resource.getVersion());
357             resource.setCreatorUserId(user.getUserId());
358             resource.setCreatorFullName(user.getFirstName() + " " + user.getLastName());
359             resource.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
360         }
361         return resource;
362     }
363
364     private Resource createResourceObject(boolean afterCreate) {
365         Resource resource = new Resource();
366         resource.setName(RESOURCE_NAME);
367         resource.addCategory(RESOURCE_CATEGORY1, RESOURCE_SUBCATEGORY);
368         resource.setDescription("My short description");
369         List<String> tgs = new ArrayList<String>();
370         tgs.add("test");
371         tgs.add(resource.getName());
372         resource.setTags(tgs);
373         List<String> template = new ArrayList<String>();
374         template.add("Root");
375         resource.setDerivedFrom(template);
376         resource.setVendorName("Motorola");
377         resource.setVendorRelease("1.0.0");
378         resource.setContactId("ya5467");
379         resource.setIcon("MyIcon");
380
381         if (afterCreate) {
382             resource.setName(resource.getName());
383             resource.setVersion("0.1");
384             resource.setUniqueId(resource.getName().toLowerCase() + ":" + resource.getVersion());
385             resource.setCreatorUserId(user.getUserId());
386             resource.setCreatorFullName(user.getFirstName() + " " + user.getLastName());
387             resource.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
388         }
389         return resource;
390     }
391
392
393
394 }