Catalog alignment
[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 fj.data.Either;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.mockito.InjectMocks;
28 import org.mockito.Mock;
29 import org.mockito.Mockito;
30 import org.mockito.MockitoAnnotations;
31 import org.openecomp.sdc.be.components.impl.exceptions.ByResponseFormatComponentException;
32 import org.openecomp.sdc.be.components.impl.exceptions.ComponentException;
33 import org.openecomp.sdc.be.components.utils.ComponentBusinessLogicMock;
34 import org.openecomp.sdc.be.components.validation.UserValidations;
35 import org.openecomp.sdc.be.components.validation.ValidationUtils;
36 import org.openecomp.sdc.be.components.validation.component.ComponentNameValidator;
37 import org.openecomp.sdc.be.dao.jsongraph.JanusGraphDao;
38 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
39 import org.openecomp.sdc.be.impl.ComponentsUtils;
40 import org.openecomp.sdc.be.model.Product;
41 import org.openecomp.sdc.be.model.User;
42 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
43 import org.openecomp.sdc.be.model.operations.api.IGraphLockOperation;
44 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
45 import org.openecomp.sdc.exception.ResponseFormat;
46
47 import java.util.ArrayList;
48 import java.util.List;
49 import java.util.Map;
50
51 import static org.junit.Assert.assertEquals;
52 import static org.junit.Assert.assertTrue;
53 import static org.mockito.ArgumentMatchers.any;
54 import static org.mockito.ArgumentMatchers.anyList;
55 import static org.mockito.ArgumentMatchers.anyString;
56 import static org.mockito.ArgumentMatchers.eq;
57 import static org.mockito.Mockito.doThrow;
58 import static org.mockito.Mockito.when;
59
60 public class ProductBusinessLogicTest extends ComponentBusinessLogicMock {
61
62         private Product product;
63         private User user;
64         private List<String> contacts;
65         private List<String> tags;
66
67         private String pId;
68         private String pName;
69         private String uId;
70         private String pCode;
71         private String pIcon;
72         private String desc;
73         private String role;
74
75         @InjectMocks
76         private ProductBusinessLogic productBusinessLogic;
77
78         @Mock
79         private ToscaOperationFacade toscaOperationFacade;
80
81         @Mock
82         private JanusGraphDao janusGraphDao;
83
84         @Mock
85         private ValidationUtils validationUtils;
86
87         @Mock
88         private ComponentsUtils componentsUtils;
89
90         @Mock
91         private IGraphLockOperation iGraphLockOperation;
92
93         @Mock
94         private UserValidations userValidations;
95
96         @Mock
97         private ComponentInstanceBusinessLogic componentInstanceBusinessLogic;
98
99         @Mock
100         ComponentNameValidator componentNameValidator;
101
102         @Before
103         public void setUp() {
104                 productBusinessLogic = new ProductBusinessLogic(elementDao, groupOperation, groupInstanceOperation,
105                         groupTypeOperation, groupBusinessLogic, interfaceOperation, interfaceLifecycleTypeOperation,
106                         artifactsBusinessLogic, componentInstanceBusinessLogic, artifactToscaOperation, componentContactIdValidator,
107                         componentNameValidator, componentTagsValidator, componentValidator,
108                         componentIconValidator, componentProjectCodeValidator, componentDescriptionValidator );
109                 MockitoAnnotations.initMocks(this);
110                 product = new Product();
111                 user = new User();
112                 contacts = new ArrayList<>();
113                 tags = new ArrayList<>();
114
115                 pName = "product1";
116                 pId = "productId";
117                 uId = "userId";
118                 pCode = "productCode";
119                 pIcon = "projectIcon";
120                 desc = "Testing Product Business Logic";
121                 role = "PROJECT_MANAGER";
122
123                 user.setUserId(uId);
124                 user.setRole(role);
125         }
126
127
128         @Test(expected = ComponentException.class)
129         public void testCreateProduct_givenEmptyUserId_thenReturnsException() {
130                 when(userValidations.validateUserNotEmpty(Mockito.any(User.class), Mockito.anyString()))
131                                 .thenThrow(new ByResponseFormatComponentException(new ResponseFormat()));
132                 productBusinessLogic.createProduct(product, user);
133         }
134
135
136         @Test(expected = ComponentException.class)
137         public void testCreateProduct_givenInvalidUserRole_thenReturnsException() {
138                 user.setRole("CREATOR");
139                 doThrow(new ByResponseFormatComponentException(new ResponseFormat())).when(userValidations).validateUserRole(any(), anyList());
140                 assertTrue(productBusinessLogic.createProduct(product, user).isRight());
141         }
142
143         @Test
144         public void testCreateProduct_givenProductIsNull_thenReturnsError() {
145                 product = null;
146                 assertTrue(productBusinessLogic.createProduct(product, user).isRight());
147         }
148
149         @Test
150         public void testValidateProductName_givenValidName_thenReturnsSuccessful() {
151                 when(userValidations.validateUserExists(anyString()))
152                                 .thenReturn(user);
153                 when(toscaOperationFacade.validateComponentNameUniqueness(eq(pName), any(), any(ComponentTypeEnum.class)))
154                                 .thenReturn(Either.left(Boolean.TRUE));
155
156                 Map result = productBusinessLogic.validateProductNameExists(pName, uId).left().value();
157                 assertEquals(Boolean.TRUE, result.get("isValid"));
158         }
159
160         @Test
161         public void testValidateProductName_givenInvalidName_thenReturnsError() {
162                 String invalidProductName = "~~";
163                 when(userValidations.validateUserExists(anyString()))
164                                 .thenReturn(user);
165                 when(toscaOperationFacade.validateComponentNameUniqueness(eq(invalidProductName), any(), any(ComponentTypeEnum.class)))
166                                 .thenReturn(Either.left(Boolean.FALSE));
167                 Map result = productBusinessLogic.validateProductNameExists(invalidProductName, uId).left().value();
168                 assertEquals(Boolean.FALSE, result.get("isValid"));
169         }
170
171         @Test
172         public void testValidateProductName_givenNameUniquenessCheckFails_thenReturnsError() {
173                 when(userValidations.validateUserExists(anyString()))
174                                 .thenReturn(user);
175                 when(toscaOperationFacade.validateComponentNameUniqueness(eq(pName), any(), any(ComponentTypeEnum.class)))
176                                 .thenReturn(Either.right(StorageOperationStatus.ENTITY_ALREADY_EXISTS));
177                 assertTrue(productBusinessLogic.validateProductNameExists(pName, uId).isRight());
178         }
179
180         @Test
181         public void testGetProduct_givenValidProductIdAndUser_thenReturnsSuccessful() {
182                 when(toscaOperationFacade.getToscaElement(eq(pName)))
183                                 .thenReturn(Either.left(product));
184                 assertTrue(productBusinessLogic.getProduct(pName, user).isLeft());
185         }
186
187         @Test
188         public void testGetProduct_givenInvalidProductId_thenReturnsError() {
189                 when(toscaOperationFacade.getToscaElement(eq(pName)))
190                                 .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
191                 assertTrue(productBusinessLogic.getProduct(pName, user).isRight());
192         }
193
194         @Test
195         public void testDeleteProduct_givenValidProductIdAndUser_thenReturnsSuccessful() {
196                 when(toscaOperationFacade.deleteToscaComponent(pId))
197                                 .thenReturn(Either.left(product));
198                 assertTrue(productBusinessLogic.deleteProduct(pId, user).isLeft());
199         }
200
201         @Test
202         public void testDeleteProduct_givenInvalidProductId_thenReturnsError() {
203                 when(toscaOperationFacade.deleteToscaComponent(pId))
204                                 .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
205                 assertTrue(productBusinessLogic.deleteProduct(pId, user).isRight());
206         }
207
208         @Test
209         public void testUpdateProductMetadata_givenUpdateProductNull_thenReturnsError() {
210                 Product updateProduct = null;
211                 String productId = null;
212                 assertTrue(productBusinessLogic.updateProductMetadata(productId, updateProduct, user).isRight());
213         }
214
215         @Test
216         public void testUpdateProductMetadata_givenProductDoesNotExist_thenReturnsError() {
217                 String productId = "product1";
218                 when(toscaOperationFacade.getToscaElement(eq(productId)))
219                                 .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
220                 assertTrue(productBusinessLogic.updateProductMetadata(productId, product, user).isRight());
221         }
222
223         @Test
224         public void testGetProductByNameAndVersion_givenValidNameAndVersion_thenReturnsSuccessful() {
225                 String productVersion = "2.0";
226
227                 when(toscaOperationFacade.getComponentByNameAndVersion(eq(ComponentTypeEnum.PRODUCT), eq(pName), eq(productVersion)))
228                                 .thenReturn(Either.left(product));
229                 assertTrue(productBusinessLogic.getProductByNameAndVersion(pName, productVersion, uId).isLeft());
230         }
231
232         @Test
233         public void testGetProductByNameAndVersion_givenInvalidDetails_thenReturnsError() {
234                 String productVersion = "2.0";
235                 when(toscaOperationFacade.getComponentByNameAndVersion(eq(ComponentTypeEnum.PRODUCT), eq(pName), eq(productVersion)))
236                                 .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
237                 assertTrue(productBusinessLogic.getProductByNameAndVersion(pName, productVersion, uId).isRight());
238         }
239
240         private List<String> getContacts() {
241                 contacts.add("user1");
242                 return contacts;
243         }
244
245         private List<String> getTags() {
246                 tags.add("product1");
247                 return tags;
248         }
249 }