2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 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=========================================================
21 package org.openecomp.sdc.be.components.merge.instance;
23 import fj.data.Either;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.mockito.ArgumentCaptor;
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.ByActionStatusComponentException;
32 import org.openecomp.sdc.be.components.impl.exceptions.ComponentException;
33 import org.openecomp.sdc.be.components.utils.ResourceBuilder;
34 import org.openecomp.sdc.be.config.ConfigurationManager;
35 import org.openecomp.sdc.be.dao.api.ActionStatus;
36 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
37 import org.openecomp.sdc.be.impl.ComponentsUtils;
38 import org.openecomp.sdc.be.model.Component;
39 import org.openecomp.sdc.be.model.ComponentInstance;
40 import org.openecomp.sdc.be.model.ComponentParametersView;
41 import org.openecomp.sdc.be.model.Resource;
42 import org.openecomp.sdc.be.model.Service;
43 import org.openecomp.sdc.be.model.User;
44 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
45 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
46 import org.openecomp.sdc.common.impl.ExternalConfiguration;
47 import org.openecomp.sdc.common.impl.FSConfigurationSource;
48 import org.openecomp.sdc.exception.ResponseFormat;
50 import java.util.Arrays;
52 import static org.junit.Assert.assertEquals;
53 import static org.junit.Assert.assertFalse;
54 import static org.mockito.Mockito.verify;
55 import static org.mockito.Mockito.when;
57 public class ComponentInstanceMergeDataBusinessLogicTest {
60 private ComponentInstanceMergeDataBusinessLogic testInstance;
63 private ToscaOperationFacade toscaOperationFacade;
66 private ComponentsUtils componentsUtils;
69 private ComponentInstanceMergeInterface componentInstanceMergeInterfaceMock1;
72 private ComponentInstanceMergeInterface componentInstanceMergeInterfaceMock2;
75 public void setUp() throws Exception {
76 MockitoAnnotations.openMocks(this);
77 testInstance.setComponentInstancesMergeBLs(Arrays.asList(componentInstanceMergeInterfaceMock1, componentInstanceMergeInterfaceMock2));
78 new ConfigurationManager(new FSConfigurationSource(ExternalConfiguration.getChangeListener(), "src/test/resources/config/catalog-be"));
82 public void saveAllDataBeforeDeleting_allInstanceMergeInterfacesAreCalled() throws Exception {
83 Component container = new Service();
84 ComponentInstance instance = new ComponentInstance();
85 Component instanceOriginResource = new Resource();
86 DataForMergeHolder dataForMergeHolder = testInstance.saveAllDataBeforeDeleting(container, instance, instanceOriginResource);
87 verify(componentInstanceMergeInterfaceMock1).saveDataBeforeMerge(dataForMergeHolder, container, instance, instanceOriginResource);
88 verify(componentInstanceMergeInterfaceMock2).saveDataBeforeMerge(dataForMergeHolder, container, instance, instanceOriginResource);
92 public void mergeComponentUserOrigData_allInstanceMergeInterfacesAreCalled() throws Exception {
93 ArgumentCaptor<ComponentParametersView> componentsFilterCapture = ArgumentCaptor.forClass(ComponentParametersView.class);
94 Service persistedService = new Service();
95 User user = new User();
96 DataForMergeHolder dataHolder = new DataForMergeHolder();
97 when(toscaOperationFacade.getToscaElement(Mockito.eq("newContainerId"), componentsFilterCapture.capture())).thenReturn(Either.left(persistedService));
98 when(componentInstanceMergeInterfaceMock1.mergeDataAfterCreate(user, dataHolder, persistedService, "instId")).thenReturn(persistedService);
99 when(componentInstanceMergeInterfaceMock2.mergeDataAfterCreate(user, dataHolder, persistedService, "instId")).thenReturn(persistedService);
100 Component mergeResult = testInstance.mergeComponentUserOrigData(user, dataHolder, new Service(), "newContainerId", "instId");
101 assertEquals(persistedService, mergeResult);
102 assertComponentFilter(componentsFilterCapture.getValue());
105 @Test(expected = ComponentException.class)
106 public void mergeComponentUserOrigData_failToGetPersistedComponent_doNotTryToMerge() throws Exception {
107 User user = new User();
108 DataForMergeHolder dataHolder = new DataForMergeHolder();
109 ResponseFormat rf = new ResponseFormat();
110 Resource container = new ResourceBuilder().setComponentType(ComponentTypeEnum.SERVICE).build();
111 when(toscaOperationFacade.getToscaElement(Mockito.eq("newContainerId"), Mockito.any(ComponentParametersView.class))).thenReturn(Either.right(StorageOperationStatus.GENERAL_ERROR));
112 when(componentsUtils.convertFromStorageResponse(StorageOperationStatus.GENERAL_ERROR, ComponentTypeEnum.SERVICE)).thenReturn(ActionStatus.GENERAL_ERROR);
113 when(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR)).thenReturn(rf);
114 testInstance.mergeComponentUserOrigData(user, dataHolder, container, "newContainerId", "instId");
117 @Test(expected = ComponentException.class)
118 public void mergeComponentUserOrigData_failOnOneMerge_doNotCallOtherMerge() throws Exception {
119 Service persistedService = new Service();
120 User user = new User();
121 DataForMergeHolder dataHolder = new DataForMergeHolder();
122 ResponseFormat rf = new ResponseFormat();
123 when(toscaOperationFacade.getToscaElement(Mockito.eq("newContainerId"), Mockito.any(ComponentParametersView.class))).thenReturn(Either.left(persistedService));
124 when(componentInstanceMergeInterfaceMock1.mergeDataAfterCreate(user, dataHolder, persistedService, "instId")).thenThrow(new ByActionStatusComponentException(ActionStatus.GENERAL_ERROR));
125 testInstance.mergeComponentUserOrigData(user, dataHolder, new Service(), "newContainerId", "instId");
128 private void assertComponentFilter(ComponentParametersView value) {
129 assertFalse(value.isIgnoreComponentInstances());
130 assertFalse(value.isIgnoreComponentInstancesProperties());
131 assertFalse(value.isIgnoreComponentInstancesInputs());
132 assertFalse(value.isIgnoreArtifacts());
133 assertFalse(value.isIgnoreComponentInstancesInterfaces());