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