2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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 * ================================================================================
22 package org.openecomp.sdc.be.components.impl;
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;
47 import java.util.ArrayList;
48 import java.util.List;
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;
60 public class ProductBusinessLogicTest extends ComponentBusinessLogicMock {
62 private Product product;
64 private List<String> contacts;
65 private List<String> tags;
76 private ProductBusinessLogic productBusinessLogic;
79 private ToscaOperationFacade toscaOperationFacade;
82 private JanusGraphDao janusGraphDao;
85 private ValidationUtils validationUtils;
88 private ComponentsUtils componentsUtils;
91 private IGraphLockOperation iGraphLockOperation;
94 private UserValidations userValidations;
97 private ComponentInstanceBusinessLogic componentInstanceBusinessLogic;
100 ComponentNameValidator componentNameValidator;
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();
112 contacts = new ArrayList<>();
113 tags = new ArrayList<>();
118 pCode = "productCode";
119 pIcon = "projectIcon";
120 desc = "Testing Product Business Logic";
121 role = "PROJECT_MANAGER";
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);
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());
144 public void testCreateProduct_givenProductIsNull_thenReturnsError() {
146 assertTrue(productBusinessLogic.createProduct(product, user).isRight());
150 public void testValidateProductName_givenValidName_thenReturnsSuccessful() {
151 when(userValidations.validateUserExists(anyString()))
153 when(toscaOperationFacade.validateComponentNameUniqueness(eq(pName), any(), any(ComponentTypeEnum.class)))
154 .thenReturn(Either.left(Boolean.TRUE));
156 Map result = productBusinessLogic.validateProductNameExists(pName, uId).left().value();
157 assertEquals(Boolean.TRUE, result.get("isValid"));
161 public void testValidateProductName_givenInvalidName_thenReturnsError() {
162 String invalidProductName = "~~";
163 when(userValidations.validateUserExists(anyString()))
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"));
172 public void testValidateProductName_givenNameUniquenessCheckFails_thenReturnsError() {
173 when(userValidations.validateUserExists(anyString()))
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());
181 public void testGetProduct_givenValidProductIdAndUser_thenReturnsSuccessful() {
182 when(toscaOperationFacade.getToscaElement(eq(pName)))
183 .thenReturn(Either.left(product));
184 assertTrue(productBusinessLogic.getProduct(pName, user).isLeft());
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());
195 public void testDeleteProduct_givenValidProductIdAndUser_thenReturnsSuccessful() {
196 when(toscaOperationFacade.deleteToscaComponent(pId))
197 .thenReturn(Either.left(product));
198 assertTrue(productBusinessLogic.deleteProduct(pId, user).isLeft());
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());
209 public void testUpdateProductMetadata_givenUpdateProductNull_thenReturnsError() {
210 Product updateProduct = null;
211 String productId = null;
212 assertTrue(productBusinessLogic.updateProductMetadata(productId, updateProduct, user).isRight());
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());
224 public void testGetProductByNameAndVersion_givenValidNameAndVersion_thenReturnsSuccessful() {
225 String productVersion = "2.0";
227 when(toscaOperationFacade.getComponentByNameAndVersion(eq(ComponentTypeEnum.PRODUCT), eq(pName), eq(productVersion)))
228 .thenReturn(Either.left(product));
229 assertTrue(productBusinessLogic.getProductByNameAndVersion(pName, productVersion, uId).isLeft());
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());
240 private List<String> getContacts() {
241 contacts.add("user1");
245 private List<String> getTags() {
246 tags.add("product1");