Fix test cases failing incorrectly
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / components / merge / path / ComponentInstanceForwardingPathMergeTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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  */
20
21 package org.openecomp.sdc.be.components.merge.path;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.mockito.ArgumentMatchers.any;
26 import static org.mockito.ArgumentMatchers.anyBoolean;
27 import static org.mockito.Mockito.when;
28
29 import fj.data.Either;
30 import java.util.Set;
31 import org.junit.jupiter.api.BeforeEach;
32 import org.junit.jupiter.api.Test;
33 import org.mockito.InjectMocks;
34 import org.mockito.Mock;
35 import org.mockito.MockitoAnnotations;
36 import org.openecomp.sdc.be.components.impl.ServiceBusinessLogic;
37 import org.openecomp.sdc.be.components.merge.instance.ComponentInstanceForwardingPathMerge;
38 import org.openecomp.sdc.be.components.path.BaseForwardingPathVersionChangeTest;
39 import org.openecomp.sdc.be.impl.ForwardingPathUtils;
40 import org.openecomp.sdc.be.model.Component;
41 import org.openecomp.sdc.be.model.Service;
42 import org.openecomp.sdc.be.model.User;
43 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
44 import org.openecomp.sdc.common.api.UserRoleEnum;
45
46 public class ComponentInstanceForwardingPathMergeTest extends BaseForwardingPathVersionChangeTest {
47
48     @InjectMocks
49     private ComponentInstanceForwardingPathMerge testInstance;
50
51     @Mock
52     private ServiceBusinessLogic serviceBusinessLogic;
53
54     @Mock
55     private ToscaOperationFacade toscaOperationFacade;
56     private User user;
57
58     @BeforeEach
59     public void setUpData() {
60         MockitoAnnotations.initMocks(this);
61         user = new User();
62         user.setUserId("44");
63         user.setRole(UserRoleEnum.ADMIN.getName());
64     }
65
66     @Test
67     public void testIgnoreMergeSinceItIsNotService() {
68
69         testInstance.saveDataBeforeMerge(dataHolder, service, nodeACI, newNodeAC);
70         assertEquals(nodeACI.getName(), dataHolder.getOrigComponentInstId());
71         Component componentResponseFormatEither = testInstance
72             .mergeDataAfterCreate(user, dataHolder, newNodeAC, "3344");
73         assertNotNull(componentResponseFormatEither);
74         assertEquals(newNodeAC, componentResponseFormatEither);
75     }
76
77     @Test
78     public void mergeShouldDelete() {
79         Set<String> forwardingPathNamesToDeleteOnComponentInstanceDeletion = new ForwardingPathUtils()
80             .findForwardingPathNamesToDeleteOnComponentInstanceDeletion(service, nodeACI.getUniqueId());
81         nodeACI.getCapabilities().clear();
82         newNodeAC.getCapabilities().clear();
83         Set<String> returnValue = forwardingPathNamesToDeleteOnComponentInstanceDeletion;
84         when(serviceBusinessLogic.deleteForwardingPaths(any(), any(), any(), anyBoolean()))
85             .thenReturn(returnValue);
86         when(toscaOperationFacade.getToscaFullElement(any())).thenReturn(Either.left(newNodeAC));
87
88         // Change internal ci, just like change version do
89         service.getComponentInstances().remove(nodeACI);
90         service.getComponentInstances().add(newNodeACI);
91
92         testInstance.saveDataBeforeMerge(dataHolder, service, nodeACI, newNodeAC);
93         assertEquals(nodeACI.getName(), dataHolder.getOrigComponentInstId());
94         Component componentResponseFormatEither = testInstance
95             .mergeDataAfterCreate(user, dataHolder, service, newNodeA);
96         assertNotNull(componentResponseFormatEither);
97         assertEquals(0, ((Service) componentResponseFormatEither).getForwardingPaths().size());
98     }
99
100     @Test
101     public void mergeShouldUpdate() {
102         when(serviceBusinessLogic.updateForwardingPath(any(), any(), any(), anyBoolean()))
103             .then(invocationOnMock -> service);
104         when(toscaOperationFacade.getToscaFullElement(any())).thenReturn(Either.left(newNodeAC));
105         testInstance.saveDataBeforeMerge(dataHolder, service, nodeACI, newNodeAC);
106         assertEquals(nodeACI.getName(), dataHolder.getOrigComponentInstId());
107
108         // Change internal ci, just like change version do
109         service.getComponentInstances().remove(nodeACI);
110         service.getComponentInstances().add(newNodeACI);
111
112         Component component = testInstance.mergeDataAfterCreate(user, dataHolder, service, newNodeA);
113         assertNotNull(component);
114     }
115
116     @Test
117     public void handleNullCapailities() {
118         nodeACI.setCapabilities(null);
119         testInstance.saveDataBeforeMerge(dataHolder, service, nodeACI, newNodeAC);
120     }
121 }