972ea4e620db662d2edcae71c9eecfd8c1164474
[sdc.git] /
1 package org.openecomp.sdc.be.components.merge.path;
2
3 import static groovy.util.GroovyTestCase.assertEquals;
4 import static junit.framework.Assert.assertNotNull;
5 import static junit.framework.TestCase.assertEquals;
6 import static junit.framework.TestCase.assertTrue;
7 import static org.mockito.ArgumentMatchers.any;
8 import static org.mockito.ArgumentMatchers.anyBoolean;
9 import static org.mockito.Mockito.when;
10
11 import java.util.Set;
12
13 import org.junit.Before;
14 import org.junit.Test;
15 import org.mockito.InjectMocks;
16 import org.mockito.Mock;
17 import org.mockito.MockitoAnnotations;
18 import org.openecomp.sdc.be.components.impl.ServiceBusinessLogic;
19 import org.openecomp.sdc.be.components.merge.instance.ComponentInstanceForwardingPathMerge;
20 import org.openecomp.sdc.be.components.path.BaseForwardingPathVersionChangeTest;
21 import org.openecomp.sdc.be.impl.ForwardingPathUtils;
22 import org.openecomp.sdc.be.model.Component;
23 import org.openecomp.sdc.be.model.Service;
24 import org.openecomp.sdc.be.model.User;
25 import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade;
26 import org.openecomp.sdc.common.api.UserRoleEnum;
27 import org.openecomp.sdc.exception.ResponseFormat;
28
29 import fj.data.Either;
30
31 public class ComponentInstanceForwardingPathMergeTest extends BaseForwardingPathVersionChangeTest {
32
33     @InjectMocks
34     private ComponentInstanceForwardingPathMerge testInstance;
35
36     @Mock
37     private ServiceBusinessLogic serviceBusinessLogic;
38
39     @Mock
40     private ToscaOperationFacade toscaOperationFacade;
41     private User user;
42
43     @Before
44     public void setUpData() {
45         MockitoAnnotations.initMocks(this);
46         user = new User();
47         user.setUserId("44");
48         user.setRole(UserRoleEnum.ADMIN.getName());
49     }
50
51     @Test
52     public void testIgnoreMergeSinceItIsNotService() {
53
54         testInstance.saveDataBeforeMerge(dataHolder, service, nodeACI, newNodeAC);
55         assertEquals(nodeACI.getUniqueId(), dataHolder.getOrigComponentInstId());
56         Either<Component, ResponseFormat> componentResponseFormatEither = testInstance
57             .mergeDataAfterCreate(user, dataHolder, newNodeAC, "3344");
58         assertNotNull(componentResponseFormatEither);
59         assertTrue(componentResponseFormatEither.isLeft());
60         assertEquals(newNodeAC, componentResponseFormatEither.left().value());
61     }
62
63     @Test
64     public void mergeShouldDelete() {
65         Set<String> forwardingPathNamesToDeleteOnComponentInstanceDeletion = new ForwardingPathUtils()
66             .findForwardingPathNamesToDeleteOnComponentInstanceDeletion(service, nodeACI.getUniqueId());
67         nodeACI.getCapabilities().clear();
68         newNodeAC.getCapabilities().clear();
69         when(serviceBusinessLogic.deleteForwardingPaths(any(), any(), any(), anyBoolean()))
70             .thenReturn(Either.left(forwardingPathNamesToDeleteOnComponentInstanceDeletion));
71         when(toscaOperationFacade.getToscaFullElement(any())).thenReturn(Either.left(newNodeAC));
72
73         // Change internal ci, just like change version do
74         service.getComponentInstances().remove(nodeACI);
75         service.getComponentInstances().add(newNodeACI);
76
77         testInstance.saveDataBeforeMerge(dataHolder, service, nodeACI, newNodeAC);
78         assertEquals(nodeACI.getUniqueId(), dataHolder.getOrigComponentInstId());
79         Either<Component, ResponseFormat> componentResponseFormatEither = testInstance
80             .mergeDataAfterCreate(user, dataHolder, service, newNodeA);
81         assertNotNull(componentResponseFormatEither);
82         assertTrue(componentResponseFormatEither.isLeft());
83         assertEquals(0, ((Service) componentResponseFormatEither.left().value()).getForwardingPaths().size());
84     }
85
86     @Test
87     public void mergeShouldUpdate() {
88         when(serviceBusinessLogic.updateForwardingPath(any(), any(), any(), anyBoolean()))
89             .thenReturn(Either.left(service));
90         when(toscaOperationFacade.getToscaFullElement(any())).thenReturn(Either.left(newNodeAC));
91         testInstance.saveDataBeforeMerge(dataHolder, service, nodeACI, newNodeAC);
92         assertEquals(nodeACI.getUniqueId(), dataHolder.getOrigComponentInstId());
93
94         // Change internal ci, just like change version do
95         service.getComponentInstances().remove(nodeACI);
96         service.getComponentInstances().add(newNodeACI);
97
98         Either<Component, ResponseFormat> componentResponseFormatEither = testInstance
99             .mergeDataAfterCreate(user, dataHolder, service, newNodeA);
100         assertNotNull(componentResponseFormatEither);
101         assertTrue(componentResponseFormatEither.isLeft());
102     }
103 }