Upgrade SDC from Titan to Janus Graph
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / components / impl / ProductBusinessLogicTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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  * ================================================================================
21  */
22 package org.openecomp.sdc.be.components.impl;
23
24 import java.util.ArrayList;
25 import java.util.List;
26 import java.util.Map;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.mockito.InjectMocks;
30 import org.mockito.Mock;
31 import org.mockito.Mockito;
32 import org.mockito.MockitoAnnotations;
33 import org.openecomp.sdc.be.components.impl.exceptions.ByActionStatusComponentException;
34 import org.openecomp.sdc.be.components.impl.exceptions.ByResponseFormatComponentException;
35 import org.openecomp.sdc.be.components.impl.exceptions.ComponentException;
36 import org.openecomp.sdc.be.components.validation.UserValidations;
37 import org.openecomp.sdc.be.components.validation.ValidationUtils;
38 import org.openecomp.sdc.be.dao.api.ActionStatus;
39 import org.openecomp.sdc.be.dao.jsongraph.JanusGraphDao;
40 import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum;
41 import org.openecomp.sdc.be.datatypes.elements.ProductMetadataDataDefinition;
42 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
43 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
44 import org.openecomp.sdc.be.impl.ComponentsUtils;
45 import org.openecomp.sdc.be.model.ComponentMetadataDefinition;
46 import org.openecomp.sdc.be.model.Product;
47 import org.openecomp.sdc.be.model.User;
48 import org.openecomp.sdc.be.model.category.CategoryDefinition;
49 import org.openecomp.sdc.be.model.category.GroupingDefinition;
50 import org.openecomp.sdc.be.model.category.SubCategoryDefinition;
51 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
52 import org.openecomp.sdc.be.model.operations.api.IElementOperation;
53 import org.openecomp.sdc.be.model.operations.api.IGraphLockOperation;
54 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
55 import fj.data.Either;
56 import org.openecomp.sdc.exception.ResponseFormat;
57
58 import static org.junit.Assert.assertEquals;
59 import static org.junit.Assert.assertTrue;
60 import static org.mockito.ArgumentMatchers.any;
61 import static org.mockito.ArgumentMatchers.anyBoolean;
62 import static org.mockito.ArgumentMatchers.anyList;
63 import static org.mockito.ArgumentMatchers.anyString;
64 import static org.mockito.ArgumentMatchers.eq;
65 import static org.mockito.Mockito.doThrow;
66 import static org.mockito.Mockito.when;
67
68 public class ProductBusinessLogicTest {
69
70         private Product product;
71         private User user;
72         private List<String> contacts;
73         private List<String> tags;
74
75         private String pId;
76         private String pName;
77         private String uId;
78         private String pCode;
79         private String pIcon;
80         private String desc;
81         private String role;
82
83         @InjectMocks
84         private ProductBusinessLogic productBusinessLogic;
85
86         @Mock
87         private ToscaOperationFacade toscaOperationFacade;
88
89         @Mock
90         private JanusGraphDao janusGraphDao;
91
92         @Mock
93         private ValidationUtils validationUtils;
94
95         @Mock
96         private ComponentsUtils componentsUtils;
97
98         @Mock
99         private IGraphLockOperation iGraphLockOperation;
100
101         @Mock
102         private UserValidations userValidations;
103
104         @Mock
105         private IElementOperation elementOperation;
106
107         @Before
108         public void setUp() {
109                 productBusinessLogic = new ProductBusinessLogic();
110                 MockitoAnnotations.initMocks(this);
111                 product = new Product();
112                 user = new User();
113                 contacts = new ArrayList<>();
114                 tags = new ArrayList<>();
115
116                 pName = "product1";
117                 pId = "productId";
118                 uId = "userId";
119                 pCode = "productCode";
120                 pIcon = "projectIcon";
121                 desc = "Testing Product Business Logic";
122                 role = "PROJECT_MANAGER";
123
124                 user.setUserId(uId);
125                 user.setRole(role);
126         }
127
128         @Test
129         public void testCreateProduct_givenValidProductAndUser_thenReturnsProduct() {
130                 product.setName(pName);
131                 product.setFullName("avengers");
132                 product.setInvariantUUID("ABCD1234");
133                 product.setContacts(getContacts());
134                 product.setTags(getTags());
135                 product.setIcon(pIcon);
136                 product.setProjectCode(pCode);
137                 product.setDescription(desc);
138
139                 when(userValidations.validateUserNotEmpty(Mockito.any(User.class), Mockito.anyString()))
140                                 .thenReturn(user);
141                 when(userValidations.validateUserExists(Mockito.anyString(), Mockito.anyString(), Mockito.anyBoolean()))
142                                 .thenReturn(user);
143                 when(toscaOperationFacade.validateComponentNameExists(Mockito.anyString(), Mockito.any(), Mockito.any(ComponentTypeEnum.class)))
144                                 .thenReturn(Either.left(Boolean.FALSE));
145                 when(iGraphLockOperation.lockComponentByName(Mockito.any(), Mockito.any(NodeTypeEnum.class)))
146                                 .thenReturn(StorageOperationStatus.OK);
147                 when(toscaOperationFacade.createToscaComponent(any(org.openecomp.sdc.be.model.Product.class)))
148                                 .thenReturn(Either.left(product));
149                 Either result = productBusinessLogic.createProduct(product, user);
150                 assertTrue(result.isLeft());
151                 Product returnedProduct = (Product) result.left().value();
152
153                 assertEquals(product.getFullName(), returnedProduct.getFullName());
154
155         }
156
157         @Test(expected = ComponentException.class)
158         public void testCreateProduct_givenEmptyUserId_thenReturnsException() {
159                 when(userValidations.validateUserNotEmpty(Mockito.any(User.class), Mockito.anyString()))
160                                 .thenThrow(new ByResponseFormatComponentException(new ResponseFormat()));
161                 productBusinessLogic.createProduct(product, user);
162         }
163
164         @Test(expected = ComponentException.class)
165         public void testCreateProduct_givenUnknownUser_thenReturnsException() {
166                 ComponentException componentException = new ByActionStatusComponentException(ActionStatus.USER_NOT_FOUND);
167                 when(userValidations.validateUserNotEmpty(any(User.class), anyString()))
168                                 .thenReturn(user);
169                 when(userValidations.validateUserExists(anyString(), anyString(), anyBoolean()))
170                                 .thenThrow(componentException);
171                 productBusinessLogic.createProduct(product, user);
172         }
173
174         @Test(expected = ComponentException.class)
175         public void testCreateProduct_givenInvalidUserRole_thenReturnsException() {
176                 user.setRole("CREATOR");
177                 doThrow(new ByResponseFormatComponentException(new ResponseFormat())).when(userValidations).validateUserRole(any(), anyList());
178                 assertTrue(productBusinessLogic.createProduct(product, user).isRight());
179         }
180
181         @Test
182         public void testCreateProduct_givenProductIsNull_thenReturnsError() {
183                 product = null;
184                 assertTrue(productBusinessLogic.createProduct(product, user).isRight());
185         }
186
187         @Test
188         public void testCreateProduct_givenInvalidProductFullNames_thenReturnsErrors() {
189                 List<String> invalidProductNames = new ArrayList<>();
190                 invalidProductNames.add(null);
191                 invalidProductNames.add("~~");
192                 invalidProductNames.add("yo");
193                 invalidProductNames.add("infinity");
194                 when(toscaOperationFacade.validateComponentNameExists(anyString(), any(), any(ComponentTypeEnum.class)))
195                                 .thenReturn(Either.left(Boolean.TRUE));
196                 for (String s : invalidProductNames) {
197                         product.setName(s);
198                         assertTrue(productBusinessLogic.createProduct(product, user).isRight());
199                 }
200         }
201
202         @Test
203         public void testValidateProductName_givenValidName_thenReturnsSuccessful() {
204                 when(userValidations.validateUserExists(anyString(), anyString(), anyBoolean()))
205                                 .thenReturn(user);
206                 when(toscaOperationFacade.validateComponentNameUniqueness(eq(pName), any(), any(ComponentTypeEnum.class)))
207                                 .thenReturn(Either.left(Boolean.TRUE));
208
209                 Map result = productBusinessLogic.validateProductNameExists(pName, uId).left().value();
210                 assertEquals(Boolean.TRUE, result.get("isValid"));
211         }
212
213         @Test
214         public void testValidateProductName_givenInvalidName_thenReturnsError() {
215                 String invalidProductName = "~~";
216                 when(userValidations.validateUserExists(anyString(), anyString(), anyBoolean()))
217                                 .thenReturn(user);
218                 when(toscaOperationFacade.validateComponentNameUniqueness(eq(invalidProductName), any(), any(ComponentTypeEnum.class)))
219                                 .thenReturn(Either.left(Boolean.FALSE));
220                 Map result = productBusinessLogic.validateProductNameExists(invalidProductName, uId).left().value();
221                 assertEquals(Boolean.FALSE, result.get("isValid"));
222         }
223
224         @Test
225         public void testValidateProductName_givenNameUniquenessCheckFails_thenReturnsError() {
226                 when(userValidations.validateUserExists(anyString(), anyString(), anyBoolean()))
227                                 .thenReturn(user);
228                 when(toscaOperationFacade.validateComponentNameUniqueness(eq(pName), any(), any(ComponentTypeEnum.class)))
229                                 .thenReturn(Either.right(StorageOperationStatus.ENTITY_ALREADY_EXISTS));
230                 assertTrue(productBusinessLogic.validateProductNameExists(pName, uId).isRight());
231         }
232
233         @Test
234         public void testGetProduct_givenValidProductIdAndUser_thenReturnsSuccessful() {
235                 when(toscaOperationFacade.getToscaElement(eq(pName)))
236                                 .thenReturn(Either.left(product));
237                 assertTrue(productBusinessLogic.getProduct(pName, user).isLeft());
238         }
239
240         @Test
241         public void testGetProduct_givenInvalidProductId_thenReturnsError() {
242                 when(toscaOperationFacade.getToscaElement(eq(pName)))
243                                 .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
244                 assertTrue(productBusinessLogic.getProduct(pName, user).isRight());
245         }
246
247         @Test
248         public void testDeleteProduct_givenValidProductIdAndUser_thenReturnsSuccessful() {
249                 when(toscaOperationFacade.deleteToscaComponent(pId))
250                                 .thenReturn(Either.left(product));
251                 assertTrue(productBusinessLogic.deleteProduct(pId, user).isLeft());
252         }
253
254         @Test
255         public void testDeleteProduct_givenInvalidProductId_thenReturnsError() {
256                 when(toscaOperationFacade.deleteToscaComponent(pId))
257                                 .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
258                 assertTrue(productBusinessLogic.deleteProduct(pId, user).isRight());
259         }
260
261         @Test
262         public void testUpdateProductMetadata_givenValidProductAndUser_thenReturnsSuccessful() {
263                 String componentId = "component1";
264                 String projectName = "Product1";
265                 String version = "2.0";
266                 String lifecycleState = "NOT_CERTIFIED_CHECKOUT";
267                 String uniqueId = "pUniqueId";
268
269                 Product product = new Product();
270                 ProductMetadataDataDefinition productMetadataDataDefinition = new ProductMetadataDataDefinition();
271                 ComponentMetadataDefinition componentMetadataDefinition = new ComponentMetadataDefinition(productMetadataDataDefinition);
272                 CategoryDefinition categoryDefinition = new CategoryDefinition();
273                 SubCategoryDefinition subCategoryDefinition = new SubCategoryDefinition();
274                 GroupingDefinition groupingDefinition = new GroupingDefinition();
275
276                 List<CategoryDefinition> categoryDefinitionList = new ArrayList<>();
277                 List<SubCategoryDefinition> subCategoryDefinitionList = new ArrayList<>();
278                 List<GroupingDefinition> groupingDefinitionsList = new ArrayList<>();
279
280                 categoryDefinition.setName("cat1");
281                 subCategoryDefinition.setName("subCat1");
282                 groupingDefinition.setName("subCatGroup1");
283
284                 groupingDefinitionsList.add(groupingDefinition);
285                 subCategoryDefinition.setGroupings(groupingDefinitionsList);
286                 subCategoryDefinitionList.add(subCategoryDefinition);
287                 categoryDefinition.setSubcategories(subCategoryDefinitionList);
288                 categoryDefinitionList.add(categoryDefinition);
289
290                 productMetadataDataDefinition.setFullName(projectName);
291                 productMetadataDataDefinition.setName(projectName);
292                 productMetadataDataDefinition.setState(lifecycleState);
293                 productMetadataDataDefinition.setUniqueId(uniqueId);
294                 productMetadataDataDefinition.setComponentType(ComponentTypeEnum.PRODUCT);
295
296                 product.setMetadataDefinition(componentMetadataDefinition);
297                 product.setLastUpdaterUserId(uId);
298                 product.setDescription(desc);
299                 product.setVersion(version);
300                 product.setProjectCode(pCode);
301                 product.setIcon(pIcon);
302                 product.setCategories(categoryDefinitionList);
303                 product.setContacts(contacts);
304                 product.setTags(tags);
305
306                 when(userValidations.validateUserExists(eq(uId), anyString(), anyBoolean()))
307                                 .thenReturn(user);
308                 when(toscaOperationFacade.getToscaElement(eq(componentId)))
309                                 .thenReturn(Either.left(product));
310                 when(toscaOperationFacade.getToscaElement(eq(componentId), any(JsonParseFlagEnum.class)))
311                                 .thenReturn(Either.left(product));
312                 when(elementOperation.getAllProductCategories())
313                                 .thenReturn(Either.left(categoryDefinitionList));
314                 when(iGraphLockOperation.lockComponent(anyString(), any(NodeTypeEnum.class)))
315                                 .thenReturn(StorageOperationStatus.OK);
316                 when(toscaOperationFacade.updateToscaElement(any(Product.class)))
317                                 .thenReturn(Either.left(product));
318
319                 assertTrue(productBusinessLogic.updateProductMetadata(componentId, product, user).isLeft());
320         }
321
322         @Test
323         public void testUpdateProductMetadata_givenUpdateProductNull_thenReturnsError() {
324                 Product updateProduct = null;
325                 String productId = null;
326                 assertTrue(productBusinessLogic.updateProductMetadata(productId, updateProduct, user).isRight());
327         }
328
329         @Test
330         public void testUpdateProductMetadata_givenProductDoesNotExist_thenReturnsError() {
331                 String productId = "product1";
332                 when(toscaOperationFacade.getToscaElement(eq(productId)))
333                                 .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
334                 assertTrue(productBusinessLogic.updateProductMetadata(productId, product, user).isRight());
335         }
336
337         @Test
338         public void testUpdateProductMetada_givenUserRestricted_thenReturnsError() {
339
340                 ProductMetadataDataDefinition productMetadataDataDefinition = new ProductMetadataDataDefinition();
341                 productMetadataDataDefinition.setLifecycleState("CERTIFIED");
342                 ComponentMetadataDefinition componentMetadataDefinition = new ComponentMetadataDefinition(productMetadataDataDefinition);
343                 product.setMetadataDefinition(componentMetadataDefinition);
344
345
346                 when(userValidations.validateUserExists(eq(uId), anyString(), anyBoolean()))
347                                 .thenReturn(user);
348                 when(toscaOperationFacade.getToscaElement(eq(pId)))
349                                 .thenReturn(Either.left(product));
350                 when(toscaOperationFacade.getToscaElement(eq(pId), eq(JsonParseFlagEnum.ParseMetadata)))
351                                 .thenReturn(Either.left(product));
352                 assertTrue(productBusinessLogic.updateProductMetadata(pId, product, user).isRight());
353         }
354
355
356         @Test
357         public void testGetProductByNameAndVersion_givenValidNameAndVersion_thenReturnsSuccessful() {
358                 String productVersion = "2.0";
359
360                 when(toscaOperationFacade.getComponentByNameAndVersion(eq(ComponentTypeEnum.PRODUCT), eq(pName), eq(productVersion)))
361                                 .thenReturn(Either.left(product));
362                 assertTrue(productBusinessLogic.getProductByNameAndVersion(pName, productVersion, uId).isLeft());
363         }
364
365         @Test
366         public void testGetProductByNameAndVersion_givenInvalidDetails_thenReturnsError() {
367                 String productVersion = "2.0";
368                 when(toscaOperationFacade.getComponentByNameAndVersion(eq(ComponentTypeEnum.PRODUCT), eq(pName), eq(productVersion)))
369                                 .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
370                 assertTrue(productBusinessLogic.getProductByNameAndVersion(pName, productVersion, uId).isRight());
371         }
372
373         private List<String> getContacts() {
374                 contacts.add("user1");
375                 return contacts;
376         }
377
378         private List<String> getTags() {
379                 tags.add("product1");
380                 return tags;
381         }
382 }