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