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;
27 import org.openecomp.sdc.be.components.utils.ResourceBuilder;
28 import org.openecomp.sdc.be.dao.api.ActionStatus;
29 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
30 import org.openecomp.sdc.be.impl.ComponentsUtils;
31 import org.openecomp.sdc.be.model.*;
32 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
33 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
34 import org.openecomp.sdc.exception.ResponseFormat;
36 import java.util.Arrays;
38 import static org.junit.Assert.assertEquals;
39 import static org.junit.Assert.assertFalse;
40 import static org.mockito.Mockito.*;
42 public class ComponentInstanceMergeDataBusinessLogicTest {
45 private ComponentInstanceMergeDataBusinessLogic testInstance;
48 private ToscaOperationFacade toscaOperationFacade;
51 private ComponentsUtils componentsUtils;
54 private ComponentInstanceMergeInterface componentInstanceMergeInterfaceMock1;
57 private ComponentInstanceMergeInterface componentInstanceMergeInterfaceMock2;
60 public void setUp() throws Exception {
61 MockitoAnnotations.initMocks(this);
62 testInstance.setComponentInstancesMergeBLs(Arrays.asList(componentInstanceMergeInterfaceMock1, componentInstanceMergeInterfaceMock2));
66 public void saveAllDataBeforeDeleting_allInstanceMergeInterfacesAreCalled() throws Exception {
67 Component container = new Service();
68 ComponentInstance instance = new ComponentInstance();
69 Component instanceOriginResource = new Resource();
70 DataForMergeHolder dataForMergeHolder = testInstance.saveAllDataBeforeDeleting(container, instance, instanceOriginResource);
71 verify(componentInstanceMergeInterfaceMock1).saveDataBeforeMerge(dataForMergeHolder, container, instance, instanceOriginResource);
72 verify(componentInstanceMergeInterfaceMock2).saveDataBeforeMerge(dataForMergeHolder, container, instance, instanceOriginResource);
76 public void mergeComponentUserOrigData_allInstanceMergeInterfacesAreCalled() throws Exception {
77 ArgumentCaptor<ComponentParametersView> componentsFilterCapture = ArgumentCaptor.forClass(ComponentParametersView.class);
78 Service persistedService = new Service();
79 User user = new User();
80 DataForMergeHolder dataHolder = new DataForMergeHolder();
81 when(toscaOperationFacade.getToscaElement(Mockito.eq("newContainerId"), componentsFilterCapture.capture())).thenReturn(Either.left(persistedService));
82 when(componentInstanceMergeInterfaceMock1.mergeDataAfterCreate(user, dataHolder, persistedService, "instId")).thenReturn(Either.left(persistedService));
83 when(componentInstanceMergeInterfaceMock2.mergeDataAfterCreate(user, dataHolder, persistedService, "instId")).thenReturn(Either.left(persistedService));
84 Either<Component, ResponseFormat> mergeResult = testInstance.mergeComponentUserOrigData(user, dataHolder, new Service(), "newContainerId", "instId");
85 assertEquals(persistedService, mergeResult.left().value());
86 assertComponentFilter(componentsFilterCapture.getValue());
90 public void mergeComponentUserOrigData_failToGetPersistedComponent_doNotTryToMerge() throws Exception {
91 User user = new User();
92 DataForMergeHolder dataHolder = new DataForMergeHolder();
93 ResponseFormat rf = new ResponseFormat();
94 Resource container = new ResourceBuilder().setComponentType(ComponentTypeEnum.SERVICE).build();
95 when(toscaOperationFacade.getToscaElement(Mockito.eq("newContainerId"), Mockito.any(ComponentParametersView.class))).thenReturn(Either.right(StorageOperationStatus.GENERAL_ERROR));
96 when(componentsUtils.convertFromStorageResponse(StorageOperationStatus.GENERAL_ERROR, ComponentTypeEnum.SERVICE)).thenReturn(ActionStatus.GENERAL_ERROR);
97 when(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR)).thenReturn(rf);
98 Either<Component, ResponseFormat> mergeResult = testInstance.mergeComponentUserOrigData(user, dataHolder, container, "newContainerId", "instId");
99 assertEquals(rf, mergeResult.right().value());
100 verifyZeroInteractions(componentInstanceMergeInterfaceMock1, componentInstanceMergeInterfaceMock2);
104 public void mergeComponentUserOrigData_failOnOneMerge_doNotCallOtherMerge() throws Exception {
105 Service persistedService = new Service();
106 User user = new User();
107 DataForMergeHolder dataHolder = new DataForMergeHolder();
108 ResponseFormat rf = new ResponseFormat();
109 when(toscaOperationFacade.getToscaElement(Mockito.eq("newContainerId"), Mockito.any(ComponentParametersView.class))).thenReturn(Either.left(persistedService));
110 when(componentInstanceMergeInterfaceMock1.mergeDataAfterCreate(user, dataHolder, persistedService, "instId")).thenReturn(Either.right(rf));
111 Either<Component, ResponseFormat> mergeResult = testInstance.mergeComponentUserOrigData(user, dataHolder, new Service(), "newContainerId", "instId");
112 assertEquals(rf, mergeResult.right().value());
113 verifyZeroInteractions(componentInstanceMergeInterfaceMock2);
116 private void assertComponentFilter(ComponentParametersView value) {
117 assertFalse(value.isIgnoreComponentInstances());
118 assertFalse(value.isIgnoreComponentInstancesProperties());
119 assertFalse(value.isIgnoreComponentInstancesInputs());
120 assertFalse(value.isIgnoreArtifacts());
121 assertFalse(value.isIgnoreComponentInstancesInterfaces());