1 package org.openecomp.sdc.be.components.merge.instance;
4 import org.junit.Before;
7 import org.openecomp.sdc.be.components.utils.ResourceBuilder;
8 import org.openecomp.sdc.be.dao.api.ActionStatus;
9 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
10 import org.openecomp.sdc.be.impl.ComponentsUtils;
11 import org.openecomp.sdc.be.model.*;
12 import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade;
13 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
14 import org.openecomp.sdc.exception.ResponseFormat;
16 import java.util.Arrays;
18 import static org.junit.Assert.assertEquals;
19 import static org.junit.Assert.assertFalse;
20 import static org.mockito.Mockito.*;
22 public class ComponentInstanceMergeDataBusinessLogicTest {
25 private ComponentInstanceMergeDataBusinessLogic testInstance;
28 private ToscaOperationFacade toscaOperationFacade;
31 private ComponentsUtils componentsUtils;
34 private ComponentInstanceMergeInterface componentInstanceMergeInterfaceMock1;
37 private ComponentInstanceMergeInterface componentInstanceMergeInterfaceMock2;
40 public void setUp() throws Exception {
41 MockitoAnnotations.initMocks(this);
42 testInstance.setComponentInstancesMergeBLs(Arrays.asList(componentInstanceMergeInterfaceMock1, componentInstanceMergeInterfaceMock2));
46 public void saveAllDataBeforeDeleting_allInstanceMergeInterfacesAreCalled() throws Exception {
47 Component container = new Service();
48 ComponentInstance instance = new ComponentInstance();
49 Component instanceOriginResource = new Resource();
50 DataForMergeHolder dataForMergeHolder = testInstance.saveAllDataBeforeDeleting(container, instance, instanceOriginResource);
51 verify(componentInstanceMergeInterfaceMock1).saveDataBeforeMerge(dataForMergeHolder, container, instance, instanceOriginResource);
52 verify(componentInstanceMergeInterfaceMock2).saveDataBeforeMerge(dataForMergeHolder, container, instance, instanceOriginResource);
56 public void mergeComponentUserOrigData_allInstanceMergeInterfacesAreCalled() throws Exception {
57 ArgumentCaptor<ComponentParametersView> componentsFilterCapture = ArgumentCaptor.forClass(ComponentParametersView.class);
58 Service persistedService = new Service();
59 User user = new User();
60 DataForMergeHolder dataHolder = new DataForMergeHolder();
61 when(toscaOperationFacade.getToscaElement(Mockito.eq("newContainerId"), componentsFilterCapture.capture())).thenReturn(Either.left(persistedService));
62 when(componentInstanceMergeInterfaceMock1.mergeDataAfterCreate(user, dataHolder, persistedService, "instId")).thenReturn(Either.left(persistedService));
63 when(componentInstanceMergeInterfaceMock2.mergeDataAfterCreate(user, dataHolder, persistedService, "instId")).thenReturn(Either.left(persistedService));
64 Either<Component, ResponseFormat> mergeResult = testInstance.mergeComponentUserOrigData(user, dataHolder, new Service(), "newContainerId", "instId");
65 assertEquals(persistedService, mergeResult.left().value());
66 assertComponentFilter(componentsFilterCapture.getValue());
70 public void mergeComponentUserOrigData_failToGetPersistedComponent_doNotTryToMerge() throws Exception {
71 User user = new User();
72 DataForMergeHolder dataHolder = new DataForMergeHolder();
73 ResponseFormat rf = new ResponseFormat();
74 Resource container = new ResourceBuilder().setComponentType(ComponentTypeEnum.SERVICE).build();
75 when(toscaOperationFacade.getToscaElement(Mockito.eq("newContainerId"), Mockito.any(ComponentParametersView.class))).thenReturn(Either.right(StorageOperationStatus.GENERAL_ERROR));
76 when(componentsUtils.convertFromStorageResponse(StorageOperationStatus.GENERAL_ERROR, ComponentTypeEnum.SERVICE)).thenReturn(ActionStatus.GENERAL_ERROR);
77 when(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR)).thenReturn(rf);
78 Either<Component, ResponseFormat> mergeResult = testInstance.mergeComponentUserOrigData(user, dataHolder, container, "newContainerId", "instId");
79 assertEquals(rf, mergeResult.right().value());
80 verifyZeroInteractions(componentInstanceMergeInterfaceMock1, componentInstanceMergeInterfaceMock2);
84 public void mergeComponentUserOrigData_failOnOneMerge_doNotCallOtherMerge() throws Exception {
85 Service persistedService = new Service();
86 User user = new User();
87 DataForMergeHolder dataHolder = new DataForMergeHolder();
88 ResponseFormat rf = new ResponseFormat();
89 when(toscaOperationFacade.getToscaElement(Mockito.eq("newContainerId"), Mockito.any(ComponentParametersView.class))).thenReturn(Either.left(persistedService));
90 when(componentInstanceMergeInterfaceMock1.mergeDataAfterCreate(user, dataHolder, persistedService, "instId")).thenReturn(Either.right(rf));
91 Either<Component, ResponseFormat> mergeResult = testInstance.mergeComponentUserOrigData(user, dataHolder, new Service(), "newContainerId", "instId");
92 assertEquals(rf, mergeResult.right().value());
93 verifyZeroInteractions(componentInstanceMergeInterfaceMock2);
96 private void assertComponentFilter(ComponentParametersView value) {
97 assertFalse(value.isIgnoreComponentInstances());
98 assertFalse(value.isIgnoreComponentInstancesProperties());
99 assertFalse(value.isIgnoreComponentInstancesInputs());
100 assertFalse(value.isIgnoreArtifacts());
101 assertFalse(value.isIgnoreComponentInstancesInterfaces());