Remove usage of outdated library JMockit (catalog-dao)
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / components / impl / ProductBusinessLogicTest.java
index cbb5f7b..42d4e4b 100644 (file)
+/*-
+ * ============LICENSE_START=======================================================
+ * SDC
+ * ================================================================================
+ * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ * Modifications copyright (c) 2019 Nokia
+ * ================================================================================
+ */
 package org.openecomp.sdc.be.components.impl;
 
-import java.util.List;
-import java.util.Map;
-
-import org.apache.commons.math3.stat.descriptive.summary.Product;
+import fj.data.Either;
+import org.junit.Before;
 import org.junit.Test;
+import org.mockito.InjectMocks;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.mockito.MockitoAnnotations;
+import org.openecomp.sdc.be.components.impl.exceptions.ByResponseFormatComponentException;
+import org.openecomp.sdc.be.components.impl.exceptions.ComponentException;
+import org.openecomp.sdc.be.components.utils.ComponentBusinessLogicMock;
+import org.openecomp.sdc.be.components.validation.UserValidations;
+import org.openecomp.sdc.be.components.validation.ValidationUtils;
+import org.openecomp.sdc.be.components.validation.component.ComponentNameValidator;
+import org.openecomp.sdc.be.dao.janusgraph.JanusGraphDao;
 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
-import org.openecomp.sdc.be.model.Component;
-import org.openecomp.sdc.be.model.ComponentInstance;
+import org.openecomp.sdc.be.impl.ComponentsUtils;
+import org.openecomp.sdc.be.model.Product;
 import org.openecomp.sdc.be.model.User;
-import org.openecomp.sdc.be.model.operations.api.ICacheMangerOperation;
-import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
-import org.openecomp.sdc.be.ui.model.UiComponentDataTransfer;
+import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
+import org.openecomp.sdc.be.model.operations.api.IGraphLockOperation;
+import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
 import org.openecomp.sdc.exception.ResponseFormat;
 
-import fj.data.Either;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
 
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyList;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.doThrow;
+import static org.mockito.Mockito.when;
 
-public class ProductBusinessLogicTest {
+public class ProductBusinessLogicTest extends ComponentBusinessLogicMock {
 
-       private ProductBusinessLogic createTestSubject() {
-               return new ProductBusinessLogic();
-       }
+       private Product product;
+       private User user;
+       private List<String> contacts;
+       private List<String> tags;
 
-       @Test
-       public void testValidateProductNameExists() throws Exception {
-               ProductBusinessLogic testSubject;
-               String productName = "";
-               String userId = "";
-               Either<Map<String, Boolean>, ResponseFormat> result;
-
-               // default test
-               testSubject = createTestSubject();
-       }
+       private String pId;
+       private String pName;
+       private String uId;
+       private String pCode;
+       private String pIcon;
+       private String desc;
+       private String role;
 
-       @Test
-       public void testSetDeploymentArtifactsPlaceHolder() throws Exception {
-               ProductBusinessLogic testSubject;
-               Component component = null;
-               User user = null;
-
-               // default test
-               testSubject = createTestSubject();
-               testSubject.setDeploymentArtifactsPlaceHolder(component, user);
-       }
+       @InjectMocks
+       private ProductBusinessLogic productBusinessLogic;
 
-       @Test
-       public void testDeleteMarkedComponents() throws Exception {
-               ProductBusinessLogic testSubject;
-               Either<List<String>, ResponseFormat> result;
+       @Mock
+       private ToscaOperationFacade toscaOperationFacade;
 
-               // default test
-               testSubject = createTestSubject();
-               
-       }
+       @Mock
+       private JanusGraphDao janusGraphDao;
 
-       @Test
-       public void testGetComponentInstanceBL() throws Exception {
-               ProductBusinessLogic testSubject;
-               ComponentInstanceBusinessLogic result;
+       @Mock
+       private ValidationUtils validationUtils;
 
-               // default test
-               testSubject = createTestSubject();
-               
-       }
+       @Mock
+       private ComponentsUtils componentsUtils;
 
-       @Test
-       public void testGetComponentInstancesFilteredByPropertiesAndInputs() throws Exception {
-               ProductBusinessLogic testSubject;
-               String componentId = "";
-               ComponentTypeEnum componentTypeEnum = null;
-               String userId = "";
-               String searchText = "";
-               Either<List<ComponentInstance>, ResponseFormat> result;
-
-               // default test
-               testSubject = createTestSubject();
-               
-       }
+       @Mock
+       private IGraphLockOperation iGraphLockOperation;
 
-       @Test
-       public void testGetCacheManagerOperation() throws Exception {
-               ProductBusinessLogic testSubject;
-               ICacheMangerOperation result;
+       @Mock
+       private UserValidations userValidations;
 
-               // default test
-               testSubject = createTestSubject();
-               
-       }
+       @Mock
+       private ComponentInstanceBusinessLogic componentInstanceBusinessLogic;
 
-       @Test
-       public void testSetCacheManagerOperation() throws Exception {
-               ProductBusinessLogic testSubject;
-               ICacheMangerOperation cacheManagerOperation = null;
+       @Mock
+       ComponentNameValidator componentNameValidator;
 
-               // default test
-               testSubject = createTestSubject();
-               testSubject.setCacheManagerOperation(cacheManagerOperation);
-       }
+       @Before
+       public void setUp() {
+               productBusinessLogic = new ProductBusinessLogic(elementDao, groupOperation, groupInstanceOperation,
+                       groupTypeOperation, groupBusinessLogic, interfaceOperation, interfaceLifecycleTypeOperation,
+                       artifactsBusinessLogic, componentInstanceBusinessLogic, artifactToscaOperation, componentContactIdValidator,
+                       componentNameValidator, componentTagsValidator, componentValidator,
+                       componentIconValidator, componentProjectCodeValidator, componentDescriptionValidator );
+               MockitoAnnotations.initMocks(this);
+               product = new Product();
+               user = new User();
+               contacts = new ArrayList<>();
+               tags = new ArrayList<>();
 
-       @Test
-       public void testGetUiComponentDataTransferByComponentId() throws Exception {
-               ProductBusinessLogic testSubject;
-               String componentId = "";
-               List<String> dataParamsToReturn = null;
-               Either<UiComponentDataTransfer, ResponseFormat> result;
-
-               // default test
-               testSubject = createTestSubject();
-               
-       }
+               pName = "product1";
+               pId = "productId";
+               uId = "userId";
+               pCode = "productCode";
+               pIcon = "projectIcon";
+               desc = "Testing Product Business Logic";
+               role = "PROJECT_MANAGER";
 
-       
-       @Test
-       public void testCreateProduct() throws Exception {
-               ProductBusinessLogic testSubject;
-               Product product = null;
-               User user = null;
-               Either<Product, ResponseFormat> result;
-
-               // test 1
-               testSubject = createTestSubject();
-               product = null;
-               
+               user.setUserId(uId);
+               user.setRole(role);
        }
 
-       
-       @Test
-       public void testCheckUnupdatableProductFields() throws Exception {
-               ProductBusinessLogic testSubject;
-               Product product = null;
 
-               // default test
-               testSubject = createTestSubject();
+       @Test(expected = ComponentException.class)
+       public void testCreateProduct_givenEmptyUserId_thenReturnsException() {
+               when(userValidations.validateUserNotEmpty(Mockito.any(User.class), Mockito.anyString()))
+                               .thenThrow(new ByResponseFormatComponentException(new ResponseFormat()));
+               productBusinessLogic.createProduct(product, user);
        }
 
-       
-       @Test
-       public void testValidateProductBeforeCreate() throws Exception {
-               ProductBusinessLogic testSubject;
-               Product product = null;
-               User user = null;
-               AuditingActionEnum actionEnum = null;
-               Either<Product, ResponseFormat> result;
-
-               // default test
-               testSubject = createTestSubject();
-               
-       }
 
-       
-       @Test
-       public void testValidateProductFieldsBeforeCreate() throws Exception {
-               ProductBusinessLogic testSubject;
-               User user = null;
-               Product product = null;
-               AuditingActionEnum actionEnum = null;
-               Either<Boolean, ResponseFormat> result;
-
-               // default test
-               testSubject = createTestSubject();
-               
+       @Test(expected = ComponentException.class)
+       public void testCreateProduct_givenInvalidUserRole_thenReturnsException() {
+               user.setRole("CREATOR");
+               doThrow(new ByResponseFormatComponentException(new ResponseFormat())).when(userValidations).validateUserRole(any(), anyList());
+               assertTrue(productBusinessLogic.createProduct(product, user).isRight());
        }
 
-       
        @Test
-       public void testValidateAndUpdateProductContactsList() throws Exception {
-               ProductBusinessLogic testSubject;
-               User user = null;
-               Product product = null;
-               AuditingActionEnum actionEnum = null;
-               Either<Boolean, ResponseFormat> result;
-
-               // default test
-               testSubject = createTestSubject();
-               
+       public void testCreateProduct_givenProductIsNull_thenReturnsError() {
+               product = null;
+               assertTrue(productBusinessLogic.createProduct(product, user).isRight());
        }
 
-       
        @Test
-       public void testValidateGrouping() throws Exception {
-               ProductBusinessLogic testSubject;
-               User user = null;
-               Product product = null;
-               AuditingActionEnum actionEnum = null;
-               Either<Boolean, ResponseFormat> result;
-
-               // default test
-               testSubject = createTestSubject();
-               
-       }
+       public void testValidateProductName_givenValidName_thenReturnsSuccessful() {
+               when(userValidations.validateUserExists(anyString()))
+                               .thenReturn(user);
+               when(toscaOperationFacade.validateComponentNameUniqueness(eq(pName), any(), any(ComponentTypeEnum.class)))
+                               .thenReturn(Either.left(Boolean.TRUE));
 
-       
-       @Test
-       public void testGetProduct() throws Exception {
-               ProductBusinessLogic testSubject;
-               String productId = "";
-               User user = null;
-               Either<Product, ResponseFormat> result;
-
-               // default test
-               testSubject = createTestSubject();
-               
+               Map result = productBusinessLogic.validateProductNameExists(pName, uId).left().value();
+               assertEquals(Boolean.TRUE, result.get("isValid"));
        }
 
-       
        @Test
-       public void testDeleteProduct() throws Exception {
-               ProductBusinessLogic testSubject;
-               String productId = "";
-               User user = null;
-               Either<Product, ResponseFormat> result;
-
-               // default test
-               testSubject = createTestSubject();
-               
+       public void testValidateProductName_givenInvalidName_thenReturnsError() {
+               String invalidProductName = "~~";
+               when(userValidations.validateUserExists(anyString()))
+                               .thenReturn(user);
+               when(toscaOperationFacade.validateComponentNameUniqueness(eq(invalidProductName), any(), any(ComponentTypeEnum.class)))
+                               .thenReturn(Either.left(Boolean.FALSE));
+               Map result = productBusinessLogic.validateProductNameExists(invalidProductName, uId).left().value();
+               assertEquals(Boolean.FALSE, result.get("isValid"));
        }
 
-       
        @Test
-       public void testValidateProductFullNameAndCleanup() throws Exception {
-               ProductBusinessLogic testSubject;
-               User user = null;
-               Product product = null;
-               AuditingActionEnum actionEnum = null;
-               Either<Boolean, ResponseFormat> result;
-
-               // default test
-               testSubject = createTestSubject();
-               
+       public void testValidateProductName_givenNameUniquenessCheckFails_thenReturnsError() {
+               when(userValidations.validateUserExists(anyString()))
+                               .thenReturn(user);
+               when(toscaOperationFacade.validateComponentNameUniqueness(eq(pName), any(), any(ComponentTypeEnum.class)))
+                               .thenReturn(Either.right(StorageOperationStatus.ENTITY_ALREADY_EXISTS));
+               assertTrue(productBusinessLogic.validateProductNameExists(pName, uId).isRight());
        }
 
-       
        @Test
-       public void testValidateProductNameAndCleanup() throws Exception {
-               ProductBusinessLogic testSubject;
-               User user = null;
-               Product product = null;
-               AuditingActionEnum actionEnum = null;
-               Either<Boolean, ResponseFormat> result;
-
-               // default test
-               testSubject = createTestSubject();
+       public void testGetProduct_givenValidProductIdAndUser_thenReturnsSuccessful() {
+               when(toscaOperationFacade.getToscaElement(eq(pName)))
+                               .thenReturn(Either.left(product));
+               assertTrue(productBusinessLogic.getProduct(pName, user).isLeft());
        }
 
-       
        @Test
-       public void testValidateTagsListAndRemoveDuplicates() throws Exception {
-               ProductBusinessLogic testSubject;
-               User user = null;
-               Product product = null;
-               String oldProductName = "";
-               AuditingActionEnum actionEnum = null;
-               Either<Boolean, ResponseFormat> result;
-
-               // default test
-               testSubject = createTestSubject();
+       public void testGetProduct_givenInvalidProductId_thenReturnsError() {
+               when(toscaOperationFacade.getToscaElement(eq(pName)))
+                               .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
+               assertTrue(productBusinessLogic.getProduct(pName, user).isRight());
        }
 
-       
        @Test
-       public void testUpdateProductMetadata() throws Exception {
-               ProductBusinessLogic testSubject;
-               String productId = "";
-               Product updatedProduct = null;
-               User user = null;
-               Either<Product, ResponseFormat> result;
-
-               // test 1
-               testSubject = createTestSubject();
-               updatedProduct = null;
+       public void testDeleteProduct_givenValidProductIdAndUser_thenReturnsSuccessful() {
+               when(toscaOperationFacade.deleteToscaComponent(pId))
+                               .thenReturn(Either.left(product));
+               assertTrue(productBusinessLogic.deleteProduct(pId, user).isLeft());
        }
 
-       
        @Test
-       public void testValidateAndUpdateProductMetadata() throws Exception {
-               ProductBusinessLogic testSubject;
-               User user = null;
-               Product currentProduct = null;
-               Product updatedProduct = null;
-               Either<Product, ResponseFormat> result;
-
-               // default test
-               testSubject = createTestSubject();
-               
+       public void testDeleteProduct_givenInvalidProductId_thenReturnsError() {
+               when(toscaOperationFacade.deleteToscaComponent(pId))
+                               .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
+               assertTrue(productBusinessLogic.deleteProduct(pId, user).isRight());
        }
 
-       
        @Test
-       public void testValidateAndUpdateProductName() throws Exception {
-               ProductBusinessLogic testSubject;
-               User user = null;
-               Product currentProduct = null;
-               Product updatedProduct = null;
-               Either<Boolean, ResponseFormat> result;
-
-               // default test
-               testSubject = createTestSubject();
-               
+       public void testUpdateProductMetadata_givenUpdateProductNull_thenReturnsError() {
+               Product updateProduct = null;
+               String productId = null;
+               assertTrue(productBusinessLogic.updateProductMetadata(productId, updateProduct, user).isRight());
        }
 
-       
        @Test
-       public void testValidateAndUpdateFullName() throws Exception {
-               ProductBusinessLogic testSubject;
-               User user = null;
-               Product currentProduct = null;
-               Product updatedProduct = null;
-               Either<Boolean, ResponseFormat> result;
-
-               // default test
-               testSubject = createTestSubject();
-               
+       public void testUpdateProductMetadata_givenProductDoesNotExist_thenReturnsError() {
+               String productId = "product1";
+               when(toscaOperationFacade.getToscaElement(eq(productId)))
+                               .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
+               assertTrue(productBusinessLogic.updateProductMetadata(productId, product, user).isRight());
        }
 
-       
        @Test
-       public void testValidateAndUpdateCategory() throws Exception {
-               ProductBusinessLogic testSubject;
-               User user = null;
-               Product currentProduct = null;
-               Product updatedProduct = null;
-               Either<Boolean, ResponseFormat> result;
-
-               // default test
-               testSubject = createTestSubject();
-               
-       }
+       public void testGetProductByNameAndVersion_givenValidNameAndVersion_thenReturnsSuccessful() {
+               String productVersion = "2.0";
 
-       
-       @Test
-       public void testValidateAndUpdateContactList() throws Exception {
-               ProductBusinessLogic testSubject;
-               User user = null;
-               Product currentProduct = null;
-               Product updatedProduct = null;
-               Either<Boolean, ResponseFormat> result;
-
-               // default test
-               testSubject = createTestSubject();
-               
+               when(toscaOperationFacade.getComponentByNameAndVersion(eq(ComponentTypeEnum.PRODUCT), eq(pName), eq(productVersion)))
+                               .thenReturn(Either.left(product));
+               assertTrue(productBusinessLogic.getProductByNameAndVersion(pName, productVersion, uId).isLeft());
        }
 
-       
        @Test
-       public void testValidateAndUpdateTags() throws Exception {
-               ProductBusinessLogic testSubject;
-               User user = null;
-               Product currentProduct = null;
-               Product updatedProduct = null;
-               Either<Boolean, ResponseFormat> result;
-
-               // default test
-               testSubject = createTestSubject();
-               
+       public void testGetProductByNameAndVersion_givenInvalidDetails_thenReturnsError() {
+               String productVersion = "2.0";
+               when(toscaOperationFacade.getComponentByNameAndVersion(eq(ComponentTypeEnum.PRODUCT), eq(pName), eq(productVersion)))
+                               .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
+               assertTrue(productBusinessLogic.getProductByNameAndVersion(pName, productVersion, uId).isRight());
        }
 
-       
-       @Test
-       public void testValidateTagPattern() throws Exception {
-               ProductBusinessLogic testSubject;
-               String tag = "";
-               boolean result;
-
-               // default test
-               testSubject = createTestSubject();
-               
+       private List<String> getContacts() {
+               contacts.add("user1");
+               return contacts;
        }
 
-       
-       @Test
-       public void testGetProductByNameAndVersion() throws Exception {
-               ProductBusinessLogic testSubject;
-               String productName = "";
-               String productVersion = "";
-               String userId = "";
-               Either<Product, ResponseFormat> result;
-
-               // default test
-               testSubject = createTestSubject();
-               
+       private List<String> getTags() {
+               tags.add("product1");
+               return tags;
        }
 }
\ No newline at end of file