b9e0b594ea05b9855c984667f92bfb8f06525c07
[sdc.git] /
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.test.utils.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.jsontitan.utils.InterfaceUtils;
47 import org.openecomp.sdc.be.model.operations.api.IElementOperation;
48 import org.openecomp.sdc.be.model.operations.api.IPropertyOperation;
49 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
50 import org.openecomp.sdc.be.model.operations.impl.GraphLockOperation;
51 import org.openecomp.sdc.be.user.Role;
52 import org.openecomp.sdc.be.user.UserBusinessLogic;
53 import org.openecomp.sdc.common.api.ConfigurationSource;
54 import org.openecomp.sdc.common.api.Constants;
55 import org.openecomp.sdc.common.impl.ExternalConfiguration;
56 import org.openecomp.sdc.common.impl.FSConfigurationSource;
57 import org.openecomp.sdc.exception.ResponseFormat;
58 import org.springframework.web.context.WebApplicationContext;
59
60 import javax.servlet.ServletContext;
61 import java.util.*;
62 import java.util.stream.Collectors;
63 import java.util.stream.Stream;
64
65 import static org.mockito.ArgumentMatchers.*;
66 import static org.mockito.Mockito.when;
67
68 public class InterfaceOperationBusinessLogicTest {
69
70     public static final String RESOURCE_CATEGORY1 = "Network Layer 2-3";
71     public static final String RESOURCE_SUBCATEGORY = "Router";
72
73     private String resourceId = "resourceId1";
74     private String operationId = "uniqueId1";
75     Resource resourceUpdate;
76     Operation operation;
77
78     public static final String RESOURCE_NAME = "My-Resource_Name with   space";
79
80     final ServletContext servletContext = Mockito.mock(ServletContext.class);
81     IElementOperation mockElementDao;
82     TitanDao mockTitanDao = Mockito.mock(TitanDao.class);
83     UserBusinessLogic mockUserAdmin = Mockito.mock(UserBusinessLogic.class);
84     ToscaOperationFacade toscaOperationFacade = Mockito.mock(ToscaOperationFacade.class);
85     NodeTypeOperation nodeTypeOperation = Mockito.mock(NodeTypeOperation.class);
86     NodeTemplateOperation nodeTemplateOperation = Mockito.mock(NodeTemplateOperation.class);
87     TopologyTemplateOperation topologyTemplateOperation = Mockito.mock(TopologyTemplateOperation.class);
88     final IPropertyOperation propertyOperation = Mockito.mock(IPropertyOperation.class);
89     final ApplicationDataTypeCache applicationDataTypeCache = Mockito.mock(ApplicationDataTypeCache.class);
90     WebAppContextWrapper webAppContextWrapper = Mockito.mock(WebAppContextWrapper.class);
91     UserValidations userValidations = Mockito.mock(UserValidations.class);
92     WebApplicationContext webAppContext = Mockito.mock(WebApplicationContext.class);
93     ArtifactCassandraDao artifactCassandraDao = Mockito.mock(ArtifactCassandraDao.class);
94     InterfaceOperation interfaceOperation = Mockito.mock(InterfaceOperation.class);
95     InterfaceOperationValidation operationValidator = Mockito.mock(InterfaceOperationValidation.class);
96
97     ResponseFormatManager responseManager = null;
98     GraphLockOperation graphLockOperation = Mockito.mock(GraphLockOperation.class);
99     User user = null;
100     Resource resourceResponse = null;
101     ComponentsUtils componentsUtils;
102     ArtifactsBusinessLogic artifactManager = new ArtifactsBusinessLogic();
103     private GenericTypeBusinessLogic genericTypeBusinessLogic = Mockito.mock(GenericTypeBusinessLogic.class);
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         when(operationValidator.validateInterfaceOperations(anyCollection(), anyString(), anyBoolean())).thenReturn(Either.left(true));
170         when(interfaceOperation.addInterface(anyString(), anyObject())).thenReturn(Either.left(InterfaceOperationTestUtils.mockInterfaceDefinitionToReturn(RESOURCE_NAME)));
171         when(interfaceOperation.updateInterface(anyString(), anyObject())).thenReturn(Either.left(InterfaceOperationTestUtils.mockInterfaceDefinitionToReturn(RESOURCE_NAME)));
172         when(interfaceOperation.addInterfaceOperation(anyObject(), anyObject(), anyObject())).thenReturn(Either.left(InterfaceOperationTestUtils.mockOperationToReturn(RESOURCE_NAME)));
173         when(interfaceOperation.updateInterfaceOperation(anyObject(), anyObject(), anyObject())).thenReturn(Either.left(InterfaceOperationTestUtils.mockOperationToReturn(RESOURCE_NAME)));
174         when(interfaceOperation.getInterfaceOperation(anyObject(), anyObject())).thenReturn(Either.left(InterfaceOperationTestUtils.mockOperationToReturn(RESOURCE_NAME)));
175         when(interfaceOperation.deleteInterfaceOperation(anyObject(), anyObject(), anyObject())).thenReturn(Either.left(InterfaceOperationTestUtils.mockOperationToReturn(RESOURCE_NAME)));
176         when(interfaceOperation.deleteInterfaceOperation(any(),any(), any())).thenReturn(Either.left(InterfaceOperationTestUtils.mockOperationToReturn(RESOURCE_NAME)));
177         when(interfaceOperation.updateInterface(any(),any())).thenReturn(Either.left(InterfaceOperationTestUtils.mockInterfaceDefinitionToReturn(RESOURCE_NAME)));
178         when(mockTitanDao.commit()).thenReturn(TitanOperationStatus.OK);
179
180         // BL object
181         artifactManager.setNodeTemplateOperation(nodeTemplateOperation);
182         bl = new InterfaceOperationBusinessLogic();
183
184         bl.setUserAdmin(mockUserAdmin);
185         bl.setComponentsUtils(componentsUtils);
186         bl.setGraphLockOperation(graphLockOperation);
187         bl.setTitanGenericDao(mockTitanDao);
188         bl.setGenericTypeBusinessLogic(genericTypeBusinessLogic);
189         toscaOperationFacade.setNodeTypeOperation(nodeTypeOperation);
190         toscaOperationFacade.setTopologyTemplateOperation(topologyTemplateOperation);
191         bl.setToscaOperationFacade(toscaOperationFacade);
192         bl.setUserValidations(userValidations);
193         bl.setInterfaceOperation(interfaceOperation);
194         bl.setInterfaceOperationValidation(operationValidator);
195         Resource resourceCsar = createResourceObjectCsar(true);
196         setCanWorkOnResource(resourceCsar);
197         Either<Component, StorageOperationStatus> oldResourceRes = Either.left(resourceCsar);
198         when(toscaOperationFacade.getToscaFullElement(resourceCsar.getUniqueId())).thenReturn(oldResourceRes);
199         responseManager = ResponseFormatManager.getInstance();
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         operation = InterfaceOperationTestUtils.createMockOperation();
209         Either<Operation, ResponseFormat> interfaceOperation = bl.createInterfaceOperation(resourceId, operation, user, true);
210         Assert.assertTrue(interfaceOperation.isLeft());
211         Assert.assertNotNull(interfaceOperation.left().value().getWorkflowId());
212         Assert.assertNotNull(interfaceOperation.left().value().getWorkflowVersionId());
213     }
214
215     @Test
216     public void updateInterfaceOperationTest() {
217         validateUserRoles(Role.ADMIN, Role.DESIGNER);
218         operation = InterfaceOperationTestUtils.createMockOperation();
219         Resource resource = createResourceForInterfaceOperation();
220         resource.setComponentType(ComponentTypeEnum.RESOURCE);
221         when(toscaOperationFacade.getToscaElement(resourceId)).thenReturn(Either.left(resource));
222         Either<Operation, ResponseFormat> interfaceOperation = bl.updateInterfaceOperation(resourceId, operation, user, true);
223         Assert.assertTrue(interfaceOperation.isLeft());
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         Either<Operation, ResponseFormat> deleteResourceResponseFormatEither = bl.deleteInterfaceOperation(resourceId, operationId, user, true);
234         Assert.assertTrue(deleteResourceResponseFormatEither.isLeft());
235     }
236
237     @Test
238     public void getInterfaceOperationTest() {
239         Resource resource = createResourceForInterfaceOperation();
240         resource.setComponentType(ComponentTypeEnum.RESOURCE);
241         validateUserRoles(Role.ADMIN, Role.DESIGNER);
242         when(toscaOperationFacade.getToscaElement(resourceId)).thenReturn(Either.left(resource));
243         Either<Operation, ResponseFormat> getResourceResponseFormatEither = bl.getInterfaceOperation(resourceId, operationId, user, true);
244         Assert.assertTrue(getResourceResponseFormatEither.isLeft());
245     }
246
247     private void validateUserRoles(Role... roles) {
248         List<Role> listOfRoles = Stream.of(roles).collect(Collectors.toList());
249      }
250
251     private Resource setCanWorkOnResource(Resource resource) {
252         resource.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
253         resource.setLastUpdaterUserId(user.getUserId());
254         return resource;
255     }
256
257     private Resource setUpResourceMock(){
258         Resource resource = new Resource();
259         resource.setUniqueId(resourceId);
260         resource.setName(RESOURCE_NAME);
261         resource.addCategory(RESOURCE_CATEGORY1, RESOURCE_SUBCATEGORY);
262         resource.setDescription("My short description");
263         resource.setInterfaces(InterfaceOperationTestUtils.createMockInterfaceDefinition(RESOURCE_NAME));
264
265         List<InputDefinition> inputDefinitionList = new ArrayList<>();
266         inputDefinitionList.add(createInputDefinition("uniqueId1"));
267         resource.setInputs(inputDefinitionList);
268
269         return  resource;
270     }
271
272     private InputDefinition createInputDefinition(String inputId) {
273         InputDefinition inputDefinition = new InputDefinition();
274         inputDefinition.setInputId(inputId);
275         inputDefinition.setDescription("Input Description");
276
277         return  inputDefinition;
278
279     }
280     private Resource createResourceForInterfaceOperation() {
281         Resource resource = new Resource();
282         resource.setUniqueId(resourceId);
283         resource.setName(RESOURCE_NAME);
284         resource.addCategory(RESOURCE_CATEGORY1, RESOURCE_SUBCATEGORY);
285         resource.setDescription("Resource name for response");
286         resource.setInterfaces(InterfaceOperationTestUtils.createMockInterfaceDefinition(RESOURCE_NAME));
287         return resource;
288     }
289
290     private Resource createResourceObjectCsar(boolean afterCreate) {
291         Resource resource = new Resource();
292         resource.setName(RESOURCE_NAME);
293         resource.addCategory(RESOURCE_CATEGORY1, RESOURCE_SUBCATEGORY);
294         resource.setDescription("My short description");
295         List<String> tgs = new ArrayList<String>();
296         tgs.add("test");
297         tgs.add(resource.getName());
298         resource.setTags(tgs);
299         List<String> template = new ArrayList<String>();
300         template.add("Root");
301         resource.setDerivedFrom(template);
302         resource.setVendorName("Motorola");
303         resource.setVendorRelease("1.0.0");
304         resource.setResourceVendorModelNumber("");
305         resource.setContactId("ya5467");
306         resource.setIcon("MyIcon");
307         resource.setCsarUUID("valid_vf.csar");
308         resource.setCsarVersion("1");
309
310         if (afterCreate) {
311             resource.setName(resource.getName());
312             resource.setVersion("0.1");
313
314             resource.setUniqueId(resource.getName().toLowerCase() + ":" + resource.getVersion());
315             resource.setCreatorUserId(user.getUserId());
316             resource.setCreatorFullName(user.getFirstName() + " " + user.getLastName());
317             resource.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
318         }
319         return resource;
320     }
321
322     private Resource createResourceObject(boolean afterCreate) {
323         Resource resource = new Resource();
324         resource.setName(RESOURCE_NAME);
325         resource.addCategory(RESOURCE_CATEGORY1, RESOURCE_SUBCATEGORY);
326         resource.setDescription("My short description");
327         List<String> tgs = new ArrayList<String>();
328         tgs.add("test");
329         tgs.add(resource.getName());
330         resource.setTags(tgs);
331         List<String> template = new ArrayList<String>();
332         template.add("Root");
333         resource.setDerivedFrom(template);
334         resource.setVendorName("Motorola");
335         resource.setVendorRelease("1.0.0");
336         resource.setContactId("ya5467");
337         resource.setIcon("MyIcon");
338
339         if (afterCreate) {
340             resource.setName(resource.getName());
341             resource.setVersion("0.1");
342             resource.setUniqueId(resource.getName().toLowerCase() + ":" + resource.getVersion());
343             resource.setCreatorUserId(user.getUserId());
344             resource.setCreatorFullName(user.getFirstName() + " " + user.getLastName());
345             resource.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
346         }
347         return resource;
348     }
349
350
351
352 }