re base code
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / components / merge / resource / ResourceDataMergeBusinessLogicTest.java
1 package org.openecomp.sdc.be.components.merge.resource;
2
3 import fj.data.Either;
4 import org.junit.Before;
5 import org.junit.Test;
6 import org.junit.runner.RunWith;
7 import org.mockito.InjectMocks;
8 import org.mockito.Mock;
9 import org.mockito.junit.MockitoJUnitRunner;
10 import org.openecomp.sdc.be.components.merge.ComponentsMergeCommand;
11 import org.openecomp.sdc.be.components.utils.ObjectGenerator;
12 import org.openecomp.sdc.be.dao.api.ActionStatus;
13 import org.openecomp.sdc.be.model.Resource;
14
15 import static java.util.Arrays.asList;
16 import static org.junit.Assert.assertEquals;
17 import static org.mockito.Mockito.*;
18
19 @RunWith(MockitoJUnitRunner.class)
20 public class ResourceDataMergeBusinessLogicTest {
21
22     @InjectMocks
23     private ResourceDataMergeBusinessLogic testInstance;
24
25     @Mock
26     private MergeCommandsFactory mergeCommandsFactory;
27
28     @Mock
29     private ComponentsMergeCommand commandA;
30
31     @Mock
32     private ComponentsMergeCommand commandB;
33
34     @Mock
35     private ComponentsMergeCommand commandC;
36
37     private Resource oldResource, newResource;
38
39     @Before
40     public void setUp() throws Exception {
41         oldResource = ObjectGenerator.buildBasicResource();
42         newResource = ObjectGenerator.buildBasicResource();
43         when(mergeCommandsFactory.getMergeCommands(oldResource, newResource)).thenReturn(Either.left(asList(commandA, commandB, commandC)));
44     }
45
46     @Test
47     public void whenCommandsFactoryFails_propagateTheFailure() {
48         when(mergeCommandsFactory.getMergeCommands(oldResource, newResource)).thenReturn(Either.right(ActionStatus.GENERAL_ERROR));
49         ActionStatus actionStatus = testInstance.mergeResourceEntities(oldResource, newResource);
50         assertEquals(ActionStatus.GENERAL_ERROR, actionStatus);
51         verifyZeroInteractions(commandA, commandB, commandC);
52     }
53
54     @Test
55     public void mergeResources_allMergeClassesAreCalled() {
56         when(commandA.mergeComponents(oldResource, newResource)).thenReturn(ActionStatus.OK);
57         when(commandB.mergeComponents(oldResource, newResource)).thenReturn(ActionStatus.OK);
58         when(commandC.mergeComponents(oldResource, newResource)).thenReturn(ActionStatus.OK);
59         ActionStatus actionStatus = testInstance.mergeResourceEntities(oldResource, newResource);
60         assertEquals(ActionStatus.OK, actionStatus);
61     }
62
63     @Test
64     public void mergeResources_mergeCommandFailed_dontCallOtherMergeMethods() {
65         when(commandA.mergeComponents(oldResource, newResource)).thenReturn(ActionStatus.GENERAL_ERROR);
66         ActionStatus actionStatus = testInstance.mergeResourceEntities(oldResource, newResource);
67         assertEquals(ActionStatus.GENERAL_ERROR, actionStatus);
68         verify(commandA).description();
69         verifyZeroInteractions(commandB, commandC);
70     }
71
72 }